Threads, processes and concurrency

process

What is the process? A process is a program that is executing; a process is an instance of a program that is executing on a computer; a process is an entity that can be assigned to and executed by a processor. A process generally includes an instruction set and a system resource set. The instruction set here refers to program code, and the system resource set here refers to I/O, CPU, memory, and so on. To sum up, we can also understand that a process is a running activity of a program with certain independent functions on a certain data set, and a process is an independent unit for the system to allocate and schedule resources.
When a process executes, a process can be uniquely represented, consisting of the following elements:

  • Process descriptor: The unique identifier of the process, used to distinguish it from other processes. In Linux, it is called the process ID, which is generated during the system call fork, but what we return through getpid is not its pid field, but its thread group number tgid.
  • Process status: we often say suspended, running and other states, which represent the current state.
  • Priority: The execution scheduling between processes is related, relative to other processes.
  • Program Counter: The address of the next instruction to be executed in the program, which is a memory address in kernel or user memory space.
  • Memory pointers: Including pointers to program code and process-related data, as well as pointers to shared memory blocks with other processes.
  • Context data: The data of the processor's registers while the process is executing.
  • I/O status information: including explicit I/O requests, I/O devices assigned to the process, etc.
  • Accounting information: may include total processor time, total number of clocks used, time limits, etc.

All of the above elements are placed in a data structure called a process control block. A process control block is a structure by which the operating system can support multiple processes and provide multiprocessing. When the operating system performs a process switch, it performs two steps, one is to interrupt the process in the current processor, and the other is to execute the next process. Whether interrupted or executed, the program counter, context data, and process state in the process control block change. When the process is interrupted, the operating system will save the program counter and the processor register (corresponding to the context data in the process control block) to the corresponding position in the process control block, and the process state will also change, it may enter the blocking state, or it may be enter the ready state. When the next process is executed, the operating system sets the next process to the running state according to the rules, and loads the program context data and program counter of the process to be executed.

thread

A process has two characteristic parts: resource ownership and scheduling execution. Resource ownership means that the process includes the memory space, I/O and other resources required for the process to run. Scheduled execution refers to the execution path in the middle of the process execution process, or the instruction execution flow of the program. These two feature parts can be separated. After separation, the one that owns the data is usually called a process, and the one that owns the dispatchable part of the execution code is called a thread or lightweight process.
Threads have the meaning of "execution clues", and processes are defined as resource owners in a multi-threaded environment, which still stores the process control block of the process. Threads are structured differently from processes, and each thread consists of:

  • Thread state: The current state of the thread.
  • an execution stack
  • Private data area: static storage space for each thread-local variable
  • Register Set: Stores some state of the processor

Each process has a process control block and user address space, each thread has an independent stack and an independent control block, and has its own independent execution context. Its structure is shown in Figure 8.1.

Threads are somewhat different from processes in their execution. Each independent thread has an entry for program execution, a sequential execution sequence, and an exit for the program. However, threads cannot be executed independently, and must depend on the process, and the process provides multiple thread execution control. From a logical point of view, the meaning of multithreading is that in a process, there are multiple execution parts that can be executed simultaneously. At this point, the process itself is not the basic running unit, but the container of the thread.
Compared with processes, threads have the advantage of being faster, whether it is creating a new thread or terminating a thread; whether it is switching between threads or sharing data or communication between threads, its speed has a greater advantage than processes.

Concurrency and Parallelism

Concurrency, also known as co-occurrence, refers to the ability to handle multiple simultaneous activities, and concurrent events do not necessarily have to occur at the same time. For example, modern computer systems can load multiple programs into memory in the form of processes at the same time, and use time-division multiplexing of processors to appear to be running simultaneously on one processor.
Parallelism refers to two concurrent events that occur at the same time, and has the meaning of concurrency, while concurrency is not necessarily parallel.
The difference between concurrency and parallelism is that a processor handles multiple tasks at the same time and multiple processors or multi-core processors handle multiple different tasks at the same time. The former is logically simultaneous (simultaneous), while the latter is physically simultaneous.

 

Reference:
"Operating System Essence and Design Principles"

http://www.php-internals.com/book/?p=chapt08/08-02-thread-process-and-concurrent

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324657226&siteId=291194637