#include #include //ftok() convert pathname + ProjecID to System V IPC key #include #include #include "queue.h" void report_and_exit(const char* msg) { perror(msg); exit(-1); } int main() { //Sys V IPC key key_t key = ftok(PATHNAME, PROJECTID); printf("key is %i\n", key); if(key<0) report_and_exit("key not gotten"); //Create if needed, otherwise access. Not create again. int qid = msgget(key, 0666 | IPC_CREAT); if(qid < 0) report_and_exit("no access to queue..."); //Different from sender.c int types[] = {3,1,2,1,3,2}; int i; //Sender.c uses msgsend(). Receiver.c uses msgrcv() for(i=0; i < MESSAGECOUNT ; i++) { queuedMessage msg; if(msgrcv(qid,&msg, MESSAGELENGTH + 1, types[i], MSG_NOERROR | IPC_NOWAIT) < 0) puts("msgrcv trouble..."); printf("%s received as type %i\n", msg.payload, (int) msg.type); } //Remove the q if(msgctl(qid, IPC_RMID, NULL) < 0) report_and_exit("trouble removing queue..."); return 0; }