loading...

Discussion

markdown guide
 

A process is like a big barn: It takes a long time to build, and it has big, sturdy walls to keep others out. It has places to store things like food for horses. Inside the barn, horses are safe to run and play. The barn knows about all the horses, food, training schedules for the horses, and stuff like that.

A barn's a happening place, I tell you!

A thread is like a single horse running around inside that barn. The horse is kind of silly; it only knows where it is right now and it can retrace its steps. It can eat the grain from the barn along with the other horses in the pasture. Sometimes, it steps on the other horses, and they fight with one another. Sometimes they eat each others grain, and they go hungry. Sometimes they just run around crazy until someone from the farm trains them.

 

I'm sure someone will explain this far better than I'll ever can. So I'll go for the shortest way I can put this :)

I think you can look at things like this:

  • Process is like this object that is scheduled for execution. It has it's own memory allocated for it, and an "scope" of it separated from other processes.
  • Thread is more like part of the process. One process can have multiple threads, but they actually have their own sequences of execution. But all threads of one process share the memory allocated for that process.

What's the main difference - threads can share memory and data, and be able to work on the same memory set, while processes are isolated.

 

You are 5y old now, until you are 18 you are dependent and attached to your mother, she protects you from outside world, she delivers you the filtered messages and most important she decided what are your house chores.

You share the same house / environment and have access to the same rooms / books from the shelves and utilities (water, power etc).

Your mother consumes more resources than you and it's bigger of course. A mother can have multiple children, that consume fewer resources.

Mother - process
Kid - thread
House - RAM (is shared)
House chores - the data to execute / instructions


You can use threads directly example in Java/C++, but other languages have a smaller kind of threads. Example GO has goroutines, and the runtime can pack thousands of goroutines in a managed thread. From the programmer point of view there is no difference.

 
  • Programs are like recipes. They are plans for how to do something.
  • Processes are like cooking. They are the activity of actually doing something.
    • Processes can be started or stopped
    • Multiple processes can be based off the same program
    • Each process requires some resources (CPU time and memory)
    • Processes do not share resources
  • Threads are like cooks. They are the actors who perform steps.
    • Every process has at least one thread.
    • Each thread requires some separate CPU time.
    • Threads inside a process can share (and screw up) each other's memory.