Linux Linux program Exercise 13 (signal blocking, capturing)

Source: Internet
Author: User

/** 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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.