Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

RE: Threads vs forking

by jreades (Friar)
on Aug 25, 2000 at 22:03 UTC ( [id://29672]=note: print w/replies, xml ) Need Help??


in reply to Threads vs forking
in thread When is a while not a while?

Well, you supplied a great explanation (particularly in combination with the geeksalad article) -- I now know just enough to be dangerous. :^P

Let me see if I understood what was written and can apply it to the problem at hand...

We need one application/process/thread ('a') to instantiate/launch/spawn another ('b') that will perform function 'x' while 'a' waits for keyboard input...

Under Unix/Perl, this would be an exec situation as we don't need 'a' to clone itself, but we *do* need it to launch 'b' as a distinct process.

Now there are two possibilities, depending on whether or not 'a' and 'b' need to communicate after the exec().

If they do not -- i.e. that 'b' will run until it has done 'x' and then exit.

But if 'a' and 'b' still need a way to communicate, then we need to avoid trampling all over the shared information (a 'race condition'). One way of avoiding this is supplied by tilly as SimpleLock, and I think that this LockFile-Simple might also allow the processes to pass information back and forth.

Am I on the right track?

Replies are listed 'Best First'.
Just enough to be dangerous (on threads) indeed! :-)
by tilly (Archbishop) on Aug 25, 2000 at 22:20 UTC
    In Unix when you fork you usually wind up communicating between the processes in a pipe. With threads the two can communicate directly through an internal buffer (they share the same memory). With Unix you don't have to worry about how you use data, but there is more overhead. With threads you do have to be careful because the threads can trample over each other, but you can get better performance.

    Where locking is necessary is when two things (threads, processes, machines in a cluster, whatever) need to synchronize access to the same resource. Think here of Highlander - allow only one invocation at a time of an expensive CGI script, or also think of some CGI script that updates some user file. If your user submits twice you now have 2 processes, one of which might be reading while the other is deleting...

    That is a race condition. If the first cleanly read, rewrote, and exited, you would be fine. The second likewise. If they collide...

    It just happens to be really easy for two threads to collide. And much less common for programs to do it.

      > With threads the two can communicate directly 
      > through an internal buffer (they share the same memory).
      
      You can do this with separate processes by using SysV IPC.
      > With Unix you don't have to worry about how 
      > you use data, but there is more overhead. With
      > threads you do have to be careful because 
      > the threads can trample over each other, 
      > but you can get  better performance.
      
      And you gain (just about) all the benefits (and problems; plus a few new ones) of threads when you use SysV IPC.

      Not coming down on tilly, just bringing up an alternative.

        Ack! I knew that as well. It is just that fork() is so much a Unix idea that I think of it as "the Unix way" even though Unix also has many other ways of working.

        Incidentally on machins that support it, you can use shared memory from Perl with IPC::Shareable.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://29672]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (9)
As of 2024-03-28 18:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found