Please help Write a C program msqQTestc that creates a messa

Please help

Write a C program (msqQTest.c) 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_NOWIAT or not in the msgrcv(…) system call.

Report your observation and justify your answer (use the next section, background material, to understand the behavior of msg queues)

Solution

#include #include #include #include static int askuser(); extern void exit(); extern char *malloc(); extern void perror(); char first_on_queue[] = \"-> first message on queue\", full_buf[] = \"Message buffer overflow. Extra message discarded.\"; main() { register int c; /* message text input */ int choice; /* user\'s selected operation code */ register int i; /* loop control for mtext */ int msgflg; /* message flags for the operation */ struct msgbuf *msgp; /* pointer to the message buffer */ int msgsz; /* message size */ long msgtyp; /* desired message type */ int msqid, /* message queue ID to be used */ maxmsgsz, /* size of allocated message buffer */ rtrn; /* return value from msgrcv or msgsnd */ /* Get the message queue ID and set up the message buffer. */ (void) fprintf(stderr, \"Enter msqid: \"); (void) scanf(\"%i\", &msqid); (void) fprintf(stderr, \"Enter the message buffer size you want:\"); (void) scanf(\"%i\", &maxmsgsz); if (maxmsgsz < 0) { (void) fprintf(stderr, \"msgop: %s\ \", \"The message buffer size must be >= 0.\"); exit(1); } msgp = (struct msgbuf *)malloc((unsigned)(sizeof(struct msgbuf) - sizeof msgp->mtext + maxmsgsz)); if (msgp == NULL) { (void) fprintf(stderr, \"msgop: %s %d byte messages.\ \", \"could not allocate message buffer for\", maxmsgsz); exit(1); } /* Loop through message operation until the user is ready to quit. */ while (choice = ask()) { switch (choice) { case 1: /* msgsnd() requested: Get the arguments, make the call, and report the results. */ (void) fprintf(stderr, \"Valid msgsnd message %s\ \", \"types are positive integers.\"); (void) fprintf(stderr, \"Enter msgp->mtype: \"); (void) scanf(\"%li\", &msgp->mtype); if (maxmsgsz) { while ((c = getchar()) != \'\ \' && c != EOF); (void) fprintf(stderr, \"Enter a %s:\ \", \"one line message\"); for (i = 0; ((c = getchar()) != \'\ \'); i++) { if (i >= maxmsgsz) { (void) fprintf(stderr, \"\ %s\ \", full_buf); while ((c = getchar()) != \'\ \'); break; } msgp->mtext[i] = c; } msgsz = i; } else msgsz = 0; (void) fprintf(stderr,\"\ Meaningful msgsnd flag is:\ \"); (void) fprintf(stderr, \"\\tIPC_NOWAIT =\\t%#8.8o\ \", IPC_NOWAIT); (void) fprintf(stderr, \"Enter msgflg: \"); (void) scanf(\"%i\", &msgflg); (void) fprintf(stderr, \"%s(%d, msgp, %d, %#o)\ \", \"msgop: Calling msgsnd\", msqid, msgsz, msgflg); (void) fprintf(stderr, \"msgp->mtype = %ld\ \", msgp->mtype); (void) fprintf(stderr, \"msgp->mtext = \\\"\"); for (i = 0; i < msgsz; i++) (void) fputc(msgp->mtext[i], stderr); (void) fprintf(stderr, \"\\\"\ \"); rtrn = msgsnd(msqid, msgp, msgsz, msgflg); if (rtrn == -1) perror(\"msgop: msgsnd failed\"); else (void) fprintf(stderr, \"msgop: msgsnd returned %d\ \", rtrn); break; case 2: /* msgrcv() requested: Get the arguments, make the call, and report the results. */ for (msgsz = -1; msgsz < 0 || msgsz > maxmsgsz; (void) scanf(\"%i\", &msgsz)) (void) fprintf(stderr, \"%s (0 <= msgsz <= %d): \", \"Enter msgsz\", maxmsgsz); (void) fprintf(stderr, \"msgtyp meanings:\ \"); (void) fprintf(stderr, \"\\t 0 %s\ \", first_on_queue); (void) fprintf(stderr, \"\\t>0 %s of given type\ \", first_on_queue); (void) fprintf(stderr, \"\\t<0 %s with type <= |msgtyp|\ \", first_on_queue); (void) fprintf(stderr, \"Enter msgtyp: \"); (void) scanf(\"%li\", &msgtyp); (void) fprintf(stderr, \"Meaningful msgrcv flags are:\ \"); (void) fprintf(stderr, \"\\tMSG_NOERROR =\\t%#8.8o\ \", MSG_NOERROR); (void) fprintf(stderr, \"\\tIPC_NOWAIT =\\t%#8.8o\ \", IPC_NOWAIT); (void) fprintf(stderr, \"Enter msgflg: \"); (void) scanf(\"%i\", &msgflg); (void) fprintf(stderr, \"%s(%d, msgp, %d, %ld, %#o);\ \", \"msgop: Calling msgrcv\", msqid, msgsz, msgtyp, msgflg); rtrn = msgrcv(msqid, msgp, msgsz, msgtyp, msgflg); if (rtrn == -1) perror(\"msgop: msgrcv failed\"); else { (void) fprintf(stderr, \"msgop: %s %d\ \", \"msgrcv returned\", rtrn); (void) fprintf(stderr, \"msgp->mtype = %ld\ \", msgp->mtype); (void) fprintf(stderr, \"msgp->mtext is: \\\"\"); for (i = 0; i < rtrn; i++) (void) fputc(msgp->mtext[i], stderr); (void) fprintf(stderr, \"\\\"\ \"); } break; default: (void) fprintf(stderr, \"msgop: unknown operation\ \"); break; } } exit(0); } static askuser() { int resp; /* User\'s response. */ do { (void) fprintf(stderr, \"Your options are:\ \"); (void) fprintf(stderr, \"\\tExit =\\t0 or Control-D\ \"); (void) fprintf(stderr, \"\\tmsgsnd =\\t1\ \"); (void) fprintf(stderr, \"\\tmsgrcv =\\t2\ \"); (void) fprintf(stderr, \"Enter your choice: \"); /* set response so \"^D\" will be interpreted as exit. */ resp = 0; (void) scanf(\"%i\", &resp); } while (resp < 0 || resp > 2); return(resp); }
Please help Write a C program (msqQTest.c) that creates a message queue to communicate between processes. The program expects inputs from the user through the a
Please help Write a C program (msqQTest.c) that creates a message queue to communicate between processes. The program expects inputs from the user through the a

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site