/* * private-queue-hello-world.c - a "hello world" example in which a single * process creates a message queue and sends * itself a "hello world" message via this queue. */ #include #include #include #include struct msgbuf { long mtype; /* message type, must be > 0 */ char mtext[1]; /* message data */ }; int main(int argc, char* argv[]) { /* create a private message queue, with access only to the owner. */ int queue_id = msgget(IPC_PRIVATE, 0600); struct msgbuf* msg; struct msgbuf* recv_msg; int rc; if (queue_id == -1) { perror("main: msgget"); exit(1); } printf("IPC_PRIVATE=%d\n",IPC_PRIVATE); printf("message queue created, queue id '%d'.\n", queue_id); msg = (struct msgbuf*)malloc(sizeof(struct msgbuf)+strlen("hello world")); msg->mtype = 1; strcpy(msg->mtext, "hello world"); rc = msgsnd(queue_id, msg, strlen(msg->mtext)+1, 0); if (rc == -1) { perror("main: msgsnd"); exit(1); } free(msg); printf("message placed on the queue successfully.\n"); recv_msg = (struct msgbuf*)malloc(sizeof(struct msgbuf)+strlen("hello world")); rc = msgrcv(queue_id, recv_msg, strlen("hello world")+1, 0, 0); if (rc == -1) { perror("main: msgrcv"); exit(1); } printf("msgrcv: received message: mtype '%d'; mtext '%s'\n", recv_msg->mtype, recv_msg->mtext); return 0; }