我在尝试在c中编写一个smallshell时遇到了一些问题。Unix编程 - 信号处理器
问题是:假设我为信号处理程序编写了一些代码,在这种情况下,它被修改为捕获SIGCHLD
信号,我怎么能通知我的程序信号已被捕获?
如果我使用全局变量,问题很简单,但这不是我想要去做的。所以任何建议/提示将不胜感激!
这就是我现在要解决的问题。
volatile sig_atomic_t exit_status; /* <--global variabel */
void sigchld_handler(int signal) {
switch (signal) {
case SIGCHLD:
exit_status = 1; /* SIGCHLD was caught, notify program.. */
break;
default:
fprintf(stderr, "Some signal catched\n"); /* not a signal of intrest */
break;
}
}
//感谢
绝对使用全局变量。另外,在使用'volatile'之前,请确保您阅读[this](http://www.cs.utah.edu/~regehr/papers/emsoft08-preprint.pdf),它基本上表明(至少)普通编译器混乱打开优化时使用'volatile'。或[内核文档中的这一点](http://www.mjmwired.net/kernel/Documentation/volatile-considered-harmful.txt)。更何况丹尼斯里奇也反对'挥发性' – Shahbaz
谢谢,我会看看它:) –