Home  >  Article  >  Operation and Maintenance  >  What is a thread in linux

What is a thread in linux

青灯夜游
青灯夜游Original
2021-11-26 11:02:272707browse

In Linux, thread refers to an execution route within a program, that is, a control sequence within a process. All processes have at least one execution thread. Thread is the smallest unit of program execution. Threads share process data, but also have their own part of data.

What is a thread in linux

#The operating environment of this tutorial: linux5.9.8 system, Dell G3 computer.

The concept of Linux threads

What is a thread?

  • #An execution route in a program is called a thread. A more accurate definition is: a thread is "a control sequence within a process."
  • All processes have at least one execution thread.

Processes and threads

  • The process is the basic unit of resource allocation and competition
  • The thread is The smallest unit of program execution
  • Threads share process data, but also have their own part of the data
  • Thread ID
  • A set of registers: IP, PSW, stack pointer
  • Stack
  • errno
  • Signal status
  • Priority

Linux 线程的概念和使用

## The difference between fork and creating a new thread

    When a process executes a fork call, a new copy of the process will be created. The new process will have its own variables and its own PID. The runtime of this new process is independent; it executes almost entirely independently of the process that created it.
  • When creating a new thread in a process, the new execution process will have its own stack (and therefore its own local variables), but it will share global variables and file descriptors with its creator. , signal handler and current working directory status.

Advantages of threads

    The cost of creating a new thread is much smaller than creating a new process
  • Compared with switching between processes, switching between threads requires the operating system to do much less work
  • Threads occupy much fewer resources than processes
  • Can Make full use of the parallel number of multi-processors
  • While waiting for the end of slow I/O operations, the program can perform other computing tasks
  • Computing-intensive applications, in order to be able to run multi-processing Running on the server system, the calculation is decomposed into multiple threads to implement
  • I/O-intensive applications. In order to improve the system, I/O operations are overlapped. Threads can wait for different I/O operations at the same time

Disadvantages of threads

    Performance loss
  • A computationally intensive thread that is rarely blocked by external events often cannot share the same processor with other threads. If the number of compute-intensive threads exceeds the available processors, there may be a large performance loss, where the performance loss refers to the addition of additional synchronization and scheduling overhead, while the available resources remain unchanged.
  • Reduced robustness
  • Writing multi-threads requires more comprehensive and in-depth considerations. In a multi-threaded program, errors may occur due to slight deviations in time allocation or sharing of variables that should not be shared. The possibility of causing bad things is very high. In other words, there is a lack of protection between threads.
  • Lack of access control
  • The process is the basic granularity of access control. Calling certain OS functions in a thread will affect the entire process.
  • Increased programming difficulty
  • Writing and debugging a multi-threaded program is much more difficult than a single-threaded program

Thread scheduling competition range

    The operating system provides various models for scheduling threads created by applications. The main difference in time between these models is: when competing for system resources (especially CPU time), the thread-scheduling contention scope is different
  • Process contention scope: each thread Compete for "scheduled CPU time" in the same process (but not directly compete with threads in other processes).
  • System contention scope: Threads directly compete with other threads within the "system scope".
Related recommendations: "

Linux Video Tutorial"

The above is the detailed content of What is a thread in linux. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn