Skip to main content

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

Create a directory for Lab 2 under your ~/csci315/Labs directory. 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/Lab2. The program must do the following:

  • allocate some space on the heap (it can be a data type of your choice: int, float, double, etc.),
  • write something to that space,
  • spawn one child process, and
  • print to the terminal the contents of the dynamically allocated memory in the parent and in the child process.

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 working 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/Lab2
  • 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. Write the complete program into a file called pipe-test.c in ~/csci315/Labs/Lab2. Make sure the program compiles and executes correctly. Modify your existing Makefile so that it builds pipe-test.c.

Finally, create a file called answers-prelab.txt to contain the answers to the questions below (remember that you’re shooting for brief, clear, and accurate text!)

  1. 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 Pre-Lab Problem 1 above.
  2. 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.
  3. 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/Lab2
  • git add pipe-test.c
  • git add Makefile
  • git add answers-prelab.txt
  • 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 of the dynamically allocated memory 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 file answers-prelab.txt describes what can be observed in the experiment in Problem 1 (sharing of heap data between parent and child processes).
  • [3 points] Submitted file answers-prelab.txt explains how parent and child processes can have access to the pipe file descriptor created by the parent.
  • [3 points] Submitted file answers-prelab.txt 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.