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
}
}
}
-
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.