lh | 758261d | 2023-07-13 05:52:04 -0700 | [diff] [blame] | 1 | #include <stdio.h> |
| 2 | #include <stdlib.h> |
| 3 | #include <sys/ipc.h> |
| 4 | #include <sys/msg.h> |
| 5 | // Define message queue structure |
| 6 | struct msg_buffer { |
| 7 | long msg_type; |
| 8 | char msg[100]; |
| 9 | } message; |
| 10 | main(int argc,char *argv[]) { |
| 11 | key_t my_key; |
| 12 | int msg_id; |
| 13 | int ret; |
| 14 | my_key = atoi(argv[1]); //create unique key |
| 15 | msg_id = msgget(my_key, 0666 | IPC_CREAT); //create message queue and return id |
| 16 | printf("Message id: %d\n", msg_id); |
| 17 | ret = msgrcv(msg_id, &message, sizeof(message), 1, 0); //used to receive message |
| 18 | // display the message |
| 19 | printf("Received Message len is %d, message is : %s \n", ret, message.msg); |
| 20 | //msgctl(msg_id, IPC_RMID, NULL); //destroy the message queue |
| 21 | return 0; |
| 22 | } |