Linux Signal Processing 1. linux Signal Processing

Source: Internet
Author: User

Linux Signal Processing 1. linux Signal Processing
Function prototype

NAME       signal - ANSI C signal handlingSYNOPSIS       #include <signal.h>       typedef void (*sighandler_t)(int);       sighandler_t signal(int signum, sighandler_t handler);

The signal function has the registration function and does nothing. It only tells the system to handle the signal signum in handler mode. This function is called only when a signal is sent. If the signal does not come, this function will never be called.

You can request the kernel to generate a signal by entering CTRL + c, Ctrl + \, or any other key that the Terminal Driver assigns to the signal control character.

Ctrl + c --> 2) SIGINT
Ctrl + \ --> 3) SIGQUIT

Both of the above can cause the program to exit.

Code
/*************************************************************************    > File Name: hello.c    > Author: KrisChou    > Mail:zhoujx0219@126.com     > Created Time: Mon 25 Aug 2014 09:25:15 AM CST ************************************************************************/#include <stdio.h>#include <stdlib.h>#include <string.h>#include <unistd.h>#include <sys/stat.h>#include <sys/types.h>#include <fcntl.h>#include <assert.h>#include <signal.h>#include <sys/select.h>void handler(int num){    printf("sig_num: %d \n", num);}int main(int argc, char* argv[]){    char buf[1024];    signal(2, handler);    int iret ;    fd_set read_set, ready_set ;    FD_ZERO(&read_set);    FD_SET(0, &read_set);    while(1)    {        ready_set = read_set ;        iret = select(1, &ready_set, NULL, NULL, NULL);        if(iret == 0)        {            continue ;        }else if(iret == -1)        {            perror("select");            continue ;        }else         {            iret = read(0, buf, 1024);            buf[iret] = '\0';            write(1, buf, strlen(buf));        }    }

After running the program, press ctrl + c. The result is as follows:

[purple@localhost review]$ gcc test.c[purple@localhost review]$ ./a.out^Csig_num: 2select: Interrupted system callhello world!hello world!

Because SIGINT has been registered through signal (2, handler);, it is clear that after pressing ctrl + c, the signal processing function handler will naturally capture this signal, therefore, ^ Csig_num: 2 is displayed.

The select function processes the interrupt signal. When an interruption signal is received,-1 is returned. Therefore, select: Interrupted system call select is displayed.

NOTE: In the case of 3, the select function will return, respectively: the polling time will return at the point. After receiving the signal, the system returns the result. If the descriptor has activity, it will return.

(In addition, read is a blocking function. When a signal is received, the system processes the signal first, and then continues to wait .)

Then the program runs. If there is input, select returns 1. If there is input, print it. Press ctrl + d, select returns 0, and the program continues to run.


Linux signal processing functions

This cannot be restored to the original site, which is the core of the modern computer interruption mechanism.

Linux signal processing function definition, do not understand, Masters help

P (void) should be a macro and a function pointer. For example, deifine P (Y) (Y y) where Y is the passed parameter type

Static void failure P (void) = static void failure (void), is a function name

Static void failure P (void) is the same

Void qsignal (sig, action) const int sig; void (* action) P (void); // defines the function qsignal, but I don't feel like this, it should be written as void qsignal (const int sig, void (* action) P (void); of course, it depends on your compiler. If you are using a single-chip compiler, there may also be such a writing method, which is actually
First
I affirmed the function prototype. I remember this method in C Language 89, which seems to have been used to describe the type of the parameter.
Qsignal (SIGINT, failure); it seems to be qsignal (SIGINT, & failure );

Let's see what else you don't understand.

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.