Reputation: 633
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.
Upvotes: 2
Views: 1708
Reputation: 34
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.
Upvotes: 0
Reputation: 19333
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
.
Upvotes: 3