wait() 系统调用上出现信号 11 段错误?

发布于 2024-08-14 00:05:55 字数 680 浏览 5 评论 0原文

我正在用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

策马西风 2024-08-21 00:05:55

您可能将状态参数的无效指针传递给 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).

夜巴黎 2024-08-21 00:05:55

查看调用 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文