Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

how to solve producer consumer problem in perl when threads are disabled

by david2008 (Scribe)
on Feb 17, 2014 at 07:33 UTC ( #1075135=perlquestion: print w/ replies, xml ) Need Help??
david2008 has asked for the wisdom of the Perl Monks concerning the following question:

Hi all, I want to solve the producer consumer problem in perl as described
here The problem is that the perl i am working on has threads disabled.
How would you connect forked processes so that the producers and consumers could communicate?
Thanks,
David

Comment on how to solve producer consumer problem in perl when threads are disabled
Re: how to solve producer consumer problem in perl when threads are disabled (forks)
by Anonymous Monk on Feb 17, 2014 at 07:40 UTC
Re: how to solve producer consumer problem in perl when threads are disabled
by Corion (Pope) on Feb 17, 2014 at 07:46 UTC

    I would use one of the many approaches outlined in perlipc.

Re: how to solve producer consumer problem in perl when threads are disabled
by atcroft (Monsignor) on Feb 17, 2014 at 07:51 UTC

    What have you tried thus far?

    The perl IPC (inter-process communication) document describes various methods of interprocess communication in perl, including the use of signals, fifos, pipes, sockets, and semaphores. It also includes examples of bi-directional communications. After reviewing that document, all that should remain is just a small matter of programming. :)

    Hope that helps.

Re: how to solve producer consumer problem in perl when threads are disabled
by BrowserUk (Pope) on Feb 17, 2014 at 08:25 UTC

    The simplest mechanism is a shell pipe.

    Producer: perl -e"$|++;for(1..10){ printf qq[%05d\n], $_; sleep 1 }"

    Consumer: perl -E"$|++; while(<>){ print; }"

    Communications:

    >perl -e"$|++;for(1..10){ printf qq[%05d\n], $_; sleep 1 }" | perl -E" +$|++; while(<>){ print; }" 00001 00002 00003 00004 00005 00006 00007 00008 00009 00010

    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
Re: how to solve producer consumer problem in perl when threads are disabled
by zentara (Archbishop) on Feb 17, 2014 at 12:43 UTC
    Something along this line:
    #!/usr/bin/perl # code by pg use IO::Handle; use strict; use warnings; $| ++; pipe(PARENT_RDR, CHILD_WTR); if (fork()) { if (fork()) { close CHILD_WTR; while (my $line = <PARENT_RDR>) { print "parent got $line"; } } else { close PARENT_RDR; for (100..110) { print CHILD_WTR "$_\n"; } } } else { close PARENT_RDR; for (0..10) { print CHILD_WTR "$_\n"; } }

    I'm not really a human, but I play one on earth.
    Old Perl Programmer Haiku ................... flash japh
Re: how to solve producer consumer problem in perl when threads are disabled
by Anonymous Monk on Feb 17, 2014 at 16:33 UTC
    As others have already said here ... write the producer, then write the consumer, and connect the two with a shell pipe. The IPC magic, therefore, is handled simply and automagically by the pipe, which appears to each as a simple file, but which will block the reader if there is nothing to read and block the writer if there is nothing to write. Other variations on existing "Unix plumbing" allow for multiple producers and/or consumers to be piped-together. You implement a complicated solution, mostly by avoiding the complexity altogether!

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1075135]
Approved by hdb
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (7)
As of 2014-12-27 00:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (176 votes), past polls