views:

160

answers:

2

I'm new to processes in *nix, and am working on a basic shell in C... in implementing 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, but I've apparently changed something to break 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 should the child have already terminated. So why, and in what sort of cases, would wait() cause a segfault?

Alternatively, how would I go about debugging this sort of thing?

+1  A: 

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.

unwind
+1  A: 

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

atomice
I kept wondering why in the world wait() would be accessing an invalid memory location. Silly me, this was it. pebkac!
Rob