Linux Inter-process communication-signal

Source: Internet
Author: User

First, the signal is a simulation of the interrupt mechanism at the software level, in principle, a process receives a signal and the processor receives an interrupt request can be said to be the same. The signal is asynchronous, and a process does not have to wait for the signal to arrive by any action, in fact, the process does not know exactly when the signal arrives.

ii. Types of signalsThe name of the signal is defined in the header file signal.h, and the signal is entered kill-l the command line at the beginning of the sig to see all the signals. Third, process response to the signal

A process can respond to a signal in three ways:

(1) Ignore the signal, that is, the signal does not do any processing, wherein, there are two signals can not be ignored: Sigkill and sigstop;

(2) Capture the signal. The signal processing function is defined and the corresponding processing function is executed when the signal occurs;

(3) Perform the default operation, and Linux specifies the default action for each type of signal. Note that the default response of a process to a real-time signal is a process termination.

Which of the three ways Linux responds to a signal depends on the parameters passed to the corresponding API function.

Iv. Transmission of Signals

The main functions for sending signals are: Kill (), raise (), Sigqueue (), Alarm (), Setitimer (), and Abort ().

Kill (int pid, int signl) can either send a signal to itself or send a signal to another process

Raise (int signl) can only send signals to the process itself.

Alarm () sends itself a SIGALRM signal after a specified time, and if this signal is not captured, the process is terminated by default.

unsigned int alarm (unsigned int seconds); After how many seconds

Pause () suspends the process until it snaps to a signal.

V. Processing of Signals

sighandler_t signal (int signum, sighandler_t handler);

Signal (SIGINT, my_func);

The first parameter specifies the value of the signal,

The second parameter specifies the processing of the preceding signal value, which can be ignored (the parameter set to Sig_ign) can be processed by the system default (parameter set to SIG_DFL), or you can implement the processing method (parameter specifies a function address) my_func ().

If the signal () call succeeds, returns the handler value of the last Call to signal () for the installation signal Signum, and the failure to return Sig_err.

  

#include <signal.h>#include<stdio.h>#include<stdlib.h>voidMy_func (intsign_no) {    if(sign_no==SIGINT) printf ("I have get sigint\n"); Else if(sign_no==sigquit) printf ("I have get sigquit\n");}intMain () {printf ("waiting for signal SIGINT or sigquit \ n"); /*registering signal processing functions*/signal (SIGINT, my_func);        Signal (Sigquit, my_func);    Pause (); Exit (0);}

Linux Inter-process communication-signal

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.