473,327 Members | 2,071 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

fread() blocks when reading pipe with closed write end

I am confused by the behavior of the following code. The function
copy() takes two FILE *'s and copies the data from one to the other.
In the main routine, I create a pipe and copy stdin to the write end
while copying the read end to stdout. If the child handles copying the
read end of the pipe to stdout, the main program returns. If the child
handles copying stdin to the write end of the pipe, the parent hangs in
copy(), blocking on fget() (or fread()). I've executed in gdb and
watched the child close the write end of the pipe before the parent
ever gets to the read, read the data off the pipe, and then block
indefinitely. The write end is closed, why doesn't fget() or fread()
return EOF? And why does the behavior change when the role of
parent/child is reversed? (replace "if(pid)" with "if(!pid)")

I've removed most of the error checking from this for clarity, but
point out that the fclose() succeeds. (I've tried replacing fclose
with close(filedes[1]), to no affect.)

#include<stdio.h>
#include<inttypes.h>

void
copy(FILE *in, FILE *out)
{
int x;
while( (x = fgetc(in)) != EOF)
if (fwrite( &x, sizeof(x),1,out) != 1)
break;
/*
while( fread(&x, sizeof(x),1,in) == 1)
if (fwrite( &x, sizeof(x),1,out) != 1)
break;
*/
return;
}

int
main()
{
int filedes[2];
FILE *a, *b;
int pid;

pipe(filedes);
a = fdopen(filedes[0],"r");
b = fdopen(filedes[1],"w");
pid = fork();
if (!pid) {
copy(a, stdout);
}
else {
int err;
copy (stdin, b);
err = fclose(b);
fprintf(stderr,"fclose returned: %d\n",err);
}
}
~

Nov 15 '05 #1
4 7753
>I am confused by the behavior of the following code. The function
copy() takes two FILE *'s and copies the data from one to the other.
In the main routine, I create a pipe and copy stdin to the write end
while copying the read end to stdout. If the child handles copying the
read end of the pipe to stdout, the main program returns. If the child
handles copying stdin to the write end of the pipe, the parent hangs in
copy(), blocking on fget() (or fread()). I've executed in gdb and
watched the child close the write end of the pipe before the parent
Did the *parent* close the write end of the pipe?
ever gets to the read, read the data off the pipe, and then block
indefinitely. The write end is closed,
Are *ALL* of the write ends closed?
why doesn't fget() or fread()
return EOF? And why does the behavior change when the role of
parent/child is reversed? (replace "if(pid)" with "if(!pid)")


Gordon L. Burditt
Nov 15 '05 #2
No, and no. Thank you! That was quite frustrating.

Nov 15 '05 #3
bill wrote:
No, and no. Thank you! That was quite frustrating.


What and what? Thank who? What was?

Brian

--
Please quote enough of the previous message for context. To do so from
Google, click "show options" and use the Reply shown in the expanded
header.
Nov 15 '05 #4
On 27 Oct 2005 11:03:18 -0700, "bill" <bi**********@gmail.com> wrote:
I am confused by the behavior of the following code. The function
copy() takes two FILE *'s and copies the data from one to the other.
In the main routine, I create a pipe and copy stdin to the write end
while copying the read end to stdout. If the child handles copying the
read end of the pipe to stdout, the main program returns. If the child
handles copying stdin to the write end of the pipe, the parent hangs in
copy(), blocking on fget() (or fread()). I've executed in gdb and
watched the child close the write end of the pipe before the parent
ever gets to the read, read the data off the pipe, and then block
indefinitely. The write end is closed, why doesn't fget() or fread()
return EOF? And why does the behavior change when the role of
parent/child is reversed? (replace "if(pid)" with "if(!pid)")

I've removed most of the error checking from this for clarity, but
point out that the fclose() succeeds. (I've tried replacing fclose
with close(filedes[1]), to no affect.)

#include<stdio.h>
#include<inttypes.h>

void
copy(FILE *in, FILE *out)
{
int x;
while( (x = fgetc(in)) != EOF)
if (fwrite( &x, sizeof(x),1,out) != 1)
In addition to any other problems, you read one byte but write more
than one (probably four).
break;
/*
while( fread(&x, sizeof(x),1,in) == 1)
if (fwrite( &x, sizeof(x),1,out) != 1)
break;
*/
return;
}

int
main()
{
int filedes[2];
FILE *a, *b;
int pid;

pipe(filedes);
a = fdopen(filedes[0],"r");
b = fdopen(filedes[1],"w");
pid = fork();
if (!pid) {
copy(a, stdout);
}
else {
int err;
copy (stdin, b);
err = fclose(b);
fprintf(stderr,"fclose returned: %d\n",err);
}
}
~

<<Remove the del for email>>
Nov 15 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Bernhard Kuemel | last post by:
Hi! I want to read/write commands and program input to/from /bin/bash several times before I close the stdin pipe. However, reading from cat hangs unless I first close the stdin pipe. <?php...
0
by: Christian Hammers | last post by:
Hello I would like to call a unix shellscript from within a PHP script and - write data to its STDIN - read data from its STDOUT *and* STDERR - get its exit code afterwards proc_open seems...
2
by: Luc Holland | last post by:
Hey, I'm working on a program that reads a binary file. It's opened with ==== if ((f1=fopen(argv,"rb"))==NULL) { fprintf(stderr,"Error opening %s for reading . . .\n",argv); exit(2); } ====...
2
by: Josh Wilson | last post by:
The fread() is what I believe is having trouble, but I do not know why. I know that the temp file is created and written to (and w/ the appropriate amount of data). fread() continues to return 0,...
6
by: Patrice Kadionik | last post by:
Hi all, I want to make a brief comparison between read() and fread() (under a Linux OS). 1. Family read and Co: open, close, read, write, ioctl... 2. Family fread and Co: fopen, fclose,...
5
by: David Mathog | last post by:
When reading a binary input stream with fread() one can read N bytes in two ways : count=fread(buffer,1,N,fin); /* N bytes at a time */ or count=fread(buffer,N,1,fin); /* 1 buffer at a...
13
by: bayer.justin | last post by:
Hi, I am trying to communicate with a subprocess via the subprocess module. Consider the following example: <subprocess.Popen object at 0x729f0> Here hey is immediately print to stdout of...
20
by: ericunfuk | last post by:
If fseek() always clears EOF, is there a way for me to fread() from an offset of a file and still be able to detect EOF?i.e. withouting using fseek(). I also need to seek to an offset in the file...
15
by: =?ISO-8859-15?Q?L=E9na=EFc?= Huard | last post by:
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Hello all, For some reasons, somewhere in a program, I'd like, if possible, to quickly parse a whole file before rewinding it and letting the...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.