Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (5)
As of 2024-04-19 02:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found