#include <unistd.h>
#include <stdio.h>
int main ()
{
pid_t fpid;
int count=0;
printf("This will not be forked.\n");
fpid=fork();
printf("Will be forked from here.\n");
if (fpid < 0)
printf("error in fork!");
else if (fpid == 0) {
printf("i am the child process, my process id is %d\n",getpid());
count++;
}
else {
printf("i am the parent process, my process id is %d\n",getpid());
count++;
}
printf("count= %d\n",count);
return 0;
}
gcc fork_test.c
a.out
./a.out
This will not be forked.
Will be forked from here.
Will be forked from here.
i am the parent process, my process id is 28355
count= 1
i am the child process, my process id is 28356
count= 1
./a.out
This will not be forked.
Will be forked from here.
i am the parent process, my process id is 28369
Will be forked from here.
count= 1
i am the child process, my process id is 28370
count= 1
./a.out
This will not be forked.
Will be forked from here.
i am the parent process, my process id is 28383
count= 1
Will be forked from here.
i am the child process, my process id is 28384
count= 1
./a.out
This will not be forked.
Will be forked from here.
i am the parent process, my process id is 28420
count= 1
Will be forked from here.
i am the child process, my process id is 28421
count= 1