linux C 应用消息队列在两个进程间通信

服务:

#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <sys/msg.h>
 
struct msg_st
{
    long int msg_type;
    char text[BUFSIZ];
};
 
int main()
{
    int running = 1;
    int msgid = -1;
    struct msg_st data;
    long int msgtype = 0; //注意1
 
    //建立消息队列
    msgid = msgget((key_t)1234,0666 | IPC_CREAT);
    if(msgid == -1)
    {
        fprintf(stderr,"msgget failed with error: %d\n",errno);
        exit(EXIT_FAILURE);
    }
    //从队列中获取消息,直到遇到end消息为止
    while(running)
    {
        if(msgrcv(msgid,(void*)&data,BUFSIZ,msgtype,0) == -1)
        {
            fprintf(stderr,"msgrcv failed with errno: %d\n",errno);
            exit(EXIT_FAILURE);
        }
        printf("You wrote: %s\n",data.text);
        //遇到end结束
        if(strncmp(data.text,"end",3) == 0)
            running = 0;
    }
    //删除消息队列
    if(msgctl(msgid,IPC_RMID,0) == -1)
    {
        fprintf(stderr,"msgctl(IPC_RMID) failed\n");
        exit(EXIT_FAILURE);
    }
    exit(EXIT_SUCCESS);
}
 

 

 

客户端:

#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/msg.h>
#include <errno.h>
 
#define MAX_TEXT 512
struct msg_st
{
    long int msg_type;
    char text[MAX_TEXT];
};
 
int main()
{
    int running = 1;
    struct msg_st data;
    char buffer[BUFSIZ];
    int msgid = -1;
 
    //建立消息队列
    msgid = msgget((key_t)1234,errno);
        exit(EXIT_FAILURE);
    }
 
    //向消息队列中写消息,直到写入end
    while(running)
    {
        //输入数据
        printf("Enter some text: ");
        fgets(buffer,stdin);
        data.msg_type = 1;    //注意2
        strcpy(data.text,buffer);
        //向队列发送数据
        if(msgsnd(msgid,MAX_TEXT,"msgsnd failed\n");
            exit(EXIT_FAILURE);
        }
        //输入end结束输入
        if(strncmp(buffer,3) == 0)
            running = 0;
        sleep(1);
    }
    exit(EXIT_SUCCESS);
}
 

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐