Advertisement
andruhovski

unix-0104

Feb 17th, 2013
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.70 KB | None | 0 0
  1. #include <sys/types.h>
  2. #include <fcntl.h>
  3. #include <stdio.h>
  4. void main(void)
  5. {
  6.     int pid2, pid3, st; /* process 1 */
  7.     printf("Process 1, pid = %d:\n", getpid());
  8.     pid2 = fork();
  9.     if (pid2 == 0) /* process 2 */
  10.     {
  11.         printf("Process 2, pid = %d:\n", getpid());
  12.         pid3 = fork();
  13.         if (pid3 == 0) /* process 3 */
  14.         {  
  15.             printf("Process 3, pid = %d:\n", getpid());
  16.             sleep(2);
  17.             printf("Process 3: end\n");
  18.         } /* process 2 */
  19.         if (pid3 < 0) printf("Cann't create process 3: error %d\n", pid3);
  20.         wait(&st);
  21.         printf("Process 2: end\n");
  22.     }
  23.     else /* process 1 */
  24.     {   if (pid2 < 0) printf("Cann't create process 2: error %d\n", pid2);
  25.     wait(&st);
  26.     printf("Process 1: end\n");
  27.     }
  28.     exit(0);
  29. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement