Question: /* * * client.c: Client program * * to demonstrate interprocess commnuication * * with POSIX message queues * */ #include #include #include #include #include

/* * * client.c: Client program * * to demonstrate interprocess commnuication * * with POSIX message queues * */ #include#include #include #include #include #include #include #include #define SERVER_QUEUE_NAME "/sp-example-server" #define QUEUE_PERMISSIONS 0660 #define MAX_MESSAGES 10 #define MAX_MSG_SIZE 256 #define MSG_BUFFER_SIZE MAX_MSG_SIZE + 10 int main (int argc, char **argv) { char client_queue_name [64]; mqd_t qd_server, qd_client; // queue descriptors // create the client queue for receiving messages from server sprintf (client_queue_name, "/sp-example-client-%d", getpid ()); struct mq_attr attr; attr.mq_flags = 0; attr.mq_maxmsg = MAX_MESSAGES; attr.mq_msgsize = MAX_MSG_SIZE; attr.mq_curmsgs = 0; if ((qd_client = mq_open (client_queue_name, O_RDONLY | O_CREAT, QUEUE_PERMISSIONS, &attr)) == -1) { perror ("Client: mq_open (client)"); exit (1); } if ((qd_server = mq_open (SERVER_QUEUE_NAME, O_WRONLY)) == -1) { perror ("Client: mq_open (server)"); exit (1); } char in_buffer [MSG_BUFFER_SIZE]; printf ("Ask for a token (Press ): "); char temp_buf [10]; while (fgets (temp_buf, 2, stdin)) { // send message to server if (mq_send (qd_server, client_queue_name, strlen (client_queue_name), 0) == -1) { perror ("Client: Not able to send message to server"); continue; } // receive response from server if (mq_receive (qd_client, in_buffer, MSG_BUFFER_SIZE, NULL) == -1) { perror ("Client: mq_receive"); exit (1); } // display token received from server printf ("Client: Token received from server: %s ", in_buffer); printf ("Ask for a token (Press ): "); } if (mq_close (qd_client) == -1) { perror ("Client: mq_close"); exit (1); } if (mq_unlink (client_queue_name) == -1) { perror ("Client: mq_unlink"); exit (1); } printf ("Client: bye "); exit (0); }
//SERVER
/* * * server.c: Server program * * to demonstrate interprocess commnuication * * with POSIX message queues * */ #include#include #include #include #include #include #include #define SERVER_QUEUE_NAME "/sp-example-server" #define QUEUE_PERMISSIONS 0660 #define MAX_MESSAGES 10 #define MAX_MSG_SIZE 256 #define MSG_BUFFER_SIZE MAX_MSG_SIZE + 10 int main (int argc, char **argv) { mqd_t qd_server, qd_client; // queue descriptors long token_number = 1; // next token to be given to client printf ("Server: Hello, World! "); struct mq_attr attr; attr.mq_flags = 0; attr.mq_maxmsg = MAX_MESSAGES; attr.mq_msgsize = MAX_MSG_SIZE; attr.mq_curmsgs = 0; if ((qd_server = mq_open (SERVER_QUEUE_NAME, O_RDONLY | O_CREAT, QUEUE_PERMISSIONS, &attr)) == -1) { perror ("Server: mq_open (server)"); exit (1); } char in_buffer [MSG_BUFFER_SIZE]; char out_buffer [MSG_BUFFER_SIZE]; while (1) { // get the oldest message with highest priority if (mq_receive (qd_server, in_buffer, MSG_BUFFER_SIZE, NULL) == -1) { perror ("Server: mq_receive"); exit (1); } printf ("Server: message received. "); // send reply message to client if ((qd_client = mq_open (in_buffer, O_WRONLY)) == 1) { perror ("Server: Not able to open client queue"); continue; } sprintf (out_buffer, "%ld", token_number); if (mq_send (qd_client, out_buffer, strlen (out_buffer), 0) == -1) { perror ("Server: Not able to send message to client"); continue; } printf ("Server: response sent to client. "); token_number++; } }
Question 1: Shared Memory (15 pts): Write a simple program (example code available on BlackBoard; can be used as starting point) where processes synchronize via polling such that a process A prints out the strings of two other separate writing processes (B first and then C second) from shared memory. Process A needs to 'wait' by polling until B and C finish writing their strings to memory. Each of processes A, B and C should be in different code files. Create TWO different shared memory locations: (i) one for storing the integer identifier and (ii) for storing the string. Here is the sequence of events that needs to be implemented: 1. Process A writes "1" to the integer shared memory location and then waits until B and C completes. 2. Process B writes the string "shared" into the string shared memory location and then signals A & C that it is complete by writing "2" into the integer shared memory location (note B should wait to write into position the integer shared memory location until after process A writes "1" there). Then process B quits. 3. Process C writes the string "memory" into the string shared memory and then signals A that it is complete by writing "3" into the integer shared memory location (note C should wait until process B writes "2" into the integer shared memory). Then process C quits. 4. Process A needs to keep polling the integer shared memory location continuously. After process B writes "shared", process A should be able to print it out. Similarly, after process C writes "memory", process A needs to be able to print it out. Here some ad-hoc synchronization is needed. Specifically, consider using usleep() function to delay process B (and process C) between writing the string and the integer to give process A a chance to poll and print. Adjust the delay properly such that prints happen properly. 5. Process A is the last one to quit and prints out a "GoodBye" message at the very end before quitting
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
