Guest User

Untitled

a guest
Nov 26th, 2019
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.93 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<unistd.h>
  3. #include<string.h>
  4. #include<sys/types.h>
  5. #include<sys/stat.h>
  6. #include<sys/fcntl.h>
  7.  
  8. //FIFO W JEDNYM - RODZIC I POTOMEK
  9.  
  10. void read_from_fifo(int descriptor)
  11. {
  12.     char message[100]="";
  13.     if(read(descriptor,message,sizeof(message))<0)                             
  14.         perror("read");
  15.     printf("Odebrana wiadomość: %s\n",message);
  16. }
  17.  
  18. void write_to_fifo(int descriptor)
  19. {
  20.     char *message = "Systemy Operacyjne";                                      
  21.     if(write(descriptor,message,strlen(message))<0)                            
  22.         perror("write");
  23. }
  24.  
  25. int main(void)
  26. {
  27.     if(mkfifo("fifo",0600)<0)                                                  
  28.         perror("mkfifo");
  29.     int descriptor = open("fifo",O_RDWR);                                      
  30.     if(descriptor<0)
  31.         perror("open");
  32.     int pid = fork();
  33.     if (pid<0)
  34.         perror("fork");
  35.     if (pid==0)
  36.         write_to_fifo(descriptor);
  37.     else
  38.         read_from_fifo(descriptor);
  39.     if(close(descriptor)<0)
  40.         perror("close");
  41.     if(unlink("fifo")<0)                                                       
  42.         perror("unlink");      
  43.     return 0;
  44. }
Advertisement
Add Comment
Please, Sign In to add comment