简体   繁体   中英

Inter Process Communication 'URGENT' message types

I've been going through Beej's guides on inter process communication .

How would 'kirk.c' be modified to be able to send messages marked URGENT? These URGENT messages would then be ignored by 'spock.c' and instead read by another program?

In the file kirk.c , we can introduce an enum variable which will define the type of message to be sent. The structure struct my_msgbuf has a member variable long mtype; . One can initialise this to the message type before sending it to the receiver, in this case - spock.c .

The enum declaration:

enum msgType {
    URGENT = 0,
    NORMAL = 1
};

The above enum declaration can be put in a common header file to both kirk.c and spock.c .

The line buf.mtype = 1; /* we don't really care in this case */ buf.mtype = 1; /* we don't really care in this case */ should be modified to buf.mtype = URGENT; /* we don't really care in this case */ buf.mtype = URGENT; /* we don't really care in this case */

In spock.c , following lines:

for(;;) { /* Spock never quits! */
    if (msgrcv(msqid, &buf, sizeof(buf.mtext), 0, 0) == -1) {
        perror("msgrcv");
        exit(1);
    }
    printf("spock: \"%s\"\n", buf.mtext);
}

..will be modified to:

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);
    }
}

So, spock will "process" (in this case, it just prints the data as a way of processing it) the message only if the message type is anything other than URGENT .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM