简体   繁体   中英

New Process Creation in Operating System (Unix)

I am sorry if my question seems silly. I have a query regarding new process creation in operating system. Consider the following simple C code:

//hello.c
#include <stdio.h>

int main()

    {
        printf("Hello World\n");
        return 0;
    }

When is Compiled with gcc.

gcc hello.c

now executing the executable a.out

./a.out

Now I don't understand how in this case New Process is created, who calls the fork() and exec system calls and which process is duplicated to have a.out as child process? In this example , parent process explicitly calls fork system call to create child process but in above hello.c code there is no fork call.

Typically, the parent process issues the fork() system call, creating a duplicate process having (mostly) the same properties as the original process (different process ID, for example). From there, the child process issues one of the exec family system calls to replace its own process image with a new one. This is explained quite well on the Unix SE.

在此处输入图片说明

In your case, the shell is the parent process, and the "new program" you are running is the child process which eventually calls exec .

Your shell will create new process for your program and it will do exec. It means parent process for your program is your shell.

echo $$ This command will give shell process ID.

Program :

printf("PPID : %d, PID : %d\n", getppid(), getpid() );

When you are run the program you will get below output.

PPID : 19172, PID : 26388

Note :

  Parent process and shell process ID's are same.   

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM