学习信号处理的一个例子
来源:互联网 发布:中企动力域名证书 编辑:程序博客网 时间:2024/05/22 01:44
在学习<<UNIX程序设计>>的中信号处理的时候,我把书上的例子给写出来,并写出了一些自己的一些疑问:
1. 在第95行的时候,书上使用的是while(sigflag == 0),我想是不是使用if(sigflag == 0)是否也可以。
2.如果信号的动作是终止进程,则sigsuspend函数不返回。如果信号是动作的执行信号句柄,则在信号句柄返回后,sigsuspend函数返回。
代码如下:
- #include <signal.h>
- #include <iostream.h>
- #include <stdio.h>
- #include <sys/wait.h>
- static volatile sig_atomic_t sigflag;
- static sigset_t newmask,oldmask,zeromask;
- static void sig_usr(int);
- static void output(char*);
- static void WAIT_PARENT(void);
- static void TELL_WAIT(void);
- static void TELL_CHILD(pid_t);
- void pr_exit(int status)
- {
- int sig;
- if(WIFEXITED(status))
- cout<<"normal termination, exit status:"<<WEXITSTATUS(status)<<endl;
- else if(WIFSIGNALED(status))
- {
- sig = WTERMSIG(status);
- #ifdef WCOREDUMP
- cout<<"abnormal termination,signal number:"<<sig<<WCOREDUMP(status)?"(core file generated)":"";
- #else
- cout<<"abnormal termination,signal number:"<<sig;
- #endif
- }
- else if(WIFSTOPPED(status))
- {
- sig = WSTOPSIG(status);
- cout<<"child stopped,signal number:"<<sig<<endl;
- psignal(sig,"");
- }
- }
- #define MYSYNOPT 1
- int main(void)
- {
- pid_t pid;
- #ifdef MYSYNOPT
- TELL_WAIT();
- #endif
- if((pid = fork()) < 0)
- cout<<"fork error"<<endl;
- else if(pid == 0)
- {
- #ifdef MYSYNOPT
- WAIT_PARENT();
- #endif
- output("output from child/n");
- return 2;
- }
- else
- {
- output("output from parent/n");
- #ifdef MYSYNOPT
- TELL_CHILD(pid);
- #endif
- int status;
- waitpid(pid,&status,0);
- pr_exit(status);
- }
- return 1;
- }
- static void output(char* str)
- {
- char * ptr;
- int c;
- setbuf(stdout,NULL);
- for( ptr = str; c = *ptr++;)
- {
- sleep(1);
- putc(c,stdout);
- }
- }
- static void sig_usr(int signo)
- {
- sigflag = 1;
- return ;
- }
- void TELL_WAIT(void)
- {
- if(signal(SIGUSR1,sig_usr) == SIG_ERR)
- {
- cout<<"signal(SIGUSR1)error"<<endl;
- return ;
- }
- sigemptyset(&zeromask);
- sigemptyset(&newmask);
- sigaddset(&newmask,SIGUSR1);
- if(sigprocmask(SIG_BLOCK,&newmask,&oldmask) < 0)
- {
- cout<<"SIG_BLOCK_ERROR/n"<<endl;
- }
- }
- void WAIT_PARENT(void)
- {
- if(sigflag == 0)
- {
- cout<<"before sigsuspend()"<<endl;
- sigsuspend(&zeromask);
- cout<<"after sigsuspend()"<<endl;
- }
- sigflag = 0;
- if(sigprocmask(SIG_SETMASK,&oldmask,NULL)<0)
- cout<<"SIG_SETMASK error"<<endl;
- }
- void TELL_CHILD(pid_t pid)
- {
- kill(pid,SIGUSR1);
- }
- 学习信号处理的一个例子
- linux 信号处理函数一个例子
- 一个学习等待信号sigsuspend()很好的例子
- Linux下多线程编程与信号处理易疏忽的一个例子
- Linux下多线程编程与信号处理易疏忽的一个例子
- Linux下多线程编程与信号处理易疏忽的一个例子
- Linux下多线程编程与信号处理易疏忽的一个例子
- Linux下多线程编程与信号处理易疏忽的一个例子[转]
- linux 信号处理 的学习
- 搞好信号处理的一个前提
- 信号学习笔记3--信号的阻塞处理
- 一个包含了信号、信号量、共享内存的例子
- Linux多线程编程和信号易疏忽的一个例子
- linux内核中的信号机制--一个简单的例子
- Linux多线程编程和信号易疏忽的一个例子
- 信号处理 学习笔记
- [学习]信号处理基础
- 关于信号反射的仿真(针对《信号完整性分析》一书中的一个例子)
- 再谈sql注入(sql inject)
- MSN远程协助
- 09年中国软件企业最应该投资的5大技术
- 一段完整的Socket HTTP协议中 GET报文的应用
- SMPP是一种通用的短信接口协议
- 学习信号处理的一个例子
- C# 基类中读取Session
- extjs grid数据改变后刷新的实现
- 转《DLL木马进程内幕大揭秘》
- jQuery 1.3更新的内容
- JAVA四种基本排序
- GridView自动序号
- 迫于用户压力,新版易语言已取消为编译后的EXE强制添加版本信息中的“备注”
- 闭包问题