in reply to communication between forked processes

You have copies of everything in the forked processes, but those are the images taken from the moment you fork. After the fork, each process has its own memory space.

Although it is possible to share memory among processes, there are lots of articles about why it is not a good approach. For new applications, that is not a choice, unless you are touching some low level stuff.

Thread is the solution. Start to use threads, I say this to myself all the time. Let’s always ask ourselves the question whether thread is a good fit, whenever we are thinking of fork. My opinion is: use thread for tight coupling, and fork for loose coupling.

In your current case, you want to share a hash, I see it tight enough for threads. If there is other reasons unknown to me made you pick fork, go IPC’s, don’t use shared memory.
  • Comment on Re: communication between forked processes

Replies are listed 'Best First'.
Re: Re: communication between forked processes
by gnu@perl (Pilgrim) on Feb 24, 2003 at 18:48 UTC
    Thanks for your input. I have never done any work with threads before. I program in unix and don't even know if you can use threads in unix. If so, could you please point me to an article or sight that might help clear things up?

    Thanks, Chad.

      Definitely no problem with UNIX.

      For Perl, Perl only started to really support thread in 5.8.0. For the Perl part, there are some very useful information in perldoc, check out the threads, and threads::shared pragmas.

      As for online material, go google, and search for pthread (do not search for thread), it will give you whole bunch of result, I just checked it. Really useful stuffs. Those are not about Perl’s thread, but as you never used thread before, understand POSIX thread will definitely help you, and Perl’s thread mimics POSIX thread a lot, at least at the interface and concept level, so this would also help you to understand Perl thread.

      I am a little bit concerned whether it is good to use Perl to learn thread (considering it just started and a little bit prime). My path is I understood thread first, then came across Perl. Check whether you have pthread lib installed on your unix box, if yes, play it with c, while you are working on Perl at the same time. (if no pthread installed, there is always the native thread, but I would think that’s less ideal.)

      There is a good book from O’Relly called “Pthreads programming”, not thick, good for both beginners and experienced programmers, and it has a section about why “shared memory” is a bad choice.

      Update

      You might also search my write-ups for the word “threads”, I have some quick examples there. As all those examples say “use threads”, this search should work.
        This has been quite helpful...only, I'm curious now. It's been about a few months since I've installed 5.8. In fact, the machine that Chad (gnu@perl) is talkin about here is probably the one I installed it on.

        I distinctly remember reading during the installation that threads should still not be used because they are very experimental. Thus, since the machine that this Perl was being installed on is well, *very* important, I didn't put the option in to use threads. Now, I am not sure if this is the machine he is trying to use. So, the second part of that question is does threading have to be included during the install in order for Perl to do threading properly? If not, can real threading still be done? I believe there is a module you can still use (via the older method that you could use pre 5.8, right)?

        Thanks! This will clear up some questions I had in this area too.

        _ _ _ _ _ _ _ _ _ _
        - Jim
        Insert clever comment here...