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);
}
switch(pid=fork()) { int N = atoi(argv[1]);
case -1 : // Oups !!! fork n'a pas marché ! if (N <= 0) {
perror("fork"); exit(errno); fprintf(stderr, "N doit être un entier positif.\n");
exit(1);
}
pid_t pid;
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);
case 0:
printf("[fils %d]: je suis le fils de pid %d\n", fils_id, getpid());
for (int j = 0; j < 10; j++) {
printf("[fils %d]: %d\n", fils_id, j);
t.tv_sec = 0;
t.tv_nsec = i*500000000;
nanosleep(&t, NULL);
} }
system("ps -f"); system("ps -f");
exit(2); exit(2);
default : // Code du père default:
pid = wait(&status); fils_id++; // Incrémente le compteur pour le prochain fils
printf("Mon fils m'a renvoyé le code de retour %d\n", WEXITSTATUS(status); break;
printf("Fin du processus père de pid %d.\n", getpid()); }
} }
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);
}
printf("[pere]: Fin du processus pere de pid %d.\n", getpid());
exit(0); exit(0);
} }

Loading…
Cancel
Save