Example of a talk C language (85th: C language instance-Use signals for inter-process communication 2)

Source: Internet
Author: User

Example of a talk C language (85th: C language instance-Use signals for inter-process communication 2)

Hello, the last time we talked aboutUse signals for inter-process communicationIn this case, let's continue with the previous example. When you leave the rest of your time, your words will go right. Let's talk C chestnuts together!

In the previous example, we used signals for inter-process communication. In this example, we sent signals through terminals, when a process receives the signal, it is asked to execute the default action defined by the system for the signal. In this case, we will give an example of using signals for inter-process communication. However, the method for sending and processing signals is different from the previous example. In the following exampleUse kill to generate signals in a process, InThe other process receives and processes the received signal in its own way..

In this example, we use the kill function to send signals to other processes,The following is the prototype of the kill function.:

int kill(pid_t pid, int signo);
The first parameter pid indicates the PID of the process. The kill function sends the signal to the process whose PID is the same as the parameter. The second parameter indicates the signal. This parameter indicates the signal value sent by the kill function. If kill successfully sends a signal, 0 is returned; otherwise,-1 is returned.

In this example, we use the signal function to set the processing method after receiving the signal,The following is the prototype of the signal function.:

void (*signal(int signo, void (*func) (int))) (int)

Do you think this function is messy? Don't panic. Let's analyze it slowly.

This function is called signal, which is used to configure the signal processing function. In layman's terms, it is to assign a signal processing function to a signal. It has two parameters:

One parameter is signo, indicating the signal value. The other parameter is a function pointer named func, indicating the signal processing function. That is to say, the process will use this function to process the signal after receiving the signal indicated by signo. Func In the parameter is a function pointer. It points to a function that contains an int type parameter, indicating the signal value. This function returns void. Finally, let's talk about the return value of the signal function. It returns a function, which is the signal processing function pointed to by func.

Next,We use specific code to describe their usage.

# Include
  
   
# Include
   
    
# Include
    
     
Void sig_receive (int signo) {printf ("received signal: % d \ n", signo) ;}int main () {pid_t pid; int pid_res; int stat_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); // configure the signal processing function printf ("PID: % d-> Son Process receive signal \ n ", 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 ;}
    
   
  

From the code above, we can see that we use the kill function in the parent process to send signals, and then use the signal function in the child process to configure the signal processing function. When the child process receives the signal, the signal processing function processes the signal.

The readers will not write the code in the body, and the detailed code will be put into my resources. You can click here to download and use it.

The following is the running result of the program. For more information, see:

PID: 3208-> Father Process send signal // The parent Process sends the signal received signal: 14 // The signal processing function processes the signal PID: 3209-> Son Process receive signal // The sub-process receives the signal Son Process finished: PID = 3209 // The sub-process ends and the parent Process ends.

From the results of the program, we can see that,After the parent process sends a signal, the child process receives the signal and processes it.. This indicates that we useThe signal communicates between two processes..

Look, there is a small detail that you don't know if you have found: the program running result first executes the content in the signal processing function, and then executes the content in the sub-process. What happens if we change the execution sequence?

Change the execution sequence of the following two lines of code, that is, place the printf statement before the signal configuration statement.

Signal (SIGALRM, sig_receive); // configure the signal processing function printf ("PID: % d-> Son Process receive signal \ n", getpid ());

After re-Compiling and running the program, the following running results are obtained::

PID: 2611-> Father Process send signal // The signal sent by the parent process Son Process finished: PID = 2612 // The child process ends and the parent Process ends.

From the preceding results, we can see that not only does the sub-process not receive the signal, but also does not execute it. Why? This is because the child process may be blocked when the parent process sends a signal, so it is not executed. This is also a disadvantage of the signal function.

The signal function is a function provided by the early Unix system. In the new system, the sigaction function has been used to replace it, but it can still be seen in some old programs. Next, we will introduce the content related to the sigcation function.

For more information, see the example of using signals for inter-process communication. I want to know what examples will be provided later, and I will try again.

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