Talk C together (86th back: C language instance -- Use signals for inter-process communication 3)

Source: Internet
Author: User

Talk C together (86th back: C language instance -- Use signals for inter-process communication 3)

Hello, the last time we talked aboutExample of 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!

We mentioned the disadvantages of the signal function in the last time, which leads to the sigaction function. It is more robust than the signal function. Next we will introduce its usage.

The functions of the sigaction function are similar to those of the signal function: used to configure the signal processing function.However, his configuration is much more powerful.

The following is the sigaction function prototype.:

int sigaction(int sig,const struct sigaction *act,struct sigaction *oact)

We can see that this function is not complicated, so don't worry. Let's analyze it slowly:

It has three parameters.

The first parameter sig indicates the signal to be configured; the second parameter indicates the country where the signal is configured in detail; the third parameter is used to store the previously configured action of the signal. If this is not required, it can be null. If the function runs successfully, 0 is returned. If the function fails,-1 is returned.

The last two parameters in this function are:Struct sigaction pointerThe following is the definition of the struct and the role of each member:

Struct sigaction {void (* sa_handler) (int); // signal processing function, which corresponds to sigset_t sa_mask in the signal function; // signal set, the signal represented by the sig parameter is added to the int sa_flags; // It indicates the signal processing method, usually 0 void (* sa_sigaction) (int, siginfo_t *, void *); // It is similar to the first member. When sa_flags is SA_SIGINFO, // This function is used to process signals without using the function pointed by sa_handler };

From the above structure definition, we can see that the signal configuration and processing are put in the second act parameter of sigaction, so
We can modify the act parameter to meet the signal configuration requirements.

Next,We use specific code to describe their usage.

#include
  
   #include
   
    #include
    
     // signal process functionvoid sig_receive(int signo){    printf("received signal :%d \n",signo);    printf("signalprocessing in :%d \n",getpid());    // do something for signal}int main(){    pid_t pid;    int pid_res;    int stat_value;    struct sigaction act;    act.sa_handler = sig_receive;  //config the signal processing function    sigemptyset(&act.sa_mask);     //config the signal set,and it will be setted as empty    act.sa_flags = 0;              // do need to give a value to it    sigaction(SIGALRM,&act,NULL); //config signal by sigaciton function, the third param is NULL    pid = fork();    if(pid > 0)    {        printf("PID: %d -> Father Process send signal\n",getpid());        kill(pid,SIGALRM); //send signal    }    else if(pid == 0)    {        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: first configure the sigaction parameter, the specific operation is as follows:

Configure the signal processing function for the signal, set the signal set, and configure the signal processing function through the sigaction function.

You can compare it with the content in the previous chapter. We use the sigaction function to replace the signal function. This function can place the signal in the signal concentration of the process and block it until the signal processing function is ready, then let the process receive signals. This prevents the signal from being received before the signal processing function is called.

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: 4468-> Father Process send signal // The parent Process sends the signal received signal: 14 // The signal processing function in the processing signal signalprocessing in: 4469 // signal processing function in processing signal PID: 4469-> Son Process receive signal Son process finished: PID = 4469 // sub-Process running is completed

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.