Quote:
Originally Posted by aarontwc
Thanks for your comments! I actually tried out fork. and the program works fine. Will try out pthread when i have a chance. Thank you!!
|
fork() creates a different process so no memory (and therefore no variables) are shared between the parent and child, although variables will have the same value immediately after the cloning of the process - except of course for the return value of fork().
Using 'pthreads' the different 'threads' share memory.
Things are actually a bit more complicated - you'll have to read through the man pages for fork() and pthreads to see what's shared and what's not.
Personally I use pthreads more often than fork() because it saves me the effort of setting up shared memory blocks or pipes. UNIX survived without 'threads' for a long time (fork() was the only option) so in most cases the choice is a matter of personal preference. fork() and pthreads are very different beasts though and you pretty much have to decide from the start which one you'll use because that will have a huge impact on how you design the code. Switching between the two is not trivial (unless your program is very trivial to begin with). pthreads *do* make some tasks much easier though - for one, you don't need to resize the shared memory all the time and somehow signal the other processes that the size has changed etc etc.