Unix Interprocess Communication: Pipes
Credits
The material developed for this lab was developed by Prof. L. Felipe Perrone. Permission to reuse this material in parts or in its entirety is granted provided that this “credits” note is not removed. Additional students files associated with this lab, as well as any existing solutions can be provided upon request by e-mail to: perrone[at]bucknell[dot]edu
Set Up
Follow the Github Classroom instructions to accept and set up the repo for the lab. Name the directory for this prelab ~/csci315/Labs/Lab2Prelab.
You will will write a few simple programs for this pre-lab, all of which should be committed to your git repository.
In order to earn full grade in this pre-lab assignment, your solutions must be committed and pushed to your git repository before your Lab 2 session starts.
Problem 1 (15 points)
Out of deep intellectual curiosity, a student in this class once asked whether Unix processes spawned from a common parent inherit a copy of the heap. Think of how you can verify in practice if this true or not.
Create a simple C program called heap_test.c and store it in ~/csci315/Labs/Lab2Prelab. The program must do the following:
- allocate 64 bytes of memory on the heap to a variable char * buffer in the main() function; (remember malloc(3)?)
- copy the phrase “Hello” to this buffer;
- spawn one child process;
- concatenate “ world!” to the buffer in the parent process; (remember strcat(3)?)
- concatenate “ how are you?” to the buffer in the child process;
- print to the terminal the contents and the addresses of the buffer variable in the parent and in the child process.
Note that in the print-out to the screen, the output should look something like
Main: content = [Hello], address = [0x123456]
except that you will need to print from three different places, in the main() function before spawning a new process, then in the code for both the parent and the child process. Here “Hello” is the buffer content, and 0x123456 is the address for the variable buffer.
Inspecting the value of the dynamically allocated data will let you know if what was created in the parent is inherited in the child process… The program you write for this problem will test this conjecture.
Create a Makefile to compile your program. You will augment this Makefile to build ALL the deliverables for this pre-lab and lab. Not providing a Makefile that builds all deliverables will assign you a 10 point penalty to the overall assignment.
When you are done with this, you need to:
- cd ~/csci315/Labs/Lab2Prelab
- git pull
- git add heap_test.c
- git add Makefile
- git commit -m “Pre-lab 2.1 completed”
- git push
Problem 2 (15 points)
Read the man page for the following system calls:
- pipe(2)
- open(2)
- read(2)
- write(2)
- close(2)
Visit the SGG Chapter 3, section 3.7.4.1 (3.6.3.1 in 9th edition). Find the program in the figure captioned “Ordinary pipe in UNIX” (3.[21,22] in 10th edition, 3.[25,26] in 9th), and read it carefully. You can copy this source code from the textbook into a file called pipe_test.c in ~/csci315/Labs/Lab2Prelab. Make sure the program compiles and executes correctly. Modify your existing Makefile so that it builds pipe_test.c.
Finally, answer the following questions using this Google Form
Remember that you’re shooting for brief, clear, and accurate text!)
- Describe what you discovered about the sharing of heaps between a parent and a child process in Linux. That is, what you did learn from the program you wrote for Prelab Problem 1 above.
- Explain how it is possible for the child process to have access to the same pipe which was created and later is used by the parent.
- What are the similarities and the differences you observe in working with Linux files and pipes? (Never mind how differently they might be implemented by the operating system, we are talking about how you work with them via the API.)
When you are done with this, you need to:
- cd ~/csci315/Labs/Lab2Prelab
- git pull
- git add pipe_test.c
- git add Makefile
- git commit -m “Pre-lab 2.2 completed”
- git push
Grading Rubric
Problem 1 [15 points total]
- [2 points] Submitted program heap_test.c.
- [3 points] Program compiles without warnings or errors.
- [5 points] Program allocates something on the heap and spawns a child process.
- [5 points] Program prints to the terminal the contents and addresses of the buffer in the main() function, and in the parent and in the child process.
Problem 2 [15 points total]
- [6 points] Submitted program pipe_test.c with the contents of figure “Ordinary Pipe in UNIX” from SGG, which compiles without warnings or errors and executes without crashing.
- [3 points] Submitted the Google Form (see the link in this lab write-up) that describes what can be observed in the experiment in Problem 1 (sharing of heap data between parent and child processes).
- [3 points] Submitted the Google Form (see the link in this lab write-up) that explains how parent and child processes can have access to the pipe file descriptor created by the parent.
- [3 points] Submitted the Google Form (see the link in this lab write-up) that explains how similar (or dissimilar) is the API that Unix provides for working with files and pipes.
Makefile [-10 points if not provided]
- If no working Makefile is provided, subtract [10 points] from the total grade of the prelab.