add systeme's tp

master
Antoine PEREDERII 2 years ago
parent c27ba2958c
commit bac90bf2c8

Binary file not shown.

Binary file not shown.

@ -4,35 +4,63 @@
#include <unistd.h> #include <unistd.h>
#include <errno.h> #include <errno.h>
#include <time.h> #include <time.h>
#include <sys/wait.h>
int main(int argc, char **argv) { int main(int argc, char **argv) {
pid_t pid; if (argc != 2) {
int time = 2; fprintf(stderr, "Usage: %s <N>\n", argv[0]);
status = 0; exit(1);
}
int N = atoi(argv[1]);
switch(pid=fork()) { if (N <= 0) {
fprintf(stderr, "N doit être un entier positif.\n");
exit(1);
}
case -1 : // Oups !!! fork n'a pas marché ! pid_t pid;
perror("fork"); exit(errno); int etat;
struct timespec t;
int fils_id = 1; // Compteur pour l'identité des fils
case 0 : // Code du fils for (int i = 1; i <= N; i++) {
printf("je suis le fils de %d\n", getppid()); switch (pid = fork()) {
for(int i = 0; i <= 9; ++i) { case -1:
printf("%d \n", i); //\n ==> signale pour dire qu'il faut afficher à l'écran perror("fork");
usleep(500000); exit(errno);
}
system("ps -f");
exit(2);
default : // Code du père case 0:
pid = wait(&status); printf("[fils %d]: je suis le fils de pid %d\n", fils_id, getpid());
printf("Mon fils m'a renvoyé le code de retour %d\n", WEXITSTATUS(status); for (int j = 0; j < 10; j++) {
printf("Fin du processus père de pid %d.\n", getpid()); printf("[fils %d]: %d\n", fils_id, j);
} t.tv_sec = 0;
t.tv_nsec = i*500000000;
nanosleep(&t, NULL);
}
system("ps -f");
exit(2);
default:
fils_id++; // Incrémente le compteur pour le prochain fils
break;
}
}
for (int i = 0; i < N; i++) {
if ((pid = wait(&etat)) == -1) {
perror("pb avec le wait");
exit(errno);
}
if (WIFEXITED(etat))
printf("[pere]: mon fils %d a retourne le code %d\n", pid, WEXITSTATUS(etat));
else
printf("[pere]: mon fils %d s est mal termine\n", pid);
}
exit(0); printf("[pere]: Fin du processus pere de pid %d.\n", getpid());
exit(0);
} }

Loading…
Cancel
Save