曲径通幽论坛

 找回密码
 立即注册
搜索
查看: 5810|回复: 0
打印 上一主题 下一主题

给子进程发送信号

[复制链接]

4918

主题

5880

帖子

3万

积分

GROAD

曲径通幽,安觅芳踪。

Rank: 6Rank: 6

积分
34395
跳转到指定楼层
楼主
发表于 2009-7-8 15:51:33 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
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);
}
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|曲径通幽 ( 琼ICP备11001422号-1|公安备案:46900502000207 )

GMT+8, 2025-5-3 23:22 , Processed in 0.082261 second(s), 22 queries .

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表