消息队列传输消息8.5

A进程


```c
 #include<stdio.h>
 #include<sys/ipc.h>
 #include<sys/msg.h>
 #include<string.h>
 #include<stdlib.h>
 #include<sys/types.h>
 #include <pthread.h>
 
 struct msgbuf
 {
     long mtype;
     char mtext[128];
 };
 
 struct msgarr
 {
     long ntype;
     char ntext[128];
 };
 
 int msqid,msqid2;
 
 struct msgarr snd;
 
 struct msgbuf rcv;
 ssize_t res =0;
 
 void* callBack_RD(void* arg)
 {
     while(1)
     {
         res=msgrcv(msqid2,&rcv,sizeof(rcv.mtext),0,0);
         if(res<0)
         {
             perror("msgrcv");
             return NULL;
         }
         printf("res=%ld  [%ld : %s]\n",res,rcv.mtype,rcv.mtext);
 
         if(strcasecmp(rcv.mtext, "quit") == 0)
             exit(0);
     }
     pthread_exit(NULL);
 }
 void* callBack_WR(void* arg)
 {
     while(1)
     {
 
         scanf("%ld",&snd.ntype);
         getchar();
 
         fgets(snd.ntext,sizeof(snd.ntext),stdin);
         snd.ntext[strlen(snd.ntext)-1]=0;
 
 
         if(msgsnd(msqid,&snd,sizeof(snd.ntext),0)<0)
         {
             perror("msgsnd");
             return NULL;
         }
         printf("消息内容发送成功\n");
         if(strcasecmp(snd.ntext, "quit") == 0)
             exit(0);
     }
     pthread_exit(NULL);
 }
 
 int main(int argc, const char *argv[])
 {
     pthread_t tid1, tid2;
     //创建一个线程,写
     if(pthread_create(&tid1, NULL, callBack_WR, NULL) != 0)
     {
         perror("pthread_create");
         return -1;
     }
     //创建一个线程,读
     if(pthread_create(&tid2, NULL, callBack_RD, NULL) != 0)
     {
         perror("pthread_create");
         return -1;
     }
 
 
     key_t key =ftok("./",1);
     if(key<0)
     {
         perror("ftok");
         return -1;
     }
     printf("key=%#x\n",key);
 
     key_t key2 =ftok("./",2);
     if(key2<0)
     {
         perror("ftok");
         return -1;
     }
     printf("key=%#x\n",key2);
 
 
     msqid=msgget(key,IPC_CREAT|0664);
     if(msqid<0)
     {
         perror("msgget");
         return -1;
     }
     printf("msqid= %d\n",msqid);                                                             
 
     msqid2=msgget(key2,IPC_CREAT|0664);
     if(msqid2<0)
     {
         perror("msgget");
         return -1;
     }
     printf("msqid= %d\n",msqid2);
 
     pthread_join(tid1, NULL);
     pthread_join(tid2, NULL);
 
 
     msgctl(msqid2,IPC_RMID,NULL);
 
 
     return 0;
 }

B进程

 #include<stdio.h>
 #include<sys/ipc.h>
 #include<sys/msg.h>
 #include<string.h>
 #include<stdlib.h>
 #include<sys/types.h>
 #include <pthread.h>
 
 struct msgbuf
 {
     long mtype;
     char mtext[128];
 };
 
 struct msgarr
 {
     long ntype;
     char ntext[128];
 };
 int msqid,msqid2;
 
 struct msgbuf rcv;
 ssize_t res =0;
 
 struct msgarr snd;
 void* callBack_RD(void* arg)
 {
     while(1)
     {
         res=msgrcv(msqid,&rcv,sizeof(rcv.mtext),0,0);
         if(res<0)
         {
             perror("msgrcv");
             return NULL;
         }
         printf("res=%ld  [%ld : %s]\n",res,rcv.mtype,rcv.mtext);
 
         if(strcasecmp(rcv.mtext, "quit") == 0)
             exit(0);
     }
     pthread_exit(NULL);
 }
 void* callBack_WR(void* arg)
 {
     while(1)
     {
 
         scanf("%ld",&snd.ntype);
         getchar();
                                                                                                           
 
         fgets(snd.ntext,sizeof(snd.ntext),stdin);
         snd.ntext[strlen(snd.ntext)-1]=0;
 
 
         if(msgsnd(msqid2,&snd,sizeof(snd.ntext),0)<0)
         {
             perror("msgsnd");
             return NULL;
         }
         printf("消息内容发送成功\n");
         if(strcasecmp(snd.ntext, "quit") == 0)
             exit(0);
     }
     pthread_exit(NULL);
 }
 int main(int argc, const char *argv[])
 {
     pthread_t tid1, tid2;
     //创建一个线程,写
     if(pthread_create(&tid1, NULL, callBack_WR, NULL) != 0)
     {
         perror("pthread_create");
         return -1;
     }
     //创建一个线程,读 
     if(pthread_create(&tid2, NULL, callBack_RD, NULL) != 0)
     {
         perror("pthread_create");
         return -1;
     }
     //创建key
     key_t key =ftok("./",1);
     if(key<0)
     {
         perror("ftok");
         return -1;
     }
     printf("key=%#x\n",key);
 
     key_t key2 =ftok("./",2);
     if(key2<0)
     {
         perror("ftok");
         return -1;
     }
     printf("key=%#x\n",key2);
 
 
   //创建消息队列
      msqid=msgget(key,IPC_CREAT|0664);
     if(msqid<0)
     {
         perror("msgget");
         return -1;
     }
     printf("msqid= %d\n",msqid);
 
      msqid2=msgget(key2,IPC_CREAT|0664);
     if(msqid2<0)
     {
         perror("msgget");
         return -1;
     }
     printf("msqid= %d\n",msqid2);
 
 
     pthread_join(tid1, NULL);
     pthread_join(tid2, NULL);
 
     msgctl(msqid,IPC_RMID,NULL);
 
     return 0;
 }

 
 
 
                                                                                                           
                                                                                                           
                                                                                                           
                                                                                                           


 
 
                                                                                              

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值