Multi-threaded processes and threads
Processes and threads
Process: it is the basis of the operating system structure; it is a program in progress; an instance of a program running in the computer; it can be allocated to an entity executed by the processor; the execution in a single order shows the activity units described by the current status and a group of related system resources.
There are two main concepts of process:
First, a process is an entity. Each process has its own address space, generally including text region, data region, and stack region ).
- Code executed by the text storage processor.
- The data area stores variables and dynamically allocated memory used during process execution.
- The stack area stores the commands and local variables called by the active process.
Second, the process is a "program in progress ". A Running process may have three basic states: Ready, Running, and Blocked ).
Thread: a Lightweight Process (LWP) is the smallest unit of execution flow. A standard thread consists of the thread ID, current Instruction Pointer (PC), register set, and stack. In addition, a thread is an entity in a process and is the basic unit for independent scheduling and distribution by the system. A thread itself does not own system resources, but only has a few resources that are essential for running, however, it can share all resources of a process with other threads of the same process. One thread can create and withdraw another thread, and multiple threads in the same process can execute concurrently. Due to mutual constraints between threads, the threads are intermittently running. The thread also has three basic states: Ready, blocked, and running. Each program has at least one thread. If the program has only one thread, it is the program itself.
A thread is a single sequential control flow in a program. It is called multithreading.
In multi-threaded OS, a process usually contains multiple threads. Each thread is used as the basic unit of CPU utilization and is the entity that consumes the minimum overhead. A thread has the following attributes:
1) Lightweight entity
The entities in the thread basically do not have system resources, but there is only one essential resource that can ensure independent operation, for example, each thread should have a thread control block TCB used to control thread running, registers and stacks used to indicate the program counters of the executed command sequence, reserve local variables, a few status parameters, and return addresses.
2) basic unit of independent scheduling and dispatching.
In multi-threaded OS, threads are the basic unit for independent operation, and thus the basic unit for independent scheduling and allocation. Because the thread is very "light", thread switching is very fast and has low overhead.
3) Concurrent execution.
Concurrent execution can be performed between multiple threads in a process, or even all threads in a process can be concurrently executed. Similarly, threads in different processes can also be concurrently executed.
4) share process resources.
Each thread in the same process can share the resources of the process. This first shows that all threads have the same address space (the address space of the process), which means that, A thread can access every virtual address in the address space. In addition, it can also access opened files, timers, semaphore institutions, and other processes.
The differences between threads and processes can be summarized as follows:
1) address space and other resources (such as opening files): processes are independent of each other and the threads of the same process are shared. Threads in a process are invisible to other processes.
2) Communication: Interprocess Communication IPC. interthreads can directly read and write process data segments (such as global variables) for communication. Process Synchronization and mutex are required to ensure data consistency.
3) scheduling and switching: thread context switching is much faster than process context switching.
4) In a multi-threaded OS, a process is not an executable entity.