It was a mistake on my side . Semaphores are regulating the write access . I've included the sample code. Thanks for the replies wise ones.
#! /usr/bin/perl -w
use strict ;
use Socket;
use IO::Handle;
use IPC::Semaphore ;
pipe(PARENTREAD, PARENTWRITE);
PARENTWRITE->autoflush(1);
my $sem=semget(&IPC_PRIVATE, 1, &IPC_CREAT | 0666) || die "semget: $!\
+n";
warn "semid= $sem\n";
semsign($sem);
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";
}
sub main ()
{
my $msg ;
print "I'm in the parent now \n";
for (my $count = 1 ; $count < 25 ; $count++ )
{
my $child_pid = forker();
print "The child process had pid of $child_pid \n";
}
my $count = 1 ;
open (FH, '>>' , 'test.txt');
while ($msg = <PARENTREAD> )
{
if ($msg =~ /PID/)
{
$count++ ;
print "Increasing the count to $count \n";
print FH "$msg";
if($count == 24 )
{
last ;
}
}
}
}
sub forker()
{
#print " Child is forking \n" ;
my $child_pid ;
my $panic_monitor_pid ;
if(!defined($child_pid = fork()))
{
print " Forking of child failed \n";
die "Dying abruptly\n";
}elsif($child_pid > 0)
{
print "In parent \n";
return $child_pid ;
}else
{
#print "In child now \n The child had pid $$" ;
$panic_monitor_pid = grandchild();
#print " The grand child has pid of $panic_monitor_pid \n";
exit ;
}
}
sub grandchild ()
{
my $child_pid ;
my $panic_monitor_pid ;
if(!defined($child_pid = fork()))
{
print " Forking of child failed \n";
die "Dying abruptly\n";
}elsif($child_pid > 0)
{
#print "In parent \n";
return $child_pid ;
}else
{
semwait($sem);
print PARENTWRITE "In the grandchild \n";
print PARENTWRITE "PID : The child had pid $$\n" ;
semsign($sem);
#$panic_monitor_pid = panic_monitor();
#print " The grand child has pid of $panic_monitor_pid \n";
exit ;
}
}
main();
|