message queue
message queue
c
CopyEdit
if (pid == 0) {
// Receive the message from the message queue
if (msgrcv(msgid, &message, sizeof(message), message.msg_type, 0) == -1) {
perror("msgrcv failed");
exit(EXIT_FAILURE);
}
printf("Child received message: %s\n", message.msg_text);
c
CopyEdit
else {
// Send the message to the message queue
if (msgsnd(msgid, &message, sizeof(message), 0) == -1) {
perror("msgsnd failed");
exit(EXIT_FAILURE);
}
printf("Parent sent message: %s\n", message.msg_text);
c
CopyEdit
msgsnd(msgid, &message, sizeof(message), 0);
Example Output
Execution Flow
mathematica
CopyEdit
Enter a message to send to the child process: Hello, Message Queue!
Parent sent message: Hello, Message Queue!
Child received message: Hello, Message Queue!
Memory Cleanup
The child process removes the message queue after receiving the message.