progsys:index
Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| progsys:index [2018/10/18 13:13] – [Envoyer & Recevoir Signaux] orel | progsys:index [2024/03/18 15:06] (current) – external edit 127.0.0.1 | ||
|---|---|---|---|
| Line 977: | Line 977: | ||
| </ | </ | ||
| + | ==== Ctrl-C ==== | ||
| + | |||
| + | Un processus fils est dans le même PGID (process group ID) que son père par defaut (pgid père = pgid fils = pid père). Un kill sur -pid du père envoie le signal à tous le groupe, donc au fils également... | ||
| + | |||
| + | <code C sigint.c> | ||
| + | #include < | ||
| + | #include < | ||
| + | #include < | ||
| + | #include < | ||
| + | #include < | ||
| + | |||
| + | void handler(int sig) | ||
| + | { | ||
| + | printf(" | ||
| + | } | ||
| + | |||
| + | int main(void) | ||
| + | { | ||
| + | signal(SIGINT, | ||
| + | |||
| + | if (fork() == 0) | ||
| + | { | ||
| + | printf(" | ||
| + | pause(); | ||
| + | printf(" | ||
| + | exit(EXIT_SUCCESS); | ||
| + | } | ||
| + | |||
| + | printf(" | ||
| + | pause(); | ||
| + | printf(" | ||
| + | return EXIT_SUCCESS; | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | |||
| + | ==== Timeout ==== | ||
| + | |||
| + | <code C executer-avant-delai.c> | ||
| + | |||
| + | #define _GNU_SOURCE | ||
| + | #include < | ||
| + | #include < | ||
| + | #include < | ||
| + | #include < | ||
| + | |||
| + | static struct sigaction sa, old; | ||
| + | static sigjmp_buf env; | ||
| + | |||
| + | static void myalarm(int sig) | ||
| + | { | ||
| + | printf(" | ||
| + | siglongjmp(env, | ||
| + | } | ||
| + | |||
| + | int executer_avant_delai( void (*fun)(void *), void *parametre, int delai_en_seconde) | ||
| + | { | ||
| + | int ret = 1; | ||
| + | sa.sa_handler = myalarm; | ||
| + | sa.sa_flags = 0; // SA_RESETHAND; | ||
| + | sigemptyset(& | ||
| + | sigaction(SIGALRM, | ||
| + | |||
| + | alarm(delai_en_seconde); | ||
| + | if(sigsetjmp(env, | ||
| + | fun(parametre); | ||
| + | else | ||
| + | ret = 0; // alarm | ||
| + | alarm(0); | ||
| + | sigaction(SIGALRM, | ||
| + | return ret; | ||
| + | } | ||
| + | </ | ||
progsys/index.1539868390.txt.gz · Last modified: 2024/03/18 15:05 (external edit)
