Hi Wise ones,

I have created a pipe to establish communication between a parent and multiple children it has forked.

In this scenario only the children would be doing the writes while the parent will just do the reads.

To serialize the writes to the pipe, I've used a semaphore. To do the reads from the pipe I've not specified any locking on the pipe.

But the reads/writes dont seem to be happening as planned. Please let me know what I've missed. I've included the sample code.

######Semaphore implementation ##### use IPC::Semaphore sub IPC_PRIVATE {0}; sub IPC_RMID {10}; sub IPC_CREAT {0001000}; sub GETVAL {5}; sub semwait { my $sem=shift; semop($sem, pack("s3", 0, -1, 0)) || die "semw: $!\n"; } sub semsign { my $sem=shift; semop($sem, pack("s3", 0, +1, 0)) || die "sems: $!\n"; } my $sem=semget(&IPC_PRIVATE, 1, &IPC_CREAT | 0666) || die "semget: $!\ +n"; warn "semid= $sem\n"; semsign($sem); ####### Pipes implementation ######## use IO::Handle; pipe(PARENT_READ, PARENT_WRITE); PARENT_WRITE->autoflush(1); #### In the child process ##### { semwait($sem); print PARENT_WRITE "PID : Pid of the process is $$ \n" ; print PARENT_WRITE "SIGNAL : Signal sent from the process \n"; semsign($sem); } ##### In the parent ##### { while ( $msg = <PARENT_READ> && $signal_received < $total_signals ) { if($msg =~ /PID/) { chomp($msg); my @fields = split (/ /,$msg); print "Child with fields @fields has communicated to me \n"; ##### This print statement is not printing anything either on the + console or even if I redirect it to a file } } }


In reply to Regulating write access to pipes using semaphores by girishatreya2005

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.