/* Using a message queue IPC */ /* Receiving process */ /* Initial variant */ #include #include #include #include #include #include #include #define MSQSIZ 1024 struct msgbf { long mtype; char mtext [1024]; }; int main (int argc, char * argv[]) {int msid, v; int i; struct msgbf mess; if (argc != 3) {printf ("Usage: \n"); exit (1);} /* Get message queue */ msid = msgget ((key_t) atoi (argv[1]), 0); if (msid == -1) {printf("Cannot get message queue\n"); exit (1);} /* Read a message of the specified type from the specified queue (not more than 100 bytes) */ v = msgrcv(msid,(struct msgbuf *)&mess, 100, atoi(argv[2]), IPC_NOWAIT); if (v < 0){if (errno == ENOMSG) printf("No given message in the queue\n"); else printf ("ERROR reading from queue\n"); } else printf ("[%d] %s\n", mess.mtype, mess.mtext); /* Now remove the message queue */ if (msgctl(msid,IPC_RMID, 0) <0) printf("Error in removing the message queue\n"); else printf("Removed the message queue, with key = %ld\n", atoi(argv[1])); exit(0); }