Seminar 7: The shell, for real

Overview

For this week’s seminar, we’ll learn about how the shell starts programs and connects them together using file descriptors. This can teach us a lot about how file descriptors and system calls work, as well as how we can trace how processes use system callls.

Room assignments: We will split into two rooms again. We will use the same room assignments as previous weeks.

Assigned work: Since the material we want to discuss is based on Wednesday’s lecture, we have a short assignment, described below, just to seed a larger discussion during seminar. We expect that the assignment should take around 15-20 minutes to complete–please don’t spend more time than this!

As usual, please submit a brief response in the form below, along with any questions you have. Your responses are due Thursday, October 29 at 11:59pm.

“Reading”

Let’s say we have a file called git-manual.txt containing the text of the manual page for git, and we want to search it for all lines containing the word “branch”. Try this on your own by running the following commands:

# Create git-manual.txt
man git > git-manual.txt

# Search the file for lines containing 'branch' -- this is the command we're interested in
cat git-manual.txt | grep branch

(Note: this is not the most efficient way to search git’s man page for a word, or indeed the fastest way to search a file for a word. However, it’s a good demonstration for talking about the shell!)

The command cat git-manual.txt | grep branch is a shell pipeline, where the output of one command is redirected to another (using the “pipe” syntax of cmd1 | cmd2, which sends the output of cmd1 into the input of cmd2). You’ve likely used shell pipelines at some point in your CS career. In Project 4, you’ll implement them!

In this week’s seminar, we’re going to explore how the shell runs programs in order to understand how shell pipelines work. Take a look at this system call trace generated by the running strace command when executing cat git-manual.txt | grep branch. The trace contains a log of all the relevant system calls involved, with the PID number that made them in the leftmost column. Then, spend around 15 minutes brainstorming:

  • Do a bit of research on the system calls you see in the trace. Based on what you can gather (and what you might remember from your introductory systems course), how does this trace implement the pipeline we described?
  • Think about how file descriptors are involved in the pipeline: what do you think the operating system needs to be responsible for in order to make this work?

Note: the trace we give you is actually a filtered version of the real trace – the filtered version includes only the system calls involved in starting the pipeline, to make it more readable. If you want to see the real deal (as we will in the seminar), check out this full version of the trace. Think about what you can conclude about a real OS from the extra information in the full trace.

What you should turn in

By Thursday, October 28 at 11:59pm, fill out this form with:

  • Your brief response to the brainstorming question above

  • Please ask us a question about the shell, pipelines, or file descriptors (either from looking at the screenshot, what we discussed in class, or otherwise)

Once again, your goal is not to design everything you see in the trace, but to start off thinking about what else might be needed to make the shell work. We’ll use this, and your questions, as a starting point for our discussion.