commit 3234b52588297d310ef77c05e6e6f952ab1b74db Author: Pierre Valarcher Date: Wed Sep 15 10:13:18 2021 +0200 First commit since long time diff --git a/ASR31-Cours2.md b/ASR31-Cours2.md new file mode 100644 index 0000000..ed41c39 --- /dev/null +++ b/ASR31-Cours2.md @@ -0,0 +1,658 @@ + + +## Rappel sur les processus + +Un processus reflète a un instant donné l’état d’avancement de l’exécution d’un programme binaire pur : un processus est ainsi une entité dynamique. Comme nous le verrons, un processus peut au cours du temps donner l’image de l’exécution de programmes différents du fait de l’utilisation de mécanismes de recouvrement. + +Unix fournit un mécanisme uniforme de création de processus par duplication d’un processus (seul le processus originel n’est pas créé de cette manière). Un processus a ainsi un processus parent ou père et l’ensemble des processus constitue une arborescence. + +Un processus est donc constitué, outre du programme qu’il est en train d’exécuter (c’est-à-dire la suite des instructions), de l’ensemble des données que ce programme manipule et d’un ensemble d’informations dont le système a besoin pour prendre en compte le processus et qui en constitue le *contexte* d’exécution. + +On y trouve l’ensemble de valeurs des registres du processeur correspondant à l’état d’avancement de l’exécution, un état du processus, les informations liant le processus avec l’extérieur (autres processus, utilisateurs, fichiers), etc. Cet ensemble d’informations constitue le bloc de contrôle du processus. + +Un ordonnanceur (scheduler) est chargé d'allouer le ou les processeurs aux différents processus. Une première approche de l'ordonnancement peut être décrite par : +- calcul d'une priorité à chaque processus (à chaque période) +- en cas d'égalité : stratégie du tourniquet [Wikipedia](https://fr.wikipedia.org/wiki/Round-robin_(informatique)) + +### Les processus système +Ils n'ont pas de terminal de rattachement et sont propriétaires de l'utilisateur *root* : processus démons (*daemon*). Ils ont en général comme répertoire de travail la racine du SGF. +Rendent des services généraux aux utilisateurs et aux machines sur le réseau. +Un processus fondamental est **systemd** et **init** (sur d'ancien système). C'est le premier processus créé par le système (pas de père, numéroté 1); c'est à partir de lui que les autres processus sont créés. Il se termine quand le système s'arrête. + +### Les processus utilisateur +Normalement une durée de vie limitée mais non bornée : par exemple les processus de login (*shell*), un éditeur (*vi*), .... En général, les processus sont attachés à un terminal (mais pas toujours et on peut l'en détacher). + +### Exécution d'un processus +Pour qu'un processus soit exécuté, il faut que tout ou partie du programme binaire soit en mémoire centrale. La plupart du temps les programmes ne sont qu'une seule fois en mémoire (on dit que le code est **ré-entrants**). Chaque exécution de programme dispose de son propre jeu de données (espace d'adressage du processus). +Il y a deux modes d'exécution des processus : +- le mode utilisateur (*user mode*) : le processus n'accède qu'à son espace d'adressage et son code +- le mode système (*kernel mode*) : le processus exécute des instructions qui appartiennent au noyau du système. Il a aussi accès à des données du système (externe à son espace d'adressage) : appel système, interruption extérieure nécessitant une exécution d'une séquence particulière (*handler* d'interruption). + +### Caractéristiques d'un processus +- identification +- propriétaire +- groupe propriétaires +- terminal d'attachement +- priorité, répertoire de travail, ... + +### Rappel de la commande *ps* +La commande de base pour observer les processus est *ps*. + +```bash +ps -l +``` +Dont voici le résultat (sur les machines de l'IUT, après une connexion à distance via *ssh*. + +``` +F S UID PID PPID C PRI NI ADDR SZ WCHAN TTY TIME CMD +0 S 989 86846 86844 0 80 0 - 3636 wait pts/0 00:00:00 bash +0 R 989 86852 86846 0 80 0 - 8293 - pts/0 00:00:00 ps +``` + +#### Commentaires +- **UID** correspond à l'identifiant du propriétaire +- **PID** et **PPID** correspondent à l'identité du processus et celui du père +- l'état du processus (**F/S**) : *R* pour run et *S* pour sleep +- **C/PRI/NI** correspondent à des informations sur la priorité du processus +- L'adresse du processus (**ADDR**) et sa taille (**SZ**) +- le terminal de rattachement **TTY** +- La raison de la mise en sommeil **WCHAN** + +#### Remarques +Il faut faire attention car les systèmes ne donnent pas tous les mêmes résultats : la même commande sous Darwin (macOS X) + +```bash +UID PID PPID F CPU PRI NI SZ RSS WCHAN S ADDR TTY TIME CMD +501 66918 66917 4006 0 31 0 4296528 8 - S 0 ttys000 0:00.01 -bash +501 66930 66918 4006 0 31 0 4296868 772 - S 0 ttys000 0:00.37 bash +501 75794 75793 4006 0 31 0 4296528 260 - S 0 ttys001 0:00.02 -bash +501 75806 75794 4006 0 31 0 4288676 460 - S+ 0 ttys001 0:00.01 bash +``` + +### Terminaison des processus +#### Etat Zombi +C'est l'état d'un processus qui ne consomme plus aucune ressource mais est toujours dans la table des processus. Cela peut-être utile pour un père de savoir l'état d'un de ces fils (qui a fini de travailler et pourrait donc être éliminé). +#### Code de retour d'un processus +C'est l'*exit status* auquel le père peut accéder de manière asynchrone. En général, une valeur de *0* correspond à une terminaison *normale*. +#### Interruption de processus +Quand un processus est attaché à un terminal, on peut depuis le terminal l'interrompre par des caractères particuliers auxquels on accède via + +``` +[valarche@iluvatar ~]$ stty -a +speed 9600 baud; rows 57; columns 138; line = 0; +intr = ^C; quit = ^\; erase = ^?; kill = ^U; eof = ^D; eol = M-^?; eol2 = M-^?; swtch = ; start = ^Q; stop = ^S; susp = ^Z; +rprnt = ^R; werase = ^W; lnext = ^V; discard = ^O; min = 1; time = 0; +-parenb -parodd -cmspar cs8 -hupcl -cstopb cread -clocal -crtscts +-ignbrk -brkint -ignpar -parmrk -inpck -istrip -inlcr -igncr icrnl ixon -ixoff -iuclc ixany imaxbel iutf8 +opost -olcuc -ocrnl onlcr -onocr -onlret -ofill -ofdel nl0 cr0 tab0 bs0 vt0 ff0 +isig icanon iexten echo echoe -echok -echonl -noflsh -xcase -tostop -echoprt echoctl echoke -flusho -extproc +``` + +Par exemple, ``^C`` permet d'envoyer un signal de valeur *SIGINT* qui sera reçu par le groupe de processus dit **avant-plan**. L'interprétation du signal dépendra de la manière dont les processus ont "programmé" le traitement de ce signal (nous reviendrons dessus). Cela si le système supporte le contrôle de tâche (*job control*). +Le comportement par défaut est l'interruption définitive (état zombi). + +On peut envoyer sous forme de commandes un signal à un processus ``kill -n P`` avec ``n`` un numéro de signal et ``P`` le numéro d'un processus. + +##### Remarque +Le shell n'interprète pas le signal de la même manière.... + + +## Annexes + +```[valarche@iluvatar ~]$ ps -l -A --forest +F S UID PID PPID C PRI NI ADDR SZ WCHAN TTY TIME CMD +1 S 0 2 0 0 80 0 - 0 - ? 00:00:01 kthreadd +1 S 0 3 2 0 80 0 - 0 - ? 00:05:42 \_ ksoftirqd/0 +1 S 0 5 2 0 60 -20 - 0 - ? 00:00:00 \_ kworker/0:0H +1 S 0 6 2 0 80 0 - 0 - ? 00:00:00 \_ kworker/u192:0 +1 S 0 8 2 0 80 0 - 0 - ? 00:16:32 \_ rcu_preempt +1 S 0 9 2 0 80 0 - 0 - ? 00:00:00 \_ rcu_sched +1 S 0 10 2 0 80 0 - 0 - ? 00:00:00 \_ rcu_bh +1 S 0 11 2 0 -40 - - 0 - ? 00:00:00 \_ migration/0 +1 S 0 12 2 0 60 -20 - 0 - ? 00:00:00 \_ lru-add-drain +5 S 0 13 2 0 -40 - - 0 - ? 00:00:08 \_ watchdog/0 +1 S 0 14 2 0 80 0 - 0 - ? 00:00:00 \_ cpuhp/0 +1 S 0 15 2 0 80 0 - 0 - ? 00:00:00 \_ cpuhp/1 +5 S 0 16 2 0 -40 - - 0 - ? 00:00:07 \_ watchdog/1 +1 S 0 17 2 0 -40 - - 0 - ? 00:00:00 \_ migration/1 +1 S 0 18 2 0 80 0 - 0 - ? 00:00:00 \_ ksoftirqd/1 +1 S 0 20 2 0 60 -20 - 0 - ? 00:00:00 \_ kworker/1:0H +1 S 0 22 2 0 80 0 - 0 - ? 00:00:00 \_ cpuhp/2 +5 S 0 23 2 0 -40 - - 0 - ? 00:00:07 \_ watchdog/2 +1 S 0 24 2 0 -40 - - 0 - ? 00:00:00 \_ migration/2 +1 S 0 25 2 0 80 0 - 0 - ? 00:00:00 \_ ksoftirqd/2 +1 S 0 27 2 0 60 -20 - 0 - ? 00:00:00 \_ kworker/2:0H +1 S 0 29 2 0 80 0 - 0 - ? 00:00:00 \_ cpuhp/3 +5 S 0 30 2 0 -40 - - 0 - ? 00:00:07 \_ watchdog/3 +1 S 0 31 2 0 -40 - - 0 - ? 00:00:00 \_ migration/3 +1 S 0 32 2 0 80 0 - 0 - ? 00:00:06 \_ ksoftirqd/3 +1 S 0 34 2 0 60 -20 - 0 - ? 00:00:00 \_ kworker/3:0H +1 S 0 36 2 0 80 0 - 0 - ? 00:00:00 \_ cpuhp/4 +5 S 0 37 2 0 -40 - - 0 - ? 00:00:07 \_ watchdog/4 +1 S 0 38 2 0 -40 - - 0 - ? 00:00:00 \_ migration/4 +1 S 0 39 2 0 80 0 - 0 - ? 00:00:22 \_ ksoftirqd/4 +1 S 0 41 2 0 60 -20 - 0 - ? 00:00:00 \_ kworker/4:0H +... +1 S 0 77 2 0 60 -20 - 0 - ? 00:00:00 \_ kworker/10:0H +1 S 0 78 2 0 80 0 - 0 - ? 00:00:00 \_ cpuhp/11 +5 S 0 79 2 0 -40 - - 0 - ? 00:00:07 \_ watchdog/11 +1 S 0 80 2 0 -40 - - 0 - ? 00:00:00 \_ migration/11 +1 S 0 81 2 0 80 0 - 0 - ? 00:00:00 \_ ksoftirqd/11 +1 S 0 83 2 0 60 -20 - 0 - ? 00:00:00 \_ kworker/11:0H +1 S 0 84 2 0 80 0 - 0 - ? 00:00:00 \_ cpuhp/12 +5 S 0 85 2 0 -40 - - 0 - ? 00:00:07 \_ watchdog/12 +1 S 0 86 2 0 -40 - - 0 - ? 00:00:00 \_ migration/12 +1 S 0 87 2 0 80 0 - 0 - ? 00:00:08 \_ ksoftirqd/12 +1 S 0 89 2 0 60 -20 - 0 - ? 00:00:00 \_ kworker/12:0H +1 S 0 90 2 0 80 0 - 0 - ? 00:00:00 \_ cpuhp/13 +5 S 0 91 2 0 -40 - - 0 - ? 00:00:07 \_ watchdog/13 +1 S 0 92 2 0 -40 - - 0 - ? 00:00:00 \_ migration/13 +1 S 0 93 2 0 80 0 - 0 - ? 00:00:00 \_ ksoftirqd/13 +1 S 0 95 2 0 60 -20 - 0 - ? 00:00:00 \_ kworker/13:0H +1 S 0 96 2 0 80 0 - 0 - ? 00:00:00 \_ cpuhp/14 +5 S 0 97 2 0 -40 - - 0 - ? 00:00:07 \_ watchdog/14 +1 S 0 98 2 0 -40 - - 0 - ? 00:00:00 \_ migration/14 +1 S 0 99 2 0 80 0 - 0 - ? 00:00:00 \_ ksoftirqd/14 +1 S 0 101 2 0 60 -20 - 0 - ? 00:00:00 \_ kworker/14:0H +1 S 0 102 2 0 80 0 - 0 - ? 00:00:00 \_ cpuhp/15 +5 S 0 103 2 0 -40 - - 0 - ? 00:00:07 \_ watchdog/15 +1 S 0 104 2 0 -40 - - 0 - ? 00:00:00 \_ migration/15 +1 S 0 105 2 0 80 0 - 0 - ? 00:00:00 \_ ksoftirqd/15 +1 S 0 107 2 0 60 -20 - 0 - ? 00:00:00 \_ kworker/15:0H +1 S 0 108 2 0 80 0 - 0 - ? 00:00:00 \_ cpuhp/16 +5 S 0 109 2 0 -40 - - 0 - ? 00:00:07 \_ watchdog/16 +1 S 0 110 2 0 -40 - - 0 - ? 00:00:00 \_ migration/16 +1 S 0 111 2 0 80 0 - 0 - ? 00:00:05 \_ ksoftirqd/16 +1 S 0 113 2 0 60 -20 - 0 - ? 00:00:00 \_ kworker/16:0H +1 S 0 114 2 0 80 0 - 0 - ? 00:00:00 \_ cpuhp/17 +5 S 0 115 2 0 -40 - - 0 - ? 00:00:07 \_ watchdog/17 +1 S 0 116 2 0 -40 - - 0 - ? 00:00:00 \_ migration/17 +1 S 0 117 2 0 80 0 - 0 - ? 00:00:00 \_ ksoftirqd/17 +1 S 0 119 2 0 60 -20 - 0 - ? 00:00:00 \_ kworker/17:0H +1 S 0 120 2 0 80 0 - 0 - ? 00:00:00 \_ cpuhp/18 +5 S 0 121 2 0 -40 - - 0 - ? 00:00:07 \_ watchdog/18 +1 S 0 122 2 0 -40 - - 0 - ? 00:00:00 \_ migration/18 +1 S 0 123 2 0 80 0 - 0 - ? 00:00:00 \_ ksoftirqd/18 +1 S 0 125 2 0 60 -20 - 0 - ? 00:00:00 \_ kworker/18:0H +1 S 0 126 2 0 80 0 - 0 - ? 00:00:00 \_ cpuhp/19 +5 S 0 127 2 0 -40 - - 0 - ? 00:00:07 \_ watchdog/19 +1 S 0 128 2 0 -40 - - 0 - ? 00:00:00 \_ migration/19 +1 S 0 129 2 0 80 0 - 0 - ? 00:00:00 \_ ksoftirqd/19 +1 S 0 131 2 0 60 -20 - 0 - ? 00:00:00 \_ kworker/19:0H +1 S 0 132 2 0 80 0 - 0 - ? 00:00:00 \_ cpuhp/20 +5 S 0 133 2 0 -40 - - 0 - ? 00:00:07 \_ watchdog/20 +1 S 0 134 2 0 -40 - - 0 - ? 00:00:00 \_ migration/20 +1 S 0 135 2 0 80 0 - 0 - ? 00:00:03 \_ ksoftirqd/20 +1 S 0 137 2 0 60 -20 - 0 - ? 00:00:00 \_ kworker/20:0H +1 S 0 138 2 0 80 0 - 0 - ? 00:00:00 \_ cpuhp/21 +5 S 0 139 2 0 -40 - - 0 - ? 00:00:07 \_ watchdog/21 +1 S 0 140 2 0 -40 - - 0 - ? 00:00:00 \_ migration/21 +1 S 0 141 2 0 80 0 - 0 - ? 00:00:00 \_ ksoftirqd/21 +1 S 0 143 2 0 60 -20 - 0 - ? 00:00:00 \_ kworker/21:0H +1 S 0 144 2 0 80 0 - 0 - ? 00:00:00 \_ cpuhp/22 +5 S 0 145 2 0 -40 - - 0 - ? 00:00:07 \_ watchdog/22 +1 S 0 146 2 0 -40 - - 0 - ? 00:00:00 \_ migration/22 +1 S 0 147 2 0 80 0 - 0 - ? 00:00:00 \_ ksoftirqd/22 +1 S 0 149 2 0 60 -20 - 0 - ? 00:00:00 \_ kworker/22:0H +1 S 0 150 2 0 80 0 - 0 - ? 00:00:00 \_ cpuhp/23 +5 S 0 151 2 0 -40 - - 0 - ? 00:00:07 \_ watchdog/23 +1 S 0 152 2 0 -40 - - 0 - ? 00:00:00 \_ migration/23 +1 S 0 153 2 0 80 0 - 0 - ? 00:00:00 \_ ksoftirqd/23 +1 S 0 155 2 0 60 -20 - 0 - ? 00:00:00 \_ kworker/23:0H +1 S 0 156 2 0 80 0 - 0 - ? 00:00:00 \_ cpuhp/24 +5 S 0 157 2 0 -40 - - 0 - ? 00:00:07 \_ watchdog/24 +1 S 0 158 2 0 -40 - - 0 - ? 00:00:00 \_ migration/24 +1 S 0 159 2 0 80 0 - 0 - ? 00:00:02 \_ ksoftirqd/24 +1 S 0 161 2 0 60 -20 - 0 - ? 00:00:00 \_ kworker/24:0H +1 S 0 162 2 0 80 0 - 0 - ? 00:00:00 \_ cpuhp/25 +5 S 0 163 2 0 -40 - - 0 - ? 00:00:07 \_ watchdog/25 +1 S 0 164 2 0 -40 - - 0 - ? 00:00:00 \_ migration/25 +1 S 0 165 2 0 80 0 - 0 - ? 00:00:00 \_ ksoftirqd/25 +1 S 0 167 2 0 60 -20 - 0 - ? 00:00:00 \_ kworker/25:0H +1 S 0 168 2 0 80 0 - 0 - ? 00:00:00 \_ cpuhp/26 +5 S 0 169 2 0 -40 - - 0 - ? 00:00:07 \_ watchdog/26 +1 S 0 170 2 0 -40 - - 0 - ? 00:00:00 \_ migration/26 +1 S 0 171 2 0 80 0 - 0 - ? 00:00:00 \_ ksoftirqd/26 +1 S 0 173 2 0 60 -20 - 0 - ? 00:00:00 \_ kworker/26:0H +1 S 0 174 2 0 80 0 - 0 - ? 00:00:00 \_ cpuhp/27 +5 S 0 175 2 0 -40 - - 0 - ? 00:00:07 \_ watchdog/27 +1 S 0 176 2 0 -40 - - 0 - ? 00:00:00 \_ migration/27 +1 S 0 177 2 0 80 0 - 0 - ? 00:00:00 \_ ksoftirqd/27 +1 S 0 179 2 0 60 -20 - 0 - ? 00:00:00 \_ kworker/27:0H +1 S 0 180 2 0 80 0 - 0 - ? 00:00:00 \_ cpuhp/28 +5 S 0 181 2 0 -40 - - 0 - ? 00:00:07 \_ watchdog/28 +1 S 0 182 2 0 -40 - - 0 - ? 00:00:00 \_ migration/28 +1 S 0 183 2 0 80 0 - 0 - ? 00:00:02 \_ ksoftirqd/28 +1 S 0 185 2 0 60 -20 - 0 - ? 00:00:00 \_ kworker/28:0H +1 S 0 186 2 0 80 0 - 0 - ? 00:00:00 \_ cpuhp/29 +5 S 0 187 2 0 -40 - - 0 - ? 00:00:07 \_ watchdog/29 +1 S 0 188 2 0 -40 - - 0 - ? 00:00:00 \_ migration/29 +1 S 0 189 2 0 80 0 - 0 - ? 00:00:00 \_ ksoftirqd/29 +1 S 0 191 2 0 60 -20 - 0 - ? 00:00:00 \_ kworker/29:0H +1 S 0 192 2 0 80 0 - 0 - ? 00:00:00 \_ cpuhp/30 +5 S 0 193 2 0 -40 - - 0 - ? 00:00:07 \_ watchdog/30 +1 S 0 194 2 0 -40 - - 0 - ? 00:00:00 \_ migration/30 +1 S 0 195 2 0 80 0 - 0 - ? 00:00:00 \_ ksoftirqd/30 +1 S 0 197 2 0 60 -20 - 0 - ? 00:00:00 \_ kworker/30:0H +1 S 0 198 2 0 80 0 - 0 - ? 00:00:00 \_ cpuhp/31 +5 S 0 199 2 0 -40 - - 0 - ? 00:00:07 \_ watchdog/31 +1 S 0 200 2 0 -40 - - 0 - ? 00:00:00 \_ migration/31 +1 S 0 201 2 0 80 0 - 0 - ? 00:00:00 \_ ksoftirqd/31 +1 S 0 203 2 0 60 -20 - 0 - ? 00:00:00 \_ kworker/31:0H +1 S 0 204 2 0 80 0 - 0 - ? 00:00:00 \_ cpuhp/32 +5 S 0 205 2 0 -40 - - 0 - ? 00:00:07 \_ watchdog/32 +1 S 0 206 2 0 -40 - - 0 - ? 00:00:00 \_ migration/32 +1 S 0 207 2 0 80 0 - 0 - ? 00:00:00 \_ ksoftirqd/32 +1 S 0 208 2 0 80 0 - 0 - ? 00:00:00 \_ kworker/32:0 +1 S 0 209 2 0 60 -20 - 0 - ? 00:00:00 \_ kworker/32:0H +1 S 0 210 2 0 80 0 - 0 - ? 00:00:00 \_ cpuhp/33 +5 S 0 211 2 0 -40 - - 0 - ? 00:00:07 \_ watchdog/33 +1 S 0 212 2 0 -40 - - 0 - ? 00:00:00 \_ migration/33 +1 S 0 213 2 0 80 0 - 0 - ? 00:00:00 \_ ksoftirqd/33 +1 S 0 215 2 0 60 -20 - 0 - ? 00:00:00 \_ kworker/33:0H +1 S 0 216 2 0 80 0 - 0 - ? 00:00:00 \_ cpuhp/34 +5 S 0 217 2 0 -40 - - 0 - ? 00:00:07 \_ watchdog/34 +1 S 0 218 2 0 -40 - - 0 - ? 00:00:00 \_ migration/34 +1 S 0 219 2 0 80 0 - 0 - ? 00:00:00 \_ ksoftirqd/34 +1 S 0 221 2 0 60 -20 - 0 - ? 00:00:00 \_ kworker/34:0H +1 S 0 222 2 0 80 0 - 0 - ? 00:00:00 \_ cpuhp/35 +5 S 0 223 2 0 -40 - - 0 - ? 00:00:07 \_ watchdog/35 +1 S 0 224 2 0 -40 - - 0 - ? 00:00:00 \_ migration/35 +1 S 0 225 2 0 80 0 - 0 - ? 00:00:00 \_ ksoftirqd/35 +1 S 0 227 2 0 60 -20 - 0 - ? 00:00:00 \_ kworker/35:0H +1 S 0 228 2 0 80 0 - 0 - ? 00:00:00 \_ cpuhp/36 +5 S 0 229 2 0 -40 - - 0 - ? 00:00:07 \_ watchdog/36 +... +1 S 0 272 2 0 -40 - - 0 - ? 00:00:00 \_ migration/43 +1 S 0 273 2 0 80 0 - 0 - ? 00:00:00 \_ ksoftirqd/43 +1 S 0 275 2 0 60 -20 - 0 - ? 00:00:00 \_ kworker/43:0H +1 S 0 276 2 0 80 0 - 0 - ? 00:00:00 \_ cpuhp/44 +5 S 0 277 2 0 -40 - - 0 - ? 00:00:07 \_ watchdog/44 +1 S 0 278 2 0 -40 - - 0 - ? 00:00:00 \_ migration/44 +1 S 0 279 2 0 80 0 - 0 - ? 00:00:00 \_ ksoftirqd/44 +1 S 0 281 2 0 60 -20 - 0 - ? 00:00:00 \_ kworker/44:0H +1 S 0 282 2 0 80 0 - 0 - ? 00:00:00 \_ cpuhp/45 +5 S 0 283 2 0 -40 - - 0 - ? 00:00:07 \_ watchdog/45 +1 S 0 284 2 0 -40 - - 0 - ? 00:00:00 \_ migration/45 +1 S 0 285 2 0 80 0 - 0 - ? 00:00:00 \_ ksoftirqd/45 +1 S 0 287 2 0 60 -20 - 0 - ? 00:00:00 \_ kworker/45:0H +1 S 0 288 2 0 80 0 - 0 - ? 00:00:00 \_ cpuhp/46 +5 S 0 289 2 0 -40 - - 0 - ? 00:00:07 \_ watchdog/46 +1 S 0 290 2 0 -40 - - 0 - ? 00:00:00 \_ migration/46 +1 S 0 291 2 0 80 0 - 0 - ? 00:00:00 \_ ksoftirqd/46 +1 S 0 293 2 0 60 -20 - 0 - ? 00:00:00 \_ kworker/46:0H +1 S 0 294 2 0 80 0 - 0 - ? 00:00:00 \_ cpuhp/47 +5 S 0 295 2 0 -40 - - 0 - ? 00:00:07 \_ watchdog/47 +1 S 0 296 2 0 -40 - - 0 - ? 00:00:00 \_ migration/47 +1 S 0 297 2 0 80 0 - 0 - ? 00:00:00 \_ ksoftirqd/47 +... +1 S 0 321 2 0 80 0 - 0 - ? 00:00:00 \_ ksoftirqd/51 +1 S 0 323 2 0 60 -20 - 0 - ? 00:00:00 \_ kworker/51:0H +1 S 0 324 2 0 80 0 - 0 - ? 00:00:00 \_ cpuhp/52 +5 S 0 325 2 0 -40 - - 0 - ? 00:00:07 \_ watchdog/52 +1 S 0 326 2 0 -40 - - 0 - ? 00:00:00 \_ migration/52 +1 S 0 327 2 0 80 0 - 0 - ? 00:00:00 \_ ksoftirqd/52 +1 S 0 329 2 0 60 -20 - 0 - ? 00:00:00 \_ kworker/52:0H +1 S 0 330 2 0 80 0 - 0 - ? 00:00:00 \_ cpuhp/53 +5 S 0 331 2 0 -40 - - 0 - ? 00:00:07 \_ watchdog/53 +1 S 0 332 2 0 -40 - - 0 - ? 00:00:00 \_ migration/53 +1 S 0 333 2 0 80 0 - 0 - ? 00:00:00 \_ ksoftirqd/53 +1 S 0 335 2 0 60 -20 - 0 - ? 00:00:00 \_ kworker/53:0H +1 S 0 336 2 0 80 0 - 0 - ? 00:00:00 \_ cpuhp/54 +5 S 0 337 2 0 -40 - - 0 - ? 00:00:07 \_ watchdog/54 +1 S 0 338 2 0 -40 - - 0 - ? 00:00:00 \_ migration/54 +1 S 0 339 2 0 80 0 - 0 - ? 00:00:00 \_ ksoftirqd/54 +1 S 0 341 2 0 60 -20 - 0 - ? 00:00:00 \_ kworker/54:0H +1 S 0 342 2 0 80 0 - 0 - ? 00:00:00 \_ cpuhp/55 +5 S 0 343 2 0 -40 - - 0 - ? 00:00:07 \_ watchdog/55 +... +5 S 0 373 2 0 -40 - - 0 - ? 00:00:07 \_ watchdog/60 +1 S 0 374 2 0 -40 - - 0 - ? 00:00:00 \_ migration/60 +1 S 0 375 2 0 80 0 - 0 - ? 00:00:00 \_ ksoftirqd/60 +1 S 0 377 2 0 60 -20 - 0 - ? 00:00:00 \_ kworker/60:0H +1 S 0 378 2 0 80 0 - 0 - ? 00:00:00 \_ cpuhp/61 +5 S 0 379 2 0 -40 - - 0 - ? 00:00:07 \_ watchdog/61 +1 S 0 380 2 0 -40 - - 0 - ? 00:00:00 \_ migration/61 +1 S 0 381 2 0 80 0 - 0 - ? 00:00:00 \_ ksoftirqd/61 +1 S 0 383 2 0 60 -20 - 0 - ? 00:00:00 \_ kworker/61:0H +1 S 0 384 2 0 80 0 - 0 - ? 00:00:00 \_ cpuhp/62 +5 S 0 385 2 0 -40 - - 0 - ? 00:00:07 \_ watchdog/62 +1 S 0 386 2 0 -40 - - 0 - ? 00:00:00 \_ migration/62 +1 S 0 387 2 0 80 0 - 0 - ? 00:00:00 \_ ksoftirqd/62 +1 S 0 389 2 0 60 -20 - 0 - ? 00:00:00 \_ kworker/62:0H +1 S 0 390 2 0 80 0 - 0 - ? 00:00:00 \_ cpuhp/63 +5 S 0 391 2 0 -40 - - 0 - ? 00:00:07 \_ watchdog/63 +1 S 0 392 2 0 -40 - - 0 - ? 00:00:00 \_ migration/63 +1 S 0 393 2 0 80 0 - 0 - ? 00:00:00 \_ ksoftirqd/63 +1 S 0 395 2 0 60 -20 - 0 - ? 00:00:00 \_ kworker/63:0H +5 S 0 396 2 0 80 0 - 0 - ? 00:00:00 \_ kdevtmpfs +1 S 0 397 2 0 60 -20 - 0 - ? 00:00:00 \_ netns +1 S 0 398 2 0 80 0 - 0 - ? 00:00:04 \_ khungtaskd +1 S 0 399 2 0 80 0 - 0 - ? 00:00:00 \_ oom_reaper +1 S 0 400 2 0 60 -20 - 0 - ? 00:00:00 \_ writeback +1 S 0 401 2 0 80 0 - 0 - ? 00:00:00 \_ kcompactd0 +1 S 0 402 2 0 80 0 - 0 - ? 00:00:00 \_ kcompactd1 +1 S 0 403 2 0 80 0 - 0 - ? 00:00:00 \_ kcompactd2 +... +1 S 0 1149 2 0 80 0 - 0 - ? 00:00:00 \_ kworker/43:1 +1 S 0 1150 2 0 80 0 - 0 - ? 00:00:00 \_ kworker/44:1 +1 S 0 1151 2 0 80 0 - 0 - ? 00:00:00 \_ kworker/51:1 +1 S 0 1152 2 0 80 0 - 0 - ? 00:00:00 \_ kworker/53:1 +1 S 0 1154 2 0 80 0 - 0 - ? 00:00:00 \_ kworker/48:1 +1 S 0 1157 2 0 80 0 - 0 - ? 00:00:01 \_ kworker/50:1 +1 S 0 1158 2 0 80 0 - 0 - ? 00:00:00 \_ kworker/61:1 +1 S 0 1159 2 0 80 0 - 0 - ? 00:00:00 \_ kworker/63:1 +1 S 0 1160 2 0 80 0 - 0 - ? 00:00:00 \_ kworker/59:1 +1 S 0 1161 2 0 80 0 - 0 - ? 00:00:00 \_ kworker/55:1 +... +1 S 0 1367 2 0 60 -20 - 0 - ? 00:00:00 \_ kworker/43:1H +1 S 0 1371 2 0 60 -20 - 0 - ? 00:00:00 \_ kworker/45:1H +1 S 0 1377 2 0 60 -20 - 0 - ? 00:00:00 \_ kworker/42:1H +1 S 0 1380 2 0 60 -20 - 0 - ? 00:00:00 \_ ixgbe +1 S 0 1383 2 0 60 -20 - 0 - ? 00:00:00 \_ kworker/36:1H +1 S 0 1384 2 0 60 -20 - 0 - ? 00:00:00 \_ kworker/55:1H +1 S 0 1385 2 0 60 -20 - 0 - ? 00:00:00 \_ ttm_swap +1 S 0 1395 2 0 80 0 - 0 - ? 00:06:52 \_ jbd2/sdc1-8 +1 S 0 1396 2 0 60 -20 - 0 - ? 00:00:00 \_ ext4-rsv-conver +1 S 0 1418 2 0 60 -20 - 0 - ? 00:00:00 \_ kworker/50:1H +1 S 0 1424 2 0 80 0 - 0 - ? 00:00:00 \_ jbd2/sdb1-8 +1 S 0 1425 2 0 60 -20 - 0 - ? 00:00:00 \_ ext4-rsv-conver +1 S 0 1429 2 0 60 -20 - 0 - ? 00:00:00 \_ kworker/19:1H +1 S 0 1431 2 0 60 -20 - 0 - ? 00:00:00 \_ kworker/61:1H +1 S 0 1433 2 0 60 -20 - 0 - ? 00:00:00 \_ kvm-irqfd-clean +1 S 0 1436 2 0 60 -20 - 0 - ? 00:00:03 \_ kworker/8:1H +1 S 0 1437 2 0 60 -20 - 0 - ? 00:00:00 \_ edac-poller +1 S 0 1443 2 0 60 -20 - 0 - ? 00:00:00 \_ kworker/46:1H +1 S 0 1445 2 0 60 -20 - 0 - ? 00:00:01 \_ kworker/16:1H +1 S 0 1448 2 0 60 -20 - 0 - ? 00:00:00 \_ kworker/47:1H +1 S 0 1450 2 0 80 0 - 0 - ? 00:00:01 \_ kworker/44:2 +1 S 0 1460 2 0 60 -20 - 0 - ? 00:00:00 \_ kworker/18:1H +1 S 0 1477 2 0 60 -20 - 0 - ? 00:00:00 \_ kworker/54:1H +1 S 0 1512 2 0 60 -20 - 0 - ? 00:00:00 \_ bond0 +1 S 0 1515 2 0 60 -20 - 0 - ? 00:00:00 \_ kworker/52:1H +1 S 0 1581 2 0 60 -20 - 0 - ? 00:00:00 \_ kworker/40:1H +1 S 0 1630 2 0 60 -20 - 0 - ? 00:00:00 \_ kworker/62:1H +1 S 0 1645 2 0 60 -20 - 0 - ? 00:00:00 \_ kworker/35:1H +1 S 0 1668 2 0 60 -20 - 0 - ? 00:00:08 \_ kworker/4:1H +1 S 0 1669 2 0 60 -20 - 0 - ? 00:00:02 \_ kworker/12:1H +1 S 0 1673 2 0 60 -20 - 0 - ? 00:00:00 \_ nfsd4_callbacks +1 S 0 1674 2 0 80 0 - 0 - ? 00:00:00 \_ lockd +5 S 0 1676 2 0 80 0 - 0 - ? 00:00:11 \_ nfsd +5 S 0 1677 2 0 80 0 - 0 - ? 00:00:13 \_ nfsd + +....... + +0 R 989 87244 86846 0 80 0 - 8501 - pts/0 00:00:00 | \_ ps +4 S 0 87210 1622 0 80 0 - 9570 - ? 00:00:00 \_ sshd +4 S 0 1628 1 0 80 0 - 25948 - ? 00:00:00 login +4 S 0 1719 1628 0 80 0 - 3636 - tty1 00:00:00 \_ bash +5 S 32 1634 1 0 80 0 - 11528 - ? 00:00:05 rpcbind +5 S 0 1637 1 0 80 0 - 7834 - ? 00:00:00 rpc.statd +5 S 87 1638 1 0 80 0 - 23385 - ? 00:03:08 ntpd +5 S 0 1639 1 0 80 0 - 8684 - ? 00:00:04 rpc.mountd +5 S 0 1642 1 0 80 0 - 60190 - ? 00:08:26 nmbd +5 S 0 1648 1 0 80 0 - 79819 - ? 00:00:29 smbd +5 S 0 1649 1648 0 80 0 - 79069 - ? 00:00:14 \_ smbd-notifyd +1 S 0 1650 1648 0 80 0 - 79145 - ? 00:00:05 \_ cleanupd +1 S 0 1665 1648 0 80 0 - 79815 - ? 00:00:18 \_ lpqd +5 S 0 1657 1 0 80 0 - 124175 - ? 00:00:00 radiusd +4 S 0 1688 1 0 80 0 - 13107 - ? 00:00:01 systemd +5 S 0 1702 1688 0 80 0 - 28098 - ? 00:00:00 \_ (sd-pam) +4 S 3552 28791 1 0 80 0 - 13129 - ? 00:00:01 systemd +5 S 3552 28799 28791 0 80 0 - 28136 - ? 00:00:00 \_ (sd-pam) +4 S 662 35641 1 0 80 0 - 13127 - ? 00:00:00 systemd +5 S 662 35650 35641 0 80 0 - 30185 - ? 00:00:00 \_ (sd-pam) +1 S 662 35691 1 0 80 0 - 35106 - ? 00:01:28 dirmngr +1 S 662 35696 1 0 80 0 - 41554 - ? 00:01:29 gpg-agent +1 S 3552 41087 1 0 80 0 - 6343 - ? 00:02:07 rsync +1 S 3552 41091 1 0 80 0 - 91499 - ? 01:09:18 cron +1 S 3552 78683 1 0 80 0 - 85476 - ? 00:38:35 rsync +1 S 3552 77557 1 0 80 0 - 6343 - ? 00:00:05 rsync +1 S 3552 78842 1 0 80 0 - 6496 - ? 00:00:08 rsync +4 S 989 86828 1 0 80 0 - 13129 ep_pol ? 00:00:00 systemd +5 S 989 86837 86828 0 80 0 - 30185 - ? 00:00:00 \_ (sd-pam) +[valarche@iluvatar ~]$ + +``` + + +Seulement l'utilisateur : + + +``` +[valarche@iluvatar ~]$ ps -l +F S UID PID PPID C PRI NI ADDR SZ WCHAN TTY TIME CMD +0 S 989 86846 86844 0 80 0 - 3636 wait pts/0 00:00:00 bash +0 R 989 87267 86846 0 80 0 - 8293 - pts/0 00:00:00 ps +[valarche@iluvatar ~]$ ps -l -A --forest | grep 989 +5 S 989 86844 86823 0 80 0 - 27314 - ? 00:00:00 | \_ sshd +0 S 989 86846 86844 0 80 0 - 3636 wait pts/0 00:00:00 | \_ bash +0 R 989 87269 86846 0 80 0 - 8501 - pts/0 00:00:00 | \_ ps +0 S 989 87270 86846 0 80 0 - 2841 pipe_w pts/0 00:00:00 | \_ grep +4 S 989 86828 1 0 80 0 - 13129 ep_pol ? 00:00:00 systemd +5 S 989 86837 86828 0 80 0 - 30185 - ? 00:00:00 \_ (sd-pam) +[valarche@iluvatar ~]$ + +``` + +# Notes de cours +Cette première partie du cours concerne la gestion de processus et des services permettant leur manipulation : création, terminaison, chargement/recouvrement et synchronisation. Nous suivrons l'interface POSIX pour les programmes écrits en C. + +## Gestion des processus +Rappel des différences entre processus et programme. +IL est muni d'un espace d'adressage (instructions, pile d'exécution, tas entre autres). Cet espace est dynamique; il est susceptible de se modifier au cours du temps. +Les deux modes d'exécution : user et kernel. +Tout processus peut créer un/des processus (sous réserve de ressources) : la primitive *fork*. La sémantique dénotationnelle est la duplication du processus appelant. Il se crée une arborescence de processus. + +Pas d'exécution déterministe à priori (un scheduler ordonne les exécutions). On peut faire appel à des mécanismes de synchronisation. +#### Etats d'un processus +Les 7 états d'un processus +- Nouveau +- Prêt +- Execution noyau +- Execution user +- Endormi +- Zombi +- suspendu + +L'automate de l'évolution d'un processus est le suivant : +![Modèle à 7 états](img/model5etats_proc.pdf) + +#### Implémentation des processus +Pour pouvoir ordonnancé le système a besoin d'un ensemble d'informations désigné par le terme **bloc de contrôle** (BCP ou PCB). +Ce bloc contient une entrée dans la table interne du noyau. Voici des informations qu'il contient : +- le pid du processus +- l'état du processus +- son compteur ordinal (pointeur d'instruction) +- son allocation mémoire +- les fichiers ouverts +- les valeurs contenues dans les registres du processeur + +| Gestion des processus | Gestion de la mémoire | Gestion des fichiers | +|:--|:--|:--| +| Registres | Pointeur seg.code | Répertoire racine | +| Pointeur d'instruction | Pointeur seg. données | Répertoire de travail | +| Priorité | Pointeur seg.pile | fd (File Descriptors) | +| Pointeur de pile | | uid | +| Date de lancement | | gid | +| Temps UCT utilisé | | | +| Temps UCT des fils | | | +| Signaux | | | +| pid | | | +| ppid | | | + +Ce PCB est nécessaire pour effectuer le changement de contexte de processus. +#### Changement de contexte +On illustre par cette figure, en notant les différents mode : +![Changement de contexte](img/chgmentContexte.pdf) + +#### Les démons +Processus toujours en arrière plan. Pas d'association à un terminal ou processus de login. En génénal, il ne "travaille" pas. Ils lancent d'autres processus pour effectuer les tâches. + +#### Création et terminaison des processus (POSIX) + + +```c +#include +#include + +pid_t fork(void); +``` + +Cette fonction permet la création d'un processus est une copie de l'appelant. Comme indiqué auparavant, les deux processus partagent le même code (sauf si pas ré-entrant) et leur pointeur d'instruction est à la même instruction. Par contre la valeur de retour de l'appel à fork() est différente selon que c'est le père ou le fils. Les variables des deux processus sont **dupliquées**. + +Une hiérarchie est établie : père/fils. + +En fait, on peut illustrer ce qui n'est pas hérité plutôt que l'inverse. +Le processus fils n'hérite pas de : +- pid de son père, ni ppid +- les temps d'exécution (initialisés à la valeur nulle) +- les signaux pendants +- la priorité du processus (initialisée à la valeur standard) +- les verrous sur les fichiers détenus par le père qui restent la propriété exclusive de celui-ci + +Il est important de noter que la table des descripteurs est héritée. Les deux processus partagent les même descripteurs et les mêmes *offset*. Ils pointent sur le même entrée de la table des fichiers ouverts. + +A faire en exercices: +- partage de *uid*, *gid*, *cwd*, *sid*, +- partage des descripteurs; *offset* + +##### Remarques +La copie des données étant couteuse, on ne la fait que si il y a une demande d'écriture. + +##### Exemples +Voici une illustration pour un clonage simple dont voici le code : + +```c +int main() { + pid_t i; + + i = fork(); + + return EXIT_SUCCESS; +} +``` + +![Processus père et fils](img/procClone.pdf) + +Un exemple plus complet : + +```c +* usage : creerProcessus1 */ + +#include +#include +#include + +pid_t pid; // pour récupérer le pid du nouveau processus + +int main() { + + switch(pid = fork()) { + case (pid_t) -1: // l’appel de fork a échoué + perror("Création de processus"); + exit(2); + case (pid_t)0: // ici, ça concerne le processus fils + printf("valeur de fork dans le fils = %d\n",pid); + printf("je suis le processus %d de père %d\n",getpid(), getppid()); + printf("fin du processus fils\n"); + exit(0); + default: // ici, c’est le processus père + printf("valeur de fork dans le père = %d\n",pid); + printf("je suis le processus %d de père %d\n",getpid(), getppid()); + printf("fin du processus père\n"); + } + return EXIT_SUCCESS; +} +``` + + dont l'exécution donne : + +``` +[valarche@iluvatar exemples_asr31]$ gcc creerProcessus1.c +[valarche@iluvatar exemples_asr31]$ ./a.out +valeur de fork dans le père = 95256 +je suis le processus 95255 de père 95195 +fin du processus père +valeur de fork dans le fils = 0 +je suis le processus 95256 de père 1 +fin du processus fils + +``` + +##### Hiérarchie de processus : + +```c +#include +#include +#include +#include + +int main(void) { + + int n = 5; + pid_t fils_pid; + + for (int i=0; i < n; i++) { + fils_pid = fork(); + + if (fils_pid > 0) { + break; + } + + printf("Processus %d avec père %d\n", getpid(), getppid()); + + } + return EXIT_SUCCESS; +} +``` +Autres exemples avec attente (dynamique) dans les processus + +```c +#include +#include +#include +#include + +int main(void) { + + int n = 5; + pid_t fils_pid; + + for (int i=0; i < n; i++) { + fils_pid = fork(); + + if (fils_pid > 0) break; + + printf("Processus %d avec père %d\n", getpid(), getppid()); + } + + for (int j = 0; j < 10000; j++) { + for (int k=0; k < 1000; k++) { + ; + } + } + + return EXIT_SUCCESS; +} +``` + +## Annexe + +Session = groupe de processus. Par exemple, *foreground* et *background*. Un processus appartient à une unique session (celle de son père à la création). + +Un processus peut créer une nouvelle session : + +```c +#include +#include +pid_t setsid(void); +``` + +Le processus en devient le leader (le numéro du processus identifiera le numéro de la session). + +```c +#include +pid_t getpgrp(void); +``` + diff --git a/img/chgmentContexte.pdf b/img/chgmentContexte.pdf new file mode 100644 index 0000000..4c4f61f Binary files /dev/null and b/img/chgmentContexte.pdf differ diff --git a/img/model5etats_proc.pdf b/img/model5etats_proc.pdf new file mode 100644 index 0000000..044d4df Binary files /dev/null and b/img/model5etats_proc.pdf differ diff --git a/img/procClone.pdf b/img/procClone.pdf new file mode 100644 index 0000000..0d22f2c Binary files /dev/null and b/img/procClone.pdf differ