public inbox for cygwin@cygwin.com
 help / color / mirror / Atom feed
* RE: Possible Bug in Pipes with Win95
@ 1997-10-28  0:12 Sergey Okhapkin
  0 siblings, 0 replies; 3+ messages in thread
From: Sergey Okhapkin @ 1997-10-28  0:12 UTC (permalink / raw)
  To: 'gnu-win32@cygnus.com', 'David Devejian'

David Devejian wrote:
> I am having trouble implementing pipes with win95, namely, the process 
reading the pipe does not see the EOF when the writing process closes the 
file.
>
> With plain cygwin32 b18, the program would sometimes blow-up, sometimes 
hang, and occassionally run correctly.  With the installation of the 
coolview patches, the error is atleast consistent, the program hangs after 
all data has been read from the pipe, but never detects the EOF.
>

It's well known programmer's mistake :-) Close read end of pipe in the 
parent after fork(), and write end in the child.

--
Sergey Okhapkin, http://www.lexa.ru/sos
Moscow, Russia
Looking for a job.


-
For help on using this list (especially unsubscribing), send a message to
"gnu-win32-request@cygnus.com" with one line of text: "help".

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Possible Bug in Pipes with Win95
@ 1997-10-28  8:31 marcus
  0 siblings, 0 replies; 3+ messages in thread
From: marcus @ 1997-10-28  8:31 UTC (permalink / raw)
  To: gnu-win32, widsith

First off, there is a problem in the logic of your test program.

   ...

  /*  Create the pipe. */
  status = pipe (mypipe);
  if (status) 
    { 
      fprintf (stderr, "Pipe failed.\n"); 
      return EXIT_FAILURE; 
    } 
  
  /* Create the child process. */
  pid = fork ();

  if (pid == (pid_t) 0) 
    { 
      /* This is the child process. */
      read_from_pipe (mypipe[0]); 
      fprintf(stdout, "Child:  Exiting. . . ");
      exit(EXIT_SUCCESS); 
    } 

    ...

The pipe is created, then fork() is called.  At this point, both processes
have a copy of the read end and write ends of the pipe.  Now, the child
starts reading from the read end of the pipe and waits for EOF.  Note that
EOF occurs when all writers have closed the pipe and both the parent and
child process have a write end open, so the child should never see EOF
until it closes its write end of the pipe, which it never does.

Also, just a minor style point here, I would never call a file descriptor
"file", since a stream is of type FILE *.  Traditionally, it is "fd" or
something else ending in "d".  Not that it really makes any difference for
functionality, but just for readability by other Unix types..

marcus hall
-
For help on using this list (especially unsubscribing), send a message to
"gnu-win32-request@cygnus.com" with one line of text: "help".

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Possible Bug in Pipes with Win95
@ 1997-10-27 16:34 David Devejian
  0 siblings, 0 replies; 3+ messages in thread
From: David Devejian @ 1997-10-27 16:34 UTC (permalink / raw)
  To: 'gnu-win32@cygnus.com'

I am having trouble implementing pipes with win95, namely, the process reading the pipe does not see the EOF when the writing process closes the file.

With plain cygwin32 b18, the program would sometimes blow-up, sometimes hang, and occassionally run correctly.  With the installation of the coolview patches, the error is atleast consistent, the program hangs after all data has been read from the pipe, but never detects the EOF.

Below is a listing of a test program I have been using, the execution never reaches the   "fprintf(stdout, "Child: Found end of Pipe.\n");" in read_from_pipe.  The parent executes normally and hangs on the "waitpid" command.

If anyone has experiences something similar, please let me know.

-Dave Devejian
widsith@panix.com


#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>

void
read_from_pipe (int file)
{
  FILE *stream;
  int c;
  stream = fdopen(file, "r");
  fprintf(stdout, "Child: Open Pipe Successful.\n");
  while (!feof(stream)) {
    c = fgetc (stream);
    putchar (c);
  }
  fprintf(stdout, "Child: Found end of Pipe.\n");
  fclose (stream);
  fprintf(stdout, "Child: Closed Pipe.\n");
}
     
/* Write some random text to the pipe. */
     
void
write_to_pipe (int file)
{
  FILE *stream;
  stream = fdopen (file, "w");
  fprintf (stream, "hello, world!\n");
  fprintf (stream, "goodbye, world!\n");
  fclose (stream);
}

int
main (void)
{
  pid_t pid;
  int status;
  int mypipe[2];

  /*  Create the pipe. */
  status = pipe (mypipe);
  if (status) 
    { 
      fprintf (stderr, "Pipe failed.\n"); 
      return EXIT_FAILURE; 
    } 
  
  /* Create the child process. */
  pid = fork ();

  if (pid == (pid_t) 0) 
    { 
      /* This is the child process. */
      read_from_pipe (mypipe[0]); 
      fprintf(stdout, "Child:  Exiting. . . ");
      exit(EXIT_SUCCESS); 
    } 

  else if (pid < (pid_t) 0) 
    { 
      /* The fork failed. */
      fprintf (stderr, "Fork failed.\n"); 
      return EXIT_FAILURE; 
    } 

  else
    { 
      /* This is the parent process. */ 
      write_to_pipe (mypipe[1]);
      if (waitpid (pid, &status, 0) != pid)
	return EXIT_FAILURE;
      else 
	return EXIT_SUCCESS; 
  } 
}


-
For help on using this list (especially unsubscribing), send a message to
"gnu-win32-request@cygnus.com" with one line of text: "help".

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~1997-10-28  8:31 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1997-10-28  0:12 Possible Bug in Pipes with Win95 Sergey Okhapkin
  -- strict thread matches above, loose matches on Subject: below --
1997-10-28  8:31 marcus
1997-10-27 16:34 David Devejian

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).