B
BrettNem
Hi All,
I need some help with a fifo. Basically I'm trying to write a daemon
process that will sit on a fifo, looking for input data.. when it gets
the data, it processes it and returns a response (as STDOUT from
whoever manipulated the FIFO).
Essentially, I could use something like echo to test it like:
echo "Hello World" > /tmp/signalfifo
and it would reply with whatever I gave it:
Hello World
But I can't for the life of me figure out how to get the fifo to reply.
I'm using a code snippit I found in the group someitme last year:
http://groups-beta.google.com/group...13?q=signalfifo&rnum=1&hl=en#c61f5e5e1b56d613
I'm providing the code below for reference.. I added a print to the
fifo thought it might be as easy as that (nope).
btw, I tried the below with syswrite as well.
Any ideas??
Thanks,
Brett
#!/usr/bin/perl -Tw
#
# Example of using a named pipe and 4-way signal()
# to both manage a periodic task and an asynchronous
# task.
#
use strict;
# Parameters
my $interval=10; # seconds
my $signal_fifo="/tmp/signalfifo" ; # create with "mkfifo
/path/to/fifo"
# Verify the named pipe
if (!(-p $signal_fifo && -w _ && -r _))
{
print STDERR "Problem with fifo $signal_fifo!\n";
exit(1);
}
# Open the fifo read/write, not just read. This is necessary because
# of the POSIX rules about using select() on named pipes when no
writers
# are present
#open(FIFOFH,"+<",$signal_fifo) || die $!;
open(FIFOFH,"+<",$signal_fifo) || die $!;
# Now go into a select()-loop on the fifo.
my $rin='';
my $rout='';
vec($rin,fileno(FIFOFH),1)=1;
while(1)
{
my $nfound=select($rout=$rin,undef,undef,$interval);
# We got to this point because the blocking select()
# returned. It happened because 1) data came into the
# fifo, or 2) the timeout occurred.
# print "===== select() returned at ",scalar(localtime),"\n";
if($nfound) # data is in the fifo
{
# Completely drain the fifo, since we don't really
# care what the message is -- only that it exists.
print "fifo contains this data:\n";
my $fifodata;
while(select($rout=$rin,undef, undef,0)) # non-blocking select
{
sysread FIFOFH,$fifodata,1024;
print FIFOFH "$fifodata\n";
}
}
# For safety, just go ahead and perform the periodic and the
# async task any time we get here. A particular application
# may want to alter this strategy.
periodic_task();
async_task();
}
# Infinite loop above... we'll never get here.
# Something that needs to be done at least every n minutes
sub periodic_task
{
# print "Periodic task...\n";
return;
}
# Something to do only when signalled
sub async_task
{
# print "Asynchronous task...\n";
return;
}
I need some help with a fifo. Basically I'm trying to write a daemon
process that will sit on a fifo, looking for input data.. when it gets
the data, it processes it and returns a response (as STDOUT from
whoever manipulated the FIFO).
Essentially, I could use something like echo to test it like:
echo "Hello World" > /tmp/signalfifo
and it would reply with whatever I gave it:
Hello World
But I can't for the life of me figure out how to get the fifo to reply.
I'm using a code snippit I found in the group someitme last year:
http://groups-beta.google.com/group...13?q=signalfifo&rnum=1&hl=en#c61f5e5e1b56d613
I'm providing the code below for reference.. I added a print to the
fifo thought it might be as easy as that (nope).
btw, I tried the below with syswrite as well.
Any ideas??
Thanks,
Brett
#!/usr/bin/perl -Tw
#
# Example of using a named pipe and 4-way signal()
# to both manage a periodic task and an asynchronous
# task.
#
use strict;
# Parameters
my $interval=10; # seconds
my $signal_fifo="/tmp/signalfifo" ; # create with "mkfifo
/path/to/fifo"
# Verify the named pipe
if (!(-p $signal_fifo && -w _ && -r _))
{
print STDERR "Problem with fifo $signal_fifo!\n";
exit(1);
}
# Open the fifo read/write, not just read. This is necessary because
# of the POSIX rules about using select() on named pipes when no
writers
# are present
#open(FIFOFH,"+<",$signal_fifo) || die $!;
open(FIFOFH,"+<",$signal_fifo) || die $!;
# Now go into a select()-loop on the fifo.
my $rin='';
my $rout='';
vec($rin,fileno(FIFOFH),1)=1;
while(1)
{
my $nfound=select($rout=$rin,undef,undef,$interval);
# We got to this point because the blocking select()
# returned. It happened because 1) data came into the
# fifo, or 2) the timeout occurred.
# print "===== select() returned at ",scalar(localtime),"\n";
if($nfound) # data is in the fifo
{
# Completely drain the fifo, since we don't really
# care what the message is -- only that it exists.
print "fifo contains this data:\n";
my $fifodata;
while(select($rout=$rin,undef, undef,0)) # non-blocking select
{
sysread FIFOFH,$fifodata,1024;
print FIFOFH "$fifodata\n";
}
}
# For safety, just go ahead and perform the periodic and the
# async task any time we get here. A particular application
# may want to alter this strategy.
periodic_task();
async_task();
}
# Infinite loop above... we'll never get here.
# Something that needs to be done at least every n minutes
sub periodic_task
{
# print "Periodic task...\n";
return;
}
# Something to do only when signalled
sub async_task
{
# print "Asynchronous task...\n";
return;
}