C 中的 WIFSIGNALED、WIFSTOPPED、WIFCONTINUED 测试用例
我正在使用 waitpid() 和 signal() ,并且正在寻找返回 WIFSIGNALED(status) = WIFSTOPPED(status) = WIFCONTINUED (status) = true 的可靠测试用例,但找不到任何
...告诉我如何确保这些返回 true 以便我可以调试我的代码?
另外,关于我应该用 signal() 捕获哪些信号来测试这些宏的一些提示会很有帮助......
I'm playing with waitpid() and signal() and I'm looking for reliable test cases for returning WIFSIGNALED(status) = WIFSTOPPED(status) = WIFCONTINUED (status) = true but can't find any...
Care to tell me how can I make sure those return true so I can debug my code?
Also, a few hints about what signals should I catch with signal() to test those macros would be helpful...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
处理 WIFSIGNALED 很容易。 子进程可以通过
kill()
系统调用自杀。 您还可以检查核心转储 - 一些信号创建它们(SIGQUIT、IIRC); 有些信号则不然(SIGINT)。处理 WIFSTOPPED 可能会更困难。 尝试的简单步骤是让子进程再次通过
kill()
系统调用向自己发送 SIGSTOP。 事实上,我认为这应该有效。 请注意,您可能需要检查 SIGTTIN、SIGTTOU 和 SIGTSTOP - 我相信它们适用于 WIFSTOPPED。 (也有可能 SIGSTOP 只有在由调试器通过非 POSIX 系统调用 ptrace() 发送到正在运行的进程时才能正常工作。)我认为处理 WIFCONTINUED 是父级的事情不得不做; 当您检测到进程已停止后,您的调用代码应该通过向其发送 SIGCONT 信号(再次
kill()
)来使其继续。 孩子自己无法做到这一点; 它已被停止。 再说一遍,我不确定是否还有额外的皱纹需要担心——可能吧。Handling WIFSIGNALED is easy. The child process can commit suicide with the
kill()
system call. You can also check for core dumps - some signals create them (SIGQUIT, IIRC); some signals do not (SIGINT).Handling WIFSTOPPED may be harder. The simple step to try is for the child to send itself SIGSTOP with the
kill()
system call again. Actually, I think that should work. Note that you may want to check on SIGTTIN and SIGTTOU and SIGTSTOP - I believe they count for WIFSTOPPED. (There's also a chance that SIGSTOP only works sanely when sent by a debugger to a process it is running via the non-POSIX system call,ptrace()
.)Handling WIFCONTINUED is something that I think the parent has to do; after you detect a process has been stopped, your calling code should make it continue by sending it a SIGCONT signal (
kill()
again). The child can't deliver this itself; it has been stopped. Again, I'm not sure whether there are extra wrinkles to worry about - probably.类似于下面的框架将允许您检查
wait()
和waitpid()
调用的结果。您实际上不必捕获发送的信号(使用
signal()
或相关函数)。signal()
安装一个处理程序,该处理程序会覆盖特定信号的默认行为 - 因此,如果您想检查终止进程的信号,请选择一个具有该默认行为的处理程序 - “man - s7 signal
”将为您提供信号默认行为的详细信息。对于您提到的宏,请使用
SIGSTOP
表示WIFSTOPPED(status)
,使用SIGCONT
表示WIFCONTINUED (status)
和 < code>SIGINT forWIFSIGNALED(status)
如果您想要更灵活的测试,您可以使用kill(参见“
mankill
”)向您的设备发送信号。过程。kill -l
将列出所有可以发送的信号。A framework something like the below will allow you check the results of the
wait()
andwaitpid()
calls.You do not actually have to catch the signals (using
signal()
or related function) that are sent.signal()
installs a handler that overrides the default behavior for the specific signal - so if you want to check for a signal terminating your process, pick one that has that default behavior - "man -s7 signal
" will give you details a signal's default behavior.For the macros you have mentioned use
SIGSTOP
forWIFSTOPPED(status)
,SIGCONT
forWIFCONTINUED (status)
andSIGINT
forWIFSIGNALED(status)
If you want more flexibility for testing, you could use kill (see "
man kill
") to send signals to your process.kill -l
will list all the signals that can be sent.在您的测试中,您可以 fork() 并向您的子进程发送特定信号吗? 在这种情况下,您的子进程是测试用例?
编辑
我的答案是关于编写 C 测试。 你 fork 后,获取你的子进程的 pid(该进程
安装了信号处理程序),然后您可以使用
kill(2)
向其发送信号。这样就可以测试退出状态
in your tests you can fork() and send specific signal to your child processes? In this scenario your child processes are test cases?
EDIT
my answer is about coding a C test. you fork, get the pid of your child process (the process
with signal handlers installed), then you can send signal to it by using
kill(2)
.In this way you can test the exit status