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 | // structure for message queue |
| 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 | message.msg_type = 1; |
| 18 | printf("Write Message : "); |
| 19 | fgets(message.msg, 100, stdin); |
| 20 | ret = msgsnd(msg_id, &message, sizeof(message), 0); //send message |
| 21 | printf("Sent message ret is %d message is : %s \n", ret, message.msg); |
| 22 | } |