220 likes | 376 Views
What fork() Does. for (int i = 0; i < 3; ++i) fork();. Each child process will be executing this same code:. for (int i = 0; i < 3; ++i) fork();. After fork() , when each child starts the data will be the same as its parent’s – i will not start at 0 for each child.
E N D
What fork()Does • for (int i = 0; i < 3; ++i) fork();
Each child process will be executing this same code: • for (int i = 0; i < 3; ++i) fork();
After fork(),when each child startsthe data will bethe same as its parent’s –i will not start at 0 for each child. • for (int i = 0; i < 3; ++i) fork();
Start the process that has this code: • for (int i = 0; i < 3; ++i) fork();
P1 • i contains: 0 • for (int i = 0; i < 3; ++i) fork();
P1 • i contains: 0 • for (int i = 0; i < 3; ++i) fork();
P1 P2 After fork() • i contains: 0 • i contains: 0 • for (int i = 0; i < 3; ++i) fork();
P1 P2 Same Data In Child • i contains: 0 • i contains: 0 • for (int i = 0; i < 3; ++i) fork();
P1 P2 After First Pass, BOTH incr i • i contains: 0 • i contains: 0 • for (int i = 0; i < 3; ++i) fork();
P1 P2 • i contains: 1 • i contains: 1 • for (int i = 0; i < 3; ++i) fork();
P1 P2 BOTH fork() • i contains: 1 • i contains: 1 • for (int i = 0; i < 3; ++i) fork();
P1 P2 P3 P4 After fork() • i contains: 1 • i contains: 1 • i contains: 1 • i contains: 1 • for (int i = 0; i < 3; ++i) fork();
Same Data In Each Child P1 P2 P3 P4 • i contains: 1 • i contains: 1 • i contains: 1 • i contains: 1 • for (int i = 0; i < 3; ++i) fork();
After Second Pass, ALL incr i P1 P2 P3 P4 • i contains: 1 • i contains: 1 • i contains: 1 • i contains: 1 • for (int i = 0; i < 3; ++i) fork();
P1 P2 P3 P4 • i contains: 2 • i contains: 2 • i contains: 2 • i contains: 2 • for (int i = 0; i < 3; ++i) fork();
ALL fork() P1 P2 P3 P4 • i contains: 2 • i contains: 2 • i contains: 2 • i contains: 2 • for (int i = 0; i < 3; ++i) fork();
P8 P1 P2 P3 P4 P5 P6 P7 After fork() • i contains: 2 • i contains: 2 • i contains: 2 • i contains: 2 • i contains: 2 • i contains: 2 • i contains: 2 • i contains: 2 • for (int i = 0; i < 3; ++i) fork();
Same Data In Each Child P1 P2 P3 P4 P5 P6 P7 P8 • i contains: 2 • i contains: 2 • i contains: 2 • i contains: 2 • i contains: 2 • i contains: 2 • i contains: 2 • i contains: 2 • for (int i = 0; i < 3; ++i) fork();
After Third Pass, ALL incr i P1 P2 P3 P4 P5 P6 P7 P8 • i contains: 2 • i contains: 2 • i contains: 2 • i contains: 2 • i contains: 2 • i contains: 2 • i contains: 2 • i contains: 2 • for (int i = 0; i < 3; ++i) fork();
P1 P2 P3 P4 P5 P6 P7 P8 • i contains: 3 • i contains: 3 • i contains: 3 • i contains: 3 • i contains: 3 • i contains: 3 • i contains: 3 • i contains: 3 • for (int i = 0; i < 3; ++i) fork();
P1 P2 P3 P4 P5 P6 P7 P8 the for statement is complete