Signal (a)

concept:

Event system in response to certain conditions resulting from the process take appropriate action after receiving the signal.

 

Signal generating mode:

1. keyboard events

Ctrl + c → 2) SIGINT (termination / interruption) "INT" ---- Interrupt

Ctrl + z → 20) SIGTSTP (pause / stop) "T" ---- Terminal terminal.

Ctrl + → 3) SIGQUIT (exit)

2. Hardware instruction exception occurs

  •  In addition to the operation 0 → 8) SIGFPE (floating point exception) "F" ----- float float.
  • Illegal access memory → 11) SIGSEGV (segmentation fault)
  • Bus error → 7) SIGBUS

3. function / command generation signal

kill command to generate a signal: kill -SIGKILL pid

kill function: send the specified signal to the specified process

 

In linux, enter the kill -l, category linux system signals occur:

 

This is all kinds of signals. 1-31 which is a common signal 34 after a real-time signal.

 

Signal processing mode:

    1. Ignore (SIGKILL and SIGSTOP two signals can not be ignored, because he)
    2. Perform default processing mode (default processing mode, the processing mode may be defined by the system by man 7 signal specific view)
    3. Capture and processing. (Mode defined by the user)

 

Signal classification:

    1. Unreliable signal: (1-31)
      the Linux after the inheritance from the signal processing function is finished when the signal processing based on UNIX, early UNIX, the signal is restored to the default processing operation. Although Linux has been improved, but the development of inherited too many things have been impossible to put it back to the default action deleted.

Signals do not line up. At some point, when several signals to pass over and not processed, resulting in some loss of signal.

    1. Reliable signal: (34-64): No signal loss occurs.

 

Registration signal:

void (* Signal ( int Signum, void (PF *) ( int ))) ( int );
 void (PF *) ( int );         // custom function process

Send signal:

kill(pid_t pid, int signum);

pid_t Description:

    • pid> 0: the process signals to a process identifier pid not.
    • pid == 0: The process passed all processes and the same process group of current process
    • pid == -1: transmit broadcast signals to all processes in the system.
    • pid <0: the signal to all processes in a process group identification code is the absolute value of the pid.

 

singnum number representative signal, a reference signal is displayed above the kill -l

 

return value:

He succeeds it returns 0, failure to return -1.

 

error code:

EINVAG: sig argument is not valid.

ESRCH: pid parameters specified process or process group does not exist.

EPERM: enough authority can not be delivered to the specified process.

 

Example one:

Sign up and sends a signal:

/*register.c*/
#include<stdio.h>
#include<unistd.h>
#include<stdlib.h>
#include<signal.h>
#include<sys/types.h>

void handler(int s)
{
    printf("recv %d\n", s);
    exit(0);
}

int main()
{
    signal(SIGUSR1, handler);
    pid_t pid;
    pid = fork();
    if (pid == 0)
    {
        sleep(3);
        the kill (getppid (), SIGUSR1); // getppid () Gets the parent process ID 
    }
     the else 
    { 
        for (;;) 
        { 
            the printf ( " . 1 " ); 
            fflush (stdout); 
            SLEEP ( . 1 ); 
        } 
    } 
    return  0 ; 
}

 

operation result:

personality @ ubuntu: ~ / wangqinghe / signal $ ./register

1111recv 10

Guess you like

Origin www.cnblogs.com/wanghao-boke/p/12116712.html
Recommended