Guest User

Untitled

a guest
Apr 21st, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.13 KB | None | 0 0
  1. for (cid = 0; cid < 2; cid++)
  2. {
  3. printf("Filho criado! id = %d n", cid);
  4.  
  5. if((pid = fork()) == -1)
  6. {
  7. perror("fork");
  8. exit(1);
  9. }
  10.  
  11. else
  12. {
  13.  
  14. if(pid == 0 && cid == 0)
  15. {
  16. /* Child process closes up input side of pipe */
  17. close(fd[0]);
  18.  
  19. /* Send "string" through the output side of pipe */
  20. write(fd[1], string1, (strlen(string1)+1));
  21. exit(0);
  22. }
  23.  
  24. else if(pid == 0 && cid == 1)
  25. {
  26. /* Child process closes up input side of pipe */
  27. close(fd[0]);
  28.  
  29. /* Send "string" through the output side of pipe */
  30. write(fd[1], string2, (strlen(string2)+1));
  31. exit(0);
  32. }
  33.  
  34. else if (pid > 1)
  35. {
  36. printf("Eu sou o Papai !n");
  37. }
  38. }
  39. }
  40.  
  41. /*wait for childs*/
  42. while((wpid = wait(&status)) > 0);
  43.  
  44. /* Parent process closes up output side of pipe */
  45. close(fd[1]);
  46.  
  47. /* Read in a string from the pipe */
  48. nbytes = read(fd[0], readbuffer, sizeof(readbuffer));
  49.  
  50. printf("Received string: %s", readbuffer);
  51.  
  52. return(0);
Add Comment
Please, Sign In to add comment