I got some code that prints to stdout, in pseudo code it looks like
int main(){
//allocate data
while(conditional){
char *string = makedata();
fprintf(stdout,"%s",string);
}
//cleanup
}
This works fine, if the conditional is toggled to zero, but if I pipe the output like
./a.out |head -n10 >dumped
Then the code never reaches the cleanup part, I don’t understand how to check if the stdout gets closed.
Thanks
Your stdout hasn’t been closed, so checking for that will be useless. Your program has received a SIGPIPE and exited. A SIGPIPE is delivered whenever your program writes to a pipe on which there are no readers. In your example, that happens when
headexits, closing its stdin.You should ignore SIGPIPE if you want your program to continue. This code will ignore SIGPIPE:
If you don’t want to modify your program, you can arrange that something continues to read from the pipe, even after
headcloses its input. 11: The shell example is valid for bash, maybe not for csh.