In our study of Linux, because of some translation problems, we will encounter some uncommon, similar words and concepts in our study. Some are difficult to understand, some are easy to confuse. In a Linux system, for a user-created process (thread), does the CPU allocate a time slice to a thread or a process?
is a thread. A thread is a unit of actual work, and a process is simply a container for managing one or more threads.
1. Does this mean that you try to use multithreaded concurrency so that you can grab more time slices.
In theory, one of the uses of multithreading is to be able to do several things at the same time to improve efficiency. The real problem, however, is that the number of CPUs (cores, hereinafter) is limited and not much. If your CPU has 8 CPUs and there are 8 threads in the system, regardless of the interruption, each thread can theoretically continue to execute. However, after more than 8 threads, the operating system must be dispatched, that is, allocating time slices. The specific allocation scheme, or the scheduling algorithm has many kinds. If a process creates many threads, there are at most only 8 states that can be executed [2], and the rest of the threads must wait for dispatch. A context switch is required when the thread is dispatched, which is an additional overhead. When there are too many threads, the extra overhead of context switching can negatively affect the efficiency of the system.
2. Does the operating system reduce the time slices per thread or do other processing to ensure fairness for processes that have multithreading?
This is the problem that the scheduling algorithm needs to be considered and optimized. For example, threads and processes have priority, and in preemptive scheduling, high-priority threads can preempt CPUs from low-priority threads. In addition, in the multi-CPU platform, the scheduling algorithm should also consider the correlation of the cache.
Multiple threads in a process should be aware of the possibility of blocking this thread, leaving the remaining time slices to the sibling thread.
In the mainstream operating system implementation, the general process is unable to directly control the execution order of their own threads. In other words, suspending one thread does not guarantee that another thread will be able to be executed.
The Linux kernel actually does not differentiate between processes and threads, and the kernel calls the execution unit a task. The operating system actually dispatches the process, and the process creates the same other process through fork (). Each process has a PID, and the one that starts first in the same set of processes also has a tgid. Strictly speaking, the former should be called the thread ID, which should be called the process ID. Threads in Linux are actually a series of processes that share some resources.
The future of cloud computing, Linux system courses