Talk C Chestnut Bar (85th time: C-language example-using signals for interprocess communication II)

Source: Internet
Author: User
Tags signal handler

Ladies and gentlemen, crossing, the last time we were talking about a sample of interprocess communication using a signal , let's go on to the last part of the example. Gossip Hugh. Words return to the positive turn.

Let's talk about C chestnuts together.

In the last cited, we had a sample that used a signal for interprocess communication, in this example. We send a signal through the terminal. When the process receives the signal, let it run the default action defined by the system on the signal. This back. Let's take another example of using a signal for interprocess communication, just. The way we send and process signals is different from the previous example. In the following example, we use kill to generate a signal in a process . receive in another process and handle the received signal in its own way .

In the example we use the KILL function to send a signal to another process, the following is the prototype of the Kill function :

intkillint signo);
    • The first parameter PID represents the PID of a process. The KILL function sends the signal to the same process as the PID and its parameters;
    • The second parameter indicates a signal. The parameter represents the signal value sent by the Kill function.

    • If kill successfully sends a signal, then return 0, otherwise return-1.

In the example we use the signal function to set the processing mode after receiving the signal. The following is a prototype of the signal function :

void (*signal(intvoid (*func) (int))) (int)

Does everyone look at this function as a bit of a mess? Just don't panic. We are slowly analyzing it.

This function, called signal, is used to configure the signal processing function, which is a signal processing function for a signal.

It has two of the parameters:

    • One of the parameters is Signo, which represents the signal value.
    • Another parameter is a function pointer called Func, which represents the signal processing function.
    • That is, the process receives the signal represented by the Signo and uses that function to process the signal.

    • The func in the parameter is a function pointer that refers to a function that includes a parameter of type int that represents the signal value, which returns void.
    • Finally, let's talk about the return value of the signal function, which returns a function. This function is the signal processing function that the Func points to.

Next, we use specific code to illustrate how they are used .

#include <unistd.h>#include <signal.h>#include <stdio.h>void Sig_receive (intSigno) {printf("received signal:%d \ n", Signo);}intMain () {pid_t pid;intPid_res;intStat_value; PID =Fork();if(PID >0)    {printf("PID: %d --Father Process send signal\n", Getpid ());Kill(PID,SIGALRM);//Send Signal}Else if(PID = =0) {signal (sigalrm,sig_receive);//Configuring Signal Processing functionsprintf("PID: Son Process receive signal \ n \ %d ", Getpid ()); }Else{printf("Create process failed \ n");return 1; } pid_res =wait(&stat_value);if(Pid_res >0)    {printf("Son Process finished:pid = %d \ n", pid_res); }return 0;}

As you can see from the code above, we use the Kill function to send a signal in the parent process and then use the signal function in the subprocess to configure the signal handler function when the child process receives the signal. The signal processing function will process the signal.

Crossing, the text does not write code, the specific code put in my resources, we can click here to download the use.

The following is the results of the operation of the program, please refer to :

3208signal      //父进程发送信号signal :14                          //信号处理函数在处理信号3209signal      //子进程收到信号Son process finished: PID 3209             //子进程结束,父进程也结束    

Crossing, from the results of the program can be seen, the parent process signaled, the child process received a signal, and processed .

This means that we communicate through the signals between the two processes .

Crossing, there is a small detail that we do not know about: The program runs the contents of the signal handler function before running the content in the child process. What happens if we switch the sequence of operations?

The following two lines of code run in the order of the exchange, but also to put the printf statement in front of the signal configuration statement.

        signal(SIGALRM,sig_receive);  //配置信号处理函数        printf("PID: %d -> Son Process receive signal \n",getpid());

Once again, after compiling and running the program, you get the following running results :

2611send //父进程发送信号process2612         //子进程结束,父进程也结束     

From the above results to be able to see, the child process not only received no signal. And it's not running. What is the reason for this? That is because the child process may be blocked when the parent process signals, so it is not running. This is also a disadvantage of the signal function.

The signal function is a function provided by the early Unix system, and is now replaced with the Sigaction function in the new system. Just in some old programs still can see it figure. In the next one. We'll show you what the Sigcation function is about.

Crossing, for example, let's talk about the use of signals for interprocess communication.

I want to know what the following example, and listen to tell.

Talk C Chestnut Bar (85th time: C-language example-using signals for interprocess communication II)

Related Article

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: 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.