sigsuspend牛刀小试

sigsuspend的整个原子操作过程为:
(1) 设置新的mask阻塞当前进程;
(2) 收到信号,恢复原先mask;
(3) 调用该进程设置的信号处理函数;
(4) 待信号处理函数返回后,sigsuspend返回。

/********************************************************************************************************************** * FILE NAME : tell_wait.cpp * CREATE DATE : 2011/06/25 * MODULE : view * AUTHOR : zengqh *---------------------------------------------------------------------------------------------------------------------* * MEMO : **********************************************************************************************************************/ #include <stdlib.h> #include <stdio.h> #include <unistd.h> #include "apue.h" #include <signal.h> #include <errno.h> /********************************************************************************************************************** * Macros and Structs **********************************************************************************************************************/ #define SUCCESS (0) #define FAILURE (-1) /********************************************************************************************************************** * Global Variables Definition Section **********************************************************************************************************************/ static volatile sig_atomic_t sig_flag = 0; static sigset_t newmask, oldmask, zeromask; /********************************************************************************************************************** * Functions **********************************************************************************************************************/ static void sig_usr(int signo) { sig_flag = 1; } int tell_wait() { if(SIG_ERR == signal(SIGUSR1, sig_usr)) { printf("signal failed: SIGUSR1./n"); return FAILURE; } if(SIG_ERR == signal(SIGUSR2, sig_usr)) { printf("signal failed: SIGUSR2./n"); return FAILURE; } sigemptyset(&zeromask); sigemptyset(&newmask); sigaddset(&newmask, SIGUSR1); sigaddset(&newmask, SIGUSR2); if(sigprocmask(SIG_BLOCK, &newmask, &oldmask) < 0) { printf("sigprocmask failed./n"); return FAILURE; } return SUCCESS; } int tell_parent(pid_t pid) { kill(pid, SIGUSR2); return SUCCESS; } int wait_parent() { while(0 == sig_flag) { sigsuspend(&zeromask); } if(sigprocmask(SIG_SETMASK, &oldmask, NULL) < 0) { printf("sigprocmask failed./n"); return FAILURE; } return SUCCESS; } int tell_child(pid_t pid) { kill(pid, SIGUSR1); return SUCCESS; } int wait_child() { while(0 == sig_flag) { sigsuspend(&zeromask); } if(sigprocmask(SIG_SETMASK, &oldmask, NULL) < 0) { printf("sigprocmask failed./n"); return FAILURE; } return SUCCESS; } int main() { pid_t child_pid; tell_wait(); if((child_pid = fork()) < 0) { printf("fork child failed./n"); return FAILURE; } if(0 == child_pid) { sleep(5); tell_parent(getppid()); } else if(child_pid > 0) { printf("begin to wait child./n"); wait_child(); printf("end to wait child./n"); } return 0; } /********************************************************************************************************************** * Class **********************************************************************************************************************/ /********************************************************************************************************************** * Class Members **********************************************************************************************************************/ /*****************************************************EOF*************************************************************/

转载于:https://www.cnblogs.com/zengqh/archive/2011/06/25/2477407.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值