CS 370: OPERATING SYSTEMS Fall 2019 Colorado State University
HW3: Programming Assignment v3 9.26.2019.7:00PM
WORKING WITH INTER-PROCESS COMMUNICATIONS
The objective of this assignment is to write and test programs with pipes and shared memory.
Due Date: Thursday, October 10, 2019, 11:00 pm
Extended Due Date with 20% penalty: Friday, October 11, 2019, 11:00 pm
This is a modification of HW2. In this assignment the Starter first creates a child process using the program FileReader that reads a text file and transmits the text to the parent using a pipe. The parent program separates each value in the string received from the pipe, and stores it in a one dimensional array. The Starter then creates shared memory for each of the three child processes and then invokes the Red, Green, and Blue executables and passes the entire array at once. The child programs Red, Green and Blue will decode all the values in the array, and write the results to the shared memory, with 5776 values, each separated by a space, which will be read by the Starter. The child processes Red, Green and Blue, that are performing the decoding operations on the coded values should run concurrently (i.e. not one after the other).
Please see the Notes at the bottom before starting writing your program.
1. Description of Task
This assignment builds on HW2. Specifically, we will be using interprocess communication (IPC) for communications between the Starter, FileReader, Red, Green, and Blue processes. All instances of Red, Green, and Blue should run concurrently. Red, Green, and Blue processes will here on be referred to as Red/Green/Blue.
1. Starter creates a pipe and forks a child process. The child process executes the FileReader program with the name of the .txt file as an argument. (If a .txt file name is provided to Starter as a command line argument, that file will be used, otherwise the default coded_image_1.txt will be used). FileReader will open the file, read the content. In the file, there are 76 lines, each line having 76 coded values, with a space separating each one of them. FileReader reads all the lines and makes a single string, where all 76 lines are transformed into a single line, so all the 5776 values are in the string, each value separated by a space. FileReader then closes the file and then writes the contents to the pipe it inherited. When control returns back to the Starter, it will read the content from the pipe.
2.
3.
4.
Starterseparateseachvalueseparatedbyaspace,andstoreseachvalueasanarrayelement in the array of strings of size 5778 called coded_values[]. The first 5776 values will hold the coded values read from the pipe, the 5777th value will be the name of the shared memory that will be sent to the subsequent child processes, and the 5778th value is NULL. Then Starter creates a shared memory segment for each Red/Green/Blue process. It then forks three child processes to run the Red/Green/Blue programs. Each Red/Green/Blue program should be running concurrently.
After the decoding each coded value, Red/Green/Blue program writes the value for each pixel into a buffer (which is a character array) after converting the integer value to a string. The value is followed by a space. After the completion of decoding of all 5776 value, the buffer is written to the shared memory and program returns control to Starter. The Starter then stores the values in a one-dimensional integer array of size 5776. Each Red/Green/Blue programs’ values will be written to its own array.
Starter then generates the image file.
CS 370: OPERATING SYSTEMS Fall 2019 Colorado State University
Starter does the following:
1. Create a pipe using the following steps.
Create an integer array of size 2, and create a pipe using the integer array.
2. Send the details to the child process FileReader using the following steps:
Create a character array of 200. This is to store the pipe location as a string. Use sprintf to
get the write end of the pipe into the character array.
Fork a child process, and replace its executable by the FileReader executable.
Pass the character array to the FileReader as a second argument after file name.
3. Read the content from the pipe into a character buffer of size 60000 using the following steps:
Close the writing end, and then read the content from the pipe and close the reading end
too.
4. The string is separated into 5776 coded string values.
It stores the values into the array coded_values[].
5. It creates three shared memories with the names “Shared_mem_red”, “Shared_mem_green”, “Shared_mem_blue”. It then forks three child processes that run the Red/Green/Blue executables, and each child process gets the appropriate shared memory name, which is sent to the Red/Green/Blue program, which is sent as the 5777th value in the array coded_values[]. For each set, it does the following.
The shared memory segment should be of size 60000 bytes. Since a shared memory is created for each child, use O_CREATE and open in read write mode (O_RDWR). It uses mmap to create a pointer to the shared memory. The name of each shared memory should follow the standard.
Fork a child using execvp and the two arguments it will take are name of the executable and the array coded_values[].
Once the control returns from Red/Green/Blue processes, it then reads from the shared memory segment and puts copies each of the decoded values to the integer array red[5776] or green[5776] or blue[5776] depending on the child process returned.
It then generates the .ppm image file.
FileReader does the following:
1. Receives the name of the file and the file descriptor of the write end of the pipe as arguments
from the Starter.
2. Using atoi copies the pipe reference which is the third argument in the argv into an integer variable.
3. Read the contents from the .txt file.
4. Write the contents into the pipe, all 5776 coded values into a single string, each value separated by a space.
Red, Green, and Blue each do the following:
1. It receives a string array with first 5776 values being the coded values, 5777th value being the
name of the shared memory.
2. Performs the decoding operation on all the 5776 coded values, one at a time. Each decoded value is written in the string format to a buffer (character array), followed by a space, and then once all decoding is done, the buffer content is written to the shared memory, and the control is returned.
Background: For the background of the assignment, review the related material (sec. 3.5.1 POSIX shared memory and 3.6.3.1 ordinary pipes), the related self-exercise example you ran recently and
CS 370: OPERATING SYSTEMS Fall 2019 Colorado State University
consult the man pages (shm_open(), ftruncate( ), mmap( ), shm_unlink( )) as needed. You can simply search for “man shm_open()” etc. Please note that this is not conventional serial C programming.
2.
Task Requirements
The Starter creates a pipe and checks if pipe creation failed. It then forks a child process to execute FileReader.
FileReader reads the file and writes the text to the pipe, all 5776 values in a single line, separated by spaces and then closes the write end of the pipe.
Starterthenreadsthecontentsthenclosethereadend.Itthenseparatestheeachcodedvalue and stores individual values in the array coded_values[].
Starter then creates three shared memory segments with appropriate attributes (truncate to the size of 60000 bytes, use mmap with PROT_READ and MAP_SHARED). It prints the name and the file descriptor of the shared memory.
Starter then forks appropriate Red/Green/Blue program as a child process. For each of the Red/Green/Blue processes, the appropriate name (“Shared_mem_red”, “Shared_mem_green”,
“Shared_mem_blue”), is written into the second last position in the array coded_value[5778]. The last element in the array is set to NULL to indicate the end of the array. Use execvp( ) for executing the Red/Green/Blue executables.
Red/Green/Blueprocessperformstheoperationonallthecodedvaluesandwriteseachdecoded value as a string into the buffer string followed by space. When all the values are decoded it copies the buffer string to the shared memory. The Red/Green/Blue process also displays the coded value and the decoded value as per the standards that can be seen in the sample output.
Starter then copies the values from the shared memory into the appropriate integer arrays of size 5776 (red[5776] or green[5776] or blue[5776]). It unlinks the shared memory. It then creates the image file.
All the Red/Green/Blue processes should be forked to execute concurrently, that is all of them should be running at the same time. (Hint: Fork and exec should be in one for loop and wait should be in a different for loop.)
Files Provided
3.
1. 2. 3. 4.
5.
6.
7.
8.
Files provided for this assignment include the description file (this file). Sample output files are provided to you on Canvas.
You are needed to answer the questions in the README file.
4. Example Outputs (Note – The process IDs and the order may vary)
1.
.
.
Green[13916]: Received coded value 151339 Green[13916]: Decoded into 79
.
.
CS 370: OPERATING SYSTEMS Fall 2019
Colorado State University
.
Red[13538]: Received coded value 151339
Red[13538]: Decoded into 2
.
.
.
Blue[13540]: Decoded into 42
Blue[13540]: Received coded value 151339
Blue[13540]: Decoded into 43
Starter: coded_image_1_output.ppm file written and closed.
2.
.
.
Green[13970]: Received coded value 11711171 Green[13970]: Decoded into 178
.
.
.
Blue[14010]: Received coded value 11711171
Blue[14010]: Decoded into 195
.
.
.
Red[13969]: Received coded value 11711171
Red[13969]: Decoded into 178
Starter: coded_image_2_output.ppm file written and closed.
5. What to Submit
Use the CS370 Canvas to submit a single .zip or .tar file that contains:
All .c files listed below and descriptive comments within, o Starter.c
o FileReader.c o Red.c
o Green.c
o Blue.c
a Makefile that performs both a make build as well as a make clean,
a README.txt file containing a description of each file and any information you feel the grader
needs to grade your program, and answers for the 3 questions
For this and all other assignments, ensure that you have submitted a valid .zip/.tar file. After submitting your file, you can download it and examine to make sure it is indeed a valid zip/tar file, by trying to extract it.
Filename Convention: The archive file must be named as:
6. Grading
The assignments much compile and function correctly on machines in the CSB-120 Lab. Assignments that work on your laptop on your particular flavor of Linux/Mac OS X, but not on the Lab machines are considered unacceptable.
CS 370: OPERATING SYSTEMS Fall 2019 Colorado State University
The grading will be done on a 100-point scale. The points are broken up as follows:
Objective
Points
Correctly performing Tasks 1-8 (10 points each)
80 points
Descriptive comments for important lines of code
5 points
Compilation with no warnings
5 points
Providing a working Makefile
5 points
Questions in the README file
5 points
Questions: (To be answered in README file.)
1. Name the function that is used to create a pipe. Which ends denotes the read and the write ends of a pipe? (2 points)
2. Name the function used to map files or devices in to memory? (1 point)
3. Name the function used to open a shared memory object? What does it return? (2 points)
You are required to work alone on this assignment.
7. Late Policy
Click here for the class policy on submitting late assignments. Notes:
1. You may again assume that coded_image_1.txt is the default file to be opened if no arguments are provided.
2. For your testing purposes two sample input files that are provided.
3. This program may not work on your Mac OS X or other systems. Try to run the program on a
lab system, if you keep getting a segmentation fault and the code seems correct.
Revisions: Any revisions in the assignment will be noted below.