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

这里有一个进程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;
}


    


你可能感兴趣的:(linux)