/** Title: * Please write a program to set the SIGINT and sigquit signals, * and in the program to implement the operation of reading information from the file, * and ensure that the file is read and only in the process of reading the file will not be sent by the SIGINT and sigquit signal interrupted. * HINT: block the signal before the file is read and unblock the file after it is read. * */#include<stdio.h>#include<stdlib.h>#include<string.h>#include<unistd.h>#include<errno.h>#include<signal.h>intRead_file (Const Char*path) { if(Path = =NULL) {printf ("param not allow null!\n"); return-1; } FILE*PFR =NULL; PFR= fopen (Path,"R"); if(PFR = =NULL) {printf ("fopen () failed! file Path:%s;error message:%s\n", Path, strerror (errno)); return-1; } Charbuf[1024x768] = {0 }; while(Fgets (BUF,sizeof(BUF), PFR)! =NULL) {printf ("%s", BUF); Sleep (2); memset (BUF,0,sizeof(BUF)); } fclose (PFR); return 0;}voidHandlerintSign ) { if(Sign = =SIGINT) {printf ("Accept SIGINT!\n"); } Else if(Sign = =sigquit) {printf ("Accept Sigquit!\n"); } Else{printf ("Accept Other sign!\n"); }}intMainintArgChar*args[]) { if(Arg <2) {printf ("Print File name!\n"); return-1; } structSigaction Act; Act.sa_handler=handler; //initializing the signal setSigemptyset (&act.sa_mask); Act.sa_flags=0; //Install (Register) SIGINT and Sigquit signals if(Sigaction (SIGINT, &act, NULL)! =0) {printf ("sigaction () failed!\n"); return-1; } if(Sigaction (Sigquit, &act, NULL)! =0) {printf ("sigaction () failed!\n"); return-1; } //Blocking Signalsigset_t bset; //Clear Signal SetSigemptyset (&bset); //adding signals SIGINT and sigquit to the signal setSigaddset (&bset, SIGINT); Sigaddset (&bset, Sigquit); //Change process signal mask status word if(Sigprocmask (Sig_block, &bset, NULL)! =0) {printf ("Sigprocmask () failed!\n"); return-1; } read_file (args[1]); //unblocking if(Sigprocmask (Sig_unblock, &bset, NULL)! =0) {printf ("Sigprocmask () failed!\n"); return-1; } while(1) {pause (); } return 0;}
Linux Linux program Exercise 13 (signal blocking, capturing)