Explain the relationship between threads and processes.
- A running program is made up of one or more threads that execute within a process. A process is a running instance of a program. Each process has its own address space in memory, as well as its executable program and data. A single-threaded program is one with a single thread, whereas a multithreaded program is one with numerous threads. Each thread in a program has its stack to keep track of its state. Threads inside the same process share the same memory space and execution context A process is a collection of resources, whereas a thread is an object that may be scheduled for execution on the CPU. Threads are often referred to as lightweight processes.
- The operating system in a multitasking environment assigns various time slices to each interleaves their execution. A context switch is a process of momentarily suspending the execution of one process, storing all the information in the registers, and replacing it with information relevant to another process. This procedure is often seen as difficult, and the use of multithreading reduces the delay imposed by context shifts, allowing for the execution of several tasks in a lighter manner. The state representing the execution of a thread is minimal compared to the one describing a process. As a result, moving between threads is preferable to moving between processes. The usage of many threads instead of several processes is justifiable if and only if the tasks executed are logically connected and require sharing memory or other resources.
- Figure 5.4 depicts the relationship between threads and processes, as well as a simplified depiction of the runtime execution of a multithreaded program. A process, which has at least one thread, usually known as the main thread, identifies a running application. The compiler or the runtime environment that executes the program creates such a thread implicitly. This thread is likely to last for the duration of the process and to be the genesis of other threads, which have a shorter length in general. These threads, as main threads, can spawn additional threads. There is no distinction between the main thread and the additional threads that are produced over the process's lifespan. Each has its local storage and a set of instructions to execute, and they all share the memory space set aside for the overall process. When all of the threads have been completed, the procedure is deemed to be finished.
Comments
Post a Comment