简体   繁体   中英

Modifying C message queue programs for use in Linux

I've been working through this guide which outlines how to deal with message queues in Linux, and so far I haven't had any trouble.

http://beej.us/guide/bgipc/output/html/multipage/mq.html

However, I was wondering what I would have to do to modify the sample programs so that the message is delivered to a different recipient, depending on the message that is sent. Eg: If message begins with the text 'URGENT', deliver to recipient A, else, deliver to recipient B.

I know I will need a second recipient class, but am not sure how to implement this feature.

I know I will need a second recipient class, but am not sure how to implement this feature.

By a second recipient class you have got to mean a second message queue. Since the message queue identifier key in the sample programs is created by key = ftok("kirk.c", 'B') , an obvious choice for the second key is keyurg = ftok("kirk.c", 'A') , with which you msgget() the second queue (say msqidurg ) in the sender as well as in the receiver. When it comes to sending a message:

        if (msgsnd(strncmp(buf.mtext, "URGENT", 6) ? msqid : msqidurg, &buf, len+1, 0) < 0)
            perror("msgsnd");

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