Welcome to MLink Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
259 views
in Technique[技术] by (71.8m points)

Linux 如何創建並激活Message Queues

我需要創建並激活message queue,目前 我用了 msgget(key, msgflg) 創建了一個message queue,如果我沒做錯的話,但現在老師要我們激活他,我不是很懂他什麽意思,搜索了好多網站又沒有說 有人知道怎麽做嗎?
還需要使用 msgrcv 和 msgsnd 來傳送這些message 如果會的能不能解釋一下, 謝謝!

 Key = ftok(“/tmp”,“a”)
 msgflg = 0644 | IPC_CREATE | IPC_EXCL

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

这篇文章讲的非常好非常详细:
使用 UNIX System V IPC 机制共享应用程序数据 https://www.ibm.com/developer...
包括以下示范代码,不知道是不是你想要的?

#include <sys/types.h>
#include <sys/msg.h>
#include <sys/ipc.h>
#include <string.h>
#include <stdio.h>

int main (void) {

        key_t ipckey;
        int mq_id;
        struct { long type; char text[100]; } mymsg;

        /* Generate the ipc key */
        ipckey = ftok("/tmp/foo", 42);
        printf("My key is %d
", ipckey);

        /* Set up the message queue */
        mq_id = msgget(ipckey, IPC_CREAT | 0666);
        printf("Message identifier is %d
", mq_id);

        /* Send a message */
        memset(mymsg.text, 0, 100); /* Clear out the space */
        strcpy(mymsg.text, "Hello, world!");
        mymsg.type = 1;
        msgsnd(mq_id, &mymsg, sizeof(mymsg), 0);

}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to MLink Developer Q&A Community for programmer and developer-Open, Learning and Share
...