What is the difference between a process and a thread?

后端 未结 30 2027
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:05

    The following is what I got from one of the articles on The Code Project. I guess it explains everything needed clearly.

    A thread is another mechanism for splitting the workload into separate execution streams. A thread is lighter weight than a process. This means, it offers less flexibility than a full blown process, but can be initiated faster because there is less for the Operating System to set up. When a program consists of two or more threads, all the threads share a single memory space. Processes are given separate address spaces. all the threads share a single heap. But each thread is given its own stack.

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

    Threads within the same process share the Memory, but each thread has its own stack and registers, and threads store thread-specific data in the heap. Threads never execute independently, so the inter-thread communication is much faster when compared to inter-process communication.

    Processes never share the same memory. When a child process creates it duplicates the memory location of the parent process. Process communication is done by using pipe, shared memory, and message parsing. Context switching between threads is very slow.

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

    Both threads and processes are atomic units of OS resource allocation (i.e. there is a concurrency model describing how CPU time is divided between them, and the model of owning other OS resources). There is a difference in:

    • Shared resources (threads are sharing memory by definition, they do not own anything except stack and local variables; processes could also share memory, but there is a separate mechanism for that, maintained by OS)
    • Allocation space (kernel space for processes vs. user space for threads)

    Greg Hewgill above was correct about the Erlang meaning of the word "process", and here there's a discussion of why Erlang could do processes lightweight.

    0 讨论(0)
  • To explain more with respect to concurrent programming

    1. A process has a self-contained execution environment. A process generally has a complete, private set of basic run-time resources; in particular, each process has its own memory space.

    2. Threads exist within a process — every process has at least one. Threads share the process's resources, including memory and open files. This makes for efficient, but potentially problematic, communication.

    An example keeping the average person in mind:

    On your computer, open Microsoft Word and a web browser. We call these two processes.

    In Microsoft Word, you type something and it gets automatically saved. Now, you have observed editing and saving happens in parallel - editing on one thread and saving on the other thread.

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

    For those who are more comfortable with learning by visualizing, here is a handy diagram I created to explain Process and Threads.
    I used the information from MSDN - About Processes and Threads

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

    An application consists of one or more processes. A process, in the simplest terms, is an executing program. One or more threads run in the context of the process. A thread is the basic unit to which the operating system allocates processor time. A thread can execute any part of the process code, including parts currently being executed by another thread. A fiber is a unit of execution that must be manually scheduled by the application. Fibers run in the context of the threads that schedule them.

    Stolen from here.

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