Message queue sender
Thu Jun 06 2024 00:57:28 GMT+0000 (Coordinated Universal Time)
Saved by
@login
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct msgbuf {
long mtype;
char mtext[100];
};
int main() {
key_t key;
int msgid;
struct msgbuf message;
// Generate unique key
key = ftok("progfile", 65);
// Create message queue and return identifier
msgid = msgget(key, 0666 | IPC_CREAT);
if (msgid == -1) {
perror("msgget");
exit(EXIT_FAILURE);
}
// Prepare the message
message.mtype = 1; // Message type
strcpy(message.mtext, "Hello from sender!");
// Send the message
if (msgsnd(msgid, &message, sizeof(message.mtext), 0) == -1) {
perror("msgsnd");
exit(EXIT_FAILURE);
}
printf("Message sent: %s\n", message.mtext);
return 0;
}
content_copyCOPY
Comments