嗨,我正在使用一些共享内存,其中不同的进程读取和写入数据。我正在使用消息队列来存储数据在读取和写入操作之间发生更改时的消息。
/* struct that defines a message */
struct msgbuf{
long mtype; /* must be positive */
int childId; //ID of child sending message
int bufferChanged; //Buffer at which value was changed
int beforeValue; //Value before child sleeps
int afterValue; //Value after child sleeps
};
因此,在读写和检查更改时,进程以下列方式存储消息
struct msgbuf msg = {BUFFER_CHANGED, id, position, read, bufferArr[position]};
if(msgsnd(msqid, &msg, sizeof(msg), 0)== -1){
perror("msgsnd in read.write");
}
这工作正常。哦,顺便说一下,这是我创建消息队列的方式。
#define BUFFER_CHANGED 1
qKey = ftok("./", 'A');
msqid = msgget(qKey, (IPC_CREAT | 0666));
/*Perform the following if the call is unsuccessful.*/
if(msqid == -1){
printf ("\nThe msgget call failed, error number = %d\n", errno);
}
/*Return the msqid upon successful completion.*/
else{
printf ("\nMessage queue successful. The msqid = %d\n", msqid);
//exit(0);
}
所以我的问题是我不太确定如何从队列中检索消息并将它们显示在屏幕上。我一直在阅读msgrcv()
系统调用,但对我来说不是很清楚。
rc = msgrcv(msqid, &msg, sizeof(msg), BUFFER_CHANGED, IPC_NOWAIT);
rc
是一个,int
因为msgrcv()
返回一个int
。我如何将int
其指向实际消息?如何从消息中读取内容以便显示它们?我假设这应该在某种循环中完成。