[英]Inter Process Communication 'URGENT' message types
在文件kirk.c
,我們可以引入一個枚舉變量,該變量將定義要發送的消息的類型。 結構struct my_msgbuf
具有成員變量long mtype;
。 可以將其初始化為消息類型,然后再將其發送給接收者,在本例中為spock.c
。
枚舉聲明:
enum msgType {
URGENT = 0,
NORMAL = 1
};
上面的枚舉聲明可以放在kirk.c
和spock.c
的公共頭文件中。
行buf.mtype = 1; /* we don't really care in this case */
buf.mtype = 1; /* we don't really care in this case */
應該修改為buf.mtype = URGENT; /* we don't really care in this case */
buf.mtype = URGENT; /* we don't really care in this case */
在spock.c
,以下幾行:
for(;;) { /* Spock never quits! */
if (msgrcv(msqid, &buf, sizeof(buf.mtext), 0, 0) == -1) {
perror("msgrcv");
exit(1);
}
printf("spock: \"%s\"\n", buf.mtext);
}
..將修改為:
for(;;) { /* Spock never quits! */
if (msgrcv(msqid, &buf, sizeof(buf.mtext), 0, 0) == -1) {
perror("msgrcv");
exit(1);
}
if (URGENT != buf.mtype) {
printf("spock: \"%s\"\n", buf.mtext);
}
}
因此,只有消息類型不是URGENT
,spock才會“處理”消息(在這種情況下,它只是打印數據作為處理數據的一種方式)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.