Pre-lab 2

Unix Interprocess Communication: Pipes


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 data type dynamically allocated 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/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 Find the program in figure captioned “Ordinary pipe in Unix”, 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 our 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
Print Friendly