The Linux Process Execution
The programs that run on the Linux system are referred to as processes. The kernel is in charge of them. Every process has its own unique process identifier (PID). The sequence in which processes are created determines the PID. To find the ongoing processes in your system you need to run the command below.
The result shows the snapshots of two processes running in the system. Additional pieces of information about the processes are also displayed. Let’s dive into the details.
- PID: Process ID
- TTY: It stands for ‘teletypewriter’. It controls the terminal associated with the process.
- TIME: CPU usage time
- CMD: Name of the command.
Another useful command is ‘top’ which shows real-time information about the currently running processes on the Linux system. It keeps updating the results every 10 seconds.
Types of Processes
There are mainly two kinds of processes in linux system.
- Foreground processes: Foreground processes are also called as interactive processes as they are associated with terminals and take input from the keyboard. A user has to be connected to initialize these kinds of processes. Run the following command.
When the ‘pwd’ command is running in the foreground, it takes time and no other processes may run in the meantime. Because the ‘pwd’ process must be finished before the prompt can be used.
- Background Processes: They are also known as non-interactive or automatic processes as they are not associated with terminals. They run in the background without requiring keyboard input and wait until it is needed. Hence, other processes can be initialized in parallel.
$ pwd &
The command line runs in the background without taking any user input. It reaches the stop state when any user data input is given and moved to the foreground.
A new process is created when an existing process makes an exact copy of itself in the memory using the fork system call. Every process must have a parent process in this way. The environment is the same for both the child and parent processes, but their process id (PID) is different. To see the details information about the processes, let’s run the command below.
$ ps -l
Here, PPID is the parent id for a process.
It is the system’s parent process, in charge of overseeing all other processes.
It runs when the Linux system starts up and does not have a parent process because it is launched by the kernel itself. The PID of the init process is always 1. To find the PID of a process, you may run the following command.
$ pidof systemd
From the result, 1580 is the PID number of the bash process. You can also find the PID and parent id (PPID) of a process.
$ echo $$ $ echo $PPID
Depending on the environment, a process might be in many states. The following sections go through these states.
- Running (R): the process is currently running on the system.
- Waiting (S): the process is in interruptable sleep waiting for an event to finish.
- Uninterruptible sleep (D): this process can not be interrupted by the user.
- Stopped (T): the process is stopped or killed.
- Zombie (Z): Although the process is no longer active, their statuses are still being gathered.
Let’s run the following command again.
$ ps -l
Now you can see that all the process has a stage associated with them in the beginning. We can see that it shows that the ‘ps’ command is running.
All the process information is stored in the /proc file. Run the command below.
$ ls /proc/
You can see lots of PID values in the location, These are subdirectories that contain information about the process. Let’s dive into one of these directories.
$ cat /proc/129/status
Results show the tons of data about the PID 129’s states.
To control processes in the Linux, different types of signals can be sent to the process. To view the signals, let’s run:
$ kill -l
You can use the kill, pkill, or pgrep commands to deliver a signal to a process. This is how the process will react if it identifies the signals. Each signal is assigned an integer value.
Stopping a process
Stopping or terminating a process is easy. You need to use the kill command. Now, let’s explore these.
$ ps -l
If you want to stop the process that has a PID of 6150, you need to run the command below.
$ kill 6150
By default, it sends the 15 or SIGTERM signal to the process which terminates the process. You can also do this in this way.
$ kill -9 6150
It will send the SIGKILL signals which will kill the process.