Advertisement
Guest User

Dúvida SO

a guest
Nov 12th, 2019
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.34 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <unistd.h>
  3. #include <signal.h>
  4. #include <sys/wait.h>
  5.  
  6.  
  7. int processoDisplayLyric;
  8. int processoDisplayGraph;
  9. int processoTimer;
  10.  
  11. int main(){
  12.  
  13.     int numeroMusicaAtual = 0;
  14.     int continuar = 0;
  15.     int processoPai = getpid();
  16.     while(continuar == 0) {
  17.  
  18.  
  19.         //Criar filho graph.
  20.         processoDisplayGraph = fork();
  21.  
  22.         if(processoDisplayGraph == 0){
  23.             int ficar = 0;
  24.             while(ficar == 0){
  25.                 printf("Preso no gráfico!\n");
  26.                 pause();
  27.             }
  28.         }
  29.  
  30.         if(processoDisplayGraph != 0){
  31.  
  32.             //Criar filho lyric.
  33.             processoDisplayLyric =  fork();
  34.  
  35.             if(processoDisplayLyric == 0){
  36.  
  37.                 int ficar = 0;
  38.                 while(ficar == 0){
  39.                     printf("Preso nas lyrics!\n");
  40.                     pause();
  41.                 }
  42.             }
  43.  
  44.         }
  45.  
  46.         sleep(1);
  47.  
  48.         if(processoDisplayLyric != 0 && processoDisplayGraph != 0){
  49.  
  50.             //Criar filho timer.
  51.             processoTimer = fork();
  52.  
  53.             if(processoTimer == 0){
  54.                 printf("Entrei no timer, e o meu pid é: %d\n",getpid());
  55.                 printf("No Timer -> PID Graph: %d, PID Lyric: %d\n",processoDisplayGraph,processoDisplayLyric);
  56.             }
  57.  
  58.         }
  59.  
  60.  
  61.         if(getpid() != processoPai){
  62.             printf("----------- ENTREI --------\n\n\n\n");
  63.             int child_status;
  64.             pid_t wpidLyric = waitpid(processoDisplayLyric, &child_status, 0);
  65.             pid_t wpidGraph = waitpid(processoDisplayGraph, &child_status, 0);
  66.         }else{
  67.             //É o pai
  68.             int child_status;
  69.             for (int i = 0; i < 2; i++) {
  70.                 pid_t wpid = waitpid(processoDisplayLyric, &child_status, 0);
  71.                 if (WIFEXITED(child_status))
  72.                     printf("Saw %d done with %d\n", wpid, WEXITSTATUS(child_status));
  73.                 else
  74.                     printf("Child %d terminated abnormally\n", wpid);
  75.             }
  76.         }
  77.  
  78.         printf("-------------\n");
  79.         printf("IDGRAPH: %d, IDLYRIC: %d, IDTIMER: %d\n",processoDisplayGraph,processoDisplayLyric,processoTimer);
  80.         printf("-------------\n");
  81.  
  82.  
  83.  
  84.  
  85.         ///        waitpid(processoDisplayGraph,);
  86.         numeroMusicaAtual++;
  87.  
  88.     }
  89.  
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement