Home > Archive > PERL Miscellaneous > November 2004 > Using files to transfer data between processes
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Using files to transfer data between processes
|
|
|
| Hello everyone,
I hope one of you very knowledgeable perl gurus could shed some light
on a problem I am trying to solve. I have two processes (say process
A and process B) that need to talk to each other. Process A will
collect data that it needs to forward to process B. I want to put the
data onto disk so that I have a permanent record of the data in case
process A or B die. I'd like to have process B read the data from the
file. So A puts data in and B reads data out, similar to a named pipe
but using the disk so that we have a permanent record (as well as a
large storage area). I want process B to read the file but also be
able to handle input from a socket as well.
Doing some digging, I can have process B, do something like the
following :
use IO::S able;
chdir "/tmp";
$FIFO = "/tmp/fifo";
sub fhbits {
my @fhlist = @_;
my $bits;
for (@fhlist) {
vec($bits, fileno($_), 1) = 1;
}
return $bits;
}
open (FIFO, "<$FIFO") or die "can't open file $FIFO: $!";
my $rin = fhbits(qw(STDIN FIFO));
while (1) {
($nfound, $timeleft) = select($rout=$rin, undef, undef, undef);
if (($nfound != -1) && (vec($rout, fileno(FIFO), 1) == 1)) {
sysread FIFO, $data, 2000;
print "Just read from FIFO: $data\n";
if (eof(FIFO)) {
FIFO->clearerr();
}
} elsif (($nfound != -1) && (vec($rout, fileno(STDIN), 1) == 1)) {
sysread STDIN, $data, 2000;
print "Just read from STDIN: $data\n";
}
}
The problem with this code is that the FIFO file handle keeps firing
because of the end of file, i can clear it but it constantly fires off
if there is no data to process. I realize that I can force a wait by
doing something like :
$data = <FIFO>;
But this keeps the code from reading from the other file handle.
I'm thinking that there may be a way to stop the select from returning
on an eof condition but my feeling is that it will probably not help.
I hope that this isn't a new issue and that someone out there has done
something similar and can help me out.
As always, many thanks for taking the time to read and reply, your
help is much appreciated.
| |
| Villy Kruse 2004-11-26, 9:02 am |
| On 25 Nov 2004 23:06:07 -0800,
Larry <lada77@yahoo.com> wrote:
> Hello everyone,
>
> I hope one of you very knowledgeable perl gurus could shed some light
> on a problem I am trying to solve. I have two processes (say process
> A and process B) that need to talk to each other.
There is a perl document called perlipc dedicated to this subject so
that seems a fair place to start. mandoc perlipc is supposed to give
you said document on any system with perl installed.
Villy
| |
| Anno Siegel 2004-11-26, 9:02 am |
| Villy Kruse <nobody> wrote in comp.lang.perl.misc:
> On 25 Nov 2004 23:06:07 -0800,
> Larry <lada77@yahoo.com> wrote:
>
>
>
>
> There is a perl document called perlipc dedicated to this subject so
> that seems a fair place to start. mandoc perlipc is supposed to give
s/mandoc/perldoc/
> you said document on any system with perl installed.
Anno
| |
| Villy Kruse 2004-11-26, 9:02 am |
| On 26 Nov 2004 08:07:09 GMT,
Anno Siegel <anno4000@lublin.zrz.tu-berlin.de> wrote:
>
> s/mandoc/perldoc/
>
That would help quite a lot.
Villy
| |
| Tad McClellan 2004-11-26, 4:08 pm |
| Villy Kruse <vek@station02.ohout.pharmapartners.nl> wrote:
> On 26 Nov 2004 08:07:09 GMT,
> Anno Siegel <anno4000@lublin.zrz.tu-berlin.de> wrote:
>
>
>
> That would help quite a lot.
on many systems, this would help just as much:
s/mandoc/man/
--
Tad McClellan SGML consulting
tadmc@augustmail.com Perl programming
Fort Worth, Texas
| |
|
| >
> There is a perl document called perlipc dedicated to this subject so
> that seems a fair place to start. mandoc perlipc is supposed to give
> you said document on any system with perl installed.
>
>
Thanks for the response Villy. I've actually gone through this man
page and it doesn't really help much as i'm trying to use a disk file
as a FIFO pipe between two processes. I'm hoping that a simple
solution exists but I may have to resort to having my receiving
process fork off a child reader who's dedicated to monitoring the file
and then forwarding that information via a pipe to its parent. This
just complicates things and I was hoping for a simpler solution.
| |
|
| K,
Never mind as I think i have a solution for this. I guess we can't
get around resetting the eof marker and polling the file to read a
file that is growing. I'll just have the process that updates the
file send a signal (or communicate via a socket) to the reading
process that there is data available to read so that I can avoid
having to poll for updates.
| |
| Scott W Gifford 2004-11-29, 4:05 pm |
| lada77@yahoo.com (Larry) writes:
[...]
> The problem with this code is that the FIFO file handle keeps firing
> because of the end of file, i can clear it but it constantly fires off
> if there is no data to process. I realize that I can force a wait by
> doing something like :
AFAIK, there's no way to tell select "only return if the file has
grown". Essentially what you're doing is emulating "tail -f", which
leads to this FAQ:
http://www.perl.com/doc/FAQs/FAQ/oldfaq-html/Q5.22.html
The solution employed there, and the one I've used in similar
situations, is to simply sleep() after reaching EOF, then check after
that to see if the file has grown. Essentially you're polling, but
since you know how the programs work, you can time the sleep()s so
that you usually don't poll unless there's data waiting.
You could also use something like tee(1) to send the data from A to B
using a pipe, but record the results as they're sent, since pipes have
the behavior you want. Or just have A record the data in a file as
it's sent to a pipe or socket, or have B record the data as it reads
it.
-----ScottG.
|
|
|
|
|