Advertisement
jamilton

Untitled

Jan 28th, 2021
1,493
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.06 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <sys/wait.h>
  4. #include <unistd.h>
  5. #include <sys/types.h>
  6. #include <time.h>
  7.  
  8. int main(){
  9.  
  10.     int filho1, filho2, neto1, neto2;
  11.  
  12.     printf("Início do processo pai PID=%d\n", getpid());
  13.     sleep(14);
  14.     filho1 = fork();
  15.     if(!filho1){
  16.         printf("Filho1 com pid=%d e ppid=%d nasce aos 14 segundos\n", getpid(), getppid());
  17.         sleep(13);
  18.         neto1=fork();
  19.         if(!neto1){
  20.             printf("Neto1 com pid=%d e ppid=%d\n", getpid(), getppid());
  21.             sleep(12);
  22.             printf("Neto1 morre aos 12 anos\n");
  23.             exit(0);
  24.         }
  25.         sleep(17);
  26.         printf("Filho1 morre aos 30 anos\n");
  27.         exit(0);
  28.     }
  29.  
  30.     sleep(2);
  31.     if(filho1>0){
  32.         filho2=fork();
  33.         if(!filho2){
  34.             printf("Filho2 com pid=%d e ppid=%d nasce aos 16 segundos\n", getpid(), getppid());
  35.             sleep(15);
  36.             neto2=fork();
  37.             if(!neto2){
  38.                 printf("Neto2 com pid=%d e ppid=%d\n", getpid(), getppid());
  39.                 sleep(18);
  40.                 exit(0);
  41.             }
  42.             sleep(15);
  43.             printf("filho2 morre aos 30 anos\n");      
  44.             exit(0);
  45.         }
  46.     }
  47.     sleep(46);
  48.     printf("Pai morre aos 60 anos\n");
  49.     exit(0);
  50.     return 0;
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement