-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3b6c293
commit 0cbbc70
Showing
6 changed files
with
134 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
/* Proposer un programme c qui attrape tous les signaux. Dès qu’il attrape un signal, il | ||
* affiche le signal reçu. | ||
* Utiliser la commande kill dans le terminal sous le format suivant : kill –signal pid. (Essayez les | ||
* commandes ctrl+c, ctrl+z, de redimensionner la fenêtre...)*/ | ||
#include <signal.h> | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <unistd.h> | ||
|
||
void capturerSignal(const int sig) | ||
{ | ||
printf("\n Signal %d reçu\n", sig); | ||
} | ||
|
||
int main(int argc, char const *argv[]) | ||
{ | ||
printf("Voici mon pid : %d\n", getpid()); | ||
|
||
for (int i = 1; i < NSIG; ++i) | ||
{ | ||
if (signal(i, capturerSignal) == SIG_ERR) | ||
{ | ||
printf("Erreur attente signal %d\n", i); | ||
} | ||
} | ||
while (1); | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/* Proposer un programme c qui demande un pid (entier) et envoie tous les signaux au premier programme */ | ||
#include <signal.h> | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <unistd.h> | ||
|
||
int pid; | ||
|
||
void lirePid() | ||
{ | ||
printf("Saisir un PID\n"); | ||
scanf("%d", &pid); | ||
} | ||
|
||
void envoyerSignal(const int signal) | ||
{ | ||
printf("\nEnvoi du signal %d\n", signal); | ||
kill(pid, signal); | ||
} | ||
|
||
int main(int argc, char const *argv[]) | ||
{ | ||
lirePid(); | ||
|
||
for (int signal = 1; signal < NSIG; signal++) | ||
{ | ||
if (signal != 9) | ||
{ | ||
envoyerSignal(signal); | ||
} | ||
sleep(1); | ||
} | ||
|
||
// ceci pour tuer l'exercice 1 | ||
envoyerSignal(9); | ||
|
||
return EXIT_SUCCESS; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
/* Donner un programme c qui génère n fils (n est passé en argument à la commande). Chaque fils écrit | ||
* son PID ainsi que son numéro de création toutes les secondes. Le père tue tous ses fils après 5 | ||
* secondes. (Signal 9) */ | ||
#include <signal.h> | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <sys/wait.h> | ||
#include <unistd.h> | ||
|
||
void tuerFils(const int pid) | ||
{ | ||
printf("On tue le fils %d\n", pid); | ||
kill(pid, 9); | ||
} | ||
|
||
void tuerLesFils(const int fils[], const int nombre) | ||
{ | ||
for (int i = 0; i < nombre; ++i) | ||
{ | ||
tuerFils(fils[i]); | ||
} | ||
} | ||
|
||
int main(int argc, char const *argv[]) | ||
{ | ||
if (argc != 2) | ||
{ | ||
printf("On attend 1 argument\n"); | ||
exit(EXIT_FAILURE); | ||
} | ||
|
||
int nombre = atoi(argv[1]); | ||
int fils[nombre]; | ||
|
||
for (int i = 0; i < nombre; i++) | ||
{ | ||
switch (fils[i] = fork()) | ||
{ | ||
case -1: | ||
printf("Erreur fork. Arret du programme.\n"); | ||
exit(EXIT_FAILURE); | ||
case 0: | ||
while (1) | ||
{ | ||
printf("Le fils %d (%d), j'attends\n", i + 1, getpid()); | ||
sleep(1); | ||
} | ||
} | ||
} | ||
|
||
sleep(4); | ||
|
||
// on teste si on est le père, normalement pas besoin | ||
if (fils[0] != 0) | ||
{ | ||
tuerLesFils(fils, nombre); | ||
} | ||
|
||
for (int j = 0; j < nombre; ++j) | ||
{ | ||
waitpid(fils[j], NULL, 0); | ||
} | ||
|
||
printf("Je suis le père. Fin du programme\n"); | ||
|
||
return EXIT_SUCCESS; | ||
} |