Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
AyatoKirishima authored Dec 14, 2021
1 parent 3b6c293 commit 0cbbc70
Show file tree
Hide file tree
Showing 6 changed files with 134 additions and 0 deletions.
Binary file added TD4/ex1
Binary file not shown.
29 changes: 29 additions & 0 deletions TD4/ex1.c
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;
}
Binary file added TD4/ex2
Binary file not shown.
38 changes: 38 additions & 0 deletions TD4/ex2.c
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;
}
Binary file added TD4/ex3
Binary file not shown.
67 changes: 67 additions & 0 deletions TD4/ex3.c
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;
}

0 comments on commit 0cbbc70

Please sign in to comment.