Linux programming _ 10 _ signal and linux programming _ 10 Signal

Source: Internet
Author: User

Linux programming _ 10 _ signal and linux programming _ 10 Signal

The signal processing mechanism is very important in linux programming. It is similar to the Interrupt System in single chip microcomputer. When writing an interrupt function, we need to set the address of the interrupt function and set its registers, in order to jump to the interrupt function correctly for execution when an interrupt event occurs;

The linux CITIC number is similar to this. The general programming model is to define the interrupt function, and then register the interrupt function so that when the process receives a specific signal, it can jump to the signal processing function for execution;


1. kill and raise Functions

The kill function is used to send signals to processes or process groups;

-- Int kill (pid_t pid, int signo );

The raise function allows a process to send signals to itself;

-- Int raise (int signo );

Call raise (signo); equivalent to kill (getpid (), signo );


The kill command is not only a kill function, but also used to send signals:

For example:

Kill-l -- list all signals

Kill-s 10 pid -- 10 represents SIGUSR1 pid represents the process to which the USR1 signal is sent

You can also write it like this: kill-USR1 pid


Return to the kill function;

Note: The first parameter of the kill function:



The following is an example of using the kill function:

#include <stdio.h>#include <stdlib.h>#include <signal.h>pid_t pid;void fun(int a){    if(a == SIGABRT)        printf("Have fun!\n");    if(a == SIGUSR1)    {        printf("Fuck!\n");    }}void send(){    kill(pid, SIGUSR1);// Only send msg to progress or progress group!}int main(){    int cnt = 0;    signal(SIGABRT, fun);    signal(SIGUSR1, fun);    pid = fork();    if(pid == 0)    {        while(1)        {            printf("Child!\n");            sleep(1);        }    }    while(1)    {        printf("Father!\n");        cnt++;        if(cnt == 5)            send();        sleep(1);    }}


2. alarm and pause Functions

The alarm function is used to set the timer. a sigalrm signal is generated when a specified time-out occurs. If the signal is not captured, the process that calls the alarm function is terminated by default.

-- Unsigned int alarm (unsigned int seconds );

Pause FunctionThe process hangs until a signal is captured..

-- Int pause (void );

Instance:

#include <signal.h>#include <stdio.h>void fun(int id){    if(id == SIGUSR1)    {        printf("SIGUSR1\n");    }    if(id == SIGALRM)    {        printf("SIGALRM!\n");    }}int main(){    signal(SIGUSR1, fun);    signal(SIGALRM, fun);    alarm(5);   // if not catch, progress return;    for(;;)        pause();    return 0;}

3. Signal Set

A signal set that can contain multiple signals. The data type is sigset_t, and corresponding signal set functions are also provided;



There are also the following functions:








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.