Advertisement
Guest User

Untitled

a guest
Jul 24th, 2019
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.44 KB | None | 0 0
  1. #include <iostream>
  2. #include <unistd.h>
  3. #include <stdio.h>
  4.  
  5. using namespace std;
  6.  
  7. void firstFork();
  8. void secondFork();
  9. void parentTask();
  10. void execTask();
  11. void countingTask();
  12.  
  13. void firstFork()
  14. {
  15. pid_t pid = fork();
  16.  
  17. if (pid == 0)
  18. {
  19. // child process
  20. cout << "Child 1 created by fork \n";
  21. execTask();
  22. }
  23. else if (pid < 0)
  24. {
  25. // fork failed
  26. cout << "First child fork failed \n";
  27. exit(EXIT_FAILURE);
  28. }
  29. }
  30. void secondFork()
  31. {
  32. pid_t pid = fork();
  33.  
  34. if (pid == 0)
  35. {
  36. // child process
  37. cout << "Child 2 created by fork \n";
  38. countingTask();
  39. }
  40. else if (pid < 0)
  41. {
  42. // fork failed
  43. cout << "Second child fork failed \n";
  44. exit(EXIT_FAILURE);
  45. }
  46. }
  47.  
  48. int main()
  49. {
  50. parentTask();
  51. }
  52.  
  53. /*Forks itself 2 times say bye and finish immidiately */
  54. void parentTask()
  55. {
  56. // execl("/bin/ls","ls", (char *) 0);
  57. firstFork();
  58. // secondFork();
  59. cout << "Bye\n";
  60. exit(EXIT_SUCCESS);
  61. }
  62.  
  63. /*exec its command and finishs */
  64. void execTask()
  65. {
  66. execl("/bin/ls", "ls", NULL);
  67. exit(EXIT_SUCCESS);
  68. }
  69.  
  70. /*spawn 5 threads which would count to 30
  71. (by printing their name and counter to console output)
  72. and then finish. Numbers printed must be in order
  73. and each thread can print at most 1
  74. number more than other threads. */
  75. void countingTask()
  76. {
  77. exit(EXIT_SUCCESS);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement