- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我正在为我的操作系统类做一个 shell 项目,我们已经完成了信号处理部分。作业要求我们捕获 SIGINT
和 SIGTSTP
并将信号发送到子进程。这是我到目前为止所得到的。如果你遇到 undefined variable 或函数,希望你能根据标识符理解它的含义:
char input[ MAX_INPUT ];
sigset_t sig;
pid_t *suspendedChildren = NULL;
int nSuspendedChildren = 0;
pid_t currentChild = 0;
int main( int argc, char *argv[] )
{
char quit = 0;
setup();
do
{
getInput();
quit = handleInput( input );
} while( quit != EXIT_NUMBER );
return 0;
}
void setup( void )
{
// block the interrupt signal
sigaddset( &sig, SIGINT);
sigprocmask( SIG_BLOCK, &sig, NULL);
// handle the suspend signal
signal( SIGTSTP, suspendChild );
}
void suspendChild( int signal )
{
if (currentChild) // meaning that there is a child process currently running
{
// increment suspended children counter
nSuspendedChildren++;
// reallocate the array of suspended children
suspendedChildren = (pid_t *)realloc( suspendedChildren, nSuspendedChildren*sizeof(pid_t));
suspendedChildren[nSuspendedChildren-1] = currentChild;
// send suspend signal to child
kill( currentChild, SIGTSTP );
printf( "\n[%d]+ Stopped\t\t", nSuspendedChildren );
puts( input );
putchar( '\n' );
// set the global to 0
currentChild = 0;
main( 0, NULL );
}
}
int handleInput( char *s )
{
// string tokenizing / parsing...
// checks for redirection / background process requests
// (not relevant to question being asked so omitted)
currentChild = fork();
if (currentChild) // parent process
{
wait( &status );
}
else // child process
{
execvp( prgm, tokens );
}
}
因此,为了处理 SIGINT
,我简单地阻止了信号,以便子进程(执行的命令)接收它,而父进程(shell)忽略它。这工作得很好,但它是 SIGTSTP
并且暂停了我遇到问题的进程。对于这个信号,我选择在它到达时调用一个信号处理程序。这非常有效,因为我相信进程的默认 SIGTSTP
处理行为是挂起,但由于我的 shell 正在等待(请参阅 wait(&status)
)子进程返回(目前已暂停),我的整个终端都处于僵尸状态。我无法按 ctrl+D 退出,我只需要关闭窗口并重新登录...
所以重申这篇文章的标题,有什么方法可以从信号处理程序的 wait(int*)
提前返回?我查阅了文档并发现了这个声明:
wait also returns if a signal is received and is not ignored.
但是,仅此而已,并没有提供进一步的见解。
最佳答案
而不是 wait
考虑它是否真的是您真正想要使用的 waitpid
(带有特定选项)。 – 鸭子
关于c - shell 实现 : signal handling: Any way to return early from wait()?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20353407/
来自Unix&Linux Stack Exchange的 This question was migrated,因为可以在Stack Overflow上进行回答。
所以我有这段代码(部分取自 python 文档): import signal def handler(signum, frame): print 'Signal handler called
是否可以在信号块中调用用户定义的方法? method my-method ( ... ) { signal(SIGTERM,SIGINT,SIGQUIT,SIGHUP).tap( -> $si
我正在研究一个类的概念性伪代码信号量分配。 我想知道是否可以在某个进程调用 wait() 之前在信号量上调用 signal()。例如: Shared data: Semaphore x = 0; Pr
我正在为远程调试注册一个信号: signal.signal(signal.SIGUSR1, lambda x,y: remote_debug(x, y, emp_id)) 虽然通常非常快,但日志语句显
关于 PySide signals and slots page它说:“信号是实例拥有的运行时对象,它们不是类属性”。显然,QObject 构造函数在 Signals 的类属性中查找并将它们复制到对象
关于 PySide signals and slots page它说:“信号是实例拥有的运行时对象,它们不是类属性”。显然, QObject 构造函数查找 Signals 的类属性并将它们复制到对象实
关于PySide signals and slots page它说:“信号是实例拥有的运行时对象,它们不是类属性”。显然,QObject 构造函数在类属性中查找信号并将它们复制到对象实例。我的测试程序
使用 ReactiveCocoa,我如何从重复信号中链接信号? 我想做这样的事情:每 5 秒,我运行一次网络请求。 为此,我创建了一个重复信号 RACSignal *each5SecondSignal
正如标题中所写 - “信号和插槽”是一种简单的响应式(Reactive)编程方式吗? 最佳答案 Signals/Slots是 Observer Pattern 的实现. 来自维基 Reactive P
您好,我想知道信号声明如何在 VHDL 中真正起作用。它是否意味着延迟,因为它是内部信号?信号有内部存储器吗? 例子: Architecture SD_BEH of SD is signal C: s
我正在尝试编写一些代码来使用 python 执行一些数据包嗅探 使用 pyshark.I 有以下一段代码: import pyshark print('Pyshark demo') capture =
**披露 - 我是一名初学者 C 程序员,并不完全熟悉如何解释手册页,也不完全熟悉谈论 C 时正确的单词选择 - 对这个问题的任何编辑或澄清都是感谢。* 问题:我正在阅读有关 signal(7) 的手
我的应用程序运行完美,没有任何问题。但是当我尝试调试它时,android studio 卡住了。所以我收到消息“等待调试器”,然后该消息消失,接下来我在模拟器中只看到黑屏。 我也收到了消息 SIGNA
我在 Laravel 5.7 中使用 Snappy PDF 库。在本地一切正常,但在我的开发环境中出现此错误:该进程已收到信号“11”的信号。到现在为止,我找不到可以帮助我的解决方案。这是我在 Con
当我想在 linux 上激活 mongo 时,我得到了这个错误,它不会工作 $ sudo systemctl status mongod ● mongod.service - MongoDB 数据库服
我正在尝试设置函数超时,但我无法成功。 我运行来自 https://docs.python.org/3/library/signal.html?highlight=signal%20sigalrm#e
我在尝试使用 Phantomjs 和 Symfony 的 Process 和 Reposonse 文件创建 PDF 文档时遇到此错误。 这是我收到的错误信息 fatal error :未捕获的异常 '
我正在尝试调试一个复杂的 Perl 应用程序,该应用程序以错误消息“收到信号 SIGCHLD,但未设置信号处理程序”而终止。我知道它来自 Perl 解释器本身,特别是来自文件 mg.c它不能被捕获。但
我有以下代码,它按预期工作: import signal def printer(signum, frame): print("hi!") signal.signal(signal.SIGAL
我是一名优秀的程序员,十分优秀!