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 <sys/types.h>

#include <sys/ipc.h>

#include <sys/msg.h>

#include <stdio.h>

#include <string.h>

#define MSGSZ     128

/*

* Declare the message structure.

*/

typedef struct msgbuf {

         long    mtype;

         char    mtext[MSGSZ];

         } message_buf;

main()

{

    int msqid;

    int msgflg = IPC_CREAT | 0666;

    key_t key;

    message_buf sbuf;

    size_t buf_length;

    /*

     * Get the message queue id for the

     * \"name\" 1234, which was created by

     * the server.

     */

  key = 1234;

(void) fprintf(stderr, \"\ msgget: Calling msgget(%#lx,\\

%#o)\ \",

key, msgflg);

    if ((msqid = msgget(key, msgflg )) < 0) {

        perror(\"msgget\");

        exit(1);

    }

    else

     (void) fprintf(stderr,\"msgget: msgget succeeded: msqid = %d\ \", msqid);

    /*

     * We\'ll send message type 1

     */

    

    sbuf.mtype = 1;

   

    (void) fprintf(stderr,\"msgget: msgget succeeded: msqid = %d\ \", msqid);

   

    (void) strcpy(sbuf.mtext, \"Did you get this?\");

   

    (void) fprintf(stderr,\"msgget: msgget succeeded: msqid = %d\ \", msqid);

   

    buf_length = strlen(sbuf.mtext) + 1 ;

   

   

    /*

     * Send a message.

     */

    if (msgsnd(msqid, &sbuf, buf_length, IPC_NOWAIT) < 0) {

       printf (\"%d, %d, %s, %d\ \", msqid, sbuf.mtype, sbuf.mtext, buf_length);

        perror(\"msgsnd\");

        exit(1);

    }

   else

      printf(\"Message: \\\"%s\\\" Sent\ \", sbuf.mtext);

     

  

}

The msgsz argument specifies the length of the message in bytes.

The structure member msgtype is the received message\'s type as specified by the sending process.

The argument msgflg specifies the action to be taken if one or more of the following are true:

These actions are as follows:

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
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