Linux C Programming Learning-signal processing

Source: Internet
Author: User
Tags stdin

Signal processing is a feature of Linux programs. Signal processing is used to simulate the interrupt function of the operating system. To use the signal processing function, all you have to do is fill out a signal processing function.

1#include <stdio.h>2#include <sys/types.h>3#include <stdlib.h>4#include <signal.h>5   6 intFlag =1; 7   8 voidFuncintSig)9 {   Tenprintf"I get a signal!\n");  OneFlag =0;  A }     -    - intMain () the {     - signal (SIGINT, func);  -printf"pid:%ld\n",(Long) Getpid ());  -    +      while(flag) - pause ();  +    A     return 0;  at}

Perform:

#gcc sig.c-o sig    #. /Sig    on another terminal:     #kill333//

To process a signal, you need to give the processing function called by the system when this signal occurs. The corresponding processing function can be registered for a specific signal (except for the Sigkill and Sigstop signals). After registering a signal's handler function, when the process receives this signal, no matter what state the process is in, it will stop the current task to execute the signal processing function.

1. Register the Signal function

#include <signal.h>    void(*signal (int signumber,void (*func) (int)) ( int)  

The signumber represents the signal that the signal processing function corresponds to. Func is a function pointer. This function has an integer parameter and returns a void type. In fact, Func can also take other fixed values such as: SIG_IGN,SIG_DFL. Sig_ign said: Ignore the signal indicated by Signumber. The SIG_DFL represents a call to the system's default handler function. The return value type of the signal function is the same as the parameter func, which is a pointer to a function that has a null return value with an integer argument. Its correct return value should be the last time the signal was processed. Error returned SIG_ERR.

The signal example is as follows:

1#include <stdio.h>2#include <sys/types.h>3#include <stdlib.h>4#include <signal.h>5 6 voidFuncintSig)7 {  8printf"I Get asignal!\n"); 9 }   Ten  One intMain () A {     -charbuffer[ -];  -    the    if(Signal (SIGINT, func) = =Sig_err) -      {   -printf"Signalerror exit now\n");  -Exit0);  +      }   -printf"pid:%ld\n",(Long) Getpid ());  +    A     for(;;)  at      {   -Fgets (Buffer,sizeof(buffer), stdin);  -printf"bufferis:%s\n", buffer);  -      }   -      return 0;  -}

Typically, a user process needs to process multiple signals. You can register multiple signal processing functions in one program. A signal can correspond to a processing function, while multiple signals can correspond to a processing function. For SIGINT signals we can use CTRL + C or CTRL + Z to interrupt the process to execute SIGINT registered functions.

2. Advanced Signal Processing

A more powerful system call is provided on the Linux system.

#include <signal.h>    int sigaction (int signumbet,const structsigaction *act, struct sigaction *oldact)

This function, in addition to registering the signal function, provides more detailed information about the exact details of the process receiving the signal. The struct sigaction is defined as follows:

struct sigaction    {         void(*sa_handler) (int);          void (*sa_sigaction) (int, siginfo_t *,void *);         Sigset_tsa_mask;         intsa_flags;  }  

The value of Sa_flags is the following table, with 0 indicating all default options are selected.

Sa_nocldstop: Used to represent the signal sigchld, which does not produce this signal when the child process is interrupted, and only when the child process has ended.

Sa_nocldwati: When the signal is SIGCHLD, the child process can be avoided zombie.

Sa_nodefer: When the signal processing function is in progress, do not block the signal processing function itself signal function.

Sa_nomask: With Sa_nodefer

Sa_oneshot: When the signal processing function of the user registration is executed once, the processing function of the signal is set to the system default handler function.

Sa_resethand: With Sa_oneshot

Sa_restart: A system call that could not be rerun is automatically rerun.

Sa_siginfo: Indicates that the signal processing function is specified by sa_sigaction and not by Sa_handler, it will display more signal processing function information.

In fact Sinaction can completely replace the signal function:

1#include <stdio.h>2#include <sys/types.h>3#include <stdlib.h>4#include <signal.h>5     6 voidFuncintSig)7 {    8printf"I get a signal!\n"); 9 }    Ten    One intMain () A {    -     Charbuffer[ -];  -    the     structSigaction Act;  -Act.sa_handler=func;  -Sigemptyset (&act.sa_mask);  -Act.sa_flags =0;  +    -     if(Sigaction (sigint,&act, NULL) = =-1)   +     {   Aprintf"sigaction Error Exit now\n");  atExit0);  -     }   -    -printf"pid:%ld\n",(Long) Getpid ());  -    -      for(;;)  in     {   -Fgets (Buffer,sizeof(buffer), stdin);  toprintf"Buffer is:%s\n", buffer);  +     }   -    the     return 0;  *}

Linux C Programming Learning-signal processing

Related Article

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.