perl threads and inter thread communication

B

brad

Hi,
I am writing some test scripts where I want to fork() and in the child
I want to continually monitor one condition(if a chip resets) and if
that condition is met, I want to be able to send a message to the
parent and stop him and exit. Further more, If the child process
never gets satisfied, and the parent finishes its job, I want to be
able to stop the child process from within the parent and then exit
the parent thread. There must be a way to talk among threads, but I
have been searching with little luck. How can I send messages among
threads or share variables among them? Does anyone have any examples
of such a thing? Thanks
 
T

thumb_42

brad said:
Hi,
I am writing some test scripts where I want to fork() and in the child
^^^^^?
Are you talking threads of processes? (I'm going to assume processes
but I could be wrong.)
I want to continually monitor one condition(if a chip resets) and if
that condition is met, I want to be able to send a message to the
parent and stop him and exit.

What you could do is simply keep the parents process ID around in the child,
and use kill() to send the parent a message. Save a copy of $$ prior to
calling fork().
Further more, If the child process
never gets satisfied, and the parent finishes its job, I want to be
able to stop the child process from within the parent and then exit
the parent thread.

Again, you could use signals, (with kill()) if you're talking about
processes. since fork() returns the child PID on success.
There must be a way to talk among threads, but I
have been searching with little luck. How can I send messages among
threads or share variables among them? Does anyone have any examples
of such a thing? Thanks

Have a look at 'man perlipc' for more details.

There is shared memory, mmap, file handles, UNIX sockets, pipes, signals,
message queues, and several modules that interfaces with all the above. All
kinds of things to play with.

Each of them have their pros and cons. (mmap last I checked, required a
module and was not portable, but if you're talking to chips, portability is
probably a null issue eh? :) ) Shared memory was nice, but it's not freed
when the processes leave, which can be a feature but it can also be a problem.

Jamie
 
I

Ian Cass

brad said:
Hi,
I am writing some test scripts where I want to fork() and in the child
I want to continually monitor one condition(if a chip resets) and if
that condition is met, I want to be able to send a message to the
parent and stop him and exit. Further more, If the child process
never gets satisfied, and the parent finishes its job, I want to be
able to stop the child process from within the parent and then exit
the parent thread. There must be a way to talk among threads, but I
have been searching with little luck. How can I send messages among
threads or share variables among them? Does anyone have any examples
of such a thing? Thanks

I've found http://search.cpan.org/~elizabeth/forks-0.15/lib/forks.pm to be
pretty useful for threaded applications that use forked processes instead of
the experimental thread architecture. The clever thing is, the 'use forks;'
API is the same as 'use threads;', so you should be able to flip between the
two.

To learn how to use this API, read the Perl thread tutorial
http://search.cpan.org/~nwclark/perl-5.8.3/pod/perlthrtut.pod . You can then
use what you've learnt by 'use forks' rather than 'use threads'.

YMMV.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top