Skip to main content

Command Palette

Search for a command to run...

Processes in Linux

Updated
•4 min read•View as Markdown
Processes in Linux
M

Computer science graduate.

Processes are just programs that are running on Linux machines. These processes are generally managed by the kernel and each process has a PID(process id). Processes are terminated when the terminal associated with it is closed.

General commands of processes

CommandDescription
pslist the current processes of the shell when this command runs
ps auxlist all processes at the time when this command runs a- all processes u- more details about processes x- processes which didn't require tty terminal
toplist all processes interactively

Capture 3

These commands give information about virtual memory allocated (memory allocated by the kernel), process id (PID), NI (priority value), user, memory used and others. As shown in the above picture.

How process starts

A new process in the Linux system starts with a mechanism known as Fork System Call. In this, the new process(child) clones the present process(parent process) by requesting the kernel. Kernel decides the resources for the process.

As we observe that when we run ps -l command, the parent id (PPID) of the ps process is 44 and 44 is the process id of bash (current shell). So we can conclude that every process is a clone of its parent process.

Mother Process

The mother Process is the first process initiated by the kernel when the system boots up which has a PID of 1. The mother process is also known as init and this process runs on root privileges.

Demon Process

Demon processes are the child processes of the mother. These are essential for the system running.

How processes terminates

When the process completes, it calls its kernel by the exit system call with the argument exit code. The exit status code is used to indicate the reason for the process's termination, and it can be retrieved by the parent process using a wait system call. And the parent waits until free up the memory is used. For successful termination of the process, the termination status is 0.

Wait for system call provides a mechanism for managing the termination and collecting information about their execution.

#include <sys/wait.h>
pid_t wait(int *status); // Basic syntax of wait system call.

This is the basic syntax of the wait system call. As we can observe it is taking an exit status argument.

Orphan Process

If the parent process dies, then the child process of it is adopted to the mother(init) by the kernel for termination of the process. So, that mother can able to acknowledge the termination process by the wait system call. In this case, the child process is known as the orphan process.

Zombie Process

When the child process termination is not acknowledged by the parent, then the child process is treated as a zombie process by the kernel. Further, if the parent process acknowledges the zombie process termination then this is known as reaping. If reaping didn't occur then the wait system call is done by the mother to terminate the zombie process.

Signals

It is a notification to the process that something has happened.

SignalDescription
SIGHUP/HUP/1HangUp
SIGINT/INT/2Interupt
SIGKILL/KILL/9Kill
SIGSEGV/SEGV/11Segmentation fault
SIGTER/TERM/15Terminate
SIGSTOP/STOP/19Stop

Signal Mask

A signal mask is used to block signals but there are some signals like kill can't be blocked.

Nice & Renice

Processes aren't continuously run by the system. They are known in timeslots known as time slices in CPU and as cyclic as shown in the below example. So, the process will take the almost same time. But, we can prioritize the process by nice & renice commands. And every process has a nice which indicates priority value. If the nice value is less, the system will prioritize more or vice versa.

CommandDescription
nice -n priorityValue newProcessfor new process( ex: touch file.txt)
renice priorityValue -p processIdfor already exit process

States of processes

State of processesDescription
RThe process is running
DInterruptable sleep
SUninterruptable sleep
ZZombie
TStopped

References

https://www.youtube.com/watch?v=cnbvsWKf-qM&list=PL2kSRH_DmWVZp_cu6MMPWkgYh7GZVFS6i&index=5

More from this blog

Hindu's Blogs

7 posts