pipe() problem - one reader, multiple writers?

pipe() problem - one reader, multiple writers?

Post by Swati Pate » Fri, 31 Jan 1997 04:00:00



Hi
I want to have a process (a daemon) fork off children whenever a certain
event happens and then to have the children write back some data when
they have finished.

I tried to implement this with the parent creating a pipe when it
initialised and then forking children when necessary.  I pictured the
parent reading from the pipe and each child being able to write to it,
but I can't get this to work.  I keep getting EBADF for the child pipe
write.

This is how my program looks:

 create_pipe
 while (1) {
        while (nothing_to_do)
                ; /* parent waits */    
        fork_off_child  /* something arrives */
                child_processes_event
                child_writes_to_parent
                child_exits
        parent_continues        

Quote:}

Is it not possible to have one pipe with multiple children writing and
the parent reading?
Thanks
Swati

 
 
 

pipe() problem - one reader, multiple writers?

Post by Ken Pizzi » Wed, 05 Feb 1997 04:00:00




Quote:>This is how my program looks:

> create_pipe
> while (1) {
>    while (nothing_to_do)
>            ; /* parent waits */    
>    fork_off_child  /* something arrives */
>            child_processes_event
>            child_writes_to_parent
>            child_exits
>    parent_continues        
>}

It's hard to debug your problem at this high a level of pseudo-code.

Quote:>Is it not possible to have one pipe with multiple children writing and
>the parent reading?

It is possible.

   int p[2];
   if (pipe(p)==-1) error();
   for (;;) {
     pid_t child_pid;
     parent_wait_for_something_to_do();
     if ((child_pid=fork()) == -1) error();
     if (child_pid == 0) {
        close(p[0]);  /* child doesn't need it */
        child_process_event();
        write(p[1], some_data, some_data_len);
        exit(child_exit_status);
     }
     parent_continuation_code(p[0]);
     while (waitpid(-1, NULL, WNOHANG) != -1)
        ;
   }

Note, however, that the parent will never see end-of-file with this
setup.  (The parent keeps p[1] open in case it might fork some child
which would write on it, and so there is always at least one data
source open.)  Also, you will likely need to take care that your writes
are no larger than PIPE_BUF chars per message.

                --Ken Pizzini

 
 
 

1. Fork multiple readers on one file, multiple writers on another?

Hello all,

I have a pretty basic question which I hope you can help me with.
I've read a number of man pages & searched Google for links, but
I haven't been able to come up with a solution.

I want several processes (created by fork) to read one file and
write another file. Each process is assigned one block in the
input file and one block in the output file -- the blocks don't
overlap.

The problem is that with more than 1 process, the processes step
on each other's toes. I guess this must be because all i/o must
eventually go from/to the same physical file. I believe what's
directly causing trouble is a shared offset -- I want each process
to seek to its block in the input or output, and then read and
write from there; it doesn't seem to work that way, though.

What is the appropriate way to coordinate multiple readers &
writers which don't share memory? If I were using pthreads, I
could use a pthreads mutex to coordinate. However, I need to
have a non-shared heap, so pthreads is not feasible, I think.

The relevant snippet of code is shown below. I've tried moving
the fopen's before the fork loop, and I've tried putting flock
(on both files) around the reading & writing, but that doesn't
help.

Any suggestions? Thanks in advance. I appreciate it.

best,
Robert Dodier

PS. I'm working on Linux, but I need something to run on other Unices.

---------------- begin relevant code -----------------
    for ( j = 0; j < nprocesses; j++ )
    {
        if ( (child_pid = fork()) == 0 )
        {
            (in  = fopen( infile,  "r" )) != 0 || die( infile );
            (out = fopen( outfile, "a" )) != 0 || die( outfile );

            fseek( in,  j*bs, SEEK_SET );
            fseek( out, j*bs, SEEK_SET );

            for ( i = 0; i < bs; i++ )
                fputc( fgetc(in), out );

            if ( j == nprocesses-1 )
            {
                off_t rem = size - (j+1)*bs;

                for ( i = 0; i < rem; i++ )
                    fputc( fgetc(in), out );
            }

            return 0;
        }
    }

2. R/W problem with NFS-mounted directories

3. Semaphores, one writer, multiple readers? (Linux)

4. remove the start up programs

5. multiple readers, one writer on shared memory

6. Diamond Stealth 32; HELP!

7. Disk based buffer, one writer, many readers problem

8. Applications & Gnomen/kde?

9. 1 pipe, many writers, 1 reader

10. flash media reader/writer , which one works with Linux

11. "tee", but with fast writer, 1 slow reader and 1 fast reader

12. Multiple readers on 1 pipe

13. Pipe example with multiple readers