Skip to content

Commit 0cbbc70

Browse files
Add files via upload
1 parent 3b6c293 commit 0cbbc70

File tree

6 files changed

+134
-0
lines changed

6 files changed

+134
-0
lines changed

TD4/ex1

23.9 KB
Binary file not shown.

TD4/ex1.c

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/* Proposer un programme c qui attrape tous les signaux. Dès qu’il attrape un signal, il
2+
* affiche le signal reçu.
3+
* Utiliser la commande kill dans le terminal sous le format suivant : kill –signal pid. (Essayez les
4+
* commandes ctrl+c, ctrl+z, de redimensionner la fenêtre...)*/
5+
#include <signal.h>
6+
#include <stdio.h>
7+
#include <stdlib.h>
8+
#include <unistd.h>
9+
10+
void capturerSignal(const int sig)
11+
{
12+
printf("\n Signal %d reçu\n", sig);
13+
}
14+
15+
int main(int argc, char const *argv[])
16+
{
17+
printf("Voici mon pid : %d\n", getpid());
18+
19+
for (int i = 1; i < NSIG; ++i)
20+
{
21+
if (signal(i, capturerSignal) == SIG_ERR)
22+
{
23+
printf("Erreur attente signal %d\n", i);
24+
}
25+
}
26+
while (1);
27+
28+
return 0;
29+
}

TD4/ex2

24.1 KB
Binary file not shown.

TD4/ex2.c

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/* Proposer un programme c qui demande un pid (entier) et envoie tous les signaux au premier programme */
2+
#include <signal.h>
3+
#include <stdio.h>
4+
#include <stdlib.h>
5+
#include <unistd.h>
6+
7+
int pid;
8+
9+
void lirePid()
10+
{
11+
printf("Saisir un PID\n");
12+
scanf("%d", &pid);
13+
}
14+
15+
void envoyerSignal(const int signal)
16+
{
17+
printf("\nEnvoi du signal %d\n", signal);
18+
kill(pid, signal);
19+
}
20+
21+
int main(int argc, char const *argv[])
22+
{
23+
lirePid();
24+
25+
for (int signal = 1; signal < NSIG; signal++)
26+
{
27+
if (signal != 9)
28+
{
29+
envoyerSignal(signal);
30+
}
31+
sleep(1);
32+
}
33+
34+
// ceci pour tuer l'exercice 1
35+
envoyerSignal(9);
36+
37+
return EXIT_SUCCESS;
38+
}

TD4/ex3

24.2 KB
Binary file not shown.

TD4/ex3.c

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/* Donner un programme c qui génère n fils (n est passé en argument à la commande). Chaque fils écrit
2+
* son PID ainsi que son numéro de création toutes les secondes. Le père tue tous ses fils après 5
3+
* secondes. (Signal 9) */
4+
#include <signal.h>
5+
#include <stdio.h>
6+
#include <stdlib.h>
7+
#include <sys/wait.h>
8+
#include <unistd.h>
9+
10+
void tuerFils(const int pid)
11+
{
12+
printf("On tue le fils %d\n", pid);
13+
kill(pid, 9);
14+
}
15+
16+
void tuerLesFils(const int fils[], const int nombre)
17+
{
18+
for (int i = 0; i < nombre; ++i)
19+
{
20+
tuerFils(fils[i]);
21+
}
22+
}
23+
24+
int main(int argc, char const *argv[])
25+
{
26+
if (argc != 2)
27+
{
28+
printf("On attend 1 argument\n");
29+
exit(EXIT_FAILURE);
30+
}
31+
32+
int nombre = atoi(argv[1]);
33+
int fils[nombre];
34+
35+
for (int i = 0; i < nombre; i++)
36+
{
37+
switch (fils[i] = fork())
38+
{
39+
case -1:
40+
printf("Erreur fork. Arret du programme.\n");
41+
exit(EXIT_FAILURE);
42+
case 0:
43+
while (1)
44+
{
45+
printf("Le fils %d (%d), j'attends\n", i + 1, getpid());
46+
sleep(1);
47+
}
48+
}
49+
}
50+
51+
sleep(4);
52+
53+
// on teste si on est le père, normalement pas besoin
54+
if (fils[0] != 0)
55+
{
56+
tuerLesFils(fils, nombre);
57+
}
58+
59+
for (int j = 0; j < nombre; ++j)
60+
{
61+
waitpid(fils[j], NULL, 0);
62+
}
63+
64+
printf("Je suis le père. Fin du programme\n");
65+
66+
return EXIT_SUCCESS;
67+
}

0 commit comments

Comments
 (0)