/*执行两个程序,程序间通过一个消息队列实现同时收发消息*/
#include <myhead.h>
// 消息队列的大小
#define SIZE (sizeof(struct msgbuf) - sizeof(long))
// 消息队列结构体
struct msgbuf
{
    long mtype;       // 消息类型
    char mtext[1024]; // 消息内容
};
// 创建消息队列并返回消息队列ID
int create_msg_queue(key_t key)
{
    int msqid = msgget(key, IPC_CREAT | 0664);
    if (msqid == -1)
    {
        perror("msgget error");
        exit(EXIT_FAILURE);
    }
    return msqid;
}
int main(int argc, const char *argv[])
{
    // 1、创建key值,用于生产消息队列
    key_t key = ftok("/", 'k');
    if (key == -1)
    {
        perror("ftok error");
        return -1;
    }
    printf("key = %#x\n", key);
    // 2、通过key值创建一个消息队列
    int msqid = create_msg_queue(key);
    struct msgbuf buf;
    pid_t pid = fork();
    if (pid > 0) // 父进程
    {
        while (1)
        {
            buf.mtype = 1;
            printf("请输入消息:\n");
            fgets(buf.mtext, SIZE, stdin);        // 从终端获取数据
            buf.mtext[strlen(buf.mtext) - 1] = 0; // 将换行换成 '\0'
            // 将消息发送到消息队列中
            msgsnd(msqid, &buf, SIZE, 0);
            printf("发送成功\n");
            if (strcmp(buf.mtext, "quit") == 0)
            {
                break;
            }
        }
    }
    else if (pid == 0) // 子进程
    {
        while (1)
        {
            msgrcv(msqid, &buf, SIZE, 2, 0);
            printf("收到消息为:%s\n", buf.mtext);
            if (strcmp(buf.mtext, "quit") == 0)
            {
                //printf ("1111");
                 _exit(EXIT_SUCCESS);
                //break;
            }
            //printf("请输入消息类型:\n");
            printf("请输入消息:\n");
        }
    }
    // 父进程等待子进程结束
    if (pid > 0)
        wait(NULL);
    // 删除消息队列
    if (msgctl(msqid, IPC_RMID, NULL) == -1)
    {
        perror("msgctl error");
        return -1;
    }
    return 0;
}另一个程序基本相同,发送2类信号,接收1类信号
#include <myhead.h>
// 消息队列的大小
#define SIZE (sizeof(struct msgbuf) - sizeof(long))
// 消息队列结构体
struct msgbuf
{
    long mtype;       // 消息类型
    char mtext[1024]; // 消息内容
};
// 创建消息队列并返回消息队列ID
int create_msg_queue(key_t key)
{
    int msqid = msgget(key, IPC_CREAT | 0664);
    if (msqid == -1)
    {
        perror("msgget error");
        exit(EXIT_FAILURE);
    }
    return msqid;
}
int main(int argc, const char *argv[])
{
    // 1、创建key值,用于生产消息队列
    key_t key = ftok("/", 'k');
    if (key == -1)
    {
        perror("ftok error");
        return -1;
    }
    printf("key = %#x\n", key);
    // 2、通过key值创建一个消息队列
    int msqid = create_msg_queue(key);
    struct msgbuf buf;
    pid_t pid = fork();
    if (pid > 0) // 父进程
    {
        while (1)
        {
            buf.mtype = 2;
            printf("请输入消息:\n");
            fgets(buf.mtext, SIZE, stdin);        // 从终端获取数据
            buf.mtext[strlen(buf.mtext) - 1] = 0; // 将换行换成 '\0'
            // 将消息发送到消息队列中
            msgsnd(msqid, &buf, SIZE, 0);
            printf("发送成功\n");
            if (strcmp(buf.mtext, "quit") == 0)
            {
                break;
            }
        }
    }
    else if (pid == 0) // 子进程
    {
        while (1)
        {
            msgrcv(msqid, &buf, SIZE, 1, 0);
            printf("收到消息为:%s\n", buf.mtext);
            if (strcmp(buf.mtext, "quit") == 0)
            {
                _exit(EXIT_SUCCESS);
                // break;
            }
            //printf("请输入消息类型:\n");
            printf("请输入消息:\n");
        }
    }
    // 父进程等待子进程结束
    if (pid > 0)
        wait(NULL);
    // 删除消息队列
    if (msgctl(msqid, IPC_RMID, NULL) == -1)
    {
        perror("msgctl error");
        return -1;
    }
    return 0;
}运行结果:




















