Skip to content
Snippets Groups Projects
6_unix_processes.md 4.9 KiB
Newer Older
Laurent Modolo's avatar
Laurent Modolo committed
---
title: Unix Processes


---

# Unix Processes

[![cc_by_sa](/Users/laurent/Documents/formations/2020_08_UNIX/img/cc_by_sa.png)](http://creativecommons.org/licenses/by-sa/4.0/)

Objective: Understand how process works in GNU/Linux

A Program is a list of intructions to be executed on a computer. These instruction are written in one or many files in a format readable by the computer (binary files), or interpertable by the computer (text file). The interpretable format need to be processed by an interperter who is in binary format.

The execution of a program on the system is represented as one or many processes. The program is the file of instruction while the process is the instructions being read.

`mkdir` is the program, when you type `mkdir my_folder`, you launch an `mkdir` process.

Your shell is a process to manipulate other processes.

> In multitasking operating systems, processes (running programs) need a way to create new processes, e.g. to run other programs. [Fork](https://en.wikipedia.org/wiki/Fork_(system_call)) and its  variants are typically the only way of doing so in Unix-like systems.  For a process to start the execution of a different program, it first  forks to create a copy of itself. Then, the copy, called the "[child process](https://en.wikipedia.org/wiki/Child_process)", calls the [exec](https://en.wikipedia.org/wiki/Exec_(system_call)) system call to overlay itself with the other program: it ceases execution of its former program in favor of the other.

## Process attributes

- PID : the **p**rocess **id**entifier is an integer, at a given time each PID is unique to a process
- PPID : the **p**arent **p**rocess **id**entifier is the PID of the process that has stared the current process
- UID : the **u**ser **id**entifier, is the identifier of the user that has started the process
- PGID : the **p**rocess **g**roup **id**entifier (like users process have groups)

You can use the command `ps` to see thes process launched by your user

```sh
ps
```

Like for the command `ls` you can use the switch `-l` to have more details

Open another tab in your browser to log again on your VM.

In this new tab, you are going to launch a `less` process

```sh
less .bashrc
```

Comme back, to your first tab, can you see your `less` process with the command `ps` ?

The `ps` option `-u [login]` list all the process with `UID`the `UID`associated with `[login]`

```sh
ps -l -u etudiant
```

Is the number of `bash` processes consistent with the number of tabs you openned ?

What is the PPID of the `less` process ? Can you identify in which `bash` process `less` is running ?

Did you launch the `systemd` and `(sd-pam)` process ?

`pam` stand for [**p**luggable **a**uthentication **m**odules](https://www.linux.com/news/understanding-pam/), its a collection of tools that handle identification and resource access restriction. From the PPID of the `(sd-pam)` can you find which process launched `(sd-pam)` ? What is the PPID of this process ?

The option `-C` allow you to filter process by name

```sh
ps -l -C systemd
```

Who launched the first `systemd` process ?

From PPID to PPID, you can guess that like the file system, processes are organized in a tree. The command `pstree` can give you a nice representation of this tree.

The following `ps` command show information on the process with PID 1

```sh
ps -l -1
```

Can you look at the corresponding program (with the command `which`) ?

Can you look for informations on PID 0 ?

The root of the processes tree is the PID 1.

While `ps` give you a static snapshot of the processes but processes are dynamic. To see them running you can use the command `top`. While `top` is functional, most system have `htop` with a more accessible interface. You can test `top` and `htop`.

Like `ps` you can use `-u etduiant` with htop to only display your user process

With the `F6` key you can change the column on which to sort your process.

- Which process is consuming the most of CPU ?
- Which process is consuming the most of memory ?

What is the difference between `M_SIZE` (`VIRT` column), `M_RESIDENT` (`RES` column) and `M_SHARE` (`SHR` column) ? To which value, the column `MEM%` correspond to ?

-  `M_SIZE` : The  total  amount  of  virtual  memory  used  by the task.  It  includes all code, data and shared libraries  plus  pages  that  have  been  swapped out and pages that have been mapped but not  used (If an application requests 1 GB of memory but uses only 1 MB, then `VIRT` will report 1 GB).
-  `M_RESIDENT` :  what's currently in the physical memory. This does not include the swapped out memory and some of the memory may be shared with other processes (If a process uses 1 GB of memory and it calls `fork()`, the result of forking will be two processes whose `RES` is both 1 GB but only 1 GB will actually be used since Linux uses copy-on-write).
- `M_SHARE` : The amount of shared memory used by a task. It simply reflects memory that could be potentially shared with other processes.