Lab 7: Getting started with processes

Goals

Your primary goal for this lab is to explore some process-related sample code. For now this will hopefully help you understand how processes are created. Later, it should help you get started on our last pair of assignments.

Getting started

There are a lot of samples listed on the Samples page. For this lab, we’ll explore the first few of them.

First, download/copy the relevant samples to mantis. You’ll want the following:

Note that for the Makefile, the command make all won’t work yet, as not all of the samples are posted quite yet.

Part 1: creating new processes with fork

First, let’s take a look at an example that uses fork.

  • Read through forktest.c and predict what the code will do when run.

  • Now, either use the command make lab7 to compile the executables for this lab, or type the following command in the shell:

    gcc -Wall -Werror -O1 -g -o forktest forktest.c
    
  • Run the program forktest and observe the output. Does it do what you expected?

  • How does the child process determine its process ID?

  • How could the parent process determine the child’s process ID?

Part 2: Executing a program with exec*

In addition to fork and wait, there is a family of functions, which we’ll call exec* (where the * here means a wildcard, e.g., that there are lots of options) that spawn a new process to execute a specific command.

Part 2a: executing a user-defined program

Next, take a look at the code for getanumber.c.

  • Read through the code and predict what its output will be.

  • Use the command make lab7 to compile the remaining samples for this lab, if you haven’t already.

  • Run the program getanumber and observe the output. Does it do what you expected?

Now, let’s take a look at a sample that uses execlp to execute getanumber. Open up exectest.c.

  • Read through the code. Which process executes getanumber?

  • Why do both the parent and the child call fflush? Check out the man page by typing man fflush to get more information (type q to quit the man page view).

  • Run the program exectest and observe the output. Does it do what you expected?

Part 2b: executing a shell command

Instead of executing a specific user-provided program, we can also execute shell commands from C code using exec*.

  • Comment out line 32 and uncomment line 33, so that we’re no longer executing getanumber, but instead executing ls. Predict what the output should be.

  • Run the program exectest again and observe its output now. Does it do what you expected?

Part 3: a simple shell

There is a shell of a shell (hah!) provided for you in the samples. Copy to mantis the following additional files to explore:

Let’s explore these files.

  • Look through shell208.c. What do you think will happen when you run it?

  • Run shell208.c. Does it do what you epxected? Write down any questions you have.

  • Add comments in shell208.c for where you might make changes to allow you to execute commands. What about redirecting output?

  • Now explore the other two files. How do these change what we already know about working with processes and executing commands?

Next steps

There are many more process-related samples already on the Samples page. Take a look and play around with them. Gather your questions, and ask away!