Advertisement
paulogp

SISTC Fork

Jul 14th, 2011
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.54 KB | None | 0 0
  1. // exame de recurso (parte a): 2009-09-29
  2. // exercicio 2: fork, output de valores
  3.  
  4. // Apple Xcode
  5. // paulogp
  6.  
  7.  
  8. #include <stdio.h>
  9. #include <unistd.h>
  10. #include <stdlib.h>
  11.  
  12. int main (int argc, const char * argv[])
  13. {
  14.     pid_t pid;
  15.     int counter = 5;
  16.     int *p = &counter;
  17.     pid = fork();
  18.    
  19.     if (pid == 0)
  20.     {
  21.         printf("%d\n", counter);
  22.         *p = 1;
  23.         printf("%d\n", counter);
  24.     }
  25.     else
  26.     {
  27.         wait(NULL);
  28.         printf("%d\n", *p);
  29.     }
  30.    
  31.     return 0;
  32. }
  33.  
  34.  
  35. /* output
  36. 5
  37. 1
  38. 5
  39. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement