EOF not detected while reading a pipe

  • Permalink
  • submit to reddit
  • Email
  • Follow


I have a problem with implementing pipe in my own shell (written in
C++). Consider the command ls | sort. The child process executes ls (by
fork and exec), writes the output to a pipe(stdout is associated with
it) and parent process executes sort by reading input from the
pipe(stdin of this process is associated with the read end). The
problem is that the parent reads all available data from pipe but it is
still waiting for further data.i.e EOF is not detected by the parent
process. Since the child process is doing exec, I'm not able to close
the pipe in child...How can I solve the problem???

0
Reply eanbazhagan 8/8/2004 6:30:44 PM

See related articles to this posting


eanbazhagan@rediffmail.com wrote:
> I have a problem with implementing pipe in my own shell (written in
> C++). Consider the command ls | sort. The child process executes ls (by
> fork and exec), writes the output to a pipe(stdout is associated with
> it) and parent process executes sort by reading input from the
> pipe(stdin of this process is associated with the read end). The
> problem is that the parent reads all available data from pipe but it is
> still waiting for further data.i.e EOF is not detected by the parent
> process. Since the child process is doing exec, I'm not able to close
> the pipe in child...How can I solve the problem???

Did the writer close the file ?

Perhaps you can show us some code or even the output of strace ?

G
0
Reply Gianni 8/8/2004 2:37:03 PM

eanbazhagan@rediffmail.com wrote:
> I have a problem with implementing pipe in my own shell (written in
> C++). Consider the command ls | sort. The child process executes ls (by
> fork and exec), writes the output to a pipe(stdout is associated with
> it) and parent process executes sort by reading input from the
> pipe(stdin of this process is associated with the read end). The
> problem is that the parent reads all available data from pipe but it is
> still waiting for further data.i.e EOF is not detected by the parent
> process. Since the child process is doing exec, I'm not able to close
> the pipe in child...How can I solve the problem???
> 

It sounds like the write end of the pipe is still being held open by a 
process.  Are you perhaps creating the pipe in the parent and not 
closing the parent's copy of the write end?

-- ced

-- 
Chuck Dillon
Senior Software Engineer
NimbleGen Systems Inc.
0
Reply Chuck 8/9/2004 1:48:00 PM
comp.unix.programmer 10672 articles. 64 followers. Post

2 Replies
1153 Views

Similar Articles

[PageSpeed] 44


  • Permalink
  • submit to reddit
  • Email
  • Follow


Reply:

Similar Artilces:

EOF on reading a pipe when there is a writer
Hello, I need to capture a copy of the output of a script into a file, and I try to use 'tee' for that: bash my_script | tee -a logfile Now it turns out that my_script (which is not really "my" script) starts a process that does not terminate (and should not terminate) Actually, I should rather start by the symptoms: The script that contains the above pipeline never goes on afterwards. That is, I have a script containing bash my_script | tee -a logfile echo "Hello World" and it never outputs "Hello World". When I attach the process with stra...

Reading from pipe with perl -na -e and detecting last line
Hello netties, I cat a file (actually grepping some particular lines) to "perl -na -e" and have to determine when I have read the last line of the content of the file (which is actually "filtered" by grep already, so that I cannot give perl the whole file as a command line argument). Does somebody know how I detect the last line piped to perl? eof() obviously does not work. Cheers Bernd Quoth "bernd" <bew_ba@gmx.net>: > > I cat a file (actually grepping some particular lines) to "perl -na -e" > and have to determine when I have re...

broken pipe when reading and writing a pipe
hi I am trying to do a simple task: I have a buffer, I want to write this buffer as the stdin of a command, say "tac", and capture the output from the stdout and save to another buffer. after searching examples and playing a little bit, I wrote the a function (see below). However, it gave me a "Broken pipe" error when I ran the code. Can someone point out to me what was wrong? your help is greatly appreciated! Qianqian int pipemydata(void *inbuf, long inlen, void *outbuf, long *outlen){ int fd[2]; int childid; *outlen=0; if(pipe(fd)==-1...

EOF detection
Is it guaranteed that if return value of read system call (for read request of gerater than 0 bytes) is 0, eof has been reached. eg. ret = read(fd,buf,10); if (ret == 0) printf("\nend of file is reached\n"); if (ret < 0) ...handle error if (ret > 0) ... handle bytes read junky_fellow@yahoo.co.in writes: >Is it guaranteed that if return value of read system call (for >read request of gerater than 0 bytes) is 0, eof >has been reached. For plain files that is the case, for special files it depends on other factos, as, e.g., in the case of Solaris reads fro...

Detecting EOF
Hello, I'm trying to detect the EOF for scanf. But for some reason I can't get it to trigger. What am I doing wrong? Thanks. int puzzle[9][9]; // Puzzle data structure int i, j, count; // Iteration variables int temparr[81]; char x; int test; count = 0; while (count < 82){ scanf("%c", &x); if (x == EOF) { printf("FUCK!"); printf("%d", count); return 0; } test = x; if (test >= 48 && test <= 57) { test = test - 48; temparr[count]= test; count++; ...

reading from pipe
Hi, I have a program that reads from a named pipe. If there is nothing to read the program is waiting but I would like the program to proceed when there is nothing to read. How can I do that? I would be appreciative for any help. Regards Rolf Wester Greetings. In article <bmo68s$p9i$1@nets3.rz.RWTH-Aachen.DE>, Rolf Wester wrote: > I have a program that reads from a named pipe. If there is nothing to > read the program is waiting but I would like the program to proceed when > there is nothing to read. How can I do that? > I would be appreciative for any help. Try aski...

reading from pipe
Hello, is there any solution to catch if a pipe has closed? Maybe the signal modul? For Simulation: #!/usr/bin/env python # -*- coding: utf-8 -*- import sys while True: line = sys.stdin.readline() sys.stdout.write(line) sys.stdout.flush() time cat /tmp/proxy.test | test.py Solution: if line == "": break Kind Regards, Richi Hi, Richard Lamboj schrieb: > is there any solution to catch if a pipe has closed? Maybe the signal modul? Since sys.stdin is a file object, you can use sys.stdin.closed to check if it has been closed. Lutz ...

Detecting EOF
Hello, I'm working on a streaming data server which accepts a query, then sends any matching data that comes in after the query was submitted. The problem I'm having is that when a client sends a query then later goes away, I'm not always able to detect the situation to cancel their query. I can usually detect a client is gone when I get EOF when reading or writing to it, but I have nothing to read after the initial query, and as long as no data matches the query, nothing to write either. My implementation uses a BufferedReader and BufferedWriter to interact with the client, so ...

EOF on pipe
This problem occurs on both Free BSD and Linux. I have a parent-child situation, where the parent creates two pipes, and then forks(), then the child exec's a sort. The parent then reads the pipe to get the sorted output. SO... parent: establishes 2 pipes pfd1 and pfd2 which have both read and write ends. Parent writes records to The write end of the pipe connected to STDIN of the child. child: dup2() the pipe ends, the read end of pfd1 to STDIN and the write end of pfd2 to STDOUT. The child then execlp() /usr/bin/sort -n. Sort reads the input, does it's...

read() and EOF
The linux man pages say that read() returns a 0 at end of file. Is there a guarantee that it will continue to do that on subsequent read() from the same fd, or is it conformant to return a 0 once, and then an error after that? (I vaguely recall seeing that there are situations where EOF is only returned once, but I could be mistaken.) Thanks, David Mathog David Mathog <mathog@caltech.edu> writes: > The linux man pages say that read() returns a 0 at end of file. Is > there a guarantee that it will continue to do that on subsequent > read() from the same fd, or is it con...

Reading Until EOF
Hi, everyone, I'm a Python novice and would love some tips on how I should perform the following task: I'd like to have my Python script read objects (using their constructors) and terminate gracefully when an EOF is encountered. My first attempt looked like this: # This is enclosed in a 'try' block file = open(...) while 1: # The Object constructor raises an exception for end-of-file object = Object(file) self.list = self.list + [object] However, I'm not entirely comfortable having the Object constructor raise an exception when the end-of-file is hi...

need to close stdin pipe from proc_open() before reading stdout pipe??
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 $descriptorspec = array( 0 => array("pipe", "r"), // stdin is a pipe that the child will read from 1 => array("pipe", "w"), // stdout is a pipe that the child will write to 2 => array("pipe", "w") // stderr ); $process = proc_open("/bin/bash", $descriptorspec, $pipes); if (is_resource($process)) { // ...

reading from a pipe
Hi, I'd like to know how to read some bytes from a pipe or some other non-regular file such as device. I had a look at streams_io, direct_io, etc. but was not able to code something working. How do we do that in Ada ? On Wednesday, February 27, 2013 11:36:32 AM UTC-6, milouz wrote: > Hi, > > > > I'd like to know how to read some bytes from a pipe or some other non-regular file such as device. I had a look at streams_io, direct_io, etc. but was not able to code something working. > > > > How do we do that in Ada ? The following is a p...

Timeout when reading from a pipe
Hi, I would like to launch a process (external executable) and raise an exception if I cannot read any output from it on the stdout. Is that possible? Thanks in advance def execute_mencoder(command) IO.popen(command) do |pipe| pipe.each("\r") do |line| puts line end end raise MediaFormatException if $?.exitstatus != 0 end -- Posted via http://www.ruby-forum.com/. On Jun 23, 2008, at 8:30 AM, Me Me wrote: > Hi, > I would like to launch a process (external executable) and raise an > exception if I cannot read any output from it on the stdout. > I...

Detecting EOF #2
int puzzle[9][9]; // Puzzle data structure int i, j, count; // Iteration variables int temparr[81]; char x; int test; count = 0; while (count < 82){ scanf("%c", &x); if (x == EOF) { printf("END!"); printf("%d", count); return 0; } test = x; if (test >= 48 && test <= 57) { test = test - 48; temparr[count]= test; count++; } } ...

Reading from pipe or from stdin, how?
Hello. I have a program where you specify the name of the file you want to read from command line. But, I want to modify it so that if no file is specified, it reads from stdin. so here's a code snippet: my $file = ""; # Parse command line to get file option. open( FILE, "<", $file) or die "Could not open $file\n"; my $line; while( ($file && $line = <FILE> ) || (!$file && $line = <STDIN>)) { do_big_loop_here; } This doesn't seem to work: Unix> echo "blah" | ./pxmlgrep Value of <HANDLE> con...

Read from a pipe (stdin)
The "data" I'd like to read into my matlab program is the output of a C program. I'd like to be able to pipe the output of the C program into matlab and have my .m file process that data as it comes in. I know I can write the output of the C program to disk and then read that file in. But the output is likely to be very large so I don't want to use that route. Is this possible? If so, how? Thanks. Here's a solution that I found to work well in a Unix environment. (1) In the Unix shell, create a named pipe using the "mkfifo" command, for example: $...

Detecting eof #2
Hi All, I am using this segment to read a file: String str while ((str=in.readLine()!=eof) but I get error because of eof (I want to detect end of file). How can I use it? Also is there any way to detect blank lines in a file? for example: a b If I read the second line is it equal to null or something elase? Thanks for your help Jack jack.smith.sam@gmail.com wrote: > Hi All, > > I am using this segment to read a file: > > String str > while ((str=in.readLine()!=eof) What is /in/? BufferedReader? I suspect that you want to do: while((str = in.readLine()) != null) ...

How to read past EOF
One of the previous week's posts posed a problem which, as it turns out, was a problem of trying to read a file that had an errant EOF character in the middle of the file. Thus, if the eof was at record 10 of 20, SAS would only read the first 10 records. DOS copy and list would only show 10 records, as well, but Notepad would show all twenty records. Upon bringing up the file in Notepad, one could delete the square-looking character, save the file, and then SAS would read in all 20 records. Anyone have a clue as to how such a problem could be solved directly with SAS? Of course, if an...

perl and eof detection
I'm trying to do something similar to 'tail -f', in that I want to read a file and when new data appears read/print it. Consider the following and you will see I simply test for eof and if there's data there read/print and if now, test the eof again. To make this easier to fiddle with, I added a sleep 1 between eof() calls. I also removed the print I had after the #!/usr/bin/perl -w open TEST, "<test" or die; while (1) { printf "EOF: %d\n", eof(TEST); while (!eof(TEST)) { $line=<TEST>; print $line; } ...

Blocking read on pipes
Hello, I have a problem when reading from stdin (using it as pipe) in a Python program (the sender is written in C): C (sender) write(1, buffer, 4) Python (recv.py) sys.stdin.read(256) or sys.stdin.read() sender | recv.py Python's read blocks the program because the written buffer length (4) is lower than the indicated in read function (256), but when I do the same with C (read(0, buffer, 256)) it is not blocking, just returns the four bytes. That is what I need. I've tried to flush the stdout in the sending program, but it does the same. any ideas? thanks, arnau ...

How do I detect if input is from a pipe or not?
I'm writing a utility that can get input from a file or from a pipe'd output from another program: myprogram -infile test.txt or type test.txt | myprogram So for the above two scenarios, I have: if ($opt_infile) { # read from file } else { while (<>) { #read from stdin } } The problem is, if I just run myprogram it sits there waiting for input. What I really want is for it to detect that nothing is coming in from a PIPE so it should complain that the program was run incorrectly. I've searched and I've seen stuff on named pipes and getting output by calling a...

how to detect broken pipe ?
Hi Scenario: I have a shell script e.g. a.bash This script wraps a program called "generate" like this: generate > /tmp/y.txt 2>&1 # both stdout and stderr r redirected to y.txt Now , I have perl script e.g. myPerl.pl and I modify a.bash such that: generate | perl myPerl.pl # pipe output from "generate" to the perl script. And in myPerl.pl, I wrote this : sub whatSignal { print "caught ",shift, "\n" }; $SIG($_}=\ &whatSignal for keys %SIG And now I run a.bash in one xterm: Following happens: When press ctrl-C, it is caught. But whe...

read from a named pipe?
If a named pipe is created with mknod or mkfifo, how do you read from the pipe? I've tried: open(FIFO, '< /tmp/.fifo'); while(<FIFO>) { ... } close FIFO; but it doesn't seem to work. Mike Mike wrote: > If a named pipe is created with mknod or mkfifo, how do you read > from the pipe? I've tried: > > open(FIFO, '< /tmp/.fifo'); > while(<FIFO>) { > ... > } > close FIFO; > > but it doesn't seem to work. > > Mike What happens when you do: open(FIFO, '< /tmp/.fifo') or die( "Error: $!\n&q...