进程间的通讯-软中断信号:实现父->子->父同步序列

#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<signal.h>

/*进程间的通讯-软中断信号:父->子->父同步序列*/


/*定义*/
int temp;
void handle();

void handle(){
    temp=0;
}

main(){
    int pro1;
    while((pro1=fork())==-1);
    if(pro1>0){
        //父进程返回
        int i;
        for(i=0;i<3;i++){
            printf("welcome to hr!\n");
            sleep(1);
        }
        /*
        向子进程发送软中断信号
        12表示用户自定义信号量
        */
        kill(pro1,12);
        /*等待子进程结束*/
        wait(0);
        printf("father over!\n");
        exit(0);
    }else{
        /*预设软中断信号对应的函数*/
        signal(12,handle);
        temp=1;
        while(temp==1){
            printf("i am a child!\n");
            sleep(1);
        }
        printf("child over!\n");
        exit(0);
    }
}
 

猜你喜欢

转载自macrotea.iteye.com/blog/788128