in reply to Regulating write access to pipes using semaphores

Post your full code please.

It could be buffering issue, as while you turn off buffering for the childs to write, the read from the parent will still be buffered.

  • Comment on Re: Regulating write access to pipes using semaphores

Replies are listed 'Best First'.
Re^2: Regulating write access to pipes using semaphores
by girishatreya2005 (Novice) on Jul 27, 2010 at 13:19 UTC

    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();