Question
Modify the programs so that each program can both receive and send messages alternatively.



Note that when you run the two programs, you should run them in two different windows ( terminals). You should be able to sen
//msg2.cpp /* The sender program is very similar to msgl.cpp. In the main set up, delete the msg to_receive declaration and r
Note that when you run the two programs, you should run them in two different windows ( terminals). You should be able to send messages from one to the other and terminate them by entering "end" //msgl.cpp / Here's the receiver program. / #include #include #include #1nclude #include #include #include Winclude struct my msg st f long int my msg type; char some text [BUFSIz]; ti int main() int runningl; int msgid; truct my msg st some_data; long int msg_ to receive0; First, we set up the message queue. */ msgid msgget ( (key_t)1234, 0666 |IPC CREAT) if (msgid == -1) { fprintf(stderr, "msgget exit (EXIT_FAILURE) failed with %d\n", errno); error: /* Then the messages are retrieved from the queue, until an end message is encountered Lastly, the message queue is deleted. */ while(running) if (msgrcv (msgid, (void )&some data, BUFSIZ, msg to_receive, 0)-1) f fprintf(stderr, "msgrev exit (EXIT FAILURE); failed with %d\n", errno); error: printf( "You wrote: %s", sone, data . some-text); if (strnemp(some data.some text, "end, 3)0) running 0 if (msgctl (msgid, IPC RMID, 0)1) fprintf (stderr, "msgctl (IPC RMID) failedIn) exit (EXIT FAILURE) exit (EXIT SUCCESS);
//msg2.cpp /* The sender program is very similar to msgl.cpp. In the main set up, delete the msg to_receive declaration and replace it with buffer [ BUFSIZ], remove the message queue delete and make the following changes to the running loop. We now have a call to msgsnd to send the entered text to the queue. / #include #include #include #include #include #include #include include #define MAX TEXT 512 struct my_msg_st f long int my msg_type; char some_text [MAX_TEXT int main() int running 1; struct my msg_st some_data; int msgid; char buffer [BUFSIZ]; | msgid msgget (( key-t ) 1234, 0666 IPC_CREAT); = if (msgid1) fprintf(stderr , "msgget failed with error : exit (EXIT_FAILURE) %d\n", errno); while(running) printf( "Enter some text: fgets (buffer, BUFSIZ, stdin); some data.my msg type1; strcpy (some_data.some text, buffer) if (msgsnd ( msgid, (void *)&sone, data, MAX-TEXT, 0) -1) { fprintf (stderr, "msgsnd failed\n"); exit (EXIT_FAILURE); if (strncmp (buffer, "end, 3) 0) running0; exit (EXIT_SUCCESS);
0 0
Add a comment Improve this question Transcribed image text
Answer #1

//msg1.cpp
/* The sender program is very similar to msg1.cpp. In the main set up, delete th emsg_to recieve declaration and replace it with buffer[BUFSIZ], remove the message queue delete and
make th efollowing changes to the running loop.
we now have a call to msgsnd to send the entered text to the queue */
#include<stdlib.h>
#include<stdio.h>
#include<string.h>
#include<errno.h>
#include<unistd.h>

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

#define MAX_TEXT 512

struct my_msg_st{
    long int my_msg_type;
    char some_text[MAX_TEXT];
};

int main(){
    int running = 1;
    struct my_msg_st some_data;
    int msgid;
    char buffer[BUFSIZ];
    long int msg_to_receive = 0;
  
    msgid = msgget((key_t)1234, 0666|IPC_CREAT);
  
    if(msgid == -1){
        fprintf(stderr, "msgget failed with error: %d\n", errno);
        exit(EXIT_FAILURE);
    }
  
    while(running){
        printf("Enter some text:");
        fgets(buffer, BUFSIZ, stdin);
        some_data.my_msg_type = 1;
        strcpy(some_data.some_text, buffer);
        if(msgsnd(msgid,(void *)&some_data, MAX_TEXT, 0) == -1){
            fprintf(stderr, "msgsnd failed\n");
            exit(EXIT_FAILURE);
        }
        if(msgrcv(msgid, (void *)&some_data, BUFSIZ, msg_to_receive, 0) == -1){
            fprintf(stderr,"msgrcv failed with error: %d\n", errno);
            exit(EXIT_FAILURE);
        }
        printf("You wrote:%s", some_data.some_text);
        if(strncmp(buffer, "end", 3) == 0){
            running = 0;
        }
    }
    exit(EXIT_FAILURE);
}

//msg1.cpp
/*Here's the receiver program.*/
#include<stdlib.h>
#include<stdio.h>
#include<string.h>
#include<errno.h>
#include<unistd.h>

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

#define MAX_TEXT 512

struct my_msg_st{
    long int my_msg_type;
    char some_text[BUFSIZ];
};

int main(){
    int running = 1;
    int msgid;
    struct my_msg_st some_data;
    char buffer[BUFSIZ];
    long int msg_to_receive = 0;
  
    /*First, we set up the message queue. */
    msgid = msgget((key_t)1234, 0666 | IPC_CREAT);
    if(msgid == -1){
        fprintf(stderr, "msgget failed with error: %d\n", errno);
        exit(EXIT_FAILURE);
    }
  
    /*Then the messages are retrieved from the queue, until an end message is encountered. last the message queue is deleted. */
    while(running){
        if(msgrcv(msgid, (void *)&some_data, BUFSIZ, msg_to_receive, 0) == -1){
            fprintf(stderr,"msgrcv failed with error: %d\n", errno);
            exit(EXIT_FAILURE);
        }
        printf("You wrote:%s", some_data.some_text);
        printf("Enter some text:");
        fgets(buffer, BUFSIZ, stdin);
        some_data.my_msg_type = 1;
        strcpy(some_data.some_text, buffer);
        if(msgsnd(msgid,(void *)&some_data, MAX_TEXT, 0) == -1){
            fprintf(stderr, "msgsnd failed\n");
            exit(EXIT_FAILURE);
        }
        if(strncmp(some_data.some_text,"end", 3) == 0){
            running = 0;
        }
    }
    if(msgctl(msgid,IPC_RMID,0) == -1){
            fprintf(stderr, "msgctl(IPC_RMID) failed\n");
            exit(EXIT_FAILURE);
    }
  
    exit(EXIT_FAILURE);
}

Add a comment
Know the answer?
Add Answer to:
Modify the programs so that each program can both receive and send messages alternatively. Note that when you run the two programs, you should run them in two different windows ( terminals). Y...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Modify the client server system program given below so that instead of sendto() and recvfrom(), you...

    Modify the client server system program given below so that instead of sendto() and recvfrom(), you use connect() and un-addresssed write() and read() calls. //Server.c #include #include #include #include #include #include #include #include #include #include # define PortNo 4567 # define BUFFER 1024 int main(int argc, char ** argv) { int ssd; int n; socklen_t len; char msg[BUFFER]; char clientmsg[BUFFER]; struct sockaddr_in server; struct sockaddr_in client; int max_iterations = 0; int count = 0, totalChar = 0, i = 0;...

  • Run the code in Linux and provide the screenshot of the output and input #include <signal.h>...

    Run the code in Linux and provide the screenshot of the output and input #include <signal.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include <arpa/inet.h> #include <sys/types.h> #include <sys/socket.h> static void cleanup(); static void docleanup(int signum); static const char *SERVER_ADDR = "127.0.0.1"; static const int SERVER_PORT = 61234; static int cfd = -1; int main(int argc, char *argv[]) { struct sockaddr_in saddr; char buf[128]; int bufsize = 128, bytesread; struct sigaction sigact; printf("client starts running ...\n"); atexit(cleanup); sigact.sa_handler =...

  • ) Using Linux or Unix command line interpreter, compile and run the programs in Figure 3.8,...

    ) Using Linux or Unix command line interpreter, compile and run the programs in Figure 3.8, Figure 3.30. DO NOT compile and ron these programs on Windows Write the 3.16, Figare 317 and Figure 3 programs in Notepadt+, for example, then compile and run them at the command pr apt. Provide screenshots of your programs compilation, execution, and the results. 144 6 7 8 9 ry-maps a shared-memory object of the ws writing to the object. The flag shared-memory object...

  • This is for a Unix class. Please help me out. I am attaching a skeletal code of the program below, it just needs ti be filled in. Below is a skeletal code of the program. Fork a child process...

    This is for a Unix class. Please help me out. I am attaching a skeletal code of the program below, it just needs ti be filled in. Below is a skeletal code of the program. Fork a child process and then use the parent for reading and the child for writing.  This is just a way of sending and receiving messages asynchronously. /* ************************************************************* * Utility functions * ************************************************************** */ static void usageError(const char * progName, const char *msg) {...

  • IN UNIX, MODIFY CODE, PROVIDE SCREENSHOTS FOR GOOD RATING: T1. Modify Client.c program to accept two...

    IN UNIX, MODIFY CODE, PROVIDE SCREENSHOTS FOR GOOD RATING: T1. Modify Client.c program to accept two arguments (IP add & port no. of the concurrent Server with thread - conServThread.c). Similarly, modify the Server (conServThread.c) program to accept an argument which is the port number of the server to bind and listen to. Try these two updated programs (server and client) with a port number (e.g., hhmm6) with current time where hh is hours in 24-hour format and mm is...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT