Question

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?

Was it helpful?

Solution

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).

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top