Threads

In normal operating systems, the process is determined by the appropriate address space and a single control flow. But there are often situations in which in one address space it is preferable to have several quasi-parallel control processes. The process model is based on two independent concepts: grouping resources and executing the program. When they are shared, there is such a thing as threads.

The process model is based on two independent concepts: grouping resources and executing the program. When they are shared, there is such a thing as threads

Processes can be viewed as threads of executable commands. The thread has a command counter that tracks the order of execution of actions. The stream has registers in which the current variables are stored. It has a stack containing the process execution protocol, where each separate procedure is assigned a separate structure. Although the flow proceeds within the process, it is necessary to distinguish between flow and process concepts. Processes are used to group resources, and threads are objects that are run alternately on the central processor.

Threads add to the models of the process the ability to run simultaneously in one and the same process environment of several fairly independent programs. Multiple threads running in parallel in the same process are similar to several processes running in parallel on the same computer. In the first case, threads share the address space, open files and other resources. In the second, processes share physical memory, disks, printers and other resources.

Threads have some properties of processes, so they are sometimes called simplified processes. The term multithreading is also used to describe the use of multiple threads in a single process. When running a multi-threaded process in a single-processor system, the threads work in turn. Processor quickly switches between threads, giving the impression of parallel workflows , not even on a very fast processor. For example, in the case of three threads in one process, all threads will run in parallel. Each thread will have a virtual processor with a speed equal to one third of the real processor speed.

Why are flows so necessary? The main reason is that most applications run a large number of activities, some of them may be blocked from time to time. The program schema can be greatly simplified if you split the application into several consecutive threads running in quasi-parallel mode.

When using threads, there is also the possibility of sharing parallel address objects of the same address space and all the data contained therein. For some applications, this capability is significant. In such cases, the scheme of parallel processes with different address spaces is not suitable. Another argument works in favor of threads - the ease of creating and destroying them, since no resources are associated with the flow. In most systems, the creation of a thread takes about 100 times less time than creating a process. This property is especially useful when you need to dynamically and quickly change the number of threads.

Threads do not give an increase in performance, if they are limited by the capabilities of the processor. But when there is a simultaneous need for a large amount of computation and I/O operations, the presence of threads allows you to combine these procedures in time, thereby increasing the overall speed of the application.

Tools