SM1: Open-source
Operating Systems |
ASIX/DAW/DAM-1
|
Task A9: Managing
processes |
9-12-24
|
Task
A9: Managing processes
GENERAL CONDITIONS
1- Report
deadline: 22-12-24
2- Send your report as a PDF file attached to an e-mail with
the following specifications:
a) E-mail address: cf(at)collados.org or jordi.binefa(at)fje.edu depending who is your
teacher
b) File Names:
b.1) ASIX1:
asix1_surname_name_sm1act09.odt
asix1_surname_name_sm1act09.pdf
b2.) DAW1:
daw1_surname_name_sm1act09.odt
daw1_surname_name_sm1act09.pdf
c) Subject:
c.1) ASIX1:
asix1_surname_name_sm1act09
c.2) DAW1:
daw1_surname_name_sm1act09
3- Make this report individually.
4- Left, right, top and bottom margins: 2cm.
5- Character format: a) Font: Arial, b)
Size: 10, c) Questions typeface: Bold, d) Answers
typeface: Regular(Blue/Red).
6- Please note that if you submit your
report:
- Later than the deadline, 2 points will be deducted to your
final mark
- With a wrong ODT filename, 2 points will be deducted to
your final mark
- With a wrong PDF
filename, 2 points will be
deducted to your final mark
- With a
wrong ODT format, 2 points will be
deducted to your final mark
- With a
wrong PDF format, 2
points will be deducted to your
final mark
-
With a wrong
Subject, 2
points will be
deducted to
your final
mark
- Where
Questions
are not
written in
Bold
(black)
and answers
in
Regular (Red/Blue),
3
points will be
deducted to
your final
mark.
- Where
Screenshots
and answers were not properly added after each question, 3
points will be
deducted to
your final
mark.
-
With your
files
unproperly
attached or
not readable,
your report
will get 0
points.
DOCUMENTATION
0-
Some useful links
a) https://www.baeldung.com/linux/process-states
b) https://access.redhat.com/sites/default/files/attachments/processstates_20120831.pdf
c) https://idea.popcount.org/2012-12-11-linux-process-states/
1- Basic
Ideas
1.- A program is only code (a set of in instructions)
stored in a file on your hard drive (or any other permanent
storage device). When
the code of a program is sent and stored into the RAM memory,
the CPU can run the program.
2.- A process is a program
stored in RAM memory that can be run by the CPU. In other words, is a program in
action. Sometimes the process is run by the CPU and
sometimes the process is waiting to be run by the CPU.
3.- During its lifetime, a process
needs system resources such as the CPU time and RAM to hold
its code. The allocation of proper resources to each
process running in the system is a duty of Linux.
4- The total amount of memory allocated to a process
is called Virtual Memory and it is the addition of physical
RAM memory assigned to the process and Swap space assigned to
the process.
5.- Other
resources a process could need are access to:
hardware, physical or logical ports, reserved memory areas,
files, directories, etc...
6.- The process
table on Linux holds information about the processes
that are currently handled by the OS. Each process is
represented as an entry (or process slot) in
the process table. Information hold
in the process table includes (but not only):
- Process owner
- Memory and CPU usage
- Process identifier or PID
- The parent process identifier or PPID
- Process priority or niceness
- Process state
- Starting time/data of the process
and cumulative cpu time
7.- Linux is a multiuser and
multiprocessing operating system and therefore, multiple users
can be running one or more processes on the system at the same
time. That means the operating system needs to identify each
process. A process is identified by its Process ID
also called PID and by its Parent
Process ID also called PPID.
8.- A PID must be a unique
identification number for each process running on the system.
Processes can NOT share PID. As long as the process exists, it
keeps the same PID number. When a process is ended,
its PID is freed and eventually the operating system can
assign this number to another process.
9.- Multiple processes of a program can be run at the
same time. Different processes of
one single program will have different PIDs.
10.- A process is also
identified by its parent process ID or PPID:
- Whenever a process creates another
process, the former is called parent while latter is called
child process.
- A parent process can have several child
processes, but a child process can have only one parent.
- Any process running on the system (with
the exception of init) is a child process of a
parent process.
- Only init, the first process
created when you boot linux, has not a parent process. The
PID of init is 1 and its PPID is 0.
- A parent child should not be finished
until all its child processes has been finisihed and their
information released of the process table.
- After
child process finishes, the parent process has to
release any information about the child process hold by
the operating system in the process table.
11.- The operating
system is able to assign priorities to process. The
higher a process priority is, the higher will be, for
example, its CPU time or RAM allocated. When a new process is
started, the operating system assigns to the
process a value called the nice number or NI.
This number is related to process priority. The higher the
nice number is, the lower the priority of the process
is.
12.- The states of a Linux
process are the following:
a) Running
or Runnable (R):
- For any Linux process, its starting point
is the moment it is created, usually by a parent process. Once
it starts, the process goes into the running or runnable
state.
- Running: All
resources required by the process are available and the
process is executing instructions on some CPU on the
system.
- Runnable: All resources required by the process are
available and therefore the process is
ready to be run but, at the moment, the process is in a
queue waiting
to be assigned CPU time. The scheduling algorithm
might force a running process to
give up its execution right and change to runnable state.
This is to ensure each process can have a fair share of CPU
resources.
- Process state code:
R
b)
Interruptible Sleep (S):
- The process can not be
run at the moment because some external resources (for
instance, access to a printer, hard drive, network
card,...) required by the process are not available. If a
process goes to interruptible sleep immediately frees up
its access to the CPU.
- Also, a process can go
to interruptible mode because it requires that a certain
amount of time pass. For instance, when a process runs the
sleep instruction on C.
- When the resources
required by the process are available the process changes
to Running or Runnable state.
- An interruptible sleep
accepts and reacts to software or hardware signals such
as, for instance, a stoping signal.
- Process state code: S
c)
Uninterruptible Sleep (D):
- A process become
an uninterruptible sleeping process when it waits
until an specific resource is available. The process will
be awaken only if the specific resource becomes available.
- An uninterruptible
sleep process can not be stopped by a signal.
- Usually, a process
goes to uninterruptible sleep when it is performig I/O
operations such as accessing to the hard drive, to the
network card or a printer.
- Process state code: D
d) Stopped
(T):
- A process become a
stopped process when it receives the SIGSTOP signal. The
execution of the process will be suspended.
- A stopped process only
accepts the SIGKILL and SIGCONT signals. The
former will remove the process permanently, while the later
will put the process back to the Running/Runnable
state.
- Process state code:
T
e) Zombie (Z):
- A process become a
zombie process when its execution is completed and it
releases memory, cpu and any other resources.
- A zombie process waits
that its parents process releases its process slot (or
entry) in the process tables but the process does not
longer exists.
- A Zombie process only
can be removed from the process table by its parent
process or shutting down the computer.
- A Zombie process does
not accept signal because does not longer exists.
- A process can remain in a zombie state
if the parent process dies before it has the chance to
release the process slots of its child processes.
- Generally speaking, if a zombie process
remains in the process table, means that the code of
the child or parent process is a bad code (it has not
been properly developed).
- Process state code: Z
f) Idle Kernel threads (I):
- Beyond the scope of
this exercise
- Process state code: I
g)
Linux states diagram
13.- Signals are one of the ways
process communicate among themselves, with the kernel and with
users. The list of the most commonly used signals
follows:
- SIGTERM: It is the default signal
sent by
kill
command.
Asks the process to terminate voluntarily. Terminate the process in an
orderly way. Default option
- SIGKILL:
Unlike SIGTERM, forces the
process to terminate. Can't be blocked or handled.
- SIGSTOP: Suspend the process
execution, putting it in stopped state.
In this state, the process will do nothing but accept SIGKILL and SIGCONT
signals. Can’t be blocked or handled.
- SIGCONT: if a process is in stopped
state, it will put it back in the running/runnable
state and resume it execution. If the process is in any
other state, it's silently ignored.
- SIGINT: Generated when the user
type
<Ctrl>+C
in the terminal. It interrupts the current
command processing and wait for user's next command.
- SIGHUP: This signal indicates the
terminal handling the process has been disconnected and/or
the parent process terminated. If you want to run a process
that won't terminate when the terminal disconnects, you can
start it using the nohup
command. Some daemons
repurpose this signal to trigger a configuration reload
without stopping its execution.
- SIGQUIT:
Generated when
the user type
<Ctrl>+D
in
the terminal. It terminates the
process (like SIGTERM) but also causes the shell to write
a core file to help the so-called post-mortem debug.
14.- Processes that
require a user to start them or to interact with them are called
foreground processes. Processes
that are run independently of a user are referred to as background
processes. Programs or commands
started by users run as foreground processes by default.
To run a process in the background, place an ampersand (&)
at the end of the command name that you use to start the
process.
15.- Orphan
processes:
- They are those processes that are still
running even though their parent process has terminated or
finished.
- A process can be orphaned intentionally
or unintentionally.
- An intentionally orphaned process runs in
the background without any manual support. This is
usually done to start an indefinitely running service or to
complete a long-running job without user attention.
- An unintentionally orphaned process is
created when its parent process crashes or terminates.
Unintentional orphan processes can be avoided using properly
developed code.
- An orphan code is not a zombie code
because it is running and using resources (memory, cpu,...).
- There is not a special code for Orphan
processes. An orphan process can be in R, S, D or T state.
2-
Kernel. Memory areas. Type of processes
1.- The kernel:
- It is in the core of an operating system
and has a crucial role in its functioning. The kernel
manages the system resources, including cpu, memory,
hardware, drivers, processes, filesystems, users,
interactions between hardware and processes, interactions
between users and the processes, interactions between users
and hardaware, signals, input/output operations and so on.
In other words, with the help og the kernel controls
everything hardware, software and users can work
coordinately without conflicts.
- It is stored in the computer’s
memory and is loaded into memory when the system boots
up. It remains in memory until the system is shut down.
2.- On Linux there are two separeted
memory areas called user space memory area and kernel
space memory area:
- Kernel space memory
area is strictly reserved for running the kernel, some
special processes and device drivers.
- User space memory area is the memory area
reserved for processes started by users (root included).
- Primarily, this separation serves to
provide memory protection and
hardware protection from malicious or errant software
behaviour.
3.- There are three types of
processes in a Linux system:
- User processes: These are
processes initiated by a regular user and run in a memory
area called the user space. User processes work with
permissions granted to the user who started them.
- Daemon processes: A process that
is designed to work in the background, runs continuously and
independently of the terminal and shell, without direct
interaction with the users. Daemons work usually with
permissions granted to a system user. Daemons are usually
services such as Apache (web server) or CUPS (print
server). Daemons usually runs in user space. Daemons
usually are created by a parent process and then the
parent process is ended so the daemon become a kind of
orphan process and that makes that the init process
become its parent.
- Kernel processes: These are processes that run
only in the kernel space and they have special
security privileges. Kernel processes have full access to
the operating system inner structure. They are
typically used to manage hardware that's why they high
priority. Kernel processes are shown in the process tables
between squared brackets. There is an special state called I
for Idle kernel processes.
3- Displaying running processes
3.1- ps command
Display information about the active processes. It is a
complex command with a wide range of options and parameters.
Particularly interesting are the following three modes of
executing this command:
a) ps aux --> This command
displaying basic information (euid, pid, %memory, %cpu, stat,
command, ....) about every process running in the system.
b) ps
-eo
user,uid,%cpu,%mem,vsz,rss,pid,ppid,ni,stat,start,time,args
--> This command displays information about:
- the process owner
- % of CPU time and % of memory space
allocated to the process
- Virtual memory size (vsz) and RAM
memory allocated (rss)
- Process ID and parent process ID
- Nice number
- State of the process
- What time the command was started and
acumulative CPU time
- program/command name (with additional
arguments).
This command displays information about every process running in
the system.
c) ps -el --> Shows kernel proceess
The next example shows what happens when the ps -eo
user,uid,%cpu,%mem,vsz,rss,pid,ppid,ni,stat,start,time,args
command is executed:
NOTE: Any process between brackets is a process running in
the kernel space.
3.2- top command
The program provides a dynamic list in real-time of all
running processes. Information about the process ID, user
ID, % of CPU time and % of RAM allocated to the process, nice
number, state of the process, what time the command was started
and command name (with arguments) will be show on the screen. The
next example shows what happens when the top command is executed:
Press the p key if you
want to end the top
command and come back to the terminal.
4- Sending signals to
processes
4.1- kill command and signals
Command kill send signals to processes, There are multiple signals
that you can send to a process. If you want to send a signal to a
process, you will have to use the kill
command. The command kill -l shows a list of signals. Some of
the most important signals, are the following:
Signal
name |
Signal
number |
Meaning |
SIGTERM |
15 |
Terminates
or Ends the process in an orderly way and
sometimes executes a clean-up routine before exiting. By
default, if no signal is written, the kill command send
the SIGTERM signal to a process. Terminate or End are
synonymous when we talk about processes.
|
SIGKILL |
9 |
Interrupts
and Terminates immediately the process. A process can
not ignore this signal. It
does not permit to the process to do any action while
closing. It is a last resort signal and it is used when
SIGTERM or SIGQUIT are ignored by a process and do not
work.
|
SIGHUP
|
1
|
Reload configuration of
some daemons without stopping its execution. Disconnects
a process from its parent process
|
SIGINT
|
2 |
Generated when the user
type <Ctrl>+C
in the terminal. It interrupts the current
command processing and wait for user's next command. Some processes are designed to never quit
until you send them a SIGINT to stop. |
SIGQUIT
|
3
|
Generated when the user
type <Ctrl>+D
in the terminal. It
terminates the process (like SIGTERM) but also causes
the shell to write a core file to help the so-called
post-mortem debug. |
SIGSTOP
|
19
|
Suspend the process
execution, putting it in stopped
state.
|
SIGCONT
|
18
|
If a process is in
stopped state, it will put it back in the running/runnable state and resume it
execution.
|
In order to send a signal to a process, you need :
- Its PID and the command kill
- or use the <Ctrl> +
combination.
The synopsis of the kill
command is : kill
<-SIGNAL> PID.
The next example shows how we can run
the kill command
to end a process (nano) using its PID:
Some additional examples:
a) kill -SIGTERM 4388
(equivalent to kill
-15 4388 or kill
4388)
b) kill
-SIGKILL 4388 (equivalent to kill -9 4388)
As SIGKILL is a method of last resort and inherently
dangerous (data used by the process can be definitely lost), you
should use this signal cautiously. You should send the
termination signal SIGTERM when you need to end a process.
Only if this signal does not work should you attempt to use the
SIGKILL signal.
4.2- killall
It is an alternative to command kill. The killall
command sends a signals to processes by the process name. If you
have multiple processes under the same name, all of those
processes will be terminated, If no signal is specified, SIGTERM
is sent.
NOTE: The killall
command is part of a package called psmisc. If you
can not run killall because this commnad is not found on
your system then, install psmic.
The basic synopsis of the
kill command is : killall
<-SIGNAL> process_name(s).
Signals can be specified either by name (e.g. -SIGKILL )
or by number (e.g. -9). A
killall process never kills itself (but may kill other
killall processes).
The next example shows how we can run
the killall command
to end all instances of the geany
text editor:
The killall command can
send signals to two or more process if a list of process
names is provided to killall
as arguments. For instance:
killall
geany firefox-bin nano
5- Process in foreground and
background
a) To run a process in the background,
place an ampersand (&) at the end of the command name that you
use to start the process.
b) Example: nano &
c) jobs shows a list of processes started by the
user in the background.
d) fg %n moves a process %n in the
background to foreground. The value of %n is the
value shown by jobs
e) kill <SIGNAL> %n sends a signal <SIGNAL>
to process %n.
6- Process priority: nice
and renice commands
Linux can run multiple processes. It does this by sharing the CPU
and other resources among the processes. When you only have one or
a limited number of CPUs, you need to decide how to share those
limited CPU resources among several competing processes. This is
generally done by selecting one process for execution and letting
it run for a short period, or until it needs to wait for
some event, such as IO to complete. To ensure that important
processes get enough CPU time, a selection must be done based on a
scale of "priorities". By using this scale we can
allocate our CPU resources more appropriately. High priority
programs like daemons, services or I/O processes can be set to
receive more of the CPU’s focus, while lower priority programs,
which are not so important, can be set to receive a lesser CPU’s
focus.
In Linux, processes have a priority number called "nice value" ,
which is a number between -20 and 19. The value of -20 is the
highest, and 19 is the lowest priority with a default of zero.
Process priority can be set with the nice command and changed using the renice command. Larger nice
values correspond to a lower priority (you are being nice to the
other processes on the system). Processes with a lower nice value
(higher priority) run before processes with a higher nice value
(lower priority). The nice value also determines the CPU time
assigned to each process. In other words, the higher this
nice value, the “nicer” a process is to others, i.e., the lower
its priority.
6.1- Displaying the nice value
If you want to display the priority of processes running in you
system, you can run one of the following commnads:
a) ps -eo
pid,user,ni,%cpu,%mem,stat,start,time,args
b) ps alx
6.2-
Starting a process with a different priority (not its default
priority). Using the nice command.
The nice command
(with -n option) is used
to start a process with a different priority. Keep in mind the
following rules:
a) -n
option with a positive value ==> The nice values is
increased ==> The priority of your process is descreased.
b) -n
option with a negative value ==> The nice values is
decreased ==> The priority of your process is increased.
c) You usually have to be
root to specify negative
value.
Examples:
a) Starting, as a regular
user, the geany
program with a lower priority (or higher niceness):
nice -n 5 geany
&
Geany has been started with a decrease in its
default prioriy equal to 5.
b) Starting, as root
user, the geany
program with a higher priority (or lower niceness):
nice -n -4 geany
&
Geany has been started with an increase in its
default prioriy equal to 4.
6.3-
Changing priority of a running process. Using the renice
command.
The renice is used to change the priority
of a running command. Keep in mind the following rules:
a)
Opposite
to nice, renice works with absolute
priority positive value. The written value written will be the
value assigned directly to the process' nice value.
b) You have to be root to run the renice command and decrease a
nice value below its default value.
c) You will need the PID.
Examples:
a) Changing the priority
of process 6876 :
renice +10 6876
The nice value of process 6876 has been changed
to 10.
a) Changing the priority of process 9854 :
renice -15 9854
The nice value of process 9854 has been changed
to -15.
7-
Writing proper sentences about processes
Read (mandatory) the
following document: About
Processes
PRACTICAL EXERCISE
1- Start geany.
Displays
information about: the process owner, % of CPU
time and % of memory space allocated to the process,
Virtual memory size (vsz) and RAM memory allocated,
Process ID and parent process ID, Nice number, State of the
process, what time the command was started and acumulative CPU
time and program/command name.
2- Open a new instance of geany. Find
out its PID and PPID:
a) Compare the PID found in the 2nd
question and the PID found in the 1st question. Is there
any difference between them? Why?.
b) Compare
the PPID found in
the 2nd question and the PPID found in the 1st
question. Is there any difference between them?
Why?.
3- Using the terminal: End
the processes
started in question 1 and 2 in an
orderly and safe way.
4- Start a GUI program called MATE
System Monitor (Applications -> System Tools -> Mate
System Monitor). Display basic information about
the process.
5- Open a terminal.
Become user fje. As fje user, find out the PID
of the process started in question 4. Try to end the process in an orderly and safe
way. Are you
able to end the process?. Why?.
6-
Become again your "by default" user. Find out the PID of the process started in question 4.
Try to end the process in an
orderly and safe way.
Are you able to end the process? Why?.
7- Start 4 instances of geany text editor and 3
new instances of Atril (the default pdf reader that you
can find in Applications --> Office). Check the 7 processes where
started. Show basic information only about the 7 processes
started with the help of grep. Do not show the
process grep.
Help 1: -E '(geany|atril)' to show geany
and atril.
Help 2:
-v to not show grep.
8- End these 7 processes started in question 7 using
their names and one only command.
9- Find
out the PID and user owner of the
sshd process. Try to end the sshd.
What happens?. Why?.
10- As root user, find out the PID and
user ID of the sshd process. Try to end
the sshd process using the command with the signal SIGHUP.
What happens?. Why?.
11- As
root user, try to end the sshd
process using the signal SIGTERM. What happens?. Why?..
12- Start geany. Find
out the PID number and the process name of the
new geany's instance in the process
table.Now:
a) Stops geany. Show its state.
b) Try to create a new file. What
happens?. Why?
13- Put back geany
to the Run/Runnable state. What happens now?. Can
you create new files?.
14- Start a new instance of geany from your
terminal. Send the SIGINT signal to geany.
Show the state of your terminal after sending the SIGINT
signal.
15- Start a new instance of nano in the background.
Show a list of processes started by your user to the background.
16- Move nano to the foreground.
17- Run the command sleep
100000 in the background. Show a list of processes started by your user
to the background. Check the process identifier
and state.
18- Stop the process started in
question 17
19- As a regular user,
start a new instance of geany with
an increase in its default nice value equal to 9.
Check if geany is
running with the new nice value. Have you had any
problem? Why?.
20- As a regular user, start a new instance of geany with a
decrease in its default nice value equal to 5. Check if geany is running with the
new nice value. Have you had any problem? Why?.
21- As root
user , start a new instance of geany with a
decrease in its default nice value equal to 9.
Check if geany is
running with the new nice value. Have you had any
problem? Why?.
22- As root
user, close all instances of the geany program.
23- As a regular user, start a new instance of geany with an increase in
its nice default value equal to 2. Now, try to change its nice
value to -8. Are you able to change the nice value of geany? Why?.
24- As a regular user, start a new instance of geany with an increase in
its nice default value equal to 6. Now, try to change its nice
value to 3. Are you able to change the nice value of geany? Why?.
25- As a root
user, change the nice value of the process geany started in the
previous question. Try to change its nice value to -11. Are you able to change the nice value of geany? Why?.
26- Download https://www.collados.org/asix1/sm1/act09/q26.c
and:
a) Compile q26.c running:
gcc q26.c -o q26.
Run q26.
b) Open a new termimal.
Run: ps -eo
user,pid,ppid,state,command | grep
q26 | grep -v grep
c) How many q26 processes
are running in the system?
d) Is any of them a zombie
process?. How do you know it?
e) Try to send the SIGTERM or SIGKILL
to the q26 zombie process. Does it work?. Why?
f) When will the q26 zombie
process be removed from the process table?
27- Download https://www.collados.org/asix1/sm1/act09/q27.c
and:
a) Compile q27.c
running: gcc q27.c
-o q27. Run q27.
b) Run: ps
-eo user,pid,ppid,state,command |
grep q27 | grep -v grep
c) Can you find any instance of q27
that has become a orphan process?. How do you know it?
d) When will the q27
orphan process be removed from the process table?
e) Try to send the SIGTERM
or SIGKILL to the q27 orphan process.
Does it work?. Why?