thread Vs. process
Difference between thread
and process
- Process is the instance of the program, thread is the subset of process.
- One program has at least one process, one process has at least one thread.
- Process runs in a separate memory space, thread runs in a shared memory space.
- Process has its own address space, once crash, have no impact for other processes. Thread only has its own stack but shared other resources, once crashed, whole program crashed.
What is thread
Thread is subset of the process.
Shared Memory System
In terms of the thread, we need to know the base of shared memory:
In the shared memory system, there is only one single address space across the whole memory system.
- Every CPU/core can read/write all memory locations in the system.
- Only one logical memory space.
- All CPUs/cores refer to a memory location using the same address.
Multiple threads
Given these points, typically, we can say that the term of multiple threads
is mainly used in the shared memory system. Because threads can access the shared data within the whole memory system by read/write
method instead of message. It is the communication in different threads.
Each thread has its own
stack
memory, butheap
memory segment are shared.
Data
There are two different kinds of data in multiple threads
contexts:
Shared data:
can be accessed by all threads.Private data:
can be accessed by the owning thread.
Each thread has its own copy the executable(i.e., the codes), however, different threads can follow different control flow through the program because each thread has its own program counter(i.e. register).
Typically, run one thread per CPU/core. The register contains the address of the instruction being executed at the current time. Therefore, a program can control different threads.
Synchronization
By default, threads execute asynchronously.
Each thread executes through the program instructions independently of other threads. Therefore, it is very important to guarantee the correct order of shared variables processed.
What is process
A process is the instance of a computer program that is being executed.
It contains the program code and its activity. It has distinct address space, different from other executing instance of program in what it has separate resources.
Distributed-Memory Architecture
Distributed-memory refers to a multiprocessor computer system in which each processor has its own private memory. Computational tasks can only operate on local data, and if remote data is required, the computational task must communicate with one or more remote processors.
Not only a single memory space used like shared memory
. Which can avoid **data race conditions**
.
Data
All variables are private. Each process has access only to its own data.
All processes run the same program(their own copy), each process has a separate copy of data.
Processes can follow different control paths through the program, depending on their process ID
Communication & Synchronization
By sending and receiving message to other processes to finish communication. Therefore, there is no data race
, rather than dead lock
:
- A
synchronous send
is not completed until the message has started to be received. - An
asynchronous send
completes as soon as the message has gone. Receives
are usually synchronous - the receiving process must wait until the message arrives
转载请注明来源,欢迎对文章中的引用来源进行考证,欢迎指出任何有错误或不够清晰的表达。可以在下面评论区评论,也可以邮件至 gzrjzcx@qq.com
文章标题:thread Vs. process
文章字数:534
本文作者:Alex Zou
发布时间:2019-02-21, 16:29:01
最后更新:2024-07-10, 03:02:36
原始链接:https://www.hellscript.cc/2019/02/21/subposts_parallel/thread-vs-process/版权声明: "署名-非商用-相同方式共享 4.0" 转载请保留原文链接及作者。