CS45 Weekly Lab: week 2

C programming tools

C programming tools

C programming tools (make, gdb, valgrind, git), signals and signal handlers, man and appropos: see the week 1 lab page for links to resources and examples.

Lab Partners

Starting with lab 2, you will pick a partner with whom you will work on all remaining lab assignments in this class. It is advantageous to pick a partner who is in your same lab section, but you are not required to do so.

I need to know your partner before 5pm on Friday so that I can set up the lab 2 starting point for specific partner groups.

By no later than Friday at 5pm every student in class (i.e. both you AND your partner) should email me the following information:

  1. Do you have a lab partner? YES or NO
  2. If YES, what are your and your partner's names and user names
I will pair up people who do not have partners. If you would would like my help in finding a lab partner, send me email and I'll let you know about other singles as I hear from them.

Pipes and I/O redirection

Copy over some code into your cs45/weeklylabs/02 directory:
cd cs45/weeklylabs
mkdir 02
cd 02
pwd
cp ~newhall/public/cs45/week02/* .
ls
Makefile pipe_example.c
Let's look at the example code in pipe_example.c (heavily stolen from the pipe man page), and then run it to see what it is doing.

Can you answer these questions:

  1. How is the pipe set up (and who sets it up, when and how)?
  2. How does the child know when the parent is no longer going to send it anything to read from the pipe (and what triggers the child knowing this)?

Next, try comment out this call to close in the parent code, rebuild and run:

close(pipefd[1]);   // this will cause reader to see EOF on read
You can attach gdb to the child process and see where it is:
gdb prog (child's pid)
(gdb) where
What is happening with the child? and why?
pkill -9 prog   # will kill both the parent and the child