Chapter 7. Welcome to the Processes

A program under execution is known as process. When an operating system gets booted up, multiple processes get started in order to provide various functionalities and user interfaces so that a user can easily perform the required tasks. For example, when we start a command line server, we will see a terminal with bash or any other shell process that has been started.

In Linux, we have full control over processes. It allows us to create, stop, and kill processes. In this chapter, we will see how a process is created and managed by using commands such as top, ps, and kill and by changing its scheduling priority. We will also see how a signal can lead to the sudden termination of a process and also the ways to handle signals in a script using the command trap. We will also see one of the beautiful features of processes called Inter-process communication, which allows them to communicate with each other.

This chapter will cover the following topics in detail:

  • Process management
  • Listing and monitoring processes
  • Process substitution
  • Process scheduling priorities
  • Signals
  • Traps
  • Inter-process Communication

Process management

Managing processes is very important because processes are what consumes system resources. System users should be careful about the processes they are creating, in order to ensure that a process is not affecting any other critical processes.

Process creation and execution

In bash, creating a process is very easy. When a program is executed, a new process is created. In a Linux or Unix-based system, when a new process is created, a unique ID is assigned to it, which is known as PID. A PID value is always a positive number starting from 1. Depending upon a system having init or systemd, they always get the PID value 1 because this will be the first process in a system and it is the ancestor of all other processes.

The maximum value of PID is defined in the pid_max file, which should be available in the /proc/sys/kernel/ directory. By default, the pid_max file contains the value 32768 (max PID + 1), which means a maximum of 32767 processes can exist in a system simultaneously. We can change the value of the pid_max file depending upon needs.

For understanding the process creation better, we will create a new process vi from bash:

$ vi hello.txt

Here, we have created a new process vi that opens the hello.txt file in editor to read and write text. Calling the vi command causes the binary file /usr/bin/vi to execute and perform the needed tasks. A process that creates another process is known as the parent of the process. In this example, vi was created from bash, so bash is the parent of the process vi. The method of creating a child process is known as forking. During the process of fork, a child process inherits the properties of its parents such as GID, real and effective UID and GID, environment variables, shared memory, and resource limit.

To know the PID of the vi process created in the preceding section, we can use the commands such as pidof and ps. For example, run the following command in a new terminal to know the pid of the vi process:

$ pidof vi  # Process ID of vi process
21552
$ ps -o ppid= -p 21552	# Knowing parent PID of vi process
1785

Once a task is completed, a process gets terminated and PID is free to get assigned to a new process based on need.

The detailed information about each process is available in the /proc/ directory. A directory with the PID name gets created for each process in /proc/ containing its detailed information.

A process can be in any of the following states during its lifetime:

  • Running: In this state, a process is either running or ready to run
  • Waiting: A process is waiting for a resource
  • Stopped: A process has been stopped; for example, after receiving a signal
  • Zombie: A process has exited successfully, but its state change wasn't yet acknowledged by the parent

Process termination

In normal circumstances, after completing tasks, a process terminates and frees up the allocated resources. If the shell has forked any subprocesses, then it will wait for them to finish their task first (other than a background process). In some cases, a process may not behave normally and it can be waiting or consuming resources for a longer time than expected. In some other cases, it may happen that a process is now no longer required. In such cases, we can kill the process from a terminal and free up resources.

To terminate a process, we can use the kill command. The killall and pkill commands can also be used if available on a system.

Using the kill command

The kill command sends the specified signal to the specified processes. If no signal is provided, the default SIGTERM signal is sent. We will see more about signals further down in this chapter.

The following is the syntax of using the kill command:

kill PID

AND

kill -signal PID

To kill a process, first get the PID of that process as follows:

$ pidof firefox    # Getting PID of firefox process if running
1663
$ kill 1663    # Firefox will be terminated
$ vi hello.txt  # Starting a vi process
$ pidof vi
22715
$ kill -SIGSTOP 22715  # Sending signal to stop vi process
[1]+  Stopped                 vi

Here, we used the SIGSTOP signal to stop the process instead of killing it. To kill, we can use the SIGKILL signal or the associated value to this signal, which is 9.

$ kill -9 22715  # Killing vi process

OR

$ kill -SIGKILL 22715  # Killing vi process

Using the killall command

It's easy to remember a process by name rather than by PID. The killall command makes it easier to kill a process since it takes the command name as a parameter to kill a process.

The following is the syntax of the killall command:

killall process_name

AND

killall -signal process_name

For example, we can kill the firefox process by name, as follows:

$ killall firefox  # Firefox application gets terminated

Using the pkill command

The pkill command can also be used to kill a process by its name. Unlike the killall command, by default the pkill command finds all the processes beginning with the name specified in its argument.

For example, the following command demonstrates how pkill kills the firefox process from its partial name specified in an argument:

$ pkill firef    # Kills processes beginning with name firef and hence firefox

The pkill command should be used carefully because it will kill all the matching processes, which may not be our intention. We can determine which processes are going to be killed by pkill, using the pgrep command with the -l option. The pgrep command finds processes based on its name and attributes. Run the following commands to list all process names and its PID whose name begin with the firef and fire strings, respectively:

$ pgrep firef
    8168 firefox

Here, firefox is the matching process name and its PID is 8168:

$ pgrep fire
    747 firewalld
    8168 firefox

We can also tell pkill to kill a process with exact match of process name using the --exact or -x option as follows:

$ pgrep -x -l  firef  # No match found
$ pkill -x fire  # Nothing gets killed
$ pgrep --exact -l firefox	  # Process firefox found
8168 firefox
$ pkill --exact firefox  # Process firefox will be killed

The pkill command can also send a specific signal to all matching processes with the -signal_name option as follows:

$  pkill -SIGKILL firef

The preceding command sends the SIGKILL signal to all processes whose name begins with firef.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
13.59.160.58