Linux-软中断通信

本文出自svtter.github.io

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

#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();
            wait();
            printf("parent process is killed!\n");
            exit();
        }
        else
        {
            wait_mark = 1;
            signal(17, stop);
            signal(SIGINT, SIG_IGN);
            while(wait_mark!=);
            lockf(1,1,);
            printf("child process2 is killed by parent!\n");
            lockf(1,,);
            exit();
        }
    }
    else
    {
        wait_mark = 1;
        signal(16, stop);
        signal(SIGINT, SIG_IGN);
        while(wait_mark!=);
        lockf(1, 1, );
        printf("child process1 is killed by parent!\n");
        lockf(1, , );
        exit();
    }
    return ;
}
void waiting()
{
    sleep(5);
    if(wait_mark != )
        kill(getpid(), SIGALRM);
}
void alarming()
{
    wait_mark = ;
}
void stop()
{
    wait_mark = ;
}

执行结果

e3.png