Re: Process Synchronization using Pipes



On Mar 26, 10:20 am, James Antill <james-netn...@xxxxxxx> wrote:
On Sun, 25 Mar 2007 19:33:32 -0700, Arturo wrote:
On Mar 25, 8:45 pm, Barry Margolin <bar...@xxxxxxxxxxxx> wrote:
In article <1174862813.738202.285...@xxxxxxxxxxxxxxxxxxxxxxxxxxxx>,

"Arturo" <arturo.roto...@xxxxxxxxx> wrote:
Hey,

I need to have 4 processes: Producer, Filter1, Filter2 and Consumer.
Producer needs to read data from an input file line by line and pass
each line via a pipe to Filter1 who will replace all " " w/ "*". Once
Filter1 is done it will pass the newly modified line to Filter2.
Filter2 converts all lowercase letters to uppercases. Once Filter2 is
done it will pass the newly modified line to Consumer who simply
writes the line to an output file. This is repeated until the input
file is completely scanned.

I can get this to work w/ one line (no loops), so the first line of
text is completely converted but how do I get a while() loop to work
with this pipe synchronization so that the entire input file is
scanned?

I'd appreciate it if someone can help.

I'm not sure what the problem is. As long as all the processes read
their input line by line until EOF, you should be able to do:

Producer | Filter1 | Filter2 | Consumer

Are you saying that some of these processes only process one line and
then exit? You can change that process to:

{ while read line; do echo "$line" | <process>; done; }

--
Barry Margolin, bar...@xxxxxxxxxxxx
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me *** ***
PLEASE don't copy me on replies, I'll read them in the group ***

I should have mentioned I'm using C/C++ not shell script. OK so I'll
post the code so you can see how I'm approaching this. I'm a beginner at
this, the code may look a little ugly, but it's just a simple chain
where Producer forks to Filter1 whom forks to Filter2 whom forks to
Consumer.

The problem is ... The while loop seems to only execute once. I don't
understand why, I think it may have to do with close() cause I've
reading about how it deallocates the file descriptor. If this is the
case then how can I solve this?

I can see three major problems:

while( !feof( filePtr ) )
{
/* Producer forks to Filter1 */

switch( Filter1 = vfork() )

Don't use vfork().

/* Filter1 forks to Filter2 */

switch( Filter2 = vfork() )

Don't use vfork().



switch( Consumer = vfork
() )
{

Don't use vfork().

fgets( buffer1 , maxChar , filePtr );
write( fd1[1] , buffer1 ,
strlen( buffer1 ) + 1 );

You probably don't want the +1, and you aren't checking for errors on
most of the IO calls.
Also you really want to only do the fork() calls once, and then loop in
the correct places ... instead of looping around the entire thing and
creating three processes for each line.
Also create some functions.

--
James Antill -- j...@xxxxxxxxxxx://www.and.org/and-httpd/-- $2,000 security guaranteehttp://www.and.org/vstr/

I am trying to accomplish that but I can't. From what I understand,
after I fork() to a child process the parent will wait() for the child
process to complete, and _exit() is used by the child process to
signal the parent process that it has completed its task. This is
simple and I get it to work with one line from the input file. So in
order to get all the lines from the input file processed, I need a
loop in the Producer code that will read the file line by line. After
each line is read the producer fork() to its child process and wait()
for the child process to finish. The Producer should then resume, it
loops and gets the next line, but this time around the close() seems
to not wanna work properly.


.



Relevant Pages

  • Re: Process Synchronization using Pipes
    ... Producer needs to read data from an input file line by line and pass ... Filter1 is done it will pass the newly modified line to Filter2. ... where Producer forks to Filter1 whom forks to Filter2 whom forks to ...
    (comp.unix.programmer)
  • Re: Process Synchronization using Pipes
    ... Producer needs to read data from an input file line by line and pass ... Filter1 is done it will pass the newly modified line to Filter2. ... int main (int argc, char* argv) ... /* status variables are used with _exit() and wait, ...
    (comp.unix.programmer)
  • Re: Process Synchronization using Pipes
    ... I need to have 4 processes: Producer, Filter1, Filter2 and Consumer. ... Producer needs to read data from an input file line by line and pass ...
    (comp.unix.programmer)
  • Process Synchronization using Pipes
    ... I need to have 4 processes: Producer, Filter1, Filter2 and Consumer. ... Producer needs to read data from an input file line by line and pass ...
    (comp.unix.programmer)