What is the difference between a process and a thread?

后端 未结 30 2179
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 00:58

    Trying to answer this question relating to Java world.

    A process is an execution of a program but a thread is a single execution sequence within the process. A process can contain multiple threads. A thread is sometimes called a lightweight process.

    For example:

    Example 1: A JVM runs in a single process and threads in a JVM share the heap belonging to that process. That is why several threads may access the same object. Threads share the heap and have their own stack space. This is how one thread’s invocation of a method and its local variables are kept thread safe from other threads. But the heap is not thread-safe and must be synchronized for thread safety.

    Example 2: A program might not be able to draw pictures by reading keystrokes. The program must give its full attention to the keyboard input and lacking the ability to handle more than one event at a time will lead to trouble. The ideal solution to this problem is the seamless execution of two or more sections of a program at the same time. Threads allows us to do this. Here Drawing picture is a process and reading keystroke is sub process (thread).

    0 讨论(0)
  • 2020-11-22 00:58

    Process: program under execution is known as process

    Thread: Thread is a functionality which is executed with the other part of the program based on the concept of "one with other"so thread is a part of process..

    0 讨论(0)
  • 2020-11-22 00:59

    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.

    Process

    Is a program in execution. it has text section i.e the program code, current activity as represented by the value of program counter & content of processors register. It also includes the process stack that contains temporary data(such as function parameters, return addressed and local variables), and a data section, which contains global variables. A process may also include a heap, which is memory that is dynamically allocated during process run time.

    Thread

    A thread is a basic unit of CPU utilisation; it comprises a thread ID, a program counter, register set, and a stack. it shared with other threads belonging to the same process its code section, data section and other operating system resources such as open files and signals.

    -- Taken from Operating System by Galvin

    0 讨论(0)
  • 2020-11-22 00:59

    Coming from the embedded world, I would like to add that the concept of processes only exists in "big" processors (desktop CPUs, ARM Cortex A-9) that have MMU (memory management unit) , and operating systems that support using MMUs (such as Linux). With small/old processors and microcontrollers and small RTOS operating system (real time operating system), such as freeRTOS, there is no MMU support and thus no processes but only threads.

    Threads can access each others memory, and they are scheduled by OS in an interleaved manner so they appear to run in parallel (or with multi-core they really run in parallel).

    Processes, on the other hand, live in their private sandbox of virtual memory, provided and guarded by MMU. This is handy because it enables:

    1. keeping buggy process from crashing the entire system.
    2. Maintaining security by making other processes data invisible and unreachable. The actual work inside the process is taken care by one or more threads.
    0 讨论(0)
  • 2020-11-22 01:00

    Process:

    • An executing instance of a program is called a process.
    • 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 within the process gains unprecedented importance.

    I borrowed the above info from the Knowledge Quest! blog.

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

    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.

    ref-https://practice.geeksforgeeks.org/problems/difference-between-process-and-thread

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