CSE 4105 System Programming · Lab 10

Inter-process Communication · Signals

Pipes in your shell; mytimeout with signals.

Tasks

The same steps as the lab slide. Do them on a Linux PC with the files from the zip; the tools below help you check your thinking first.

Lab 10.1 · Inter-process Communication

  1. TODO 1 in mysh_pipe_starter.c: find the word | in argv
  2. TODO 2: pipe(fd)
  3. TODO 3: child 1: dup2(fd[1], 1), close both ends, run(left)
  4. TODO 4: child 2: dup2(fd[0], 0), close both ends, run(right)
  5. TODO 5: the shell closes both ends, then waits for both and reports

the finished shell (real session)

mysh> ls / | wc -l
26
mysh> cat /etc/passwd | grep root
root:x:0:0:root:/root:/bin/bash
mysh> yes | head -3
y
y
y
[908 killed by signal 13]
mysh> nosuch | wc -l
mysh: nosuch: No such file or directory
[910 exited with status 127]
0
mysh> echo hello | tr a-z A-Z
HELLO

Before TODO 1: ls / | wc -l runs ls with the arguments "/", "|", "wc", "-l": ls complains that "|" and "wc" do not exist. The | is the shell's job, not the kernel's.

  1. TODO 1 in mytimeout_starter.c: install on_alarm for SIGALRM with sigaction, without SA_RESTART
  2. TODO 2: alarm(secs) after starting the program
  3. TODO 3: waitpid returned EINTR: first alarm → SIGTERM and alarm(2); second alarm → SIGKILL
  4. TODO 4: exit status like coreutils: 124 if timed out, else the child's status (or 128 + signal)
  5. Test: ./mytimeout 2 sleep 10, ./mytimeout 5 sleep 1, ./mytimeout 1 ./stubborn; compare with timeout

strace ./mytimeout 1 ./stubborn (real)

rt_sigaction(SIGALRM, {…, sa_flags=SA_RESTORER, …}) = 0
alarm(1)                       = 0
wait4(1857, …) = ? ERESTARTSYS
--- SIGALRM {si_code=SI_KERNEL} ---
rt_sigreturn({mask=[]})  = -1 EINTR
kill(1857, SIGTERM)            = 0
alarm(2)
   # stubborn ignores SIGTERM
wait4(1857, …) = ? ERESTARTSYS
--- SIGALRM … ---
rt_sigreturn({mask=[]})  = -1 EINTR
kill(1857, SIGKILL)            = 0
+++ killed by SIGKILL +++     # 1857
wait4 … WTERMSIG(s) == SIGKILL … = 1857
+++ exited with 124 +++

Think: why must SA_RESTART be off here? (What would waitpid do after the alarm if it were on?)

Try it here

These run in your browser, with no server and no internet. They are built from the same reference programs as the lab, so their output matches what your finished C program should print. They do not replace the real lab: the kernel, gcc and your own code are the point.

Read the files

Every text file from lab10_files.zip. Read here, then download the zip to work on them.

    ← Lab 9All lecturesLab 11 →