Haifisch7734

Watki

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