1) avec kill -l,
2) dans /usr/include (man signal)
Dernière mise à jour sur le site le 16 décembre 2019 - Téléchargement
/*
* ######
* ## ps_sig1.c
* ## ~~~~~~~~~
* ## 04.02.1995: Creation TL
* ## 07.05.2018:
* ##
* ## Gestion des interuptions
* ## Detournement des IT : SIGINT, SIGUSR1, SIGUSR2
* ##
* ## Voir le man de la commande kill
* ##
* ######
*/
#include <stdio.h>
#include <sys/signal.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
typedef void (*sighandler_t) (int);
void fonc1() { printf("\n PS_SIG1> Reception signal SIGUSR1\n"); }
void fonc2() { printf("\n PS_SIG1> Reception signal SIGUSR2\n"); }
int main()
{
signal( SIGUSR1, (sighandler_t) fonc1 );
signal( SIGUSR2, (sighandler_t) fonc2 );
signal( SIGINT , SIG_IGN );
for (;;) {
putchar('\n'); putchar('*'); sleep(1);
}
}
/*-- fin ps_sig1.c --*/
Dernière mise à jour sur le site le 16 décembre 2019 - Téléchargement
/*
* ######
* ## ps_sig2.c
* ## ~~~~~~~~~
* ## 04.02.1995: Creation TL
* ## 07.05.2018:
* ######
*/
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
int main( argc, argv )
int argc; char **argv;
{
int pidf1, code_retour;
printf( " PS_SIG2> argc = %d\n", argc );
if ( argc == 0 ) {
exit( 1 );
}
/*
* Conversion de l'argument (ascii) en integer
* Dans certains systeme, la conversion genere une erreur
* provoquant l'arret premature du programe
*
* Essayer : ./ps_sig2 abcd
*/
pidf1 = atoi( argv[ argc-1 ] );
if ( pidf1 == 0 ) {
printf( " PS_SIG2> %s : argument incorrect\n", argv[ argc-1 ] );
exit( 2 );
}
printf( " PS_SIG2> argv[ arc-1 ] = %s\n", argv[ 0 ] );
printf( " PS_SIG2> PID de l'autre FILS = %d\n", pidf1 );
sleep( 5 );
code_retour = kill( pidf1, SIGUSR1 );
if ( code_retour == -1 ) {
perror( "FILS2(usr1): " );
}
sleep( 2 );
code_retour = kill( pidf1, SIGINT );
if ( code_retour == -1 ) {
perror( "FILS2(int): " );
}
sleep( 3 );
code_retour = kill( pidf1, SIGUSR2 );
if ( code_retour == -1 ) {
perror( "FILS2(usr2): " );
}
for (;;) {
putchar( '\n' ); putchar( '~' ); sleep( 1 );
}
}
/*-- fin ps_sig2 --*/
Dernière mise à jour sur le site le 16 décembre 2019 - Téléchargement
/*
* ######
* ## ps_sig3.c
* ## ~~~~~~~
* ## 04.02.1995: Creation TL
* ## 07.05.2018:
* ######
*/
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <wait.h>
int main()
{
int pid, pid1, pid2; char arg1[ 256 ]; char arg2[ 256 ];
int status;
/* Creation Fils 1 */
pid1 = fork();
if ( pid1 == -1 ) {
printf(" \n PS_SIG3> Creation FILS( 1 ) impossible\n ");
exit( 1 );
}
/* Instructions pour le fils 1 */
if ( pid1 == 0 ) {
sprintf( arg1, "%d\n", getpid() ); // UNUSED
execl( "ps_sig1", arg1, NULL );
/*
* Que se passe-t-uil en cas de succes ou d'erreur
* de l'appel de execl ?
* Faut-il mettre le PathName de la commande ?
*/
exit(3);
}
/* Creation Fils 2 */
pid2 = fork();
if ( pid2 == -1 ) {
printf("\n PS_SIG3> Creation FILS( 2 ) impossible\n" );
exit( 2 );
}
/* Instructions pour le fils 2 */
if ( pid2 == 0 ) {
sprintf( arg2, "%d\n", pid1 ); // PID du FRERE
execl( "ps_sig2", arg2, NULL );
/*
* Que se passe-t-uil en cas de succes ou d'erreur
* de l'appel de execl ?
* Faut-il mettre le PathName de la commande ?
*/
exit( 4 );
}
sleep( 10 );
/* Instructions du Pere */
pid = wait( &status );
if ( pid == pid1 ) {
printf( "\nPS_SIG3> FILS( 1 ) arrete par signal %d\n", status );
/* On demande l'arret de l'autre Fils */
if ( ( kill( pid2, SIGTERM ) ) == -1) {
perror( "\n PS_SIG3> Pb. kill (1)" );
}
} else if ( pid == pid2 ) {
printf("\n PS_SIG3> FILS( 2 ) arrete par signal %d\n", status );
/* On demande l'arret de l'autre Fils */
if ( ( kill( pid1, SIGTERM ) ) == -1) {
perror( "\n PS_SIG3> Pb. kill (2)" );
}
}
/* On attend l'arret de l'autre Fils */
pid = wait( &status );
if ( pid == pid1 ) {
printf("\n PS_SIG3> FILS( 1 ) arrete par signal %d\n", status );
} else if ( pid == pid2 ) {
printf("\n PS_SIG3> FILS( 2 ) arrete par signal %d\n", status );
}
exit( 0 );
}
/* --- fin ps_sig3 --- */
Ce Makefile prend en compte les fichiers feenable.c et sigaction.c