โ† Operating System Basics: From Zero to Hero
Level 2 ยท Explorer: Inside Linux

Programs and Processes: Start and Stop Them

Lesson 7 of 14 ยท โฑ 12 min

What's the difference between a program and a process? It's the difference between a recipe and cooking!

A program is a file of instructions on disk, like a recipe card. Opening it creates a process running in memory with its own ID.

  • A program is a file of instructions on your storage. It does nothing by itself.
  • A process is a program that is running. The OS loads it into RAM and gives it a number called a PID (process ID).

Today you will start, watch and stop processes yourself! ๐Ÿงช

๐Ÿงช Lab 1: meet your first processes

ps

You'll see a small table:

    PID TTY          TIME CMD
   2145 pts/0    00:00:00 bash
   2310 pts/0    00:00:00 ps
  • PID is the process ID, a unique number
  • CMD is the program that is running

The bash is the shell, listening to you. And ps is in the list too, because it was running when it looked!

Which PID is the shell you're typing in?

echo $$

$$ always means the PID of this shell.

๐Ÿงช Lab 2: start a process in the background

sleep 300 &
  • sleep 300 does nothing for 300 seconds (5 minutes). It's a perfect practice process.
  • The & at the end means run in the background, so you get your prompt back.

You'll see something like [1] 2455. That 2455 is the PID of your new process!

jobs
ps -o pid,ppid,stat,cmd -p $$

jobs lists your background jobs. And the ps command shows your shell with its parent (ppid), which is who started it, and its state (stat).

Now find your sleep process (use the PID you saw):

ps -o pid,ppid,stat,cmd -C sleep

The STAT column shows the process state:

Letter Meaning
R Running or ready to run
S Sleeping: waiting for something
T Stopped
Z Zombie: finished but not yet cleaned up ๐ŸงŸ

Your sleep should say S. It's waiting, using no CPU at all!

A process is New, then Ready, then Running. It can return to Ready when its time is up, or go to Waiting when it needs something, and finally it is Done.

๐Ÿงช Lab 3: stop a process

kill 2455

(Use your PID instead of 2455.) Then check:

jobs
ps -C sleep

It's gone! The OS asked the process to finish, and it did. ๐ŸŽ‰

๐Ÿ’ก kill really means send a message. The normal message is "please finish". If a process ignores you, kill -9 is the strict version. Use it only on your own lab processes.

๐Ÿงช Lab 4: watch all processes live with top

top

top is a live scoreboard of every process. It updates every few seconds.

A guide to the top screen: summary lines on top, then a table of processes with PID, user, CPU, memory and command.

Look at the columns:

  • PID the process ID
  • %CPU how much CPU it's using
  • %MEM how much RAM it's using
  • COMMAND the program name

Press q to quit top. (Press M to sort by memory, P to sort by CPU.)

๐Ÿงช Lab 5: everyone gets their own room

ps aux | head -10

This lists processes from every user. Look at the USER column: you'll see root (the system's boss) and your user name. Notice the PID numbers are all different. The OS gives every process its own protected memory, so one crashing program can't wreck the others.

๐Ÿšฆ Force Quit on your desktop does the same thing as kill: it asks the OS to stop a stuck process.

Words to know

Word What it means
Process A program that is running
PID The process's unique ID number
Parent (PPID) The process that started this one
Background (&) Running without blocking your prompt

๐ŸŽฏ Quiz time!

1. What does the & at the end of a command do?
2. What does kill 2455 do?
3. In the ps STAT column, what does S mean?
4. Which command shows live information about every process?