EH1: Linux Lab
ASIX/DAW/DAM-1
Task A2: Managing processes 19-2-24

Task A2:  Managing processes

GENERAL CONDITIONS
1
- Report deadline: 3-3-24
2- Send your report 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: asix1_surname_name_eh1act02.pdf and asix1_surname_name_eh1act02.odt
        b2.) DAW1: daw1_surname_name_eh1act02.pdf and daw1_surname_name_eh1act02.odt     
     c) Subject:
        
c.1) ASIX1 (Catalan): asix1_surname_name_eh1act02
         c.2)
DAW1 (English): daw1_surname_name_eh1act02  
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



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). 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.- 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):

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

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

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

8.-
A process is also identified by its parent process ID or PPID: 9.- 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.  

10.- The states of a Linux process are the following:

    a) Running or  Runnable (R):

    b) Interruptible Sleep (S):

    c) Uninterruptible Sleep (D):

   d) Stopped (T):

   e) Zombie (Z):

    f) Idle Kernel threads (I):

    g) Linux states diagram



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

2-  Some more advanced ideas


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

2.- Orphan processes:

3.- On Linux there are two separeted memory areas called user space memory area and kernel space memory area:
4.-  There are three types of processes in a Linux system:
5.- Processes need memory. The total amount of memory space 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.

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 :

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.



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 http://www.collados.org/asix1/eh1/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 http://www.collados.org/asix1/eh1/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?