Processes and Threads in Operating Systems
In this tutorial, we will introduce the notion of a process and thread. We will know the concept of process/ thread states and discuss how or why processes/threads make transitions between these states.
A process is a program in execution. Each process has its own address space, it consists of a text region, data region and stack region. The text region stores the code that the processor executes. The data region consists of variables and dynamically allocated memory. The stack region contains instructions and local variables.
Life Cycle of a Process :
- Running state : The process is executing on a processor.
- Ready state : The process can execute on a processor if any processor is available.
- Blocked state : The process is waiting for some tasks to happen before it can proceed.
State Transitions of a Process :
There are four possible state transitions
A. When a process is dispatched, it transitions from ready state to running state.
B. When the time quantum allotted to that process expires, it transitions from running state to ready state.
C. When a process blocks, it transitions from running state to blocked state.
D. When the event occurs, a process transitions from blocked state to ready state.
Process Management by OS :
Operating systems provide fundamental services to processes. It can create, destroy, suspend or resume process. An OS can change the identity of any process. It can block, wake up or dispatch a process. OS is responsible for Inter process Communication(IPC), too.
Thread is lightweight process ( LWP). It shares address space and other global information with its process. Registers, stack and other thread-specific data are locally available to each thread. Threads have become more useful as it scales better to multiprocessor systems and incurs less overhead than IPC.
Life Cycle of a Thread :
A. Born state B. Ready state C. Running state D. Dead state E. Blocked state F. Waiting state G. Sleeping state
Threading Models :
Three most popular threading models
User-level threads : Perform threading operations in user space. Operating system maps all threads in a multithreaded process to single execution context.
Kernel-level threads : They map each thread to its own execution context to increase scalability and throughput.
Combination of user- and kernel-level threads : Here number of user and kernel threads need not be equal. It reduces overhead as compared to one-to-one thread mappings because it implements thread pooling.
Differences Between Processes and Threads :
1. A process is a program under execution whereas, a thread is a small segment of a process.
2. A process can have its individual existence whereas a thread cannot.
3. Processes can communicate with each other using IPC only. On the other hand, threads can directly communicate with each other.