M01: Introduction to Operating Systems
TU1: Installing, configuring and exploiting a computer system
ASIX1
Practical Exercise 7: Managing processes 01-2-22

Practical Exercise 7:  Managing processes

GENERAL CONDITIONS
1
- Report deadline: 13-2-22
2- This is a mandatory practical exercicse
3-
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 Name:

        b.1)
ASIX1 (Catalan): asix1_surname_name_m01tu01pr7.pdf and asix1_surname_name_m01tu01pr7.odt
        b2.) DAW1 (English): daw1_surname_name_m01tu01pr7.pdf and daw1_surname_name_m01tu01pr7.odt     
     c) Subject:
        
c.1) ASIX1 (Catalan): asix1_surname_name_m01tu01pr7
         c.2)
DAW1 (English): daw1_surname_name_m01tu01pr7  
4- Make this report individually.
5- Left, right, top and bottom margins: 2cm.
6- Character format: a) Font: Arial, b) Size: 10, c) Questions typeface: Bold, d) Answers typeface: Regular

0-  Some links
a) http://www.refining-linux.org/archives/29/22-Processes-with-high-and-low-priority/
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). W
hen 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.- Other resources a process could need are access to: hardware, physical or logical ports, reserved memory areas, files, directories, etc...

5.- 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 or PID that must be a unique identification number. 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.

6- 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:

7.- Multiple processes of a program can be run at the same time. Different processes of one single program will have different PIDs.

8.- The process table in 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):

Operating system can show information about the process table with the help of commands such as: ps, top, pstree.

9- A process is also identified by its parent process ID or PPID:

10.- Linux processes can be in different states. Typical states are:

    a) Running or  Runnable (R):
       
* Running: All resources required by the process are availabble and the process is executing instructions on some CPU on the system.
        * Runnable:
All resources required by the process are available but, and therefore the process is ready to be run but, at the moment, CPU is not available and the process is waiting to be assigned CPU time.
        * Process state code: R

    b) Interruptible Sleep (S):
        * The process can not be run at the moment because some resources required by the process are not available. If a process goes to interruptible sleep immediately frees up its access to the CPU.
        * 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 operating system will put the process in Running or Runnable state.
        * An interruptible sleep process can be stopped (or terminated) by a software or hardware 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 it is finished 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



11.-
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.

12.- The operating system is able to assign priorities to process. The higher  a process priority is, the higher will be its CPU time. When a new process is started, the operating system assigns  to the process  a value called the  nice number or NI.  This number is the degree of friendliness or niceness of this process toward other processes. This number is related to process priority. The higher the nice number is, the lower the priority of the process is. With the  help of commands such as nice or renice, the nice value can be changed by the root user and, with some limitations, by the user starting the process.  

13.- 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.

14.- 
There are three types of processes in a Linux system:
15.- In the present practical exercise we are going to work with a set of commands  which allow us to display and manage  process running in our system.

2-Displaying running processes

2.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 two modes of executing this command: Inf
a) ps  aux  --> This command displaying basic information (euid, pid, %memory, %cpu, stat, command, ....) about every process running in the system.
b) ps  -eo  pid,ppid,uid,user,ni,%cpu,%mem,stat,start,time,args  -->  This command displaying  information about the process ID, parent process ID, effective and real user (number and name), % of CPU time and % of RAM allocated to the process, nice number, state of the process, what time the command was started, cumulative CPU time and command name (with 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  aux  command is executed:



The most important columns are:

USER:
Effective user's name
PID:
Process ID.
%CPU: % of CPU time
%MEM: % RAM memory
STAT: State (R = Running S=Sleeping T=Stopped, Z=Zombie)
COMMAND: Command (with arguments) executed.

NOTE: Any process between brackets is a process running in the kernel space

2.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.
 
3-Sending signals to processes

3.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 Terminate or End the process in an orderly way (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 Interrupt the process. A process can not ignore this signal.
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.
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.

To send a signal you need to know its PID. 

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.

3.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

4- 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.

4.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



4.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.

4.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.

PRACTICAL EXERCISE

1- Start geany. Find out  the PID  number and the process name of geany in the process table.
2- Open a new instance of geany. Find out its PID. Compare its current PID and the PID 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 the MATE Calculator. Show information about  the process ID, user identifier, username, % of CPU time and % of RAM allocated to the process and command name (with arguments).
5- Open a terminal. Become user fje. As fje user, find out the PID and the user owner of the process started in question 4. End the process in an orderly and safe way Are you able to end the process?. Why?.
6- Become another time your "by default" user. Find out the PID and the user owner 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- Find out the PID and user owner of the sshd process. Try to end the sshd.  What happens?. Why?.
8- As root user, find out the PID and user ID of the sshd process.  Try to end the ssh process using the command kill and the signal SIGHUP.  What happens?. Why?.
9- As root user, try to end the sshd process using the command kill and the signal SIGTERM. Check if the process has been ended.
10- Start three instances of geany text editor and two new instances of Atril (the default pdf reader that you can find in Applications --> Office). Check the 5 processes where started. Only show processes created from geany and atril 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.
11-  End these 5 processes using their names and one only command.
12- 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?.
13- 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?.
14- 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?.
15- As root user, close all instances of the geany program.
16- 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?.
17- 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?.
18- 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?.
19-
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?
    c) Put back geany to the Run/Runnable state. What happens now?. Can you create new files?.
20- 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.
21- Start a new instance of nano in the background. Show the state and PID of the process.
22- End the process started in  question 21 in a
n orderly and safe way.
23- Download http://www.collados.org/asix1/m01/tu1/m01tu01pr7/q23.c and:
    a) Compile q23.c running:  gcc   q23.c   -o   q23. Run q23.
    b) Open a new termimal. Run:  ps  -eo  user,pid,ppid,state,command  |  grep  q23  |  grep  -v  grep    
   
c) How many q23 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 q23 zombie process. Does it work?. Why?
    f) Why has it become a zombie process?
    g) When will the q23 zombie process be removed from the process table?
24- Download http://www.collados.org/asix1/m01/tu1/m01tu01pr7/q24.c and:
   
a) Compile q24.c running:  gcc   q24.c   -o   q24. Run q24.
    b) Run:  ps  -eo  user,pid,ppid,state,command  |  grep  q24  |  grep  -v  grep    
   
c) Can you find any instance of q24 that has become a orphan process?. How do you know it?
   
d) Why has it become a orphan process?
    e) When will the q24 orphan process be removed from the process table?
    f)
Try to send the SIGTERM or SIGKILL to the q24 orphan process. Does it work?.