#define _XOPEN_SOURCE #define _XOPEN_SOURCE_EXTENDED #include #include #include #include #include #include void myhandler(int s) { printf("[pid %d] signal %d received\n", getpid(), s); } int main() { printf("papa = %d\n", getpid()); struct sigaction act; act.sa_handler = myhandler; act.sa_flags = 0; sigemptyset(&act.sa_mask); sigaction(SIGINT, &act, NULL); // mise en place d'un handler pour ctrl-c (hérité par le fils) signal(SIGTTOU, SIG_IGN); // ignorer ce signal qui survient quand papa repasse en foreground... int fils = fork(); if(fils == 0) { /* FILS */ printf("fils = %d\n", getpid()); char c; while(read(0, &c, 1) > 0) { write(1, &c, 1); } // commande "cat" return EXIT_SUCCESS; } else { /* PAPA */ setpgid(fils, 0); // (1) le père crée un nouveau groupe pour son fils tcsetpgrp(0, fils); // (2) le groupe du fils devient le "terminal foreground process group" wait(NULL); tcsetpgrp(0, getpgid(0)); // (3) le pére redevient le "terminal foreground process group" et reçoit un SIGTTOU return EXIT_SUCCESS; } return EXIT_FAILURE; }