Advertisement
Guest User

Untitled

a guest
Apr 19th, 2019
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.86 KB | None | 0 0
  1. #include "assert.h"
  2. #include "unistd.h"
  3. #include "pthread.h"
  4. #include "wait.h"
  5.  
  6. void* thready_function()
  7. {
  8.   sleep(10);
  9.   printf("Now I am ending!\n");
  10.  
  11.   return 0;
  12. }
  13.  
  14. int main()
  15. {
  16.   pthread_t thready;
  17.   int pid = fork();
  18.  
  19.   if (pid == 0)
  20.   {
  21.     int pid_child = fork();
  22.     if (pid_child == 0)
  23.     {
  24.       for (int i = 0; i < 10; i++)
  25.         printf("The inner child is printing!\n");
  26.       assert((pthread_create(&thready, NULL, &thready_function, NULL) == 0) && "This should never happen!\n");
  27.       pthread_join(thready, NULL);
  28.     }
  29.     else
  30.     {
  31.       waitpid(pid_child, NULL, NULL);
  32.       printf("The child waited for %d!\n", pid_child);
  33.       for (int i = 0; i < 10; i++)
  34.         printf("The child is printing!\n");
  35.     }
  36.   }
  37.   else
  38.   {
  39.     waitpid(pid, NULL, NULL);
  40.     printf("The parent waited for %d!\n", pid);
  41.   }
  42.  
  43.   return 0;
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement