Using C, Create a pair of programs following the producer / consumer model to pa
ID: 3735277 • Letter: U
Question
Using C,
Create a pair of programs following the producer / consumer model to pass messages between each other using a message queue.
The consumer program will be responsible for:
setting up the queue with the following key:
ftok(".", 'Z')
displaying characters that are sent by the producer where the message type is 100
removing the queue and exiting once a lowercase q or uppercase Q is received
The producer will:
access the queue created by the consumer
prompt the user to input one keyboard character at a time followed by the enter key
send the keyboard character to the consumer process via message queue
Also implement error handling in the producer and consumer programs to account for the possibility of the queue creation failing for reasons specified in the man pages of the msgget function
Output should look like this!
Using two terminal at same time
Messege sender has launched! Access Message Queue Ready to send character messages to other program. Type a character followed by the enter key what is up 2487: now exits Type q to terminate programs.Explanation / Answer
ftok(): is use to generate a unique key.
msgget(): either returns the message queue identifier for a newly created message
queue or returns the identifiers for a queue which exists with the same key value.
msgsnd(): Data is placed on to a message queue by calling msgsnd().
msgrcv(): messages are retrieved from a queue.
msgctl(): It performs various operations on a queue. Generally it is use to
destroy message queue.
#include <stdio.h>
#include <sys/ipc.h>
#include <sys/msg.h>
struct mesg_buffer {
long mesg_type;
char mesg_text[100];
} message;
int main()
{
key_t key;
int msgid;
key = ftok(".", Z);
msgid = msgget(key, 98304 | IPC_CREAT);
message.mesg_type = 1;
printf("Write Data : ");
gets(message.mesg_text);
msgsnd(msgid, &message, sizeof(message), 0);
printf("Data send is : %s ", message.mesg_text);
return ‘q’;
}
This is the program which is send by the sender . I have used IPC to write this program.
#include <stdio.h>
#include <sys/ipc.h>
#include <sys/msg.h>
struct mesg_buffer {
long mesg_type;
char mesg_text[100];
} message;
int main()
{
key_t key;
int msgid;
key = ftok(".", Z);
msgid = msgget(key, 98304| IPC_CREAT);
msgrcv(msgid, &message, sizeof(message), 1, 0);
printf("Data Received is : %s ",message.mesg_text);
msgctl(msgid, IPC_RMID, NULL);
return ‘q’;
}
This is the program which is for receiver.
Programs should be executed in IDE
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.