Linux应用程序: IPC之消息队列(POSIX)

本文给出了一个demo,说明Linux应用编程中mq_open/mq_send/mq_receive几个API的基本使用方法。

#include <fcntl.h>
#include <sys/stat.h>
#include <mqueue.h>
#include <stdio.h>
#include <unistd.h>

#define TEST_MQ_FILE_NAME "/test_mq"
#define TEST_MESSAGE_SIZE 100
int main(int argc, char **argv)
{
    mqd_t mqd;
    struct mq_attr attr= {
        .mq_flags = 0,
        .mq_maxmsg = 1,
        .mq_msgsize = TEST_MESSAGE_SIZE,
        .mq_curmsgs = 0
    };
    char msg[TEST_MESSAGE_SIZE];
    int count;
    int ret;
    if((argc != 2) || (argv[1][0] != '0' && argv[1][0] != '1')) {
        printf("usage: %s 0|1\n", argv[0]);
        return -1;
    }
    mqd = mq_open(TEST_MQ_FILE_NAME, O_RDWR | O_CREAT, 0777, &attr);
    if(mqd < 0) {
        perror("mq_open");
        return -1;
    }
    if(argv[1][0] == '0') {
        /* send */
        while(1) {
            sprintf(msg, "test msg %d", count++);
            ret = mq_send(mqd, msg, sizeof(msg), 0);
            if(ret < 0) {
                perror("mq_send");
                continue;
            }
            sleep(1);
        }
    } else {
        /* receive */
        while(1) {
            ret = mq_receive(mqd, msg, sizeof(msg), NULL);
            if(ret < 0) {
                perror("mq_receive");
            }
            printf("received msg: %s\n", msg);
        }
    }
    return 0;
}

程序中创建了一个消息队列,并通过参数选择本进程是send还是receive。

消息队列被创建后,可以通过mount命令查看它的状态:

mkdir /tmp/mq
mount -t mqueue none /tmp/mq/
cat /tmp/mq/test_mq

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我可以帮您解答这个问题。 首先,消息队列是一种进程间通信(IPC)机制,它允许多个进程通过在队列中发送和接收消息来进行通信。在Linux中,可以使用System V IPCPOSIX IPC来创建消息队列。 下面是一个简单的石头、剪子、布游戏的C程序,使用System V IPC消息队列机制,其中包含两个进程:一个进程接收玩家输入,另一个进程生成随机的游戏选择并进行比较。 ```c #include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include <sys/ipc.h> #include <sys/msg.h> #include <time.h> #define MSG_SIZE 128 #define MSG_TYPE 1 struct msgbuf { long mtype; char mtext[MSG_SIZE]; }; int main() { int msqid; struct msgbuf msg; key_t key; // 创建一个唯一的键值,用于访问消息队列 if ((key = ftok(".", 'R')) == -1) { perror("ftok"); exit(1); } // 创建消息队列,如果已经存在则直接获取它的ID if ((msqid = msgget(key, 0666 | IPC_CREAT)) == -1) { perror("msgget"); exit(1); } // 游戏循环 while (1) { // 等待玩家输入 printf("请输入石头、剪子或布:\n"); fgets(msg.mtext, MSG_SIZE, stdin); // 将消息类型设置为1,表示这是玩家输入的消息 msg.mtype = MSG_TYPE; msgsnd(msqid, &msg, strlen(msg.mtext) + 1, 0); // 等待另一个进程的响应 msgrcv(msqid, &msg, MSG_SIZE, MSG_TYPE + 1, 0); printf("电脑出了:%s", msg.mtext); // 判断胜负 if (strcmp(msg.mtext, "石头\n") == 0) { printf("平局!\n"); } else if (strcmp(msg.mtext, "剪子\n") == 0) { printf("你胜利了!\n"); } else if (strcmp(msg.mtext, "布\n") == 0) { printf("电脑胜利了!\n"); } } // 删除消息队列 if (msgctl(msqid, IPC_RMID, NULL) == -1) { perror("msgctl"); exit(1); } return 0; } ``` 下面是另一个进程,它生成随机的游戏选择并将其发送到消息队列中,等待另一个进程的响应。 ```c #include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include <sys/ipc.h> #include <sys/msg.h> #include <time.h> #define MSG_SIZE 128 #define MSG_TYPE 1 struct msgbuf { long mtype; char mtext[MSG_SIZE]; }; int main() { int msqid; struct msgbuf msg; key_t key; // 创建一个唯一的键值,用于访问消息队列 if ((key = ftok(".", 'R')) == -1) { perror("ftok"); exit(1); } // 获取消息队列ID if ((msqid = msgget(key, 0666)) == -1) { perror("msgget"); exit(1); } // 游戏循环 while (1) { // 等待玩家输入 msgrcv(msqid, &msg, MSG_SIZE, MSG_TYPE, 0); // 生成游戏选择 srand(time(NULL)); int choice = rand() % 3; if (choice == 0) { strcpy(msg.mtext, "石头\n"); } else if (choice == 1) { strcpy(msg.mtext, "剪子\n"); } else { strcpy(msg.mtext, "布\n"); } // 将消息类型设置为2,表示这是电脑的响应消息 msg.mtype = MSG_TYPE + 1; msgsnd(msqid, &msg, strlen(msg.mtext) + 1, 0); } return 0; } ``` 这个程序使用了两个进程来实现石头、剪子、布游戏,它们通过消息队列进行通信。玩家输入的消息被发送到消息队列中,另一个进程接收该消息并生成随机的游戏选择,将其发送到消息队列中,等待另一个进程的响应。比较选择,判断胜负,并输出结果。 这是一个简单的例子,仅用于演示如何使用消息队列进行进程间通信。实际上,还有其他更好的IPC机制,如管道、信号和共享内存。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值