/* sysV IPC message passing - receiver recvmsg_sysV.c meant to work with sendmsg_sysV: start recvmsg_sysV in background, then start sendmsg_sysV */ #include #include #include #include #include #include struct my_msg_st { long int my_msg_type; char some_text[BUFSIZ]; }; int main() { int run = 1; struct my_msg_st some_data; int msqid; long int msg_to_recv = 1; msqid = msgget( (key_t)1234, 0666 | IPC_CREAT); if (msqid == -1) { perror("msgget in recvmsg_sysV failed"); exit(EXIT_FAILURE); } while (run) { if (msgrcv(msqid, &some_data, BUFSIZ, msg_to_recv, 0) == -1) { perror("msgrcv in recvmsg_sysV failed"); exit(EXIT_FAILURE); } printf("You wrote: %s", some_data.some_text); if (strncmp(some_data.some_text, "end", 3) == 0) { run = 0; } } if (msgctl(msqid, IPC_RMID, 0) == -1) { perror("msgctl in recvmsg_sysV failed"); exit(EXIT_FAILURE); } exit(EXIT_SUCCESS); }