[英]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.