cm processus
This commit is contained in:
@@ -18,7 +18,14 @@ R3.05 (Programmation système).
|
|||||||
|
|
||||||
Merci de lire les [quelques recommandations](./misc/CODE.md) sur les bonnes pratiques d'écriture de code.
|
Merci de lire les [quelques recommandations](./misc/CODE.md) sur les bonnes pratiques d'écriture de code.
|
||||||
|
|
||||||
#### Semaine 1 (01/09 - 05/09)
|
#### Semaine 1 (31/08 - 04/09)
|
||||||
Rappels sur la gestion de la [mémoire](cours/memoire.pdf), [td1](td/td1/td1.pdf), [tp1](tp/tp1/).
|
Rappels sur la gestion de la [mémoire](cours/memoire.pdf), [td1](td/td1/td1.pdf), [tp1](tp/tp1/).
|
||||||
|
|
||||||
|
#### Semaine 2 (07/09 - 11/09)
|
||||||
|
[Système de fichiers et api E/S](cours/fichier.pdf), [td2](td/td2/td2.pdf), [tp2](tp/tp2).
|
||||||
|
|
||||||
|
#### Semaine 3 (15/09 - 19/09)
|
||||||
|
[Processus](cours/processus.pdf), [interruption/ordonnancement](cours/interruption_ordoanncement.pdf), [td3](td/td3/td3.pdf), [tp3](tp/tp3).
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Executable
BIN
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -0,0 +1,117 @@
|
|||||||
|
# Processus
|
||||||
|
|
||||||
|
#### Ex1
|
||||||
|
Compilez et exécutez [ex1-stdio.c](src/ex1-stdio.c) et [ex1-syscall.c](src/ex1-syscall.c).
|
||||||
|
Expliquez.
|
||||||
|
|
||||||
|
#### Ex2
|
||||||
|
Compilez et exécutez [fork_and_fd1.c](src/fork_and_fd1.c) et [fork_and_fd2.c](src/fork_and_fd2.c).
|
||||||
|
Expliquez.
|
||||||
|
|
||||||
|
#### Ex3
|
||||||
|
Que fait le programme [copy1byte.c](src/copy1byte.c) ?
|
||||||
|
Décommentez la ligne du `fork`. Expliquez ce qui se passe.
|
||||||
|
|
||||||
|
#### Ex4
|
||||||
|
Compilez et exécutez [copy_on_write.c](src/copy_on_write.c). En utilisant l'interface `/proc/pid/smaps`, vérifiez
|
||||||
|
la technique du copy on write utilisé par le système.
|
||||||
|
|
||||||
|
#### Ex5
|
||||||
|
Ecrire un programme qui crée un processus fils.
|
||||||
|
|
||||||
|
Dans le fils :
|
||||||
|
|
||||||
|
* imprimer le retour de `fork()`, `getpid()`, `getppid()`.
|
||||||
|
* bloquer 4 secondes (`sleep`).
|
||||||
|
* se terminer avec `exit(2)`.
|
||||||
|
|
||||||
|
Dans le père :
|
||||||
|
|
||||||
|
* imprimer le retour de `fork()`, `getpid()`, `getppid()`.
|
||||||
|
* attendre la fin de son fils (`wait()`), et imprimer son code de retour.
|
||||||
|
* afficher alors la liste de tous les processus actifs (`execl()` avec
|
||||||
|
`ps -ef`).
|
||||||
|
|
||||||
|
|
||||||
|
#### Ex6
|
||||||
|
|
||||||
|
Le but de l'exercice est de détecter la présence d'un zéro dans un
|
||||||
|
tableau `unsigned char` de taille `SIZE` en découpant le travail entre
|
||||||
|
plusieurs processus. On part du programme source suivant :
|
||||||
|
|
||||||
|
```c
|
||||||
|
#define SIZE 1000
|
||||||
|
int search(const unsigned char * t,int start,int end)
|
||||||
|
{
|
||||||
|
/* renvoie 1 s'il y a un 0 dans la tranche du tableau,
|
||||||
|
* 0 sinon */
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(int argc , char * argv[])
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
unsigned char arr[SIZE];
|
||||||
|
|
||||||
|
srandom(time(NULL));
|
||||||
|
|
||||||
|
for (i = 0; i < SIZE; i++)
|
||||||
|
arr[i] = (unsigned char) (random() % 255) + 1;
|
||||||
|
|
||||||
|
printf("Enter a number between 0 and %d: ", SIZE);
|
||||||
|
scanf(" %d", &i);
|
||||||
|
if (i >= 0 && i < SIZE) arr[i] = 0;
|
||||||
|
|
||||||
|
if (search(arr,0,SIZE-1))
|
||||||
|
printf("Found !\n");
|
||||||
|
else
|
||||||
|
printf("Not found !\n");
|
||||||
|
return EXIT_SUCCES;
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
Combien y-at-il de 0 au plus dans le tableau ? Complétez la fonction
|
||||||
|
`search`, et testez.
|
||||||
|
|
||||||
|
1. Première version. Modifiez le programme pour que le processus crée
|
||||||
|
un fils. Le fils et le père cherche chacun le zéro dans une moitié
|
||||||
|
du tableau. Le fils communique le résultat à son père. Celui-ci, à
|
||||||
|
l'aide de son propre travail, donnera la réponse.
|
||||||
|
|
||||||
|
2. Deuxième version. Votre programme accepte sur la ligne de commande
|
||||||
|
un entier n entre 1 et 100. Le programme crée n fils
|
||||||
|
qui cherche chacun dans une partie du tableau. Le père attend la fin
|
||||||
|
de chacun de ses fils, récupère leur résultat et affiche la réponse.
|
||||||
|
|
||||||
|
3. Troisième version. On améliore la version précédente. Lorsque qu'un
|
||||||
|
fils trouve le 0 dans le tableau, et que le père en est averti,
|
||||||
|
faites en sorte que les autres fils vivants se terminent. On
|
||||||
|
utilisera la primitive `kill()` qui perment d'envoyer le signal de
|
||||||
|
terminaison (`SIGTERM`) à tout un groupe.
|
||||||
|
|
||||||
|
|
||||||
|
#### Ex7
|
||||||
|
Exécutez le programme [session.c](src/session.c) et interprétez avec `ps`
|
||||||
|
les informations `pid,ppid,pgid,sess,tpgid` des processus créés.
|
||||||
|
|
||||||
|
#### Ex8
|
||||||
|
Écrire un programme qui pour `n>0` donné sur la ligne de commande, engendre l'arbre généalogique :
|
||||||
|
```
|
||||||
|
0|
|
||||||
|
|\ 1
|
||||||
|
| \
|
||||||
|
| |\ 2
|
||||||
|
| | \
|
||||||
|
| | |\ ...
|
||||||
|
| | | \
|
||||||
|
| | | |\ n
|
||||||
|
x x x x x
|
||||||
|
```
|
||||||
|
|
||||||
|
Chaque processus choisit un nombre au hasard entre 0 et 127. Le processus 0 affichera la plus grande valeur choisie par tous
|
||||||
|
les processus.
|
||||||
|
|
||||||
|
Remarque : on pourra modifier la séquence de nombres aléatoires en utilisant srand dans chaque processus créé. (pourquoi ?)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@@ -0,0 +1,45 @@
|
|||||||
|
#include <sys/types.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
#include<stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include<assert.h>
|
||||||
|
#include <sys/types.h>
|
||||||
|
#include <sys/stat.h>
|
||||||
|
#include <fcntl.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
#define msg1 "je suis le pere"
|
||||||
|
#define msg2 "je suis le fils !!!"
|
||||||
|
|
||||||
|
int main(int argc,char * argv[]){
|
||||||
|
|
||||||
|
int infd,outfd;
|
||||||
|
ssize_t nbread;
|
||||||
|
char buf[1];
|
||||||
|
pid_t p;
|
||||||
|
|
||||||
|
if (argc != 3){
|
||||||
|
|
||||||
|
printf("%s infile outfile\n",argv[0]);
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
infd = open(argv[1],O_RDONLY);
|
||||||
|
assert(infd >= 0);
|
||||||
|
outfd = open(argv[2],O_WRONLY|O_CREAT|O_TRUNC,0644);
|
||||||
|
assert(outfd >= 0);
|
||||||
|
|
||||||
|
//p=fork(); // <- decommentez cette ligne
|
||||||
|
|
||||||
|
while(1){
|
||||||
|
|
||||||
|
nbread=read(infd,buf,sizeof(buf));
|
||||||
|
if (nbread <=0 ) break;
|
||||||
|
write(outfd,buf,sizeof(buf));
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
close(infd);
|
||||||
|
close(outfd);
|
||||||
|
}
|
||||||
@@ -0,0 +1,49 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
#include <sys/wait.h>
|
||||||
|
|
||||||
|
#define N 8*1024*1024
|
||||||
|
|
||||||
|
char tab[N];
|
||||||
|
|
||||||
|
int main(void)
|
||||||
|
{
|
||||||
|
for (int i = 0; i < N; i++)
|
||||||
|
tab[i] = (char)i;
|
||||||
|
|
||||||
|
printf("Avant fork : tab = %p\n", (void *)tab);
|
||||||
|
|
||||||
|
pid_t pid = fork();
|
||||||
|
|
||||||
|
if (pid < 0) {
|
||||||
|
perror("fork");
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (pid == 0) {
|
||||||
|
printf("Fils : tab = %p, tab[5000] = %d\n",
|
||||||
|
(void *)(tab+5000), tab[5000]);
|
||||||
|
printf("FILS PID = %d\n", getpid());
|
||||||
|
getchar();
|
||||||
|
|
||||||
|
tab[5000] = 42;
|
||||||
|
printf("-> modification\n");
|
||||||
|
getchar();
|
||||||
|
|
||||||
|
printf("Fils après modification : tab[5000] = %d\n",
|
||||||
|
tab[5000]);
|
||||||
|
|
||||||
|
exit(EXIT_SUCCESS);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
sleep(1);
|
||||||
|
|
||||||
|
printf("Père : tab = %p, tab[5000] = %d\n",
|
||||||
|
(void *)tab, tab[5000]);
|
||||||
|
|
||||||
|
wait(NULL);
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
@@ -0,0 +1,12 @@
|
|||||||
|
#include <sys/types.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
|
||||||
|
int main(){
|
||||||
|
|
||||||
|
printf("NON");
|
||||||
|
if (fork()) {
|
||||||
|
printf("OUI\n");
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,14 @@
|
|||||||
|
#include <sys/types.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
|
||||||
|
int main(){
|
||||||
|
|
||||||
|
//printf("NON");
|
||||||
|
write(STDOUT_FILENO,"NON",3);
|
||||||
|
if (fork()) {
|
||||||
|
//printf("OUI\n");
|
||||||
|
write(STDOUT_FILENO,"OUI\n",4);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,32 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <assert.h>
|
||||||
|
#include <sys/wait.h>
|
||||||
|
int main(int argc, char *argv[])
|
||||||
|
{
|
||||||
|
pid_t p;
|
||||||
|
int status;
|
||||||
|
p = fork();
|
||||||
|
assert( p >= 0);
|
||||||
|
|
||||||
|
if ( p == 0){
|
||||||
|
printf("@retour de fork = %d pid = %d ppid = %d\n",p,getpid(),getppid());
|
||||||
|
sleep(4);
|
||||||
|
exit(2);
|
||||||
|
}
|
||||||
|
|
||||||
|
printf("@retour de fork = %d pid = %d ppid = %d\n",p,getpid(),getppid());
|
||||||
|
|
||||||
|
assert( wait(&status) >= 0);
|
||||||
|
|
||||||
|
if (WIFEXITED(status))
|
||||||
|
printf("code retour fils = %d\n",WEXITSTATUS(status));
|
||||||
|
|
||||||
|
execlp("ls","totototo","-l","/",NULL);
|
||||||
|
|
||||||
|
/* code ici */
|
||||||
|
assert(0);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
@@ -0,0 +1,43 @@
|
|||||||
|
#include <sys/types.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
#include<stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include<assert.h>
|
||||||
|
#include <sys/types.h>
|
||||||
|
#include <sys/stat.h>
|
||||||
|
#include <fcntl.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
#define msg1 "je suis le pere"
|
||||||
|
#define msg2 "je suis le fils !!!"
|
||||||
|
|
||||||
|
int main(int argc,char * argv[]){
|
||||||
|
|
||||||
|
int outfd;
|
||||||
|
pid_t p;
|
||||||
|
|
||||||
|
if (argc != 2){
|
||||||
|
|
||||||
|
printf("%s file\n",argv[0]);
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
//p=fork();
|
||||||
|
outfd = open(argv[1],O_WRONLY|O_CREAT,0644);
|
||||||
|
assert(outfd >= 0);
|
||||||
|
p=fork();
|
||||||
|
switch(p){
|
||||||
|
case (pid_t)-1 :
|
||||||
|
perror(NULL);
|
||||||
|
exit(2);
|
||||||
|
|
||||||
|
case (pid_t)0 :
|
||||||
|
write(outfd,msg2,strlen(msg2));
|
||||||
|
break;
|
||||||
|
default :
|
||||||
|
write (outfd,msg1,strlen(msg1));
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
getchar();
|
||||||
|
close(outfd);
|
||||||
|
}
|
||||||
@@ -0,0 +1,43 @@
|
|||||||
|
#include <sys/types.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
#include<stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include<assert.h>
|
||||||
|
#include <sys/types.h>
|
||||||
|
#include <sys/stat.h>
|
||||||
|
#include <fcntl.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
#define msg1 "je suis le pere"
|
||||||
|
#define msg2 "je suis le fils !!!"
|
||||||
|
|
||||||
|
int main(int argc,char * argv[]){
|
||||||
|
|
||||||
|
int outfd;
|
||||||
|
pid_t p;
|
||||||
|
|
||||||
|
if (argc != 2){
|
||||||
|
|
||||||
|
printf("%s file\n",argv[0]);
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
p=fork();
|
||||||
|
outfd = open(argv[1],O_WRONLY|O_CREAT,0644);
|
||||||
|
assert(outfd >= 0);
|
||||||
|
//p=fork();
|
||||||
|
switch(p){
|
||||||
|
case (pid_t)-1 :
|
||||||
|
perror(NULL);
|
||||||
|
exit(2);
|
||||||
|
|
||||||
|
case (pid_t)0 :
|
||||||
|
write(outfd,msg2,strlen(msg2));
|
||||||
|
break;
|
||||||
|
default :
|
||||||
|
write (outfd,msg1,strlen(msg1));
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
getchar();
|
||||||
|
close(outfd);
|
||||||
|
}
|
||||||
@@ -0,0 +1,18 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
int main(int argc, char *argv[])
|
||||||
|
{
|
||||||
|
if (fork()){
|
||||||
|
printf("session = %d\n",getsid(getpid()));
|
||||||
|
while(1);
|
||||||
|
} else {
|
||||||
|
setsid();
|
||||||
|
printf("session = %d\n",getsid(getpid()));
|
||||||
|
while(1);
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
Reference in New Issue
Block a user