Reputation: 5146
I was trying to understand what is the difference between process and thread and I understood all the important points but I was not able to understand this line:
The typical difference is that threads (of the same process) run in a shared memory space, while processes run in separate memory spaces.
Here is the reference.
Can anyone help me understand what does memory space means here? And what does above line means in layman terms so that I can understand?
Upvotes: 2
Views: 5072
Reputation: 8928
In the specific context of Java, a single invocation of Java, starting one JVM (Java Virtual Machine), is one process; code in the JVM has access to memory in that JVM, but not in other JVMs.
Within each JVM there can be multiple threads. These threads share one memory space in the sense that they can access objects created by other threads in that same JVM. They cannot, however, access objects created by threads in other JVMs, since those objects are in the other JVM's memory spaces.
Upvotes: 0
Reputation: 13300
There are couple of good answers already.
Some details : Each process is given a virtual memory addresses with a range of 2 power word size ( 10 years ago it would have been 2 power 32 ( for 32 bit machines)). The process can uses these addresses as if it was physical memory. In the background the OS does a lot of swapping/paging/translation etc., from virtual to physical ( virtual address translation ). The OS makes sure when two processes reads or writes, the physical memories are not interleaved. But two processes can have the same addresses(virtual) when running but maps to different physical address in RAM/main memory.
Thread variables comes under stack and it is relative and is within the thread memory space/boundary hence an address in one thread cannot be reference in another but can access something outside of the thread's stack but within the process's address space.
Upvotes: 0
Reputation: 38889
If all memory in a system is a stack of paper. And each process is a little boy or girl who has it's own pile from the stack of papers on which to do their artwork. These are your applications.
Now if some of these applications are threaded, think of it like being two little girls or boys working together with the same pile of papers. They are both scribbling their creations on the same pieces of paper given to them. While other little girls and boys are not sharing their papers with each other, they are independent - single threaded.
In other words, two threads can read from the same memory and access the same global variables. i.e. memory is shared because it's the same address space.
Separate processes can't do that, they are independent of each other.
Upvotes: 8
Reputation:
memory space is like a boundary. An isolation to not to overstep, overwrite each other hence processes cannot access each other's variable etc.,hence have to use IPC to communicate to each other. But threads can access global variables (typically use some kind of protection like mutex when reading writing ).
Assume ( T = Thread, P = Process, and V = variable )
P1
global_var
T1
can set global_var = 2
T2
can read global_var
and can ever reset global_var
P2
It cannot read P1::global_var
Hope this helps!
Upvotes: 1
Reputation: 182763
By "memory space", they mean the mapping of addresses to contents. So, for example, in two different processes, the contents of the same address may be different because they have different memory spaces.
Upvotes: 0