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) {
if (argc != 2) {
fprintf(stderr, "Usage: %s <N>\n", argv[0]);
exit(1);
}
int N = atoi(argv[1]);
if (N <= 0) {
fprintf(stderr, "N doit être un entier positif.\n");
exit(1);
}
pid_t pid; pid_t pid;
int time = 2; int etat;
status = 0; struct timespec t;
int fils_id = 1; // Compteur pour l'identité des fils
for (int i = 1; i <= N; i++) {
switch (pid = fork()) { switch (pid = fork()) {
case -1:
perror("fork");
exit(errno);
case -1 : // Oups !!! fork n'a pas marché ! case 0:
perror("fork"); exit(errno); printf("[fils %d]: je suis le fils de pid %d\n", fils_id, getpid());
for (int j = 0; j < 10; j++) {
case 0 : // Code du fils printf("[fils %d]: %d\n", fils_id, j);
printf("je suis le fils de %d\n", getppid()); t.tv_sec = 0;
for(int i = 0; i <= 9; ++i) { t.tv_nsec = i*500000000;
printf("%d \n", i); //\n ==> signale pour dire qu'il faut afficher à l'écran nanosleep(&t, NULL);
usleep(500000);
} }
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