Signal communication between Linux interprocess communication

Source: Internet
Author: User
Tags posix signal handler

This article refers to the following:

http://blog.csdn.net/yikai2009/article/details/8643818

Http://blog.chinaunix.net/uid-1877180-id-3011232.html


The function of the sigaction function is to inspect or modify the processing action associated with the specified signal (both operations can be done simultaneously).

He is the POSIX signal interface, and signal () is the standard C signal interface (this interface should be used if the program must be running on a non-POSIX system)

A new signal processing function is set for the Signal Signum Act, while preserving the original signal processing function of the signal oldact

int sigaction ( int signo Const struct sigaction * restrict Act,

struct sigaction *restrict oact);


1, which Signo Choice has many:

Signal Processing:

one, ignoring this signal; SIGKILL and the SIGSTOP signal cannot be ignored)

Second, execute the action that the user wants; (call a user function to perform the processing desired by the user)

Third, perform the system default action; (The system default action for most signals is to terminate the process)

Signal Sending: (main function has kill and raise)

Difference:

Kill You can either send a signal to itself or send a signal to another process, Raise is to send a signal to the process itself.

int Kill (pid_t pid, int signo)

int raise (int signo)

Alarm Way to send:

Use the alarm function to set a time value that generates a sigalrm signal when the time set is reached . If this signal is not captured, the default action is to terminate the process.

unsigned int alarm (unsigned int seconds)

seconds: after the specified seconds seconds will produce SIGALRM

Pause Function:

suspends the calling process until it snaps to a signal.

int pause (void)

Suspend only ends when a signal handler function is executed


can use Kill command to send

kill-s SIGINT 1234 or kill-s sigquit 1234 (where 1234 is the PID of the process)

You can also press CTRL + C(SIGINT signal) or Ctrl + \(sigquit signal)



2. The structure sigaction is defined as follows:

structsigaction{
void (*Sa_handler)(int);
sigset_t Sa_mask;
intSa_flag;
void (*Sa_sigaction)(int,siginfo_t*,void*);
};

The Sa_handler field contains the address of a signal capture function

The Sa_mask field describes a set of signals that is added to the signal screen word of the process before the signal capture function is called. The signal screen word of the process is reset to its original value only when it is returned from the signal capture function.

Sa_flag is an option that mainly understands two

Sa_interrupt system calls that are interrupted by this signal do not restart automatically
Sa_restart system calls that are interrupted by this signal are automatically restarted

Sa_siginfo provides additional information, a pointer to the SIGINFO structure, and a pointer to the process context identifier

The last parameter is an alternative signal handler that will be used when setting sa_siginfo.

Example:

#include <stdio.h>
#include <signal.h>
#include <unistd.h>

void Show_handler (int sig)
{
printf ("I got signal%d\n", SIG);
int i;
for (i = 0; i < 5; i++) {
printf ("i =%d\n", i);
Sleep (1);
}
}

int main (void)
{
int i = 0;
structsigactionAct, oldact;
Act.sa_handler = Show_handler;
Sigaddset (&act.sa_mask, sigquit);//See note (1)
Act.sa_flags = Sa_resethand | Sa_nodefer;//See note (2)
act.sa_flags = 0;//See note (3)

sigaction(SIGINT, &act, &oldact);
while (1) {
Sleep (1);
printf ("Sleeping%d\n", i);
i++;
}
}

Note:
(1) If in the signal SIGINT (Ctrl + C) Signal processing function Show_handler execution process, the process receives a signal sigquit (crt+\), will block the signal until the end of Show_handler execution will not process the signal sigquit.


(2) Sa_nodefer Generally, when the signal processing function is running, the kernel will block < The given signal-sigint>. However, if the sa_nodefer tag is set, the kernel will not block the signal when the signal handler is running. Sa_nodefer is the official POSIX name for this tag (there is also a name sa_nomask, for the portability of the software, usually without this name)
Sa_resethand resets the signal processing function to the default value when the signal handler function is called. Sa_resethand is the official POSIX name for this tag (there is also a name sa_oneshot, for the portability of the software, usually without this name)


(3) If the handler function that does not need to reset the given signal is the default value and does not need to block the given signal (no setting of the SA_FLAGS flag), then the sa_flags must be zeroed out, otherwise the run will result in a segment error. But sa_flags can cause signal loss after 0!

Signaling communication between Linux interprocess communication

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.