Advertisement
Guest User

Untitled

a guest
Oct 21st, 2019
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.50 KB | None | 0 0
  1. include <sys/wait.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <unistd.h>
  5. #include <string.h>
  6. #include <fcntl.h>
  7. #include <sys/types.h>
  8. #include <sys/stat.h>
  9.  
  10. int main(int argc, char *argv[])
  11. {
  12. int potok[2];
  13. int strumien;
  14. char wiadomosc[] = "Wiadomosc";
  15.  
  16. int pid;
  17. char bufor[255],bufor1[255],bufor2[255];
  18.  
  19. printf("Podaj tresc wiadomosci /n");
  20. scanf("%s",bufor1);
  21.  
  22. if (pipe(potok) == -1) {
  23. perror("pipe:");
  24. exit(EXIT_FAILURE);
  25. }
  26.  
  27. if (mkfifo("fifo", 0600) == -1) {
  28. perror("Blad");
  29. exit(EXIT_FAILURE);
  30. }
  31.  
  32. fcntl(potok[0], O_NDELAY);
  33. fcntl(potok[1], O_NDELAY);
  34.  
  35. pid = fork();
  36. if (pid == -1) {
  37. perror("fork:");
  38. exit(EXIT_FAILURE);
  39. }
  40.  
  41. if (pid == 0) {
  42. close(potok[1]);
  43. read(potok[0], bufor, sizeof(bufor));
  44. printf("\nWiadomosc od rodzica(nienazwane): %s\n",bufor);
  45. close(potok[0]);
  46.  
  47. strumien = open("fifo", O_WRONLY);
  48. if (strumien == -1) {
  49. perror("Blad otwarcia");
  50. exit(EXIT_FAILURE);
  51. }
  52.  
  53. if (write(strumien, wiadomosc, sizeof(wiadomosc)) == -1) {
  54. perror("Blad zapisu");
  55. exit(EXIT_FAILURE);
  56. }
  57.  
  58. exit(EXIT_SUCCESS);
  59.  
  60. }
  61. else {
  62. close(potok[0]);
  63. write(potok[1], bufor1, strlen(bufor1));
  64. close(potok[1]);
  65.  
  66. strumien = open("fifo", O_RDONLY);
  67. if (strumien == -1)
  68. perror("Blad otwarcia");
  69.  
  70. if (read(strumien, bufor2, 50) == -1) {
  71. perror("Blad odczytu");
  72. exit(1);
  73. }
  74.  
  75. printf("Wiadomosc od potomka(nazwane): %s\n\n", bufor2);
  76. unlink("fifo");
  77. wait(0);
  78.  
  79. return 0;
  80. }
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement