First commit
This commit is contained in:
commit
5c269302ed
59 changed files with 5613 additions and 0 deletions
BIN
content/progsys/TDM_3-processus/files/tdm3.pdf
Normal file
BIN
content/progsys/TDM_3-processus/files/tdm3.pdf
Normal file
Binary file not shown.
59
content/progsys/TDM_3-processus/index.md
Normal file
59
content/progsys/TDM_3-processus/index.md
Normal file
|
@ -0,0 +1,59 @@
|
|||
---
|
||||
Title: "TDM : Les Processus"
|
||||
Date: 2018-09-18
|
||||
Category: Programmation système
|
||||
tags: ['TD Machine', 'programmation', 'C', 'processus']
|
||||
---
|
||||
|
||||
[Télécharger les questions](./files/tdm3.pdf)
|
||||
## Question 1
|
||||
|
||||
Sans exécuter le programme, je pense qu'il y aura Il y a 4 processus de créé.
|
||||
|
||||
```
|
||||
q1.bin─┬─q1.bin
|
||||
│
|
||||
├─q1.bin
|
||||
└─q1.bin
|
||||
```
|
||||
|
||||
Les processus se "clonent" et l'arbre des processus est plus complexe que prévu
|
||||
: il y a 8 processus au total.
|
||||
|
||||
```shell
|
||||
$ ./q1.bin &
|
||||
[1] 8895
|
||||
$ pstree 8895
|
||||
q1.bin─┬─q1.bin─┬─q1.bin───q1.bin
|
||||
│ └─q1.bin
|
||||
├─q1.bin───q1.bin
|
||||
└─q1.bin
|
||||
```
|
||||
|
||||
## Question 2
|
||||
|
||||
Je suis parti au départ pour créer la commande avec une boucle for pour
|
||||
parcourir `*argv` mais il est plus simple de construire la commande avec
|
||||
` &argv[1]` qui retourne le tableau après l'élément 1.
|
||||
|
||||
```C
|
||||
int out = execvp(argv[1], &argv[1]);
|
||||
```
|
||||
J'ai aussi fait en sorte que le programme père se termine sur un `EXIT_FAILURE`
|
||||
si le fils se termine sur une erreur.
|
||||
|
||||
```C
|
||||
while ((pid = waitpid(pid, &status, 0)) > 0){
|
||||
(status == 0) ? exit(EXIT_SUCCESS) : exit(EXIT_FAILURE);
|
||||
}
|
||||
```
|
||||
Télécharger mon [code](./src/td3/q2.c).
|
||||
|
||||
## Question 3
|
||||
|
||||
Ma proposition était un peu complexe et surtout ne correspondant pas vraiment à
|
||||
la demande. J'ai donc modifié ma réponde (q3bis.c)
|
||||
|
||||
Télécharger mon [code](./src/td3/q3.c).
|
||||
Et la version remaniée [code](./src/td3/q3bis.c).
|
||||
|
9
content/progsys/TDM_3-processus/src/q1.c
Normal file
9
content/progsys/TDM_3-processus/src/q1.c
Normal file
|
@ -0,0 +1,9 @@
|
|||
#include <unistd.h>
|
||||
#include <stdlib.h>
|
||||
int main(int argc, char **argv) {
|
||||
fork();
|
||||
fork();
|
||||
fork();
|
||||
sleep(10);
|
||||
exit(EXIT_SUCCESS);
|
||||
}
|
33
content/progsys/TDM_3-processus/src/q2.c
Normal file
33
content/progsys/TDM_3-processus/src/q2.c
Normal file
|
@ -0,0 +1,33 @@
|
|||
#include <stdio.h>
|
||||
#include <unistd.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <errno.h>
|
||||
#include <sys/wait.h>
|
||||
|
||||
int main (int argc, char **argv) {
|
||||
pid_t pid;
|
||||
int status;
|
||||
pid = fork ();
|
||||
if ( pid < 0 ){
|
||||
printf("Error: %s\n", strerror(errno));
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
if ( pid == 0){
|
||||
printf(" Into child, command : %s\n",argv[1]);
|
||||
// we are in children
|
||||
int out = execvp(argv[1], &argv[1]);
|
||||
if ( out == -1 ){
|
||||
printf("Error : %s\n",strerror(errno));
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
exit(EXIT_SUCCESS);
|
||||
}
|
||||
else {
|
||||
// if child exit code is not 0 then exit father with EXIT_FAILURE
|
||||
while ((pid = waitpid(pid, &status, 0)) > 0){
|
||||
(status == 0) ? exit(EXIT_SUCCESS) : exit(EXIT_FAILURE);
|
||||
};
|
||||
}
|
||||
}
|
37
content/progsys/TDM_3-processus/src/q3.c
Normal file
37
content/progsys/TDM_3-processus/src/q3.c
Normal file
|
@ -0,0 +1,37 @@
|
|||
#include <stdio.h>
|
||||
#include <unistd.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <errno.h>
|
||||
#include <sys/wait.h>
|
||||
|
||||
int main(int argc, char **argv){
|
||||
if (argc > 2) {
|
||||
printf("Error : %s must have one argument\n", argv[0]);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
if (argv[1] == NULL) {
|
||||
printf("Execute child process\n");
|
||||
exit(EXIT_SUCCESS);
|
||||
}
|
||||
else {
|
||||
int x = atoi(argv[1]);
|
||||
printf("Number of children to create : %d\n", x);
|
||||
for (int i=0; i < x ; i++){
|
||||
int pid = fork();
|
||||
if (pid == -1) {
|
||||
printf("Error : %s", strerror(errno));
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
if (pid == 0) {
|
||||
// we are in our children
|
||||
if ((execl(argv[0],"",NULL)) < 0 ) {
|
||||
printf("Error : %s\n", strerror(errno));
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
exit(EXIT_SUCCESS);
|
||||
}
|
35
content/progsys/TDM_3-processus/src/q3bis.c
Normal file
35
content/progsys/TDM_3-processus/src/q3bis.c
Normal file
|
@ -0,0 +1,35 @@
|
|||
#include <stdio.h>
|
||||
#include <unistd.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <errno.h>
|
||||
#include <sys/wait.h>
|
||||
|
||||
int main(int argc, char **argv){
|
||||
if (argv[1] == NULL) {
|
||||
printf("Error : %s must have one argument\n", argv[0]);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
printf("Execute programme number %s\n",argv[1]);
|
||||
int r = atoi(argv[1]) - 1;
|
||||
if (r > 0){
|
||||
int pid = fork();
|
||||
if (pid == -1) {
|
||||
printf("Error : %s", strerror(errno));
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
if (pid == 0) {
|
||||
// we are in our children
|
||||
char args[12] = "";
|
||||
sprintf(args,"%i", r);
|
||||
if ((execl(argv[0], argv[0], args, NULL)) < 0 ) {
|
||||
printf("Error : %s\n", strerror(errno));
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
printf("This is the end!\n");
|
||||
}
|
||||
exit(EXIT_SUCCESS);
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue