fork.c
ALP, p. 53
#include <stdio.h> // for printf()
#include <sys/types.h> // for pid_t
#include <unistd.h> // getpid(), fork()
int main()
{
pid_t childid;
printf ("The main program process ID is %d\n", (int)getpid());
childid = fork(); // fork() returns the child PID to the parent
if (childid != 0)
{
printf ("This is the parent process, with id %d\n", (int)getpid());
printf ("The child's process ID is %d\n", (int)childid);
}
else // fork() returns 0 to the child process
{printf("This is the child process, with id %d\n", (int)getpid());}
return 0;
}
/*
gcc fork.c -o fork
./fork
The main program process ID is 90652
This is the parent process, with id 90652
The child's process ID is 90653
This is the child process, with id 90653
*/
Comments
Post a Comment