A process is a program in execution, it is created by any command executed by a user, In this article we discuss types of processes and the common commands used for process management.
Table of contents.
- Types of processes.
- View processes: top.
- Killing a process: kill.
- Changing process priority: nice.
- Process status: ps.
- Background and foreground processes: jobs.
A program is a series of instructions used by the computer to perform actions, during execution, the instructions are copied to memory and space is allocated for them which will handle their execution.
A process is a program in execution or a running instance of a program. Processes can be parent child, orphan, zombie, daemon, they can be in states such as the running, sleeping, stopped or zombie states.
In this article we discuss types of processes, their states and common Linux commands used to manage these processes.
Types of processes.
Generally a process can either be a background process or a foreground process.
Other types are;
A parent process is created by the user and may have multiple child processes.
All processes have a parent process.
These are processes still running even when their parent processes have been terminated or completed.
These processes are created when a parent process gets executed before its own child process, the child process then becomes an orphan.
Orphan processes can be intentionally orphaned or unintentionally orphaned. Unintentionally orphaned processes are created when the parent processes terminates unexpectedly.
Intentionally orphaned processes run in the background without manual support.
These are processes whose execution is completed but still have an entry in the process table. It is usual for these processes to occur for child processes as the parent process needs to read it child's process exit status.
When this is done the the zombie process is reaped, that is, wait system call removes the zombie process from the process table.
View processes: top.
Linux is a multitasking and multiuser operating system meaning that multiple processes could be running at a time furthermore multiple users could also be using the system also running their processes.
The top command is used to get snapshots of real time currently running processes.
The syntax is as follows,
The up/down arrow keys are used for navigating up and down the process list.
To kill a process, highlight it and press k.
From the output we can see 12 columns,
- PID: The unique process id given to each process. e.g 7893
- User: Username of the process owner. e.g root
- PR: Priority given to a processes during scheduling. e.g 18
- NI: This is the nice value of the task,. e.g -20
- VIRT: This is the amount of virtual memory used by a process. e.g 4008
- RES: The amount of physical memory used up by a process. e.g 3668
- SHR: The amount of memory shared with other processes. e.g 7932
- S: State of the process. e.g D for uninterruptible sleep, R for running, S for sleeping, T for traced or stopped, Z zombie.
- %CPU: This represents the percentage of CPU used by a process. e.g 1.3
- %MEM: This is the RAM percentage used by a process. e.g 7.5
- TIME+: The total CPU time consumed by a process. e.g 1.06.31
- Command: Command used to activate a process. e.g konsole
Killing a process: kill.
To kill a process, we first need its PID, this is useful in occasions whereby the running program freezes or crashes.
The kill command is used for killing processes.
The above command is used to list all signals of the kill command.
Assuming a browser freezes, we can kill it as follows,
ps -a | grep 'firefox'
When we execute the above command, we get the line representing the browser process, from the line we get the PID.
sudo kill -9 PID
We kill the process by passing its PID in the above command.
We can also kill processes by name, i.e assuming we have run multiple compilations of c++ and wrongly exited compilation, these processes are not terminated, to eliminate all of the we can write.
To check, the to kill
sudo killall -9 a.out
Changing process priority: nice.
We can also opt to prioritize processes over others by changing the niceness value. Niceness values range from -20 - 19.0.
To change a process' priority, we can either start it out with a niceness value or change the same for it while it is running.
Starting out with a priority
nice -n [value] [process name]
Changing value while its running
renice [value] -p 'PID'
Process status: ps.
The process status command is used to display currently running processes although not in real time.
The syntax is as follows,
We use the -a option to show all processes.
From the output we can see four columns,
- PID: This is the process id.
- TTY: this represents the terminal type.
- TIME: This is the total time the process has been running for.
- CMD: This is the command that launches the process.
We can also get more information by using the -u option as follows,
ps -a -u
Background, foreground processes: jobs.
When running a program normally, it is executed in the foreground however, some processes take time and we need to execute them in the background so that they don't interfere with what we are currently doing also so that we don't interrupt them as the work. We can do this by running them in the background.
We use an & to run processes in the background as follows,
Lets say we want to run 10 pings but we don't want the system to wait until they are complete so we can continue working or interrupt them while the ping command is executing.
ping -c 10 184.108.40.206 > out.txt &
The above process runs in the background and out.txt gets updated with the results. To view background processes we use the bg command.
We can also use the jobs command to view background processes running. Notice how they are numbered, We can use these numbers together with the fg command to bring processes back to the foreground as follows.
Meaning bring process 1 to the foreground.
A process is a program in execution. It could run in the foreground or background.
We can change process priorities using the niceness value and the lower the value the higher the priority.
We have discussed bg, fg, ps, nice, top, jobs, nice, renice, kill commands used for process management in this article at OpenGenus.
Other useful commands during process management are df command which returns free hdd space in the system, free command which returns the free memory on the system.
- man command for the commands' manuals.
- Linux process management