Linux-软中断通信

点击量:16

本文出自svtter.com
本文可以随意转载,但是转载请保留本信息.

#include <stdio.h>
#include <unistd.h>
#include <signal.h>
#include <unistd.h>
#include <wait.h>
void waiting(), stop(), alarming();
int wait_mark;
int main()
{
    int p1, p2;
    if(p1 = fork())
    {
        if(p2 = fork())
        {
            wait_mark = 1;
            signal(SIGINT, stop);
            signal(SIGALRM, alarming);
            waiting();
            kill(p1, 16);
            kill(p2, 17);
            wait(0);
            wait(0);
            printf("parent process is killed!\n");
            exit(0);
        }
        else
        {
            wait_mark = 1;
            signal(17, stop);
            signal(SIGINT, SIG_IGN);
            while(wait_mark!=0);
            lockf(1,1,0);
            printf("child process2 is killed by parent!\n");
            lockf(1,0,0);
            exit(0);
        }
    }
    else
    {
        wait_mark = 1;
        signal(16, stop);
        signal(SIGINT, SIG_IGN);
        while(wait_mark!=0);
        lockf(1, 1, 0);
        printf("child process1 is killed by parent!\n");
        lockf(1, 0, 0);
        exit(0);
    }
    return 0;
}
void waiting()
{
    sleep(5);
    if(wait_mark != 0)
        kill(getpid(), SIGALRM);
}
void alarming()
{
    wait_mark = 0;
}
void stop()
{
    wait_mark = 0;
}

执行结果

e3.png

发表评论

电子邮件地址不会被公开。 必填项已用*标注