Advertisement
Guest User

Untitled

a guest
Jul 18th, 2019
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.95 KB | None | 0 0
  1. // C++ program to demonstrate creating processes using fork()
  2. #include <unistd.h>
  3. #include <stdio.h>
  4.  
  5. int main()
  6. {
  7. // Creating first child
  8. int n1 = fork();
  9.  
  10. // Creating second child. First child
  11. // also executes this line and creates
  12. // grandchild.
  13. int n2 = fork();
  14.  
  15. if (n1 > 0 && n2 > 0) {
  16. printf("parent\n");
  17. printf("%d %d \n", n1, n2);
  18. printf(" my id is %d \n", getpid());
  19. }
  20. else if (n1 == 0 && n2 > 0)
  21. {
  22. printf("First child\n");
  23. printf("%d %d \n", n1, n2);
  24. printf("my id is %d \n", getpid());
  25. }
  26. else if (n1 > 0 && n2 == 0)
  27. {
  28. printf("Second child\n");
  29. printf("%d %d \n", n1, n2);
  30. printf("my id is %d \n", getpid());
  31. }
  32. else {
  33. printf("third child\n");
  34. printf("%d %d \n", n1, n2);
  35. printf(" my id is %d \n", getpid());
  36. }
  37. return 0;
  38. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement