wait() 系统调用上出现信号 11 段错误?
我正在用 C 语言开发一个基本的 shell。在我的 管道 的实现中,我计算了命令上线并迭代fork()
一个新进程。
在每次迭代结束时,我都会在子进程上wait()
,然后再继续执行下一个命令。这在早期的代码中工作得很好,但不知何故我破坏了它:
Program terminated with signal 11, Segmentation fault.
#0 0xfef28730 in _waitpid () from /usr/lib/libc.so.1
(gdb) backtrace
#0 0xfef28730 in _waitpid () from /usr/lib/libc.so.1
#1 0xfef28770 in _wait () from /usr/lib/libc.so.1
#2 0xfef696d1 in wait () from /usr/lib/libc.so.1
#3 0x08051428 in main ()
我知道如果子进程已经终止, wait()
将简单地收获僵尸进程。
为什么以及在什么情况下 wait()
会导致段错误?我该如何调试这种事情?
I'm working on a basic shell in C. In my implementation of pipes, I count the commands on the line and iteratively fork()
a new process.
At the end of each iteration I wait()
on the child before proceeding to the next command. This was working fine in earlier code, but somehow I've broken it:
Program terminated with signal 11, Segmentation fault.
#0 0xfef28730 in _waitpid () from /usr/lib/libc.so.1
(gdb) backtrace
#0 0xfef28730 in _waitpid () from /usr/lib/libc.so.1
#1 0xfef28770 in _wait () from /usr/lib/libc.so.1
#2 0xfef696d1 in wait () from /usr/lib/libc.so.1
#3 0x08051428 in main ()
I understand that wait()
will simply reap the zombie process if the child has already terminated.
Why, and in what cases will wait()
cause a segfault? How do I go about debugging this sort of thing?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可能将状态参数的无效指针传递给 wait(2)。
至于如何调试这类事情,我的第一步是为你的 C 库安装调试符号。然后查看哪个指针出错,并在堆栈中跟踪它(如果可能)。
You are probably passing an invalid pointer for the status argument to wait(2).
As for how to debug this sort of thing, my first step would be to install the debugging symbols for your C library. Then look at which pointer its faulting on and trace it back up the stack (if possible).
查看调用 wait() 所用的参数,并查找内存覆盖问题。通过 Valgrind 运行您的程序,以帮助您轻松检测许多覆盖。
Look at the arguments you're calling wait() with, and also look for memory overwrite issues. Run your program through Valgrind to get help detecting many overwrites very easily.