Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include <sys/types.h>
- #include <signal.h>
- /* Najpierw, zainicjujmy licznik Ctrl+C z początkowa wartością 0 */
- int ctrl_c_count = 0;
- #define CTRL_C_THRESHOLD 5
- /* Funkcja uchwytu sygnału Ctrl+C */
- void catch_int(int sig_num)
- {
- sigset_t mask_set; /* używane do ustawiania grupy maski sygnału */
- sigset_t old_set; /* używane do przechowywania starej maski */
- /* ponowne ustawienie uchwytu do sygnału, w celu złapania go nastepnym razem */
- signal(SIGINT, catch_int);
- /* maskuj wszystkie kolejne sygnały dopoki jestesmy w funkcji uchwytu */
- sigfillset(&mask_set);
- sigprocmask(SIG_SETMASK, &mask_set, &old_set);
- /* przy czwartej probie wylaczenie zmienia tryb na SIGHUP */
- if(ctrl_c_count == 3)
- {
- kill(getpid(), SIGHUP);
- }
- ctrl_c_count++;
- /* przywróæ star± maske sygna³u */
- sigprocmask(SIG_SETMASK, &old_set, NULL);
- }
- /* Funkcja uchwytu sygna³u Ctrl+Z */
- void catch_suspend(int sig_num)
- {
- sigset_t mask_set; /* u¿ywane do ustawiania grupy maski sygna³u */
- sigset_t old_set; /* u¿ywane do przechowywania starej maski */
- /* ponowne ustawienie uchwytu do sygna³u, w celu z³apania go nastepnym razem */
- signal(SIGTSTP, catch_suspend);
- /* maskuj wszystkie kolejne sygna³y dopoki jestesmy w funkcji uchwytu */
- sigfillset(&mask_set);
- sigprocmask(SIG_SETMASK, &mask_set, &old_set);
- /* wypisz aktualn± warto¶æ licznika Ctrl+C */
- printf("\n\nDotychczas naliczylem '%d' wcisniec klawiszy Ctrl+C\n\n", ctrl_c_count);
- ctrl_c_count++;
- fflush(stdout);
- /* przywróæ star± maske sygna³u */
- sigprocmask(SIG_SETMASK, &old_set, NULL);
- }
- int main(int argc, char* argv[])
- {
- /* Gdzieś w funkcji main...
- ustaw uchwyty sygnałów Ctrl+C oraz Ctrl+Z */
- signal(SIGINT, catch_int);
- signal(SIGTSTP, catch_suspend);
- /* i dalsza część programu */
- for ( ;; )
- pause();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment