This is the seventh day of my participation in the First Challenge 2022. For details: First Challenge 2022.

From the user’s point of view, a process is a running program instance, and a thread is the basic unit in which tasks are actually performed in a process. That is, a running program contains at least one process, and a process contains at least one thread. Threads cannot exist independently of processes.

process

Process is the basic unit of resource allocation in the operating system. A Process has its own heap, stack, virtual space (page table), file descriptor and other information. To understand a Process from a programming point of view, it can be regarded as a class or a STRUCTURE of a PCB (Process Control Block) Process Control Block, which roughly contains the following contents:

  1. PID: indicates the ID of a process.
  2. Process status:
    1. The new state
    2. The ready state
    3. Running state
    4. The blocking state
    5. Destruction of state
  3. Executive priority
  4. Context: The process of saving the state of this execution so that it can continue next time is a context.
  5. Memory address

thread

Thread is the basic unit of operation scheduling in operating system. It is contained in the process and is the actual running unit in the process. In Unix System V and SunOS, threads are also called Lightweight processes, but more commonly referred to as kernel threads and user threads.

PS: User threads can be understood as the application’s own threads, which are created and controlled by programmers; Kernel threads are threads supported and used by the kernel.

Thread advantage

Threads are lightweight processes that contain multiple threads in a process. Therefore, multiple threads can share process resources. The relationship between threads and processes is shown in the following figure:The heap and method areas are areas that can be shared, while the program counter and stack are private to each thread.

  • A program counter is an area of memory that records the address of the instruction currently being executed by the thread.
  • The stack is used to record each thread’s own local variables.
  • The heap holds all objects created by the current program.
  • The method area stores information such as constants and static variables.

The difference between processes and threads

The main differences between processes and threads are the following.

Difference 1: Different affiliations

The dependency relationship is different: a process is an instance of a running program, a process contains threads, and threads cannot contain processes.

Difference 2: Different emphasis in description

Processes are the basic unit of resource allocation in the operating system, while threads are the basic unit of scheduling in the operating system.

Difference 3: Different shared resources

Different shared resources: multiple processes cannot share resources, each process has its own heap, stack, virtual space (page table), file descriptors, and other information, while threads can share process resource files (heap and method area).

Difference 4: Different context switching speeds

Context switching speed is different: threads switch context quickly (context switching refers to switching from one thread to another), while processes switch context slowly.

Difference 5: Different manipulators

The operator is different: in general, the operator of the process is the operating system, and the operator of the thread is the programmer.

conclusion

Processes are the basic unit of operating system resource allocation, and threads are the basic unit of operating system scheduling. A process contains at least one thread. Threads cannot exist independently of the process. Processes cannot share resources, whereas threads can. Threads can be considered as lightweight processes, with major differences in dependency, emphasis on description, shared resources, context-switching speed, and manipulation of objects.

The resources

The Beauty of Concurrent Programming in Java

Judge right and wrong from yourself, praise to listen to others, gain and loss in the number.

Public number: Java interview analysis

Interview collection: gitee.com/mydb/interv…