|
Listing Directories and Closing Pipes |
|
Providing a way to list just directories, closing off an unclosed pipe, the history of memory, and more By Ray SwartzNadine Palmer asks for a way to list only the directories in
the current directory. I provide a Getting a Short ListQuestion: Is there some way to list just the directories in a directory? Nadine Palmer / Knoxville, Tenn.Answer: Listing out directories seems like something
that would be handled by one of Though The Pipes Are FrozenQuestion: I'm writing a C++ class named file. I want it to be able to read the standard output of a command, such as: open(file, "/bin/ls -l /dev |"); I wrote the code [Listing 2A] to implement this. It almost works. The problem is that it doesn't terminate. Can you help? Ricardo Pinninghoff / Osorno, ChileAnswer: There are two errors with your program, both
of them subtle. The first problem is the result of the As a result, the pipe is open for writing by two processes. A read() on a pipe (line 9) detects an end of file only if the writing end of the pipe has been closed by all the processes with write access to the pipe. Thus, even though the child process closes the write end of the pipe when it exits, the parent process never closes fd[1], its write end of the pipe. The parent process doesn't terminate because the read() that controls the while loop in line 9 is waiting for something to read from the pipe whose write end is still open. To get the parent to terminate, you need to close the write
end of the pipe before you start reading from it with the call
Another small error in this program gives some repeated
output. In line 10, When the pipe is full of output, The error is evident on the last call to The solution is to store the number of characters that
The changes required to close the pipe's write end in the parent process and to correct the output error are shown in Listing 2B. I've only listed lines 3-11 because the changes are all within that range. The added lines are noted by a lower case ``a'' placed after the line number. Changed lines are marked with an asterisk. Thanks for the MemoryAndy Young from San Diego had this comment about my September 1993 column on avoiding core files. He writes: Sorry to ``dump'' on you, Ray, but you obviously weren't around when computers used real core memory. The cores were ferrite, but they went out of favor about the time disks came into wide-spread use. So there was no such thing as dumping core to disks. Alas, it got dumped to the line printer, which meant we had to wait about half an hour for a six-inch stack of paper to come chugging off at 120 lines per minute. A lot of us learned to do octal arithmetic and annotate those core-dump listings with manual relocation of addresses to figure out what had gone wrong. But, if we only got one run per day, it was worth a couple hours of this drudgery to find the bug. Rather than recompile the program and wait yet another day to run it, we would use a hand punch to fix the bad instruction. Or, if a hole had to be covered up, we'd cover them up with little red squares of sticky tape. However, the operators didn't like us to do that because the little red squares had a tendency to come off when the cards went through the card reader. More About ZombiesNeil Cook of Nottingham, England, comments about Jon Hanrath's feedback about zombie processes. Neil writes: In the July 1993 issue, Jon Hanrath suggests that another way
to clear zombie processes is to explicitly ignore the child
processes using This solution only works under System V-flavored Unix, it is
not the behavior seen with BSD Unix. The best way to stop
zombies in BSD is to use the A Random CorrectionAllan Johnson Jr. spotted an error in the October 1993 column about getting the Korn shell to supply random numbers to a program that printed random sayings from a file. He writes: In your answer, ``A Random Comment,'' you showed how to
generate a random number between 1 and 25,000 using the Korn
shell's Note that this same approach will work outside the Korn shell. In the C shell on my system, I ran the command shown [in Listing 4]. It returns a different number after each invocation. |
Print This Page Send as e-mail |












