Linux under C Programming: Signal and Sigaction

Source: Internet
Author: User
Tags exit printf linux

To process a signal, you need to give the handler function that the system calls when this signal occurs. The corresponding handler function can be registered for a particular signal (excluding Sigkill and sigstop signals). After registering a processing function for a signal, when the process receives this signal, no matter what state the process is in, it stops the current task to perform the processing function of the signal.

1, registration signal function.

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

Signumber represents a signal that corresponds to a signal processing function. Func is a function pointer. This function has an integer parameter and returns a void type. In fact, Func can also take other values such as: SIG_IGN,SIG_DFL.

Sig_ign says: Ignore the signal that Signumber points out. SIG_DFL represents the calling system default handler function. The return value type of the signal function is the same as the parameter func, a function pointer that points to a return value that is null and has an integer parameter. Its correct return value should be the handler function for the last signal. Error return Sig_err

Signal examples are as follows:

#include <stdio.h>

#include <sys/types.h>

#include <stdlib.h>

#include <signal.h >

void func (int sig)
{
printf ("I get asignal!\n");
}
int main ()
{    charbuffer[100];

   if (Signal (SIGINT, func) = = Sig_err)
     {
     printf ("Signalerror exit now\n");
     Exit (0);
     }
     printf ("pid:%ld\n", (long) getpid ());

   for (;;) 

     {

     fgets (buffer,sizeof (buffer), stdin);

     printf ("bufferis:%s\n", buffer);

     }
 return 0;      
}

Typically, a user process needs to handle 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 handler function.

For SIGINT signals we can use CTRL + C or ctrl+z to interrupt the process to perform SIGINT registered functions.

2, advanced signal processing.

The Linux system provides a more powerful system call.

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

In addition to registering the signal function, this function provides more detailed information about the specific details of the process receiving the signal.

The definition of struct sigaction is as follows: implemented in Linux2.6.39/include/asm-generic/signal.h

struct sigaction     

{ 

     void (*sa_handler) (int);

     void (*sa_sigaction) (int,siginfo_t *,void *);

     Sigset_tsa_mask;

     intsa_flags;

}

Siginfo_t is implemented in Linux2.6.39/include/asm-generic/siginfo.h:

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

Sa_nocldstop: Used to represent signal sigchld, when a subprocess is interrupted, this signal is not generated when and only when the child process ends.

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

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

Sa_nomask: With Sa_nodefer

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

Sa_resethand: With Sa_oneshot

Sa_restart: is a system call that could not be rerun to run automatically rerun.

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

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.