zoukankan      html  css  js  c++  java
  • 进程同步

    #include "apue.h"

    static volatile sig_atomic_t sigflag; /* set nonzero by sig handler */
    static sigset_t newmask, oldmask, zeromask;

    static void
    sig_usr(int signo)   /* one signal handler for SIGUSR1 and SIGUSR2 */
    {
        sigflag = 1;
    }

    void
    TELL_WAIT(void)
    {
        if (signal(SIGUSR1, sig_usr) == SIG_ERR)
            err_sys("signal(SIGUSR1) error");
        if (signal(SIGUSR2, sig_usr) == SIG_ERR)
            err_sys("signal(SIGUSR2) error");
        sigemptyset(&zeromask);
        sigemptyset(&newmask);
        sigaddset(&newmask, SIGUSR1);
        sigaddset(&newmask, SIGUSR2);

        /*
         * Block SIGUSR1 and SIGUSR2, and save current signal mask.
         
    */
        if (sigprocmask(SIG_BLOCK, &newmask, &oldmask) < 0)
            err_sys("SIG_BLOCK error");
    }

    void
    TELL_PARENT(pid_t pid)
    {
        kill(pid, SIGUSR2);              /* tell parent we're done */
    }

    void
    WAIT_PARENT(void)
    {
        while (sigflag == 0)
            sigsuspend(&zeromask);   /* and wait for parent */
        sigflag = 0;

        /*
         * Reset signal mask to original value.
         
    */
        if (sigprocmask(SIG_SETMASK, &oldmask, NULL) < 0)
            err_sys("SIG_SETMASK error");
    }

    void
    TELL_CHILD(pid_t pid)
    {
        kill(pid, SIGUSR1);             /* tell child we're done */
    }

    void
    WAIT_CHILD(void)
    {
        while (sigflag == 0)
            sigsuspend(&zeromask);  /* and wait for child */
        sigflag = 0;

        /*
         * Reset signal mask to original value.
         
    */
        if (sigprocmask(SIG_SETMASK, &oldmask, NULL) < 0)
            err_sys("SIG_SETMASK error");
    }
  • 相关阅读:
    TED_Topic9:How we're priming some kids for college — and others for prison
    TED_Topic8:How to control someone else's arm with your brain
    ML—随机森林·1
    ML—R常用多元统计分析包(持续更新中……)
    ML—机器学习常用包(持续更新….)
    TED_Topic7:How we unearthed the spinosaurus
    R9—R常用函数分类汇总
    将博客搬至CSDN
    产品经理读书笔记(一)
    Python数据结构:集合
  • 原文地址:https://www.cnblogs.com/hbt19860104/p/2626401.html
Copyright © 2011-2022 走看看