1
Chapter 6
Process and Memory
Control
ITP3901 OPERATING SYSTEMS FUNDAMENTALS
(AY 2019/20)
2
Process
Management
Chapter 6 - Process and Memory Control ITP3901 Operating Systems Fundamentals
3
The Linux Multitasking Model
Process
A program running on system
Started from:
Command line
Graphical desktop
Kernel itself
Another process
Kernel maintains information about processes
Chapter 6 - Process and Memory Control ITP3901 Operating Systems Fundamentals
4
The Linux Multitasking Model
(Cont.)
Linux is a multitasking operating system
Single microprocessor can only perform one task at a time
Time slice
A few microseconds
Allocated to each process by kernel
Common to all operating systems that do not have multiple microprocessors
Chapter 6 - Process and Memory Control ITP3901 Operating Systems Fundamentals
5
Creating Processes
Kernel starts the process called systemd
When Linux first started PID of “systemd” is “1”
Every process running on Linux is “descendant” of init process
Process id (PID)
Unique number identifying process within Linux kernel
End Process
When process ends
Information about process that kernel was maintaining would be discarded
Resources that kernel had allocated to process would be released
Chapter 6 - Process and Memory Control ITP3901 Operating Systems Fundamentals
6
Process States
Running (R)
Sleeping (S)
Stopped (T)
Multi-threaded (I)
Zombie (Z)
A zombie process is one which is no longer in effect. It is represented by
“z” state in ps command. It is a process which has been killed.
Chapter 6 - Process and Memory Control ITP3901 Operating Systems Fundamentals
7
Managing Linux Processes
View information about Linux processes (ps command)
Control status of processes (nice, renice commands)
Chapter 6 - Process and Memory Control ITP3901 Operating Systems Fundamentals
8
Starting Processes from the Shell
When you start a program
Program takes control of command line
Parent process pauses to wait for new process to finish
A process can be executed as a background task by adding a & following
the command
Shell forks new process without pausing itself
Called process will be placed in background
Can start another command immediately
e.g. myprocess&
run in background
Chapter 6 - Process and Memory Control ITP3901 Operating Systems Fundamentals
9
Starting Processes from the Shell
(Cont.)
What is job ?
Process associated with shell
jobs command
Lists all jobs or processes running as descendants of current shell
Also called child processes
Ctrl+Z key combination
Suspend job that shell is running
Chapter 6 - Process and Memory Control ITP3901 Operating Systems Fundamentals
10
bg and fg Commands
bg command
Run suspended program in background
fg command
Place suspended job in foreground
Must specify:
Job number using %
Or PID
Chapter 6 - Process and Memory Control ITP3901 Operating Systems Fundamentals
11
Daemon
Daemon is a process that runs on background
Not under the direct control of an interactive user
Does not have any screen output but waits for certain system activity
then acts on it
The parent process of a daemon is usually the init process
launched during booting (i.e., computer startup) and run unobtrusively
in the background until they are activated by a particular event or
condition.
Chapter 6 - Process and Memory Control ITP3901 Operating Systems Fundamentals
12
Process information – ps command
ps (process status) command
This command comes in two versions, BSD and SVR4 (the options are
different).
Lists processes currently running on system
Output:
PID
Terminal that process is using for output
CPU time that process has used so far
Command that started process
……
Chapter 6 - Process and Memory Control ITP3901 Operating Systems Fundamentals
13
Options for ps command (SVR4)
Option Description
-e Display all processes running on terminals as well as processes that
do not run on a terminal (i.e. daemons)
-f Display a full list of information about each process, including the
UID, PID, PPID, CPU utilization, start time, terminal, processor time
and command name
-l Display a long list of information about each process, including flag,
state, UID, PID, PPID, CPU utilization, priority, nice value, address,
size, WCHAN, terminal and command name
The -l option generates a long listing. and when used together with the -e
and -f options creates a table with 15 columns
Chapter 6 - Process and Memory Control ITP3901 Operating Systems Fundamentals
14
Example of ps command
ps –el
Process ID of the parent process
Process ID
Nice Value
F S UID PID PPID C PRI NI ADDR SZ WCHAN TTY TIME CMD
4 S 0 1 0 0 76 0 - 497 - ? [Link] systemd
… … … …
4 S 0 1875 1872 0 75 0 - 1113 wait pts/0 [Link] bash
0 S 0 1897 1875 0 76 0 - 1101 wait pts/0 [Link] [Link]
0 S 0 1902 1897 0 76 0 - 909 358270 pts/0 [Link] sleep
4 R 0 1903 1875 0 77 0 - 1035 - pts/0 [Link] ps
Owner of this process
Chapter 6 - Process and Memory Control ITP3901 Operating Systems Fundamentals
15
Options for ps command (BSD)
Option Description
a Display all processes running on terminals
x Display all process that do not run on terminals
u Display the process for a specific user.
Note: “-” is not required!
Chapter 6 - Process and Memory Control ITP3901 Operating Systems Fundamentals
16
Example of ps command
A common and convenient way of using ps to obtain much more complete
information about the processes currently on the system is to use the
following:
ps ax | more
The a option tells ps to list the processes of all users on the system.
The x option adds to the list processes that have no controlling terminal,
such as daemons
Chapter 6 - Process and Memory Control ITP3901 Operating Systems Fundamentals
17
Example of ps command (Cont.)
Chapter 6 - Process and Memory Control ITP3901 Operating Systems Fundamentals
18
Example of ps command (Cont.)
An alternative set of options for viewing all the processes running on a system
is
ps -ef | more
The -e option generates a list of information about every process currently
running. The -f option generates a listing that contains fewer items of
information for each process than the -l option (explained in next slide).
Among the columns displayed by ps -ef, UID contains the username of the
account that owns the process (which is usually the same user that started the
process) and STIME displays the time the process started, or the starting date if
it started more than 24 hours ago.
Chapter 6 - Process and Memory Control ITP3901 Operating Systems Fundamentals
19
Example of ps command (Cont.)
Chapter 6 - Process and Memory Control ITP3901 Operating Systems Fundamentals
20
top command
top command
Displays list of running processes
Arranged by how much CPU time each is using
Process consuming greatest amount of CPU time shown at top of list
Updated regularly
Normally started without any options
Cannot run in background
Can be used to monitor the processes of only one user
Can be used to kill processes
Chapter 6 - Process and Memory Control ITP3901 Operating Systems Fundamentals
21
top command (Cont.)
Chapter 6 - Process and Memory Control ITP3901 Operating Systems Fundamentals
22
top command (Cont.)
%CPU field
Compares amount of CPU time used by process with total time elapsed since
previous computation of %CPU field
Form of percentage
Does not show average amount of CPU time used by process since it was started
%WCPU field
Weighted to show 30-second average of percentage of CPU time used by
process
Helpful for showing overall usage pattern
Chapter 6 - Process and Memory Control ITP3901 Operating Systems Fundamentals
23
top command (Cont.)
TIME field
Provides cumulative measure of amount of CPU time consumed by
process
Chapter 6 - Process and Memory Control ITP3901 Operating Systems Fundamentals
24
Controlling Processes – kill
command
kill command can be used to send signal to a running process, usually to
request termination of a process.
Controls any process
Sends signals to processes
-l option views all signals
Examples:
$kill -9 1482
$kill -SIGTERM 1482
Process ID
Chapter 6 - Process and Memory Control ITP3901 Operating Systems Fundamentals
25
Controlling Processes - kill
command (Cont.)
Signals
Messages sent between processes
About 30 different signals available
Has name and number associated with it
Software developer decides to which signals program responds
SIGTERM signal
Number 15
Requests that program end
Almost all programs respond
Chapter 6 - Process and Memory Control ITP3901 Operating Systems Fundamentals
26
Controlling Processes - kill
command (Cont.)
SIGKILL signal
Number 9
Handled by Linux kernel
Shuts down indicated process
Unsaved data in program will be lost
Renegade program virtually never crashes Linux kernel
Chapter 6 - Process and Memory Control ITP3901 Operating Systems Fundamentals
27
Popular kill signals
Name Number Description
SIGTERM 15 The software termination signal is the most common kill
signal used by program to kill other processes. It is the default
kill signal used by the kill command (i.e. when there is no
option, kill assumes -15).
SIGKILL 9 Also known as the absolute kill signal, it forces the Linux
kernel to stop executing the process by sending the process’s
resources to a special device file called /dev/null.
Chapter 6 - Process and Memory Control ITP3901 Operating Systems Fundamentals
28
Controlling Processes - killall
command
killall command
Sends signal to all processes started by given command
Useful when program makes copies of itself faster than you can locate PIDs and
use kill to shut them down
The assigned priority of a process determines how much CPU time granted
to process
Normally all processes have same priority
Chapter 6 - Process and Memory Control ITP3901 Operating Systems Fundamentals
29
How to find Process ID (PID)
ps command ( e.g. ps aux | grep sshd)
top command
pidof command
finds the process id's (pids) of the named programs.
e.g. pidof sshd
Chapter 6 - Process and Memory Control ITP3901 Operating Systems Fundamentals
30
Controlling Process Priority
Nice Level
Another name for process priority
Standard level is 0
Highest level 19 (Large value means slower)
For general user, nice value is in the range of 0 to 19.
For superuser/root, it can be negative value (up to -20)
System administrator can make any process nicer (by nice and
renice commands)
User can raise nice level of process that he or she started
Chapter 6 - Process and Memory Control ITP3901 Operating Systems Fundamentals
31
nice and renice Commands
Alter process priority by adjusting the nice value
nice command (to change the nice level when starting a new process)
$nice –n 5 myprocess(or nice -5 myprocess)
$nice –n -10 myprocess
renice command (to change the nice level of a running process)
$renice +10 1776
$renice -8 1776
PID
Chapter 6 - Process and Memory Control ITP3901 Operating Systems Fundamentals
32
System Monitor
System Monitor is a GUI system monitoring utility that will display running
processes, memory usage, file system usage. Apart from monitoring, you can
also take several actions from the system monitor user interface — such as
killing a running process.
Chapter 6 - Process and Memory Control ITP3901 Operating Systems Fundamentals
33
Memory
Management
Chapter 6 - Process and Memory Control ITP3901 Operating Systems Fundamentals
34
Memory
When a program runs in a computer, data are read from hard disk drive and
placed into the temporary storage of RAM. The computer uses its memory to
execute the program and frees the memory space once the program exits.
Since memory is a limited system resource, the administrator may solve the
lack of memory by using virtual memory.
Chapter 6 - Process and Memory Control ITP3901 Operating Systems Fundamentals
35
Virtual Memory
Virtual memory is hard disk space that is used to supplement RAM.
A swap file is an area of the hard disk that is used for virtual memory.
Chapter 6 - Process and Memory Control ITP3901 Operating Systems Fundamentals
36
Swap Files
Although Windows uses a swap file, it does not have to be configured. The
swap file is created as a file in the NOS partition.
Linux system typically dedicates an entire partition to swap space.
This partition is called the swap partition.
Usually the size of the swap partition should be equal to twice the computer RAM on
a Red Hat (including Fedora) Linux system.
Chapter 6 - Process and Memory Control ITP3901 Operating Systems Fundamentals
37
Swap Space in Linux
Swap space in Linux is used when the amount of physical memory is full. If
the system needs more memory resources and the physical memory is full,
inactive pages in memory are moved to the swap space.
While swap space can help machines with a small amount of RAM, it should
not be considered a replacement for more RAM.
Swap space is located on hard drives, which have a slower access time than
physical memory.
Chapter 6 - Process and Memory Control ITP3901 Operating Systems Fundamentals
38
Swap Space in Linux (Cont.)
Swap space can be a dedicated swap partition, a swap file, or a combination
of swap partitions and swap file.
Acts like extension of system’s RAM
Swapped-out process cannot run until swapped again
Thrashing
Kernel spends so much time moving processes to and from swap
space that kernel and processes bog down and work inefficiently
Chapter 6 - Process and Memory Control ITP3901 Operating Systems Fundamentals
39
Swap
Can use swap file (512MB) instead of swap partition
dd if=/dev/zero of=/swapfile bs=1024 count=512k
mkswap /swapfile
Enable swap
swapon /swapfile
swapon /dev/sda2
Disable swap
swapoff /swapfile
swapoff /dev/sda2
Check swap resource usage
cat /proc/swaps
Chapter 6 - Process and Memory Control ITP3901 Operating Systems Fundamentals