免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 940 | 回复: 0
打印 上一主题 下一主题

父子进程退出后通知对方--例子 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2009-08-18 12:55 |只看该作者 |倒序浏览

#include stdio.h>
#include stdlib.h>
#include unistd.h>
#include sys/types.h>
#include sys/wait.h>

static void FC_exit(int sig)
{
        printf("test [%d][%d] exit[%d]\n",getppid(),getpid(),sig);
        return ;
}

int main( void )
{
        struct sigaction act;
        sigset_t sigset;
        pid_t childpid;
        int status;

        /* set siguser1 funcation */
        act.sa_handler = (void (*)()) FC_exit;
        sigemptyset (&act.sa_mask);
        act.sa_flags = 0;
        sigaction (SIGUSR1, &act, NULL);

        sigfillset (&sigset);
        sigdelset (&sigset, SIGUSR1);
        sigdelset (&sigset, SIGCHLD);

        if (sigprocmask (SIG_BLOCK, &sigset, NULL)  0)
        {
                printf ("sigprocmask error");
                exit (-1);
        }

        childpid = fork();
        if ( -1 == childpid )
        {
                printf( "fork()" );
                return -1;
        }
        else if ( 0 == childpid )
        {
                kill(getppid(),SIGUSR1);
                printf("child pid = %d child ppid = %d \n", getpid(),getppid());
                sleep(1);
                return 0;
        }
        else
        {
                kill(childpid,SIGUSR1);
                printf( "parent pid = %d\n ppid = %d", getpid(),getppid() );
                sleep(1);
        }
        return 0;
}


本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u1/53810/showart_2030658.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP