消息队列函数

  A进程
 1 #include <head.h>//所用头文件全部放在head.h文件中
 2 //消息包结构体
 3 struct msgbuf
 4 {
 5     long mtype;
 6     char mtext[128];
 7 };
 8 
 9 void *send(void *arg)
10 {                                                            
11     int msqid=*(int*)arg;
12 
13     struct msgbuf snd;
14     snd.mtype=1; 
15     while(1)
16     {
17         printf("A说>>>");
18         fgets(snd.mtext,sizeof(snd.mtext),stdin);
19         snd.mtext[strlen(snd.mtext)-1]='\0';
20 
21         //以阻塞的方式发送
22         if(msgsnd(msqid,&snd,sizeof(snd.mtext),0)<0)
23         {
24             perror("msgsnd");
25             break;
26         }
27         if(strcmp(snd.mtext, "quit") == 0)
28             exit(1);
29     }
30     pthread_exit(NULL);
31 }
32 
33 void *rcv(void *arg)
34 {
35     int msqid=*(int*)arg;
36     //消息包
 37     struct msgbuf rcv;
 38     ssize_t res=0;
 39 
 40     while(1)
 41     {
 42         res=msgrcv(msqid,&rcv,sizeof(rcv.mtext),2,0);           
 43         if(res<0)
 44         {
 45             perror("msgcrv");
 46 
 47             msgctl(msqid,IPC_RMID,NULL);
 48             break;
 49         }
 50         if(strcmp(rcv.mtext, "quit") == 0)
 51         {
 52             exit(1);
 53         }
 54 
 55         printf("A听到的%s\n",rcv.mtext);
 56         fflush(stdout);
 57     }
 58 
 59     pthread_exit(NULL);
 60 }
 61 
 62 int main(int argc, const char *argv[])
 63 {
 64     //计算key值
 65     key_t key = ftok("./",1);
 66     if(key < 0)
 67     {
 68         perror("ftok");
 69         return -1;
 70     }
 71     //创建一个消息队列
 72     int msqid = msgget(key,IPC_CREAT|0777);
 73     if(msqid < 0)
 74     {
 75         perror("msgget");
 76         return -1;
 77     }
 78 
 79     pthread_t tid1, tid2;
 80     //创建一个线程,发送消息包
 81     if(pthread_create(&tid1, NULL,send,(void*)&msqid) != 0)
 82     {
 83         perror("pthread_create");
 84         return -1;
 85     }                                                             
 86 
 87     //创建一个线程,接受消息包
 88     if(pthread_create(&tid2, NULL,rcv, (void*)&msqid) != 0)
 89     {
 90         perror("pthread_create");
 91         return -1;
 92     }
 93 
 94     pthread_join(tid1, NULL);
 95     pthread_join(tid2, NULL);
 96 
 97     return 0;
 98 }
 99 
B进程
 1 #include <head.h>//所用头文件全部放在head.h文件中
 2 
 3 //消息包结构体
 4 struct msgbuf
 5 {
 6     long mtype;
 7     char mtext[128];                                    
 8 };
 9 
10 void *rcv(void *arg)
11 {
12     int msqid=*(int*)arg;
13 
14     struct msgbuf rcv;
15     ssize_t res=0;
16     while(1)
17     {
18         res=msgrcv(msqid,&rcv,sizeof(rcv.mtext),1,0);
19         if(res<0)
20         {
21             perror("msgcrv");
22 
23             msgctl(msqid, IPC_RMID, NULL);
24             break;
25         }
26         if(strcmp(rcv.mtext, "quit") == 0)
27         {
28             exit(1);
29         }
30         printf("B听到的:%s\n",rcv.mtext);
31         fflush(stdout);
32 
33     }
34     pthread_exit(NULL);
35 
36 }
 37                                                          
 38 void *send(void *arg)
 39 {
 40     int msqid=*(int*)arg;
 41     struct msgbuf snd;
 42     snd.mtype=2;
 43 
 44     while(1)
 45     {
 46         printf("B说>>>");
 47         fgets(snd.mtext,sizeof(snd.mtext),stdin);
 48         snd.mtext[strlen(snd.mtext)-1]=0;
 49         //以阻塞的方式发送
 50         if(msgsnd(msqid,&snd,sizeof(snd.mtext),0)<0)
 51         {
 52             perror("msgsnd");
 53             break;
 54         }
 55         if(strcmp(snd.mtext, "quit") == 0)
 56             exit(1);
 57     }
 58     pthread_exit(NULL);
 59 }
 60 
 61 int main(int argc, const char *argv[])
 62 {
 63     //计算key值
 64     key_t key = ftok("./",1);
 65     if(key < 0)
 66     {
 67         perror("ftok");
 68         return -1;
 69     }
 70 
 71     //创建一个消息队列
 72     int msqid = msgget(key,IPC_CREAT|0777);
 73     if(msqid < 0)                                            
 74     {
 75         perror("msgget");
 76         return -1;
 77     }
 78 
 79      pthread_t tid1, tid2;
 80     //创建一个线程,接受消息包
 81     if(pthread_create(&tid2, NULL,rcv, (void*)&msqid) != 0)
 82     {
 83         perror("pthread_create");
 84         return -1;
 85     }
 86 
 87     //创建一个线程,发送消息包
 88     if(pthread_create(&tid1, NULL,send,(void*)&msqid) != 0)
 89     {
 90         perror("pthread_create");
 91         return -1;
 92     }
 93 
 94     pthread_join(tid1, NULL);
 95     pthread_join(tid2, NULL);
 96 
 97     return 0;
 98 }
 99 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值