1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
|
============================================================================
Name : Untitled.c
Author : SAMBIA39
Version : 0.1
Copyright : Copyright (c) 11/12/2016 SAMBIA39
Description : Ansi-style
============================================================================
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <assert.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <signal.h>
/*
* Purge zombie
*/
void f_balayette_zombie( int sig ){
fprintf( stdout, "Zombie detécté...\n");
while ( 0 < waitpid(-1, NULL, WNOHANG));
fprintf( stdout, "Zombie Purger\n");
exit( EXIT_FAILURE );
}
int main(int argc, char *argv[]) {
int fds[2];
int val;
int res;
/*
* Mise en place signal de
* Détection de zombie
* et activation de la purge
*/
struct sigaction sig_act;
memset(&sig_act, 0, sizeof(sig_act));
sig_act.sa_flags = 0;
sigemptyset( &(sig_act.sa_mask) );
sig_act.sa_handler = f_balayette_zombie;
if( 0 != sigaction(SIGCHLD, &sig_act, NULL) ){
fprintf( stderr, "Erreur signal\n" );
exit( EXIT_FAILURE );
}
assert(pipe(fds) != -1);
switch (fork()) {
case -1:
exit(EXIT_FAILURE);
case 0:
printf("FILS\n");
sleep(2);
val = 123;
/* on fait crasher */
assert(0);
assert(write(fds[1], &val, sizeof(unsigned int)) == sizeof(unsigned int));
printf("val=%d\n", val);
exit(EXIT_SUCCESS);
default:
printf("Attend que le fils envoie une information\n");
res = read(fds[0], &val, sizeof(unsigned int));
printf("read = %d, val = %d\n", res, val);
}
return 0;
} |
Partager