This may be an overkill, but why not use semaphores? In pseudo-code:
$SEM = CREATE SEMAPHORE; IF ($PID == FORK()) { wait($SEM); # grab the semaphore open file; WRITE 'ALPHA'; close file; signal($SEM); # signal semaphore wait($SEM); # wait for signal from child open file; WRITE 'BETA'; close file; signal($SEM); # signal semaphore } ELSE IF(DEFINED $PID) { SLEEP(3); # make sure parent grabs semaphore wait($SEM); # wait for signal from parent open file; READ THE FILE; close file; signal($SEM); # done with file ops, signal semaphore wait($SEM); # wait for signal from parent that it is done writing +to file open file; READ THE FILE; close file; signal($SEM); }
In Perl, I would recommend using IPC::SysV. It seems better suited than just the Perl primitives. Here's a quick little Perl blurb (warning: untested).
use IPC::SysV qw(IPC_PRIVATE S_IRWXU IPC_CREAT); use IPC::Semaphore; my $sem = new IPC::Semaphore(IPC_PRIVATE, 1, S_IRWXU | IPC_CREAT); $sem->setall( (0) x 10); my $semid = $sem->getVal($sem); if(my $pid = fork()) { $sem->op($semid, -1, IPC_NOWAIT); #get the semaphore open(FILE, ">/myFile.txt") or die "error: $!\n"; print FILE "Hello, my name is Perl.\n"; close FILE; $sem->op($semid, 1, IPC_NOWAIT); # signal semaphore $sem->op($semid, -1, IPC_NOWAIT); #wait on semaphore open(FILE, ">/myFile.txt") or die "error: $!\n"; print FILE "Hello, again, my child.\n"; close FILE; $sem->op($semid, 1, IPC_NOWAIT); # signal semaphore } elsif(defined $pid) { sleep(3); $sem->op($semid, -1, IPC_NOWAIT); #wait on semaphore open(FILE, "/myFile.txt") or die "error: $!\n"; print while(<FILE>); close FILE; $sem->op($semid, 1, IPC_NOWAIT); # signal semaphore $sem->op($semid, -1, IPC_NOWAIT); #wait on semaphore open(FILE, "/myFile.txt") or die "error: $!\n"; print while(<FILE>); close FILE; $sem->op($semid, 1, IPC_NOWAIT); exit(0); #exit child } $sem->remove;
Of course, that, again, might be an overkill. You could probably accomplish it with some extra file foo or so.

Jeremy

In reply to Re: Concurrency : FORK and knife by enoch
in thread Concurrency : FORK and knife by jsl

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.