Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <sys/msg.h>
- #include <pthread.h>
- struct msgbuf{
- long type;
- int msg;
- } buf,buf2,buf3;
- void *wyslij(void* arg){
- int i,x;
- buf.type = 21;
- for(i = 0; i<10;i++){
- int* qid = (int*) arg;
- x = (rand() % 10);
- buf.msg = x;
- msgsnd((*qid),&buf,sizeof(buf)-sizeof(long),0);
- }
- }
- void *odbierz(void* arg){
- int i,s = 0;
- int* qid = (int*) arg;
- for(i = 0;i<10;i++){
- msgrcv((*qid),&buf2,sizeof(buf2)-sizeof(long),21,0);
- s += buf2.msg;
- }
- buf2.type = 22;
- buf2.msg = s;
- msgsnd((*qid),&buf2,sizeof(buf2)-sizeof(long),0);
- }
- void *pokaz(void* arg){
- int* qid = (int*) arg;
- msgrcv((*qid),&buf3,sizeof(buf3)-sizeof(long),22,0);
- printf("%d\n",buf3.msg);
- }
- int main(){
- srand(4324);
- int qid = msgget(IPC_PRIVATE,IPC_CREAT | 0666);
- pthread_t w,o,p;
- pthread_create(&w,NULL,wyslij,&qid);
- pthread_create(&o,NULL,odbierz,&qid);
- pthread_create(&p,NULL,pokaz,&qid);
- pthread_join(w,NULL);
- pthread_join(o,NULL);
- pthread_join(p,NULL);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment