1 / 21

What fork() Does

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.

samara
Download Presentation

What fork() Does

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. What fork()Does • for (int i = 0; i < 3; ++i) fork();

  2. Each child process will be executing this same code: • for (int i = 0; i < 3; ++i) fork();

  3. 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();

  4. Start the process that has this code: • for (int i = 0; i < 3; ++i) fork();

  5. P1 • i contains: 0 • for (int i = 0; i < 3; ++i) fork();

  6. P1 • i contains: 0 • for (int i = 0; i < 3; ++i) fork();

  7. P1 P2 After fork() • i contains: 0 • i contains: 0 • for (int i = 0; i < 3; ++i) fork();

  8. P1 P2 Same Data In Child • i contains: 0 • i contains: 0 • for (int i = 0; i < 3; ++i) fork();

  9. P1 P2 After First Pass, BOTH incr i • i contains: 0 • i contains: 0 • for (int i = 0; i < 3; ++i) fork();

  10. P1 P2 • i contains: 1 • i contains: 1 • for (int i = 0; i < 3; ++i) fork();

  11. P1 P2 BOTH fork() • i contains: 1 • i contains: 1 • for (int i = 0; i < 3; ++i) fork();

  12. 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();

  13. 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();

  14. 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();

  15. P1 P2 P3 P4 • i contains: 2 • i contains: 2 • i contains: 2 • i contains: 2 • for (int i = 0; i < 3; ++i) fork();

  16. 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();

  17. 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();

  18. 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();

  19. 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();

  20. 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();

  21. P1 P2 P3 P4 P5 P6 P7 P8 the for statement is complete

More Related