消息队列实现进程间双向通信

这里有一个进程server,另一个进程client,server进程的主线程写数据,100标识,子线程读标识为200的数据,client进程的主线程读200的数据,子线程写100的数据。
server.c:
#include "sys/types.h"
#include "signal.h"
#include "unistd.h"
#include "stdio.h"
#include "stdlib.h"
#include "sys/msg.h"
#include "string.h"
//消息队列实现线程间通信
struct msgbuf{
        long type;
        char voltage[128];
        char ID[4];
};
int main(int argc,char * argv[]){
        struct msgbuf sendbuf;
        int msgid;
        struct msgbuf readbuf;
        int rednum;
        int key;
        int pid;
        key =ftok("./a.c",'a');
        if(key<0){
                printf("create key failure");
                return -2;
        }
        msgid=msgget(key,IPC_CREAT|0777);
        if(msgid<0){
                printf("create message queue failure\n");
                return -1;
        }
        printf("create message queue  success msgid=%d\n",msgid);
        system("ipcs -q");
        pid =fork();
        if(pid>0){//parent process write 100
                //write message queue
                sendbuf.type=100;
                while(1){
                        memset(sendbuf.voltage,0,128);//clear sendbuf;
                        printf("parent input msg:\n");
                        fgets(sendbuf.voltage,124,stdin);//键盘输入
                        //start write message to msg 
                        msgsnd(msgid,(void *)&sendbuf,strlen(sendbuf.voltage),0);
                }
        }
        if(pid==0){//child process read 200
                while(1){
                        memset(readbuf.voltage,0,128);
                        msgrcv(msgid,(void *) &readbuf,124,200,0);
                        printf("child process read 200 meg:%s",readbuf.voltage);
                }
        }
        msgctl(msgid,IPC_RMID,NULL);//删除消息队列
        system("ipcs -q");
        return 0;
}


client.c
#include "sys/types.h"
#include "signal.h"
#include "unistd.h"
#include "stdio.h"
#include "stdlib.h"
#include "sys/msg.h"
#include "string.h"
struct msgbuf{
        long type;
        char voltage[128];
        char ID[4];
};
int main(int argc,char * argv[]){
        struct msgbuf sendbuf;
        int msgid;
        struct msgbuf readbuf;
        int rednum;
        int key;
        int pid;
        key =ftok("./a.c",'a');
        if(key<0){
                printf("create key failure");
                return -2;
        }
        msgid=msgget(key,IPC_CREAT|0777);
        if(msgid<0){
                printf("create message queue failure\n");
                return -1;
        }
        printf("create message queue  success msgid=%d\n",msgid);
        system("ipcs -q");
        pid =fork();
        if(pid==0){//child  process write 100
                //write message queue
                sendbuf.type=200;
                while(1){
                        memset(sendbuf.voltage,0,128);//clear sendbuf;
                        printf("parent input msg:\n");
                        fgets(sendbuf.voltage,124,stdin);//键盘输入
                        //start write message to msg 
                        msgsnd(msgid,(void *)&sendbuf,strlen(sendbuf.voltage),0);
                }
        }
        if(pid>0){//parent process read 200
                while(1){
                        memset(readbuf.voltage,0,128);
                        msgrcv(msgid,(void *) &readbuf,124,100,0);
                        printf("child process read 200 meg:%s",readbuf.voltage);
                }
        }
        msgctl(msgid,IPC_RMID,NULL);//删除消息队列
        system("ipcs -q");
        return 0;
}


    


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值