Advertisement
paulogp

Fork

Jul 14th, 2011
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.70 KB | None | 0 0
  1. // criacao de varios processos (fork)
  2.  
  3. // Apple Xcode
  4. // paulogp
  5.  
  6.  
  7. #include <stdio.h>
  8. #include <unistd.h>
  9. #include <stdlib.h>
  10. #include <time.h>
  11. #include <signal.h>
  12.  
  13. int main (int argc, const char * argv[])
  14. {
  15.     int the_fork, i;
  16.    
  17.     for (i = 0; i < 10; ++i)
  18.     {
  19.         // create a new process
  20.         the_fork = fork();
  21.        
  22.         // child process
  23.         if (the_fork == 0)
  24.         {
  25.             printf("filho %d: %d\n", i + 1, getpid());
  26.             pause(); // stop until signal
  27.             exit(0);
  28.         }
  29.     }
  30.    
  31.     // wait
  32.     sleep(2);
  33.    
  34.     // processes
  35.     system("ps -o pid,ppid,command");
  36.     pause(); // stop until signal
  37.    
  38.     return 0;
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement