Lab 6 - Processes and Services

Overview

At any given moment, there are anywhere from dozens to thousands of process running on a Unix system. The large majority of these processes, called daemons, run in the background. Daemons are crucial to having a usable system and provide much of a system’s core functionality, including the graphics server, sound server, and networking services to name a few.

Today we’ll explore these background processes and create some of our own! Make sure the following exercises are executed on your VM unless explicitly specified otherwise. Make sure to answer the questions on the google form as you work through the lab:

Which processes are running on my system?

Open up a terminal and run the ps command. You should see something like this: PID TTY TIME CMD 3634 pts/2 00:00:00 ps 15015 pts/2 00:00:00 bash

Now open up another terminal and run sleep 1000 in the background, and then run ps. It should look like:

$ sleep 1000 & [1] 29756 $ ps PID TTY TIME CMD 29446 pts/2 00:00:00 bash 29756 pts/2 00:00:00 sleep 29757 pts/2 00:00:00 ps

In the first terminal run ps again. You should notice that the sleep process is not showing up, even though the thousand seconds haven’t expired. (Exercise 1) Why do you think this behavior occurs (hint: TTY column)?

We can get the process to display on the first terminal by running ps -u, which displays all the processes running as your user. Notice the PID column; each process has a unique ID assigned to it by the kernel. One thing we can do with this PID is send signals to the process. sleep 1000 is pretty useless, so go ahead and kill it – kill 29756 (substitute 29756 with whatever PID ps outputted for you).

The most common use of ps is to run ps -ef to see all the processes running on the system. Run ps -e and ps -f independently to see how the flags work together.

htop

Make sure htop is installed by running sudo apt install htop. Now, open up a terminal and run the htop command. htop can be thought of as a more extensive version of ps -ef, whereby process stats are updated in real-time.

First press <F2>, scroll down to Display options, and check “Hide userland process threads.” We won’t be dealing with those in this lab.

Now open up another terminal and SSH into your VM. Run the command yes. It uses a lot of resources as it prints a continuous stream of y’s. (Exercise 2) What resource specifically does the yes command exhaust? If you are having trouble finding this, press < to choose which resource to order processes by. Make sure to quit out of yes (^C) once you are finished.

The process hierarchy

Run htop once more. This time click <F5> to enter Tree View. You should see a visual representation of the process hierarchy on your system, with everything stemming from /sbin/init (systemd).

For curious students that are interested in seeing a more extensive process hierarchy on a large system, you are encouraged to run htop on the OCF server tsunami. Let us know of any cool processes that you find!

Orphan processes

Open a second terminal and ssh to your VM. Now run sleep 1000 &. You should see this new process pop into your htop session on your first terminal. If not, press <F3> and search for “sleep.” (Exercise 3) What is its parent?

Select this parent and press <F9> to kill it. Send the SIGTERM signal. The sleep process now has init as its new parent, which is PID 1. What you just did is manually orphan a process; when that happens said process is subsequently re-parented by the init process.

Now go through the same steps again. This time, send the parent a SIGHUP (hangup) signal. Can you still find the sleep process? When SIGHUP is sent to a parent shell, the parent subsequently sends hangup signals to any child processes before terminating; all processes that receive SIGHUP from a parent shell will terminate – this is one way to avoid creating orphan processes.

If you are interested in learning about the different signals, run man 7 signal. Note that you can run man man for an explanation about the different manual section numbers.

Cron

So much infrastructure in the computing world relies on scheduled processes. This is the job of the cron daemon. For example, at the OCF, we use a cron job that runs every thirty minutes to keep our servers and desktops updated via puppet. To get a feel for the cron scheduler, we’re going to write a basic cron job.

(Exercise 4) Open the cron editor by running crontab -e (if the editor of your choice isn’t being launched, set the EDITOR environment variable), which will create a crontab for your user. Below is a sample task. Put this in your crontab:

* * * * * date +"\%T" >> $HOME/timestamps.txt

Right now this runs every minute. Modify it to run every five minutes and quit out of the editor. Make sure to leave this running while you complete the remainder of the lab as you will be turning this file in.

If you get stuck, visit crontab.guru!

Job Control

For this section of the lab you will need to get the necessary code from GitHub:

git clone https://github.com/0xcf/decal-labs.git

If you already have the code run git pull in your decal-labs directory.

Now enter the b6 directory in the decal-labs repository and split your terminal with the multiplexer of your choice (I recommend tmux). In one pane run ./job.sh > ~/count and in the other run less +F ~/count. You should see the less command increments every half a second or so. Now run Ctrl+Z in the pane with the job.sh command. This will suspend the process. (Exercise 5) What happens when you suspend the job command?

Now, let’s resume our suspended process. Since we only have one job, we can just run bg. (Exercise 6) What happens after running the bg command?

Now let’s bring our job to the foreground. First run jobs -l. You should see both the job number (in brackets) and the pid of the job. We’ll bring the job to the foreground by running fg %i (where i is the number that showed up in the brackets when you ran jobs -l) and kill it using Ctrl+C. (Exercise 7) What is another way we can kill the job (Hint: kill also recognizes the % syntax)?

Exploration

Congratulations, you have completed the lab! This is just the tip of the iceberg when it comes to processes. If you want to learn more, here are some related topics you can look into.

Submission

Go to Gradescope for submission.