曲径通幽论坛

标题: 给子进程发送信号 [打印本页]

作者: beyes    时间: 2009-7-8 15:51
标题: 给子进程发送信号
fork() 一个子进程,然后从父进程发送一个信号给它,测试父子进程之间的信号通讯。测试代码:
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <signal.h>
#include <stdlib.h>

void handler(int signo)
{
    printf("recv the signal from parent process\n");
}


int main()
{
    pid_t pid;
   
    pid = fork();

    switch(pid)
    {
        case -1:
            perror("fork failed");
            exit(1);
        case 0:
            printf("in the child\n");
            signal(SIGCONT, handler);
            pause();
            printf("child weakup\n");
            break;
        default:
            printf("in the parent\n");
            sleep(5);
            kill(pid, SIGCONT);
            sleep(5);
            printf("parent weakup\n");
            break;
    }
    printf("bye..\n");

    exit(0);
}





欢迎光临 曲径通幽论坛 (http://www.groad.net/bbs/) Powered by Discuz! X3.2