Linux_c Development (5-3) interprocess communication _ Signaling Communication

Source: Internet
Author: User
Tags set time signal handler

Signal communication

The signaling (signal) mechanism is the oldest inter-process communication mechanism in UNIX systems, and there are many conditions that can produce a signal:
1 . When the user presses certain keys, the signal is generated.
2, the hardware abnormal generation signal: Divisor is zero, invalid storage access and so on. These signals are usually detected by hardware, notified to the kernel, and then the kernel generates an appropriate signal notification process, for example: the kernel generates a SIGSEGV signal for a process that is accessing an invalid storage area.
3 . The process uses the KILL function to send the signal to another process.
4, the user can use the KILL command to send the signal to other processes.

Signal type

a few common signals

SIGHUP: end signal sent from terminal
SIGINT: interrupt signal from the keyboard (CTRL + C)
SIGKILL: The signal ends the process of receiving a signal
SIGTERM: A signal from the kill command.
SIGCHLD: A signal indicating the stop or end of a child process
SIGSTOP: stop execution signal from the keyboard (CTRL + Z) or debug program

Signal Processing

When a signal appears, it is handled in one of the following three ways:
1. Ignore this signal
Most signals are handled in this manner, but two types of signals cannot be ignored. Sigkill and Sigstop. These two signals can not be ignored the original intention is that they provide a way for the super-family to terminate or stop the process.
2. Perform the actions desired by the user
Notifies the kernel that a user function is called when a signal occurs. In the user function, perform the processing that the user wants.
Perform system default actions
The system default action for most signals is to terminate the process.

Signal sending

The main signals sent are kill and raise.
difference:
Kill can either send a signal to itself or send a signal to another process. The raise function sends a signal to the process itself.

#include(sys/types.h)#include(signal.h)int kill(pid_t pid,int signo)int raise(int signo)

There are four different conditions for the PID parameter of Kill:
1, pid>0
Sends a signal to a process with a process ID of PID.
2, Pid==0
Sends a signal to the same set of processes.
3, Pid<0
Sending a signal to its process group ID equals PID is definitely worth the process.
4, Pid==-1
Sends a signal to all processes.

Alarm
Use the alarm function to set a time value (alarm time) to generate a SIGALRM signal when the set time is reached. If this signal is not captured, the default action is to terminate the process.

#include<unistd.h>int alarm(unsignedint seconds)

Seconds: A signal sigalrm is generated after the specified number of seconds.

Pause
The pause function suspends the calling function until it snaps to a signal.
#include<ubistd.h>
int pause(void)

The suspend does not end until a signal handler function is executed.

Processing of signals

§ When a signal is captured by the system, either ignore the signal or use the specified processing function to process the signal, or use the system default mode.
There are two main ways to deal with §§ signal, one is to use simple signal function, and the other is to use the set of signal set function.

Signal

#include<signal.h>void(*signal(int signo,void(*func)(int)))(int)//可以理解为//typedef void(*sighandler_t)(int)//sighandler_t signal(int signum,sighandler_t handler)

The Func may be:
1. Sig_ign: Ignore this signal
2, Sig_del: According to the system default mode processing
3. Signal Processing function Name: Use this function to handle

#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");/ * Register signal Processing function * /Signal (SIGINT,MY_FUNC);    Signal (SIGQUIT,MY_FUNC); Pause ();Exit(0);}


Here we run the program and then use Kill to send the message to the process. The format is:

 kill -s  信息  pid

Process number can be used: PS aux view
For example, I checked my process number to 623 so my message format is: Kill-s sigquit 623

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Linux_c Development (5-3) interprocess communication _ Signaling 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.