need to understand fork()



Please explain in details the following code and how does fork() work ?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <stdio.h>
int main()
{
printf("%d\n", getpid());   //  5
fork();
printf("%d\n", getpid());  //  5 6
printf("%d\n", getpid());  //  5 6
	
fork();
printf("%d\n", getpid());   // 5 6 6 7
fork();
printf("%d\n", getpid());   // 5 6 6 7 6 7 7 8
printf("%d\n", getpid());   // 5 6 6 7 6 7 7 8
	
return 0;
}

How many instances does printf() produce? 25
How many uniqe process result from the fork() call? 11


Last edited on
Topic archived. No new replies allowed.