您好我正在使用一些共享內存在不同的進程讀取和寫入數據。我正在使用消息隊列來存儲數據在讀取和寫入操作之間發生更改的消息。消息隊列。 msgsend msgrcv。系統V在C系統調用(Linux)
/* 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
指向實際的消息?如何從消息中讀取內容,以便我可以顯示它們?我假設這應該在某種循環中完成。