What is the difference between a process and a thread?

后端 未结 30 2185
Happy的楠姐
Happy的楠姐 2020-11-22 00:39

What is the technical difference between a process and a thread?

I get the feeling a word like \'process\' is overused and there are also hardware and software threa

相关标签:
30条回答
  • 2020-11-22 01:01

    Real world example for Process and Thread This will give you the basic idea about thread and process

    I borrowed the above info from Scott Langham's Answer - thanks

    0 讨论(0)
  • 2020-11-22 01:01
    1. A thread runs in a shared memory space, but a process runs in a separate memory space
    2. A thread is a light-weight process, but a process is a heavy-weight process.
    3. A thread is a subtype of process.
    0 讨论(0)
  • 2020-11-22 01:02

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

    I'm not sure what "hardware" vs "software" threads you might be referring to. Threads are an operating environment feature, rather than a CPU feature (though the CPU typically has operations that make threads efficient).

    Erlang uses the term "process" because it does not expose a shared-memory multiprogramming model. Calling them "threads" would imply that they have shared memory.

    0 讨论(0)
  • 2020-11-22 01:02

    Process:

    1. Process is a heavy weight process.
    2. Process is a separate program that has separate memory,data,resources ect.
    3. Process are created using fork() method.
    4. Context switch between the process is time consuming.

    Example:
    Say, opening any browser (mozilla, Chrome, IE). At this point new process will start to execute.

    Threads:

    1. Threads are light weight processes.Threads are bundled inside the process.
    2. Threads have a shared memory,data,resources,files etc.
    3. Threads are created using clone() method.
    4. Context switch between the threads are not much time consuming as Process.

    Example:
    Opening multiple tabs in the browser.

    0 讨论(0)
  • 2020-11-22 01:03

    While building an algorithm in Python (interpreted language) that incorporated multi-threading I was surprised to see that execution time was not any better when compared to the sequential algorithm I had previously built. In an effort to understand the reason for this result I did some reading, and believe what I learned offers an interesting context from which to better understand the differences between multi-threading and multi-processes.

    Multi-core systems may exercise multiple threads of execution, and so Python should support multi-threading. But Python is not a compiled language and instead is an interpreted language1. This means that the program must be interpreted in order to run, and the interpreter is not aware of the program before it begins execution. What it does know, however, are the rules of Python and it then dynamically applies those rules. Optimizations in Python must then be principally optimizations of the interpreter itself, and not the code that is to be run. This is in contrast to compiled languages such as C++, and has consequences for multi-threading in Python. Specifically, Python uses the Global Interpreter Lock to manage multi-threading.

    On the other hand a compiled language is, well, compiled. The program is processed "entirely", where first it is interpreted according to its syntactical definitions, then mapped to a language agnostic intermediate representation, and finally linked into an executable code. This process allows the code to be highly optimized because it is all available at the time of compilation. The various program interactions and relationships are defined at the time the executable is created and robust decisions about optimization can be made.

    In modern environments Python's interpreter must permit multi-threading, and this must both be safe and efficient. This is where the difference between being an interpreted language versus a compiled language enters the picture. The interpreter must not to disturb internally shared data from different threads, while at the same time optimizing the use of processors for computations.

    As has been noted in the previous posts both a process and a thread are independent sequential executions with the primary difference being that memory is shared across multiple threads of a process, while processes isolate their memory spaces.

    In Python data is protected from simultaneous access by different threads by the Global Interpreter Lock. It requires that in any Python program only one thread can be executed at any time. On the other hand it is possible to run multiple processes since the memory for each process is isolated from any other process, and processes can run on multiple cores.


    1 Donald Knuth has a good explanation of interpretive routines in The Art of Computer Programming: Fundamental Algorithms.

    0 讨论(0)
  • 2020-11-22 01:04

    Difference between Thread and Process?

    A process is an executing instance of an application and A thread is a path of execution within a process. Also, a process can contain multiple threads.It’s important to note that a thread can do anything a process can do. But since a process can consist of multiple threads, a thread could be considered a ‘lightweight’ process. Thus, the essential difference between a thread and a process is the work that each one is used to accomplish. Threads are used for small tasks, whereas processes are used for more ‘heavyweight’ tasks – basically the execution of applications.

    Another difference between a thread and a process is that threads within the same process share the same address space, whereas different processes do not. This allows threads to read from and write to the same data structures and variables, and also facilitates communication between threads. Communication between processes – also known as IPC, or inter-process communication – is quite difficult and resource-intensive.

    Here’s a summary of the differences between threads and processes:

    1. Threads are easier to create than processes since they don't require a separate address space.

    2. Multithreading requires careful programming since threads share data strucures that should only be modified by one thread at a time. Unlike threads, processes don't share the same address space.

    3. Threads are considered lightweight because they use far less resources than processes.

    4. Processes are independent of each other. Threads, since they share the same address space are interdependent, so caution must be taken so that different threads don't step on each other.
      This is really another way of stating #2 above.

    5. A process can consist of multiple threads.

    0 讨论(0)
提交回复
热议问题