Please help Write a C program that creates a message queue t
Please help
Write a C program that creates a message queue to communicate between processes. The program expects inputs from the user through the argument list passed from the command prompt. The application can do one of the following functions:
Create a message queue using the –c/C flag and the key entered by the user to identify the msg queue:
msqQTest -c/C <key>
Send a message of specific type to the intended message queue using the –s/S flag and the key used to create the msg queue. This option will print the message that was sent to the queue: msqQTest -s/S <key> <type> <text>
Receive a message of specific type from the intended message queue using the key used to create the msg queue:
msqQTest -r/R <key> <type>
Delete a mesg queue using the key used to create the msg queue: msqQTest -d/D <key>
If the user input is missing parameters, then the application should give different messages to help the user figure out the correct usage of the command.
2.)Describe the behavior of your program when the IPC_NOWAIT option is used when sending and receiving messages to a message queue:
Add a fixed number (e.g. 3) messages of a specific type (e.g. type 5)
Read messages of a specific type (e.g. type 5). When all messages of that type are read, your process will behave differently depending on whether you used IPC_NOWAIT or not in the msgrcv(…) system call.
Report your observation and justify your answer .
Solution
Code for Creating, Sending, Receiving & Delete Message Queue for IPC using command line argument is as follows:
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define MAXSIZE 128
void die(char *s)
{
perror(s);
exit(1);
}
struct msgbuf
{
long mtype;
char mtext[MAXSIZE];
};
int main(int argc, char *argv[])
{
int msqid;
int msgflg = IPC_CREAT | 0666;
key_t key;
struct msgbuf sbuf;
size_t buflen;
if( argc > 2 && argc < 6) {
if( (strcmp(argv[1],\"-c\") == 0) || (strcmp(argv[1],\"-C\") == 0) ){
if((key = atoi(argv[2])) != 0)
{
if ((msqid = msgget(key, msgflg)) < 0) //Get the message queue ID for the given key
{
printf(\"Msg ID: %d\ \",msqid);
die(\"msgget\");
}
printf(\"Create Queue\ \");
}
else
{
printf(\"Wrong Msg Key\ \");
}
}
else if ((strcmp(argv[1],\"-s\") == 0) || (strcmp(argv[1],\"-S\") == 0)) {
if((key = atoi(argv[2])) != 0)
{
if((sbuf.mtype = atoi(argv[3])) !=0){
strcpy(sbuf.mtext, argv[4]);
buflen = strlen(sbuf.mtext) + 1 ;
if ((msqid = msgget(key,0)) < 0) //Get the message queue ID for the given key
{
printf (\"Fail to get msg Queue ID : %d\ \",msqid);
}
else
{
if (msgsnd(msqid, &sbuf, buflen, IPC_NOWAIT) < 0)
{
printf (\"%d, %ld, %s, %ld\ \", msqid, sbuf.mtype, sbuf.mtext, buflen);
die(\"msgsnd\");
}
printf(\"Msg Sent\ \");
}
}
else
{
printf(\"Wrong Type\ \");
}
}
else
{
printf(\"Wrong Msg Key\ \");
}
}
else if ((strcmp(argv[1],\"-r\") == 0) || (strcmp(argv[1],\"-R\") == 0)){
if((key = atoi(argv[2])) != 0)
{
if ((msqid = msgget(key,0)) < 0) //Get the message queue ID for the given key
{
printf (\"Fail to get msg Queue ID : %d\ \",msqid);
}
else
{
if (msgrcv(msqid, &sbuf, MAXSIZE, 1 , 0) < 0)
{
die(\"msgrcv\");
}
printf (\"%d, %ld, %s\ \", msqid, sbuf.mtype, sbuf.mtext);
printf(\"Msg Received\ \");
}
}
else
{
printf(\"Wrong Msg Key\ \");
}
}
else if ((strcmp(argv[1],\"-d\") == 0) || (strcmp(argv[1],\"-D\") == 0)){
if((key = atoi(argv[2])) != 0)
{
if ((msqid = msgget(key,0)) < 0) //Get the message queue ID for the given key
{
printf(\"Fail to delete Msg Queue\");
die(\"msgget\");
}
msgctl(msqid, IPC_RMID, NULL);
printf(\"Delete Queue\ \");
}
else
{
printf(\"Wrong Msg Key\ \");
}
}
else {
printf(\"Please type correct command:\ \");
printf(\"For Creating Queue : -c/C\ \");
printf(\"For Deleting Queue : -d/D\ \");
printf(\"For Sending Queue : -s/S\ \");
printf(\"For Receiving Queue: -r/R\ \");
}
}
else {
printf(\"No or wrong Argument\ \");
}
return 1;
}
Part - 2
If message Flag is IPC_NOWAIT then the calling thread shall return immediately with a return value of -1 and set \"errno\" to [ENOMSG].


