Skip to content

Instantly share code, notes, and snippets.

@Dysta
Last active October 16, 2018 11:58
Show Gist options
  • Save Dysta/b73568304be52d3d8d7b2d846702f494 to your computer and use it in GitHub Desktop.
Save Dysta/b73568304be52d3d8d7b2d846702f494 to your computer and use it in GitHub Desktop.
Exercice prog-sys TD4
#include <stdlib.h>
#include <stdio.h>
#include <signal.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <string.h>
// pour tuer le processus faire killall -9 <nom du process>
struct sigaction father_sig, son_sig, old;
int receive = 0;
pid_t pid;
void father(int sig) {
printf("reception du signal : %d : %s\n", sig, strsignal(sig));
//receive = 1;
kill(pid, SIGUSR1);
}
void son(int sig) {
receive = 1;
}
int main(int argc, char const *argv[]) {
father_sig.sa_flags = 0;
sigemptyset(&father_sig.sa_mask);
father_sig.sa_handler = son;
son_sig.sa_flags = 0;
sigemptyset(&son_sig.sa_mask);
son_sig.sa_handler = father;
for ( int i = 1; i < NSIG; i++ ) {
sigaction(i, &son_sig, &old);
}
sigaction(SIGUSR1, &father_sig, &old);
pid = fork();
pid_t ppid = getppid();
if ( pid == 0 ) { // fils
for ( int i = 0; i < atoi(argv[1]); i++) {
for ( int j = 2; argv[j] != '\0'; j++ ) {
receive = 0;
printf("Envoi du signal : %d\n", atoi(argv[j]));
kill(ppid, atoi(argv[j]));
while( ! receive );
}
}
kill(ppid, SIGTERM);
exit(EXIT_SUCCESS);
}
while(1);
//wait(NULL);
//pause();
return EXIT_SUCCESS;
}
#include <stdlib.h>
#include <stdio.h>
#include <signal.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <string.h>
// pour tuer le processus faire killall -9 <nom du process>
struct sigaction father_sig, son_sig, old;
int receive = 0;
pid_t pid;
void father(int sig) {
if ( sig == SIGTERM ) {
printf("Reception du siganl SIGTERM, arrêt\n");
exit(EXIT_SUCCESS);
}
printf("reception du signal : %d : %s\n", sig, strsignal(sig));
//receive = 1;
kill(pid, SIGUSR1);
}
void son(int sig) {
receive = 1;
}
int main(int argc, char const *argv[]) {
if ( argc < 2 ) {
fprintf(stderr, "Usage %s <nb> <sig> <sig> ...\n", argv[0]);
exit(EXIT_FAILURE);
}
// init de la structure father_sig
father_sig.sa_flags = 0;
sigemptyset(&father_sig.sa_mask);
father_sig.sa_handler = son;
// init de la structure son_sig
son_sig.sa_flags = 0;
sigemptyset(&son_sig.sa_mask);
son_sig.sa_handler = father;
// création des masques (question 4.4)
sigset_t m, empty;
sigemptyset(&m);
sigemptyset(&empty);
sigaddset(&m, SIGUSR1);
// On rattrape tout les signaux
for ( int i = 1; i < NSIG; i++ ) {
sigaction(i, &son_sig, &old);
}
// On configure le signal SIGUSR1
sigaction(SIGUSR1, &father_sig, &old);
pid = fork();
pid_t ppid = getppid();
if ( pid == 0 ) { // fils
sigprocmask(SIG_BLOCK, &m, NULL);
for ( int i = 0; i < atoi(argv[1]); i++) {
for ( int j = 2; argv[j] != NULL; j++ ) {
receive = 0;
printf("Envoi du signal : %d\n", atoi(argv[j]));
kill(ppid, atoi(argv[j]));
sigsuspend(&empty);
//while( !receive );
}
}
kill(ppid, SIGTERM);
exit(EXIT_SUCCESS);
}
while(1);
//wait(NULL);
//pause();
return EXIT_SUCCESS;
}
@ArnaudQu
Copy link

Merci bro

@Dysta
Copy link
Author

Dysta commented Oct 16, 2018

Merci bro

De rien frérot

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment