COMP 10024 – UNIX Process Management – Taking Control

April 12, 2026 10:48 pm Published by

Until now, we have mostly run programs in the foreground—typing a command and waiting for it to finish before the shell gives us back the prompt. However, UNIX is a multitasking environment. To be truly efficient, you must learn how to manage multiple processes at once.


1. Foreground vs. Background Processes

When you run a command normally, it “owns” your terminal until it finishes. If a program takes a long time (like a complex calculation or a large file download), your terminal is essentially locked.

  • Foreground: The program receives input from your keyboard and sends output to your screen. You cannot run other commands until it ends.
  • Background (&): By adding an ampersand (&) to the end of a command, you tell the shell to run the program in the “background.” You get your prompt back immediately.
# Runs a script in the foreground (terminal is locked)
./long_task.sh

# Runs the same script in the background (terminal is free)
./long_task.sh &

# Best practice: Redirect output so it doesn't clutter your screen
./long_task.sh > output.log 2> error.log &

2. Job Control: The Shell’s Manager

The shell maintains a list of all processes it has started. These are called Jobs. Each job is assigned a small integer called a Job Number (e.g., %1, %2), which is different from the System PID (Process ID).

  • jobs : Lists all currently active jobs and their status.
  • Ctrl+z : Suspends (pauses) a running foreground process and moves it to the background as a “Stopped” job.
  • bg %1 : Resumes a stopped job in the background.
  • fg %1 : Brings a background or stopped job into the foreground.

3. Signals: Communicating with Processes

A Signal is a small, numbered message sent to a process. Think of it as “tapping the process on the shoulder” and showing it a sign. The process can either handle the signal with custom logic or let the Operating System perform the default action (usually termination).

Signal ID Keyboard Default Action
SIGINT 2 Ctrl+c Interrupt/Kill the process gracefully.
SIGTSTP 20 Ctrl+z Stop (suspend) the process.
SIGTERM 15 N/A Polite request to terminate (Standard kill).
SIGKILL 9 N/A Forceful termination (Cannot be ignored).

The Golden Rule of Killing: Always try kill [PID] (Signal 15) first to let the program save its data. Only use kill -9 [PID] if the program is completely unresponsive.


4. Daemons: The Unseen Workers

Not every process needs a human user or a terminal. Daemons are background processes that start when the system boots and run silently in the background to provide services.

  • System Daemons: init (the first process), cron (scheduled tasks), sshd (remote login).
  • Application Daemons: httpd (web server), mysqld (database).

You can usually identify daemons in a process list (ps -ef) because they often have a ? in the TTY (terminal) column.


Lab: Managing the Workload

In this lab, you will practice moving processes between the foreground and background and using signals to control them.

Task 1: Backgrounding and Job IDs

  1. Start a process that takes a long time, like a search of the entire system:

    find / -name "test" > results.txt 2> errors.txt &

  2. Immediately type jobs. You should see the task running and its job number (likely [1]).
  3. Wait a few moments and run jobs again until it shows “Done”.

Task 2: Suspending and Resuming

  1. Open a file in Vi: vi practice.txt
  2. Instead of quitting, “suspend” Vi by pressing Ctrl+z. The shell will say [1]+ Stopped.
  3. Run ls to prove you are back in the shell.
  4. Bring Vi back to the foreground so you can keep editing: fg %1
  5. Save and quit Vi normally.

Task 3: Sending Signals

  1. Start a “dummy” process that does nothing: sleep 1000 &
  2. Find its Job ID and PID using jobs -l.
  3. Kill it gracefully using its Job ID: kill %1 (or whichever number it was).
  4. Verify it is gone with jobs.
  5. Now start another: sleep 2000 & and kill it forcefully using its PID:

    kill -9 [INSERT_PID_HERE]

Task 4: Spotting Daemons

  1. Run the command ps -ef | less.
  2. Look at the TTY column. Most processes will show something like pts/0 (your terminal).
  3. Scroll down to find processes with a ? in that column. These are Daemons. Can you identify one web or system service running?

Summary Challenge

A user has a program called cleanup.sh that has “frozen” and is consuming 100% of the CPU. It is running as Job #3 with PID 4567. Write the sequence of two commands you would use to try and stop this process, starting with the most “polite” method first.

Categorised in: , ,

This post was written by amax

Comments are closed here.