Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

HELP! C UNIX A sender.c file creates two message queues and sends 200 random numbers ranging from 0-255. The goal is to have two middleman.c

HELP! C UNIX

A sender.c file creates two message queues and sends 200 random numbers ranging from 0-255.

The goal is to have two middleman.c files read messages from each queue.

One middleman forwards the even numbers to a 3rd queue and the other forwards the odd numbers to a 4th queue.

Finally, the receiver.c file reads and displays all of the numbers from queue 3 & 4.

image text in transcribed

image text in transcribed

image text in transcribed

"sender.c"

#include // for printf #include // for sleep() #include // for srand #include // for message queue #include // for message queue #include // for message queue

#include // for sleep()

#define NUM_REPEATS 200 // the number of the messages

#define MSG_key_Q1 1111 // message queue key for Q1 #define MSG_key_Q2 1112 // message queue key for Q2

#define BUFFER_SIZE 256 // max. message size

int main (void) { int i; // loop counter

int status_code; // result status

int msqid_Q1; // message queue ID (Q1) int msqid_Q2; // message queue ID (Q2)

key_t msgkey_Q1; // message-queue key (Q1) key_t msgkey_Q2; // message-queue key (Q2)

unsigned int rand_num; // a 32-bit random number float temp_rand; // the raw random numbber unsigned char eight_bit_num; // an 8-bit random number

/* Use current time to shuffle random seed */ srand(time(0));

/* definition of message ------------------- */ struct message{ long mtype; unsigned char mtext[BUFFER_SIZE]; };

/* instantiate the message buffer -----------*/ struct message buffer;

/* set up the message queue key Q1 --------- */ msgkey_Q1 = MSG_key_Q1;

/* create a new message queue -------------- */ msqid_Q1 = msgget(msgkey_Q1, 0666 | IPC_CREAT);

/* error check ----------------------------- */ if (msqid_Q1

else { printf("your new message queue (Q1) is successfully created .... "); }

/* set up the message queue key Q2 --------- */ msgkey_Q2 = MSG_key_Q2;

/* create a new message queue -------------- */ msqid_Q2 = msgget(msgkey_Q2, 0666 | IPC_CREAT);

/* error check ----------------------------- */ if (msqid_Q2

else { printf("your new message queue (Q2) is successfully created .... "); }

/* confirm the start of the two receivers --- */ printf("start your Middlemen then start the receiver ... press any key when ready .... ");

/* wait for a key stroke at the keyboard ---- */ getchar();

/* take care of "mtype" --------------------- */ buffer.mtype = 1; // UNIX standard says, any number

/* send an 8-bit number, one at a time ------------ */ for (i = 0; i

/* detect a message transmission error ------------- */ if (status_code

/* send a 8-bit number to Q2 ----------------------- */ status_code = msgsnd(msqid_Q2, (struct message *)&buffer, sizeof(buffer.mtext), 0);

/* detect a message transmission error ------------- */ if (status_code

/* give everyone else enough time to complete their tasks --- */ sleep(1);

/* delete the emsage queue ----------------------------- */ status_code = msgctl(msqid_Q1, IPC_RMID, NULL);

/* error check ----------------------------------------- */ if (status_code

else { printf("Q1 message queue has been successfully deleted ... "); }

/* delete the emsage queue ----------------------------- */ status_code = msgctl(msqid_Q2, IPC_RMID, NULL);

/* error check ----------------------------------------- */ if (status_code

else { printf("Q2 message queue has been successfully deleted ... "); } }

/* END OF LINES ============================================================= */

In this project, we implement message queues for four processes to communicate. The four processes are: Sender (sender.c), Middleman #1 (M1.c), Middleman #2 (M2.c), and Receiver (receiver.). Message queues are FIFO queues that can be used as bridges" to transfer messages (data) from a process to another (other). Message queues are the FIFO data structures that are created and managed by operating systems. User processes use system calls to create, send (enqueue) messages to a message queue, receive (dequeue) messages from message queues, and delete message queues. Sender creates two message queues, Q1 and Q2. Each of the two middleman process creates a message queue (Q3 or Q4). The structure of this project is shown in Figure 1. Middle Man #1 (M1.c) Message Queue #1 (QI) (created by Sender) Message Queue #3 (03) (created by Middle Man #1) Every random number (only even random numbers Sender (sender.c) Receiver (receiver.c) Message Queue #2 (22) (created by Sender) Message Queue #3 (04) (created by Middle Man #2) Every random number Middle Man #2 (M2.c) only odd random numbers Figure 1 project process structure Sender generates random numbers, ranging 0 (included) to 255 (included) as "unsigned char (one byte data)" values for N times, where N is a positive integer that should be declared as "#define NUM_REPEATS at the beginning of the source code file for the sender (sender.c). It is assumed that the sender process is the only process who knows how many random numbers are generated (i.e., no other process knows). Each random number in unsigned char" is an eight-bit unsigned integer (as described in a sample C source code file). Each time Sender generates a random number, Sender sends it to both Q1 and Q2. Then, Middleman #1 drops any odd numbers before it forwards them to the third message queue (Q3). Similarly, Middleman #2 drops any even numbers before it forwards them to the fourth message queue (Q4). The receiver process should receive every message from Q3 and Q4 and display each when the receiver receives it. (1) Implement Middlemen (M1 and M2) and Receiver that perform the tasks described above as *.c source code files ("M1.c, M2.c, and receiver.c). The C source code file for Sender is provided. The binary executable for Receiver is also provided for your testing purpose. (2) Your implementation of Middlemen and Receiver will be tested using the Sender process (as a C source code file), a middleman (M1 or M2), and the Receiver (as a binary executable file) posted to the course home. (3) The message key IDs should be declared as follows: In the sender (replace the keys by yours): #define MSG_key_Q1 7950 // message queue key for Q1 #define MSG_key_Q2 7951 // message queue key for Q2 In M1: #define MSG_key_Q1 7950 // the message queue key for Q1 #define MSG_key_Q3_7952 // the message queue key for Q3 In M2: #define MSG_key_Q2 7951 // the message queue key for Q2 #define MSG_key_Q4 7953 // the message queue key for Q4 In receiver: #define MSG_key_Q3 7952 // the message queue key for Q3 #define MSG_key_Q4 7953 // the message queue key for Q4 (4) Your Middleman sends only the necessary random numbers to Q3 or Q4. (5) Your receiver should receive every number originated from the sender. (6 Your Middleman and Receiver process(es) should terminate when all the random numbers from the sender are forwarded or received. (7) Your Middleman process should delete the message queues each of which creates (i.e., message queues, Q3 and Q4) when it completes and terminates. In this project, we implement message queues for four processes to communicate. The four processes are: Sender (sender.c), Middleman #1 (M1.c), Middleman #2 (M2.c), and Receiver (receiver.). Message queues are FIFO queues that can be used as bridges" to transfer messages (data) from a process to another (other). Message queues are the FIFO data structures that are created and managed by operating systems. User processes use system calls to create, send (enqueue) messages to a message queue, receive (dequeue) messages from message queues, and delete message queues. Sender creates two message queues, Q1 and Q2. Each of the two middleman process creates a message queue (Q3 or Q4). The structure of this project is shown in Figure 1. Middle Man #1 (M1.c) Message Queue #1 (QI) (created by Sender) Message Queue #3 (03) (created by Middle Man #1) Every random number (only even random numbers Sender (sender.c) Receiver (receiver.c) Message Queue #2 (22) (created by Sender) Message Queue #3 (04) (created by Middle Man #2) Every random number Middle Man #2 (M2.c) only odd random numbers Figure 1 project process structure Sender generates random numbers, ranging 0 (included) to 255 (included) as "unsigned char (one byte data)" values for N times, where N is a positive integer that should be declared as "#define NUM_REPEATS at the beginning of the source code file for the sender (sender.c). It is assumed that the sender process is the only process who knows how many random numbers are generated (i.e., no other process knows). Each random number in unsigned char" is an eight-bit unsigned integer (as described in a sample C source code file). Each time Sender generates a random number, Sender sends it to both Q1 and Q2. Then, Middleman #1 drops any odd numbers before it forwards them to the third message queue (Q3). Similarly, Middleman #2 drops any even numbers before it forwards them to the fourth message queue (Q4). The receiver process should receive every message from Q3 and Q4 and display each when the receiver receives it. (1) Implement Middlemen (M1 and M2) and Receiver that perform the tasks described above as *.c source code files ("M1.c, M2.c, and receiver.c). The C source code file for Sender is provided. The binary executable for Receiver is also provided for your testing purpose. (2) Your implementation of Middlemen and Receiver will be tested using the Sender process (as a C source code file), a middleman (M1 or M2), and the Receiver (as a binary executable file) posted to the course home. (3) The message key IDs should be declared as follows: In the sender (replace the keys by yours): #define MSG_key_Q1 7950 // message queue key for Q1 #define MSG_key_Q2 7951 // message queue key for Q2 In M1: #define MSG_key_Q1 7950 // the message queue key for Q1 #define MSG_key_Q3_7952 // the message queue key for Q3 In M2: #define MSG_key_Q2 7951 // the message queue key for Q2 #define MSG_key_Q4 7953 // the message queue key for Q4 In receiver: #define MSG_key_Q3 7952 // the message queue key for Q3 #define MSG_key_Q4 7953 // the message queue key for Q4 (4) Your Middleman sends only the necessary random numbers to Q3 or Q4. (5) Your receiver should receive every number originated from the sender. (6 Your Middleman and Receiver process(es) should terminate when all the random numbers from the sender are forwarded or received. (7) Your Middleman process should delete the message queues each of which creates (i.e., message queues, Q3 and Q4) when it completes and terminates

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Processing

Authors: David M. Kroenke, David Auer

11th Edition

B003Y7CIBU, 978-0132302678

More Books

Students also viewed these Databases questions