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 } } }
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |