关于APUE里面TELL_WAIT()之类函数的两种实现方式

Just a note....


implementation by signal


#include <apue.h>

static volatile sig_atomic_t sigflag;

static sigset_t newmask,oldmask,zeromask;

static void sig_usr(int signo)
{
        sigflag = 1;
}

void TELL_WAIT(void)
{
        struct sigaction act_usr1,act_usr2;

        sigemptyset(act_usr1.sa_mask);
        sigemptyset(act_usr2.sa_mask);

        act_usr1.flags = 0;
        act_usr2.flags = 0;

        act_usr1.sa_handler = sig_usr;
        act_usr2.sa_handler = sig_usr;

        if(sigaction(SIGUSR1,&act_usr1,NULL) < 0)
        {
                printf("sigaction error\n");
        }

        if(sigaction(SIGUSR2,&act_usr2,NULL) < 0)
        {
                printf("sigaction error\n");
        }

        sigemptyset(&zeromask);
        sigemptyset(&newmask);

        sigaddset(&newmask,SIGUSR1);
        sigaddset(&newmask,SIGUSR2);

        if(sigprocmask(SIG_BLOCK,&newmask,&oldmask) < 0)
        {
                printf("sigprocmask error\n");
        }

}

void TELL_PARENT(void)
{
        kill(pid,SIGUSR2);
}


void WAIT_PARENT(void)
{
        while(sigflag == 0)
        {
                sigsuspend(&zeromask);
        }


        sigflag = 0;


        if(sigprocmask(SIG_SETMASK,&oldmask,NULL) < 0)
        {
                printf("sigprocmask error\n");
        }
}


void TELL_CHILD(pid_t pid)
{
        kill(pid,SIGUSR1);
}


void WAIT_CHILD(void)
{
        while(sigflag == 0)
        {
                sigsuspend(&zeromask);
        }


        sigflag = 0;


        if(sigprocmask(SIG_SETMASK,&oldmask,NULL) < 0)
        {
                err_sys("SIG_SETMAKS error\n");
        }
}


implementation by pipe




#include <stdio.h>
#include <fcntl.h>

static pfd1[2],pfd2[2];

void TELL_WAIT(void)
{
        if(pipe(fpd1) < 0 || pipe(pfd2) < 0)
        {
                printf("pipe error\n");
        }
}

void TELL_PARENT(void)//tell to parent
{
        if(write(pfd2[1],"c",1) != 1)
        {
                printf("write error\n");
        }
}

void WAIT_PARENT(void)//wait for parent
{
        char c;

        if(read(pfd1[0],&c,1) != 1)
        {
                printf("read error\n");
        }

        if(c != 'p')
        {
                printf("WAIT_PARENT: incorrect data\n");
        }
}

void TELL_CHILD(pid_t pid)//tell to child
{
        if(write(pfd1[1],"p",1) != 1)
        {
                printf("write error\n");
        }
}

void WAIT_CHILD(void)//wait for child
{
        char c;
        if(read(pdf2[0],&c,1) != 1)
        {
                printf("read error\n");
        }

        if(c != 'c')
        {
                printf("WAIT_CHILD : incorrect data\n");
        }
}






就是笔记,方便查阅而已























  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值