Haifisch7734

fifo

May 7th, 2014
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.90 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <unistd.h>
  4. #include <pthread.h>
  5.  
  6.  
  7. void *wyslij(void* arg){
  8. int i,x;
  9. int fid = open("fifo12",0666);
  10. for(i = 0; i<10;i++){  
  11.     int* qid = (int*) arg;
  12.     x = (rand() % 10);
  13.     write(fid,&x,4);
  14.     }
  15. }
  16.  
  17. void *odbierz(void* arg){
  18. int i,s,buf = 0;
  19. int fid = open("fifo12",0666);
  20. for(i = 0;i<10;i++){
  21.    
  22.     read(fid,&buf,4);
  23.     s += buf;
  24.     }
  25. close(fid);
  26. fid = open("fifo23",0666);
  27. write(fid,&s,4);
  28. }
  29.  
  30. void *pokaz(void* arg){
  31.     int buf;
  32.     int fid = open("fifo23",0666);
  33.     read(fid,&buf,4);
  34. printf("%d\n",buf);
  35. close(fid);
  36. }
  37.  
  38. int main(){
  39. srand(4324);
  40. mkfifo("fifo12",0666);
  41. mkfifo("fifo23",0666);
  42. pthread_t w,o,p;
  43. pthread_create(&w,NULL,wyslij,NULL);
  44. pthread_create(&o,NULL,odbierz,NULL);
  45. pthread_create(&p,NULL,pokaz,NULL);
  46.  
  47. pthread_join(w,NULL);
  48. pthread_join(o,NULL);
  49. pthread_join(p,NULL);
  50. unlink("./fifo12");
  51. unlink("./fifo23");
  52. return 0;
  53.  
  54. }
Advertisement
Add Comment
Please, Sign In to add comment