0% found this document useful (0 votes)
2 views

LAB3 Message queue reader writer

Uploaded by

rohitm04122002
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

LAB3 Message queue reader writer

Uploaded by

rohitm04122002
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

#include <stdio.

h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <string.h>

#define MSG_SIZE 100

struct msg_buffer {
long msg_type;
char msg_text[MSG_SIZE];
};

int main() {
key_t key;
int msgid;
struct msg_buffer message;

// Generate a unique key


key = ftok("writer_reader_communication", 65);

// Create a message queue


msgid = msgget(key, 0666 | IPC_CREAT);

// Get input from the user


printf("Enter message to send: ");
fgets(message.msg_text, MSG_SIZE, stdin);

// Set message type to 1


message.msg_type = 1;

// Send the message


msgsnd(msgid, &message, sizeof(message), 0);

printf("Message sent: %s", message.msg_text);

return 0;
}

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <string.h>

#define MSG_SIZE 100

struct msg_buffer {
long msg_type;
char msg_text[MSG_SIZE];
};

int main() {
key_t key;
int msgid;
struct msg_buffer message;
// Generate the same key as the writer
key = ftok("writer_reader_communication", 65);

// Get the message queue


msgid = msgget(key, 0666 | IPC_CREAT);

// Receive the message


msgrcv(msgid, &message, sizeof(message), 1, 0);

// Print the received message


printf("Message received: %s", message.msg_text);

// Delete the message queue


msgctl(msgid, IPC_RMID, NULL);

return 0;
}

Enter message to send: Hello, reader!


Message sent: Hello, reader!

Message received: Hello, reader!

You might also like