Cooperating processes... 
Author Message
 Cooperating processes...

I have two process running that need to know when each other changes a
certain variable.  However, this variable (we'll call it $foo) is both
local to each process, however, if one changes there $foo variable,
the other process needs to know about it.  How can I do this?

.mitch



Sun, 09 Dec 2001 03:00:00 GMT  
 Cooperating processes...
: I have two process running that need to know when each other changes a
: certain variable.  However, this variable (we'll call it $foo) is both
: local to each process, however, if one changes there $foo variable,
: the other process needs to know about it.  How can I do this?

perldoc perlipc

Tie::Watch might also be useful

Andrew



Sun, 09 Dec 2001 03:00:00 GMT  
 Cooperating processes...
     [courtesy cc of this posting mailed to cited author]

In comp.lang.perl.misc,

:I have two process running that need to know when each other changes a
:certain variable.  However, this variable (we'll call it $foo) is both
:local to each process, however, if one changes there $foo variable,
:the other process needs to know about it.  How can I do this?

    #!/usr/bin/perl
    # sharetest - test shared variables across forks
    use IPC::Shareable;

    $handle = tie $buffer, 'IPC::Shareable', undef, { destroy => 1 };
    $SIG{INT} = sub { die "$$ dying\n" };

    for (1 .. 10) {
        unless ($child = fork) {        # i'm the child
            die "cannot fork: $!" unless defined $child;
            squabble();
            exit;
        }

    }

    while (1) {
        print "Buffer is $buffer\n";
        sleep 1;
    }
    die "Not reached";

    sub squabble {
        my $i = 0;
        while (1) {
            next if $buffer =~ /^$$\b/o;  
            $handle->shlock();
            $i++;
            $buffer = "$$ $i";
            $handle->shunlock();
        }
    }

But I wouldn't do that, really.  I'd use some more normal
kind of IPC.

--tom
--
Real programmers can write assembly code in any language.   :-)  



Sun, 09 Dec 2001 03:00:00 GMT  
 Cooperating processes...
If I was in UNIX, I would send a signal to the other process which would
notify it to watch for a change.  If I was in NT, I would break down and
write the info it to a temporary directory (which is probably what you're
doing until you can find something better to do).  I actually have to
communicate between two Perl processes, one on Linux, and one on an NT box.
The best way I found to do it is to send a file back and forth between the
two whenever a change is made- it works great and it takes no time to write.
Quote:
> I have two process running that need to know when each other changes a
> certain variable.  However, this variable (we'll call it $foo) is both
> local to each process, however, if one changes there $foo variable,
> the other process needs to know about it.  How can I do this?



Sun, 09 Dec 2001 03:00:00 GMT  
 Cooperating processes...

Quote:

> If I was in UNIX, I would send a signal to the other process which would
> notify it to watch for a change.  If I was in NT, I would break down and
> write the info it to a temporary directory (which is probably what you're
> doing until you can find something better to do).  I actually have to
> communicate between two Perl processes, one on Linux, and one on an NT box.
> The best way I found to do it is to send a file back and forth between the
> two whenever a change is made- it works great and it takes no time to write.

Thanks all for the reply!!!  However, these processes will not be forked.  The
two processes that need to need to know when one changes a local variable are
both started separately.  So, let's say that you start both processes:

perl5 foo.pl
perl5 foo2.pl

Both processes have a variable called $foo which each will manipulate
internally.  So, let's say that process foo2 changes its $foo to "whatever"
($foo = "whatever").  Then, somehow process foo, needs to know that process foo2
changes its $foo variable to "whatever".

Can I do it this way?

.mitch



Sun, 09 Dec 2001 03:00:00 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. Emacs Perl addons: cooperate with h2xs

2. Perl and tar not cooperating in a shell script

3. Making OpenGL cooperate with pTk

4. RTF processing or general text processing module

5. Perl process as a unix background process

6. How to capture process output by Win32::Process::Create

7. interactive process processing

8. Child processes keep appearing in process list

9. to get the process ID of a running process

10. Forking processes (getting child process id)

11. saving process output to a log file using Win32::Process

12. PERLFUNC: setpgrp - set the process group of a process

 

 
Powered by phpBB® Forum Software