这篇文章讲的非常好非常详细:
使用 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);
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…