zoukankan      html  css  js  c++  java
  • Difference between Process and thread?

    What are the differences between a process and a thread? How are they similar? How can 2 threads communicate? How can 2 process communicate?

    Both processes and threads are independent sequences of execution. The main difference is that threads (of the same process) run in a shared memory space, while processes run in separate memory spaces. Lets see the differences in detail:

    Thread vs Process

    1) A program in execution is often referred as process. A thread is a subset(part) of the process.

    2) A process consists of multiple threads. A thread is a smallest part of the process that can execute concurrently with other parts(threads) of the process.

    3) A process is sometime referred as task. A thread is often referred as lightweight process.

    4) A process has its own address space. A thread uses the process’s address space and share it with the other threads of that process.

    5)

    Per process items             | Per thread items
    ------------------------------|-----------------
    Address space                 | Program counter
    Global variables              | Registers
    Open files                    | Stack
    Child processes               | State
    Pending alarms                |
    Signals and signal handlers   |
    Accounting information        |

    6) A thread can communicate with other thread (of the same process) directly by using methods like wait(), notify(), notifyAll(). A process can communicate with other process by using inter-process communication.

    7) New threads are easily created. However the creation of new processes require duplication of the parent process.

    8) Threads have control over the other threads of the same process. A process does not have control over the sibling process, it has control over its child processes only.

    Process:

    • Process is basically a program in execution. It is an active entity.
    • Some operating systems use the term ‘task‘ to refer to a program that is being executed.
    • A process is always stored in the main memory also termed as the primary memory or random access memory.
    • Therefore, a process is termed as an active entity. It disappears if the machine is rebooted.
    • Several process may be associated with a same program.
    • On a multiprocessor system, multiple processes can be executed in parallel.
    • On a uni-processor system, though true parallelism is not achieved, a process scheduling algorithm is applied and the processor is scheduled to execute each process one at a time yielding an illusion of concurrency.
    • Example: Executing multiple instances of the ‘Calculator’ program. Each of the instances are termed as a process.

    Thread:

    • A thread is a subset of the process.
    • It is termed as a ‘lightweight process’, since it is similar to a real process but executes within the context of a process and shares the same resources allotted to the process by the kernel.
    • Usually, a process has only one thread of control – one set of machine instructions executing at a time.
    • A process may also be made up of multiple threads of execution that execute instructions concurrently.
    • Multiple threads of control can exploit the true parallelism possible on multiprocessor systems.
    • On a uni-processor system, a thread scheduling algorithm is applied and the processor is scheduled to run each thread one at a time.
    • All the threads running within a process share the same address space, file descriptors, stack and other process related attributes.
    • Since the threads of a process share the same memory, synchronizing the access to the shared data withing the process gains unprecedented importance.

  • 相关阅读:
    执行序列oracle存储过程和序列化写的demo
    快捷键列表myeclipse 设置代码提示快捷键
    鼠标管理解决win8 插上usb/鼠标蓝屏或无效方法
    JQuery实现拼图数字游戏
    Django的admin定制
    Django报:AttributeError: tuple object has no attribute get
    Django的models方法返回值异常,待解决
    主页跳转子页面的时候,模板语句中的数据未返回到页面(子页面空白)
    Django报:builtin_function_or_method' object is not iterable
    Windows Azure SDK 1.5、Windows Azure Tools for Microsoft Visual Studio 2010和新的服务管理功能发布了
  • 原文地址:https://www.cnblogs.com/lightwindy/p/9808299.html
Copyright © 2011-2022 走看看