Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: THEend8_
CPSC 351 Operating System Concepts
Programming Assignment – Sender & Receiver
Mode: Teams of 4 persons
Due Date: As shown on Titanium.
Goals:
-
To solidify your understanding of IPC principles.
-
To develop greater appreciation for the different IPC mechanisms.
-
To gain hands-on experience using shared memory.
-
To gain hands-on experience using message queues.
-
To gain hands-on experience using signals.
-
To learn how to combine shared memory and message queues in order to implement a practical application where the sender process sends information to the receiver process.
For this assignment:
You will use your knowledge of shared memory and message queues in order to implement an application which synchronously transfers files between two processes.
You are to implement two related programs: a sender program and the receiver program as described below:
-
sender: this program shall implement the process that sends files to the receiver process.
The sender shall be invoked as ./sender <FILE NAME> where sender
is the name of the executable and <FILE NAME> is the name of the file to transfer. For
example, ./sender song.mp3. When invoked, the sender shall perform the following
sequence of steps:
-
The program shall then attach to the shared memory segment, and connect to the message queue both previously set up by the receiver.
-
Send the name of the file specified at the command line to the receiver process using
the message queue. The message shall contain a field called fileName specifying the
name of the file to be sent.
-
Read a predefined number of bytes from the specified file, and store these bytes in the chunk of shared memory.
-
Send a message to the receiver (using a message queue). The message shall contain a field called size indicating how many bytes were read from the file.
-
Wait on the message queue to receive a message from the receiver confirming successful reception and saving of data to the file by the receiver.
-
Go back to step 3. Repeat until the whole file has been read.
-
When the end of the file is reached, send a message to the receiver with the size field set to 0. This will signal to the receiver that the sender will send no more.
-
Close the file, detach shared memory, and exit.
-
receiver: this program shall implement the process that receives les
from the sender process. The program shall be invoked as ./recv where recv is the
name of the executable. When invoked, the receiver program shall perform the following
sequence of steps:
-
The program shall setup a chunk of shared memory and a message queue.
-
The program shall wait on a message queue to receive a message from the sender program. When the message is received, the message shall contain a field called size denoting the number of bytes the sender has saved in the shared memory chunk.
-
If size is not 0, then the receiver reads size number of bytes from shared memory, saves them to the file (always called recvfile), sends message to the sender acknowledging successful reception and saving of data, and finally goes back to step 3.
-
Otherwise, if size field is 0, then the program closes the file, detaches the shared memory, deallocates shared memory and message queues, and exits.
-
When user presses Control-C in order to terminate the receiver, the receiver shall de-allocate memory and the message queue and then exit. This can be implemented by setting up a signal handler for the SIGINT signal. Sample file illustrating how to do this have been provided (signaldemo.cpp).
For more details, refer to “UNIX System V IPC” example in the slides “Chp 3 processes_2”.
Starter code:
Please note: by default the skeleton programs will give you errors when you run them. This is because they are accessing unallocated, unattached regions of shared memory. It’s your job to fill in the appropriate functionality in the skeleton, de-noted by the TODO comments, in order to make the programs work.
The skeleton codes for sender and receiver can be found on Titanium. The files are as follows:
-
sender.cpp: the skeleton code for the sender (see the TODO: comments in order to find out what to fill in)
-
recv.cpp: the skeleton code for the receiver (see the TODO: comments in order to find out what to fill in).
-
msg.h: the header file used by both the sender and the receiver
It contains the structure of the message relayed through message queues). The structure contains two fields:
– long mtype: represents the message type.
– int size: the number of bytes written to the shared memory.
In addition to the structure, msg.h defines macros representing two different message types:
– SENDER_DATA_TYPE: macro representing the message sent from sender to receiver. It’s type is 1.
– RECV_DONE_TYPE: macro representing the message sent from receiver to the sender acknowledging successful reception and saving of data.
-
NOTE: both message types have the same structure. The difference is how the mtype field is set. Also, the messages of type RECV_DONE_TYPE do not make use of the size field.
-
Makefile: enables you to build both sender and receiver by simply typing make at the command line.
-
signaldemo.cpp: a program illustrating how to install a signal handler for SIGSTP signal sent to the process when user presses Control-C.