What is a process?
A program is a set of machine code instructions and data stored in persistent memory on your computer. A Linux/Unix process (or task) can be thought of as a computer program.
Processes carry out specific tasks. Linux is a multitasking operating system, meaning that more processes are running in parallel at the same time. Processes run with different priorities. For example, a process that is critical for the operation of the machine will have a higher priority than say, a text editor. Processes with higher priority will be given more run time. Time allocation for each process is handled by a part of the kernel called scheduler. The scheduler can also decide to interrupt the execution of a lower priority task and run a higher priority task that needs to perform a more important function which can not wait. This mechanism is called preemption, so the Linux/Unix kernel is a preemptive kernel.
Looking at proceses
The command used to examine processes is ps .
pi@raspberrypi:~ $ ps
PID TTY TIME CMD
1225 pts/0 00:00:00 bash
2052 pts/0 00:00:00 ps
pi@raspberrypi:~ $
The above command shows the processes running in the current bash shell. (the bash shell itself and the ps command we just typed).
If we want to see all the processes running on the system with more details, we will use options aux. That's a lot of processes!
pi@raspberrypi:~ $ ps aux |more
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 1 0.0 0.6 9672 5980 ? Ss 18:12 0:02 /sbin/init splash
root 2 0.0 0.0 0 0 ? S 18:12 0:00 [kthreadd]
root 4 0.0 0.0 0 0 ? I< 18:12 0:00 [kworker/0:0H]
root 6 0.0 0.0 0 0 ? I< 18:12 0:00 [mm_percpu_wq]
root 7 0.0 0.0 0 0 ? S 18:12 0:00 [ksoftirqd/0]
root 8 0.0 0.0 0 0 ? I 18:12 0:01 [rcu_sched]
root 9 0.0 0.0 0 0 ? I 18:12 0:00 [rcu_bh]
root 10 0.0 0.0 0 0 ? S 18:12 0:00 [migration/0]
root 11 0.0 0.0 0 0 ? S 18:12 0:00 [cpuhp/0]
root 12 0.0 0.0 0 0 ? S 18:12 0:00 [cpuhp/1]
root 13 0.0 0.0 0 0 ? S 18:12 0:00 [migration/1]
root 14 0.0 0.0 0 0 ? S 18:12 0:00 [ksoftirqd/1]
root 16 0.0 0.0 0 0 ? I< 18:12 0:00 [kworker/1:0H]
root 17 0.0 0.0 0 0 ? S 18:12 0:00 [cpuhp/2]
root 18 0.0 0.0 0 0 ? S 18:12 0:00 [migration/2]
root 19 0.0 0.0 0 0 ? S 18:12 0:00 [ksoftirqd/2]
root 21 0.0 0.0 0 0 ? I< 18:12 0:00 [kworker/2:0H]
root 22 0.0 0.0 0 0 ? S 18:12 0:00 [cpuhp/3]
......
You can see now some process details, like:
PID: Process ID
CPU and MEM: CPU and Memory occupancy in percentages
TTY: Controlling terminal associated with the process
STAT: Process status code
TIME: Total CPU usage time
CMD: Name of executable/command
If you look at the man page for ps, you'll see that there are lots of command options you can use.
The top command provides real time information about the processes running on your system instead of a snapshot (like ps). By default top refreshes every 10 seconds. Top is a very useful tool that helps you see what processes are taking up a lot of your machine's resources.
pi@raspberrypi:~ $ top
top - 20:24:50 up 2:12, 3 users, load average: 0.42, 0.31, 0.28
Tasks: 154 total, 3 running, 108 sleeping, 0 stopped, 3 zombie
%Cpu(s): 0.6 us, 3.9 sy, 0.0 ni, 95.4 id, 0.1 wa, 0.0 hi, 0.1 si, 0.0 st
KiB Mem : 896672 total, 444216 free, 183392 used, 269064 buff/cache
KiB Swap: 102396 total, 102396 free, 0 used. 633112 avail Mem
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
164 root 20 0 0 0 0 S 20.7 0.0 10:14.44 w1_bus_master1
2172 pi 20 0 8112 3260 2808 R 1.3 0.4 0:00.59 top
414 pi 20 0 50780 14124 6176 S 0.3 1.6 0:04.43 Xtightvnc
599 pi 20 0 144424 27864 19848 R 0.3 3.1 3:07.85 lxpanel
818 pi 20 0 152200 27900 19848 S 0.3 3.1 3:05.81 lxpanel
982 root 20 0 14704 10512 6656 S 0.3 1.2 0:34.15 python
1 root 20 0 9672 5980 4816 S 0.0 0.7 0:02.14 systemd
2 root 20 0 0 0 0 S 0.0 0.0 0:00.01 kthreadd
4 root 0 -20 0 0 0 I 0.0 0.0 0:00.00 kworker/0:0H
6 root 0 -20 0 0 0 I 0.0 0.0 0:00.00 mm_percpu_wq
.... ... ...
Use CTRL-C
to stop top.
Write a program, run a process
Let us write a small Python program. You will use nano to create a file called hello.py. You don't need to know anything about Python for this example. Just copy the following lines into your hello.py file. This program will write the words "hello test" in the stdout console every 10 seconds.
from time import sleep
while (1):
print "hello test"
sleep(10)
Let's make sure our file look OK. We do this with:
pi@raspberrypi:~/work $ pwd
/home/pi/work
pi@raspberrypi:~/work $ ls
dir1 dir2 file1 hello.py
pi@raspberrypi:~/work $ cat hello.py
from time import sleep
while (1):
print "hello test"
sleep(10)
pi@raspberrypi:~/work
So far so good. Let's see what kind of file hello.py is. For that, we will use file command.
pi@raspberrypi:~/work $ file hello.py
hello.py: Python script, ASCII text executable
pi@raspberrypi:~/work $
pi@raspberrypi:~/work $ ls -al
total 20
drwxr-xr-x 4 pi pi 4096 Sep 2 20:34 .
drwxr-xr-x 28 pi pi 4096 Dec 31 1999 ..
drwxr-xr-x 2 pi pi 4096 Sep 1 01:04 dir1
drwxr-xr-x 2 pi pi 4096 Sep 1 01:06 dir2
-rwxr----- 1 root pi 0 Sep 1 04:43 file1
-rw-r--r-- 1 pi pi 72 Sep 2 20:34 hello.py
Let's run our Python program now.
pi@raspberrypi:~/work $ python hello.py
hello test
hello test
hello test
^CTraceback (most recent call last):
File "hello.py", line 5, in <module>
sleep(10)
KeyboardInterrupt
pi@raspberrypi:~/work
While our program is running, we can not use the bash shell to type other commands. You can stop the program with CTRL-C
.
Background processes
Users can also run processes in the background. This means that users can still use the bash shell while their programs are running. To run our process in the background, we will add the ampersand sign at the end of our command.
pi@raspberrypi:~/work $ pwd
/home/pi/work
pi@raspberrypi:~/work $ python hello.py &
[1] 2473
pi@raspberrypi:~/work $ hello test
pi@raspberrypi:~/work $
pi@raspberrypi:~/work $
pi@raspberrypi:~/work $ ps -u
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
pi 734 0.0 0.4 6096 4016 tty1 S+ 18:12 0:00 -bash
pi 1225 0.0 0.5 6472 4500 pts/0 Ss 18:19 0:01 -bash
pi 2473 1.0 0.5 9084 4588 pts/0 S 20:58 0:00 python hello.py
pi 2480 0.0 0.3 7744 2972 pts/0 R+ 20:58 0:00 ps -u
pi@raspberrypi:~/work $ hello test
pi@raspberrypi:~/work $ hello test
pi@raspberrypi:~/work $ hello test
2473 is the process id of our process. If we use ps, we can see our Python script running in the background.
Every 10 seconds we will see the "hello test" message on the screen, but we can still use the shell while our process runs in the background.
A process that runs in the background can be terminated with kill (you need to know the process ID).
hello test
hello test
hello test
hello test
pi@raspberrypi:~/work $
pi@raspberrypi:~/work $ kill 2473
pi@raspberrypi:~/work $
[1]+ Terminated python hello.py
States and Signals
There are two types of processes in Linux: foreground processes and background processes. Foreground processes (also referred to as interactive processes) are initialized and controlled through a terminal session by an user. Background processes (also referred to as non-interactive/automatic processes) are processes not connected to a terminal; they don’t expect any user input.
Daemons are special types of background processes that start at system startup and keep running forever as a service until the system stops (e.g. network daemon).
A process that starts another process is called parent process. A process that is started by a parent is called child process.
A process has a state. It can be Running. It can be Waiting (for an event to occur or for a system resource). It can be Stopped – in this state, a process has been stopped, usually by receiving a signal. A process can be in a Zombie state, meaning the process is dead, it has been halted but it still has an entry in the process table in the kernel.
The way of controlling processes in Linux is by sending signals to them. The kill command we used before is such a signal that we sent to our Python process in order to terminate it.
There are multiple signals that you can be sent to a process. Next to files, processes are a fundamental concept in Linux. You can read more about processes following the links provided in the Links chapter at the end of this tutorial.