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