Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

forking messages

by Anonymous Monk
on Jan 25, 2002 at 20:26 UTC ( [id://141526]=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi,

I've got a program that forks() 10 children.

Each child inherits a sockethandle mapped to (STDOUT) from the parent.

Each child writes a message. Things work most of the time, however, 1 in 10 times the output is garbled.

How can I stop the message getting garbled?

Nige

Replies are listed 'Best First'.
Re: forking messages
by chromatic (Archbishop) on Jan 26, 2002 at 02:33 UTC
    1. Don't fork. Use something like POE instead.
    2. Use a mutex or a semaphore or a lock somehow to control access to STDOUT.
    3. Use pipe to create a new filehandle pair for each child.
    I'd try pipe first.
Re: forking messages
by dmmiller2k (Chaplain) on Jan 25, 2002 at 22:58 UTC

    There are lots of resources here you might peruse before asking this question. At minimum, you might try typing 'fork' into the Search box at the top of any Perl Monks page (and clicking 'Search'), or using Super Search.

    dmm

    If you GIVE a man a fish you feed him for a day
    But,
    TEACH him to fish and you feed him for a lifetime
Re: forking messages
by dhable (Monk) on Jan 26, 2002 at 01:07 UTC

    So you fork 10 times and then all of the processes try to send output to a single source? I don't think you understand the concept of fork very well.

    When you fork, you create another process in memory that has an independent set of memory and execution paths. Thus, you really have two programs that can do separate things without marching on top of each other. In your case, you have 10. Since a computer can only run one instruction at a time, the operating system simulates this by switching between the processes for you.

    But on most PCs, there is only one STDOUT. So each process is now writing to a single device. The reason your output becomes garbled is because the OS is scheduling the threads in an otder it feels is best, and this isn't what you would expect.

    If you need a particular order, don't fork.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (2)
As of 2024-04-20 03:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found