0

I have read a lot of posted answers about the difference between thread and process in this link

What is the difference between a process and a thread?

However it is still to abstract to understand them, like the shared memory & separated memory, can anyone explain it by giving an example, like we have a program, which part the thread is and which part the process is?

Any help is appreciated.

Community
  • 1
  • 1
hellocoding
  • 221
  • 4
  • 13

2 Answers2

1

Lets say you have a global variable in a program. In a threaded program, all threads share the same global variable, so changing it in one thread will change it in all threads. In you fork a new process on the other hand, the global variable is separate in the different processes, and changes in one process will not be reflected in another process.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
1

Let me use an analogy here.

You're working on some homework alone in a large library. You go through it, problem by problem. When you're done with one problem, you move on to another. That's a single-threaded, single-process application.

You notice this is pretty slow, so you call over a couple of friends (spawning new threads). You start getting a lot more done, since you can work on several of the problems in parallel, and since you're all in the same room, you can talk to each other pretty easily (shared memory). Unfortunately, you only have one reference book, and have to keep passing it around (shared resources). This causes an argument when some of your group members need to work on similar problems at the same time (resource contention, deadlocks). Then there was that issue where two of your group members tried to write down conflicting answers at the same time and got into a fight (concurrency errors). That's multithreading with shared memory.

You realize that there's another copy of that textbook in a library across town. You send some of your friends over (forking a new process) with a copy of everything they've done so far (copying memory) to go work there. Now they can get a lot more done and don't fight with you so often (less resource contention) but this comes at a cost -- they can only talk to you over a cellphone (interprocess communication) so communicating questions and answers is pretty expensive. Furthermore, after a while their answers don't really resemble yours anymore unless you spend a lot of time keeping each other updated, which consumes a lot of your time (synchronization). That's multiple processes.

Christian Ternus
  • 8,406
  • 24
  • 39
  • very great analogy. However there is one thing I am not sure, in the process, it is expensive to synchronize. In the thread, it will be easy, they share the same resource and communicate more, why do they have conflicting answers, they could have deadlocks because resources are not released or hold properly, they could conflict in resource allocation, but I think their answers should be same. – hellocoding Oct 25 '13 at 07:03
  • What is it you're not sure about? – aqua Oct 25 '13 at 07:18
  • Threads can share pointers to the same memory (see [this answer](http://stackoverflow.com/questions/11909248/how-do-two-or-more-threads-share-memory-on-the-heap-that-they-have-allocated)) and if you're not careful, it's easy to have them step on each other, so you have to use primitives like locking in order to keep things sane. – Christian Ternus Oct 25 '13 at 16:34