How processes are managed in Linux

ShahanMirza
6 min readOct 21, 2020

--

https://opensource.com/article/18/9/linux-commands-process-management

A running program is called a process. If we talk about the terminal, the processes can either be running interactively in the foreground or they can be sent in the background to execute. A process which is sent to the background is referred to as a job. We can send processes in the background during runtime, we can bring background processes to the front and we can send various types of signals to the running processes to change their execution states.

The complete list of signals which can be sent to a process are given in the table below:
The complete list of signals which can be sent to a process are given in the table below:

Signal Name

Number

Description

SIGHUP

1

Hangup (POSIX)

SIGINT

2

Terminal interrupt (ANSI)

SIGQUIT

3

Terminal quit (POSIX)

SIGILL

4

Illegal instruction (ANSI)

SIGTRAP

5

Trace trap (POSIX)

SIGIOT

6

IOT Trap (4.2 BSD)

SIGBUS

7

BUS error (4.2 BSD)

SIGFPE

8

Floating point exception (ANSI)

SIGKILL

9

Kill(can’t be caught or ignored) (POSIX)

SIGUSR1

10

User defined signal 1 (POSIX)

SIGSEGV

11

Invalid memory segment access (ANSI)

SIGUSR2

12

User defined signal 2 (POSIX)

SIGPIPE

13

Write on a pipe with no reader, Broken pipe (POSIX)

SIGALRM

14

Alarm clock (POSIX)

SIGTERM

15

Termination (ANSI)

SIGSTKFLT

16

Stack fault

SIGCHLD

17

Child process has stopped or exited, changed (POSIX)

SIGCONT

18

Continue executing, if stopped (POSIX)

SIGSTOP

19

Stop executing(can’t be caught or ignored) (POSIX)

SIGTSTP

20

Terminal stop signal (POSIX)

SIGTTIN

21

Background process trying to read, from TTY (POSIX)

SIGTTOU

22

Background process trying to write, to TTY (POSIX)

SIGURG

23

Urgent condition on socket (4.2 BSD)

SIGXCPU

24

CPU limit exceeded (4.2 BSD)

SIGXFSZ

25

File size limit exceeded (4.2 BSD)

SIGVTALRM

26

Virtual alarm clock (4.2 BSD)

SIGPROF

27

Profiling alarm clock (4.2 BSD)

SIGWINCH

28

Window size change (4.3 BSD, Sun)

SIGIO

29

I/O now possible (4.2 BSD)

SIGPWR

30

Power failure restart (System V)

Foreground and Background Process Manipulation

Execute a process with the & sign appended to the command-line at the end will send the process to the background.

sleep 1000 &

sleep 2000 &

If we have a running process in the foreground, follow these steps to send it to the background.

sleep 3000

Press ctrl+z

The process is now suspended. Type:

jobs

This will list the active jobs in the terminal. Find the number of the suspended job (assumed to be 3 for our example) and type:

bg %3

To bring the same job to the foreground, type:

fg %2

Just entering fg will bring the most recent job to the foreground.

Disassociating Processes from the Terminal

A problem arises when we close the terminal. All background jobs are closed as closing sends all tasks a SIGHUP signal. We can check the running processes by issuing the following command, first before closing the terminal and afterwards when we open a new terminal.

ps -aux | grep sleep

As can be seen, all jobs are now closed due to the terminal closure. So what can we do to protect the processes from automatically closing?

Protecting tasks from SIGHUP

We can execute a process using the nohup program. This will allow the program to ignore SIGHUP when the terminal is closed.

nohup sleep 1000 &

If we do not use nohup and we want to achieve the same after running a process, we can use the disown command on a job number.

disown %3

Nohup has a problem as it redirects the output to nohup.out. This means that several processes started from the same directory will have a common nohup file making matters complex. The other problem is that a job created using nohup or disown’ed after execution cannot be brought to the foreground.

nohup sleep 9999 &

Close terminal and open

ps aux | grep sleep

sleep 8888 &

disown %1

When we disconnect, we cannot ‘fg’ it. This is a problem if we want to interact with the process. To overcome this we will now use a nifty utility called screen.

Screen

Install the screen program, if it is not already installed by typing:

apt-get install screen

Or

yum install screen

Open two terminals and in one of the terminals type:

screen

Then run a sleep program in this terminal on the foreground by typing:

sleep 99999

Press

Ctrl + a and then press the ‘d’ key to disconnect the screen session.

ps aux | grep sleep

will show you that sleep is still running.

In the other terminal, type

screen –dr

Will give us back the session on this other terminal. Close all terminals. Go to any other terminal and type:

screen –dr

This is just a tip of the iceberg of what screen can do. Please refer to screen documents to see the awesome power which screen provides to the system administrator.

Process IDs and Signals and the kill Command

When a process is created, it is assigned a PID which is just an identifier for each process running on the system. We can send signals to the process using this PID. Common signals were listed in the table in the start of this section.

The format of sending a signal is to issue the kill command with the necessary signal.

kill –SIGNAL_NUMBER PID

The default behavior of the kill command is to send the SIGTERM signal which lets the process to gracefully exit. This is the proper way to kill the process. If the process doesn’t honor the SIGTERM signal due to a locking problem, send SIGKILL which will almost always end a process. When we press Ctrl+C, a SIGINT is sent to the process. SIGHUP is more like a courtesy to inform the process that its parent is closing, so it should close itself.

The following commands can be used to test various kill commands.

Sleep 1000

Ctrl + C

sleep 1000 &

kill PID

Default kill signal is SIGTERM

sleep 1000 &

kill -9 PID

ps –aux | grep sleep

Advanced process management

We will now see some additional commands for process management namely killall, pgrep and pkill. The killall command will kill all processes with the same name. It requires that an exact match is provided for the command to work.

sleep 1000 &

sleep 1000 &

ps –eaf | grep sleep

killall sleep

killall -9 sleep

The killall command will not be able to kill other users’ processes unless we are root.

The pkill and pgrep provide a safety net against killing arbitrary processes. They don’t require the whole process name, rather a part of the process name will suffice for them to function. Additionally pgrep will catch child process with the same name. Other commands only work on the parent processes and don’t list any children with the same name.

pgrep sle

pgrep –a sle

Make a bash script sleepytime

vi sleepytime

#!/bin/bash

sleep 12345

Now the sleep process is not listed in the pkill command, so we will use pgrep.

ps –eaf | grep sleep

pgrep –a slee

pgrep –af slee

Process Priorities and Niceness

Each process on a Linux is assigned a priority. The priority can be thought of as the portion of CPU time in an interval which a process is entitled to. The higher the priority, the larger the time slice and vice versa. In Linux the priority is called the niceness level of a process. The nice levels are from -20 to 19, where -20 is the highest priority and 19 is the lowest. Each process is created with a default priority of 0. The command to manipulate nice levels are:

nice

renice

Starting Levels

To start a new process with a custom priority level use:

nice -10 application

nice — 15 application

nice -19 application

nice — 20 application

Please note that if a lower priority is to be assigned, then a single dash (-) followed by a number is given as the argument. If a priority less than 0 is to be assigned then a double dash is typed ( — ). Only the root user can increase priority of a process.

sleep 1111 &

nice -15 sleep 22222 &

nice — 10 sleep 1111 &

ps –alf

If we are not allowed to set the nice level, the command is still executed with the default niceness level. Now kill all sleep processes using:

pkill –f slee

Changing Nice Levels

Once a process is started, its niceness level can be changed using the renice command. Remember that only root user can increase the priority level of a process. If priority is lowered, we cannot mark it higher to even 0, which is the default.

sleep 1111 &

renice 19 -p PID

renice -10 -p PID; #Won’t work unless root

renice 15 -p PID; #Won’t work unless root and the priority was changed to 19 first

--

--

No responses yet