Process Management In Linux/Unix


A process is an instance of a computer program that is currently running. A program can have multiple instances created when it starts executing which means more than one process can be associated with that program. Multitasking is the method by which these processes share the system resources. Whenever we execute a command on terminal a process gets started. A process gets start in two ways-


1. Foreground process-

By default, a process started by a user runs in the foreground. It takes input from the command prompt and displays output to the computer screen. If a foreground process is running terminal prevents to initiate a new process until the existing one does not get finished.

2. Background process-

These are the processes that do not require keyboard input. You can start another process from the terminal while the previous one is running in the background. To start a background process you should add an ampersand(&) at the end of a command.


Switching between foreground and background process-

If a foreground process is taking too much time you can stop it by using ctrl+z. It will be suspended but the job still exists in the system. You can check the suspended process using the jobs command. For example, we will run a program in the terminal called tree which will show the recursive listing of directories. If it is not available in your system you can download it by the $sudo apt-get install tree command-

Once you type tree and press enter it will start listing directories recursively under root directory. To suspend its execution press ctrl+z

Now type jobscommand to see the suspended jobs in your system.

The process can be resumed in the background by using bg command it will send the job for background execution. To start job execution once again in the foreground usefg command. As you can see below the tree which was suspended resumed its execution once again in foreground


Display the running processes

You can see the running process by given methods

1. By using TOP – You can use top utility to see the running processes in the system with some other information regarding resource utilization and the priority of a process. Every process is uniquely identified by PID i.e Process ID. To exit from top press q.

Attributes description is given below-

PID – Process ID
USER – The user name or the owner of the process
PR – Priority it can have max value 20 and min -20
NI – The nice value of a process
VIRT – Virtual memory that is used in kb
RES – Physical memory that is used in kb
SHR – Shared memory used in kb
S- Status
It can have the following values

  • S – Sleeping
  • R – Running
  • T – Traced or Stopped
  • Z – Zombie process
  • D – Uninterruptable sleep

%CPU – CPU time in %
%MEM – RAM used in %
TIME+ – Total CPU time
COMMAND – Name of the process, or command used

2. By using PS – ps is another command used to display information about a selection and active process. It stands for process status. Use it as given below is to display this information about the active process. As you can see the attributes here are a little bit different from the process listed by top command.  You can check the manual page of ps in your terminal by typing $man ps to know more option available for use with ps

Attribute description is-

USER – The user name or the owner of the process
PID – Process ID
%CPU – CPU time in %
%MEM – RAM used in %
VSZ – Virtual memory size
RSS – Physical memory that process holds currently
TTY – Terminal hold status
STAT – Process Status code(use ps manual page in terminal)
START – Process start time
COMMAND – Command or process name

You can see the process above the TTY column is filled with ? sign which means the terminal is not available for these processes so these are background processes of the system.

To display the status of a single process, use the following command as following

$ ps PID


Terminate a process

To terminate a process you can use kill command, the syntax of it is given below
$ kill PID

and to find the PID of a process use the given command
$ pidof Name_of_process

Look at the example below to how to get and kill a process-

Also, you can list the processes by using the top command and select the PID of the process which you want to kill.


Change the Execution priority

A lot of processes can run simultaneously in an operating system. These processes are considered equally important, so the operating system uses algorithms like first come first serve and round-robin to allot the system resources to a process.
Linux OS associates a priority parameter with each job which is known as nice(NI). It can be changed by the user. If you think a process is more important than the other one you can change the priority for its execution.
The nice index has a value between -20 to 19. The lower the value higher would be the priority. To change the priority of a new process using the command below

$ nice -n nice_value process_name

In the example below, a  process named vlc is started with a nice value of 5

 

If the process is already running with a nice value you can renice it by using the following command.

$ renice nice_value -p PID

If you are not a root user you should use sudo preceded command. You can see the example below chrome has NI value 0(By default all process have 0 NI value) and process ID 1726 we will renice into -10. Use the command as given below-

 


The init process

The init process is the very first process that gets created when a Linux machine starts. Since every process in Linux has a parent process. so every other process of a system are children of the init process. It has some special privileges so it cannot be killed. It only gets terminated when the system is shutdown. It has the process id of 1 which never changes.


Zombie and orphan process

Suppose there are two processes one is the child and the other is the parent process. Two possible scenarios are
1.If the parent terminated or gets killed before the child process. The child process becomes an orphan process now this will be rescued and adopted by the init process. And the init process will become its parent process.
2.If the child terminated or gets killed and the parent does not get the termination status. If a child process gets terminated but the parent process is not able to fetch the termination status immediately. The terminated child process will become a zombie process.


Daemon process

Dameon is the process that requires a long period of time. And does not require a controlling terminal. For example, a printer daemon waiting for print or monitoring software used in a system, etc. I hope this will give you a good understanding of processes and their management in Linux/Unix.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.