# Processes in Linux

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

| Command | Description |
| --- | --- |
| ps | list the current processes of the shell when this command runs |
| ps aux | list 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 |
| top | list all processes interactively |

![Capture 3](https://user-images.githubusercontent.com/120579608/227507703-4f7e9dd7-4f5b-468a-89dc-35a769707a94.PNG align="left")

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.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1679773020511/15c37173-d37f-472a-8ab2-1cabf348a98c.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1679807788202/f13b9162-9d9e-425a-9e7d-b37315a96010.jpeg align="center")

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.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1679773748273/90da8f3c-bda0-4ecc-b189-c3bf17f769d6.png align="center")

## 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.

```clojure
#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**.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1679776037832/5ccfd836-aed3-4411-8e04-06a06a3be18a.png align="center")

### 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.

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

### 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.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1679776502116/46250e48-e631-4ed1-bc11-784bdd62cd60.png align="center")

| **Command** | **Description** |
| --- | --- |
| nice -n priorityValue newProcess | for new process( ex: touch file.txt) |
| renice priorityValue -p processId | for already exit process |

## States of processes

| State of processes | Description |
| --- | --- |
| R | The process is running |
| D | Interruptable sleep |
| S | Uninterruptable sleep |
| Z | Zombie |
| T | Stopped |

## References

[https://www.youtube.com/watch?v=cnbvsWKf-qM&list=PL2kSRH\_DmWVZp\_cu6MMPWkgYh7GZVFS6i&index=5](https://www.youtube.com/watch?v=cnbvsWKf-qM&list=PL2kSRH_DmWVZp_cu6MMPWkgYh7GZVFS6i&index=5)
