Why does using write(2) with more bytes than in the string keeps child process open?

Viewed 75

I had to implement a shell for my OS class. I used write(2) with some string, say "write this", to STDOUT and wrote a larger byte length, say 20, than the string that I wanted to write. I read the man page and it seemed to me using a larger number would be fine.

I then forked, waited in the parent, and used execve(2) on some input, while in the child. This should exit the child regardless of its success or failure, but just in case, I put an exit() command right after it.

The parent set an alarm before forking, and used SIGINT to kill the child if the execve took too long to complete.

This was all in a loop.

I found that my child processes where somehow staying open, via print debugging, and going to the top of the loop. When I fixed my write(2) so that I used the correct number of bytes that my output string had, I found out the problem was fixed. To be clear my alarms, forking, execve-ing, and everything else worked fine, the only thing that caused the children staying open was the incorrect byte count argument to write(2).

What causes this behavior? I couldn't fin anything in the write(2) man page or others that would indicate aside from possibly the child sharing file descriptors with the parent, but I'm not sure if this is at all related.

0 Answers
Related