Signals of interprocess communication

Source: Internet
Author: User

Speaking of a signal, like a soft interrupt, is a signal coming up later our program interrupts the code that is currently executing, and finds the execution code for the corresponding signal that was previously registered. We're actually using it very extensively, just like we're going to stop a process running in the terminal, and we'll press CTRL + C Key to terminate the program, this is a signal, is a stop signal, is a signal labeled 9, we can use the Kill-l command to view the signal that our system supports. Many of the signals are handled in a system-by-default manner, so we do not process the signal number 9th in programming, Our program will terminate the program when the CTRL + C signal arrives. There are 64 types of signals that are supported in the general Linux system and are not described here. We can use our own way to process signals while programming without invoking the system's default processing methods.

We need to process the signal first to register the signal processing function.

#include <signal.h>

void (*signal (int signum,void (*handler) (int)));

The first parameter is the number of the signal, and the second argument is a function pointer. A signal processing function, or a signal specified for a sig_ing or a slight parameter, is pointed to.

Since it is communication between processes, there will be a process for sending signals, using functions

#include <sys/types.h>

#include <signal.h>

int Kill (pid_t pid,int SIG),//pid process number of the recipient of the specified signal, number of the sig sending signal

PID > 0 is the signal sent to the process

PID = 0 is a process that sends a signal to the same process group as itself

PID =-1 broadcasts the signal to all processes

PID < 0 All processes that send the signal to the absolute value of the PID for the process identification code

Because the way the pipeline transmits data is prone to failure and blocking and the program is inconvenient to handle other things, I usually use the signal with the pipeline, when a program finishes writing a pipeline, sends a signal to the specified program, so that the specified receive signal to read the contents of the pipeline, so that the contents of the pipeline can be read in a timely manner, And it doesn't clog up, and the program can do other things.

Here is the code for signal processing:

  1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <signal.h> 4 #include <unistd.h> 5 #in Clude <errno.h> 6 #include <fcntl.h> 7 8 #define FIFO_PATH "/HOME/CY/MYFIFO" 9 int rfifo = 0;         One to 0 void mySig (int sig_num) {//Signal processing function ("n") char buf[1024] = {}; Read (rfifo,buf,1024);//reads the contents of the pipeline printf ("%s\n", buf); 0} (int main ()) {if (Mkfifo (fifo_path,0666) < errno &&!=eexist) {//Create pipe P Error ("Create error \ n"); return-1; Rfifo = open (Fifo_path,o_creat | o_rdonly,0666);         if (Rfifo < 0) {perror ("wrong\n"), return-1 33} 34 35 36 while (1) {PNS signal (10,MYSIG);//register 10th Signal Processing function ();//Because there is no other work to do, use this function to hibernate the process Waiting for the arrival of the signal of the sleeping\n printf ("the"); (RFIFO);    43     return 0; 44}

Before sending a signal to the specified process, we need to get the PID of the process, we now only know the name of the other process, and each time the program executes the PID is not the same, so that we need to get to the PID value through the name of the process, Shell command pidof process name, The returned result is the PID of the name process, which we can execute in the program code to get the PID of the specified process.

The following is the code that sends a signal to the specified process:

  1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <signal.h> 4 #include <unistd.h> 5 #in Clude <errno.h> 6 #include <fcntl.h> 7 8 #define FIFO_PATH "/HOME/CY/MYFIFO" 9 int main (int arg C,char *argv[]) One {pid_t pid = 0, FILE *fpid = NULL, + char name[] = "Pidof sig1";//shell Command, gets the PID number of the function that specifies the name. -char needpid[6] = {0}; Wfifo int = 0;         Fpid = Popen (name, "R");//Executes the command that obtains the PID number and returns the file descriptor Fgets (needpid,6,fpid);//Read the command's return result according to the file descriptor 21 PID = Atoi (needpid);//Convert the result of the character type to the PID of type int pclose (FPID);//Close the file descriptor if (Mkfifo (fifo_path,06 < 0 && errno! = eexist) {perror ("Create error \ n"), return-2, Wfifo = Ope N (fifo_path,o_creat | o_wronly,0666);         if (Wfifo < 0) {perror ("wrong\n"), return-1; 33} 34 35 while (1) {PNS Printf ("while\n"); (wfifo,name,15);//write messages to the pipeline Kill (pid,10);//Send the specified signal to the specified process. (2);//Sleep two seconds in printf ("Sending sig\n"); 42 43}

  

When using your own signal processing, if you do not need to modify the system predefined signal processing method, it is best not to use the system has already defined the signal, You can use the system reservation to user-defined signals such as 10th number 12th. There are also some signals that cannot be ignored, such as Sigkill and sigstop cannot be ignored and cannot be modified by default execution mode.

It's more useful than the alarm clock signal number 14th.

#include <unistd.h>

unsignal int alarm (unsignal int seconds);

This function is to send a timed signal to itself, the default action is to exit the program, we can also customize the action, perform periodic operation.

int raise (int sig);//This function is to send a signal to itself, personally feel that this function does not matter

-------------------------------------------------------------------------------------------------

In practical applications an application needs to process multiple signals, and for convenience, the Linux system introduces the concept of a signal set. The signal set consists of a data type of multiple signals sigset_t. You can use the following system calls to set up the data contained in the signal set.

#include <signal.h>

int Sigemptyset (sigset_t *set);//used to initialize set-directed signal sets, emptying all signals therein.

Success returns 0, and error returns-1.


#include <signal.h>

int Sigfillset (sigset_t *set);//initializes the set of signals to which all signals are filled.

Success returns 0, and error returns-1.


#include <signal.h>

A signal set directed to a set that adds/removes a signal represented by a signo.

int Sigaddset (sigset_t *set, int signo);
int Sigdelset (sigset_t *set, int signo);

Success returns 0, and error returns-1.


#include <signal.h>

int Sigismember (const sigset_t *set, int signo);// determine if the signo signal is in set-directed signal set.

True returns 1, FALSE returns 0, error returns-1.

Signals of 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.