Process and Threads - aryanjoshi0823/5143-Operating-System GitHub Wiki
A process is essentially a program in execution. It represents an instance of a program that is being executed by the operating system. Processes are fundamental to multitasking and the execution of programs within a computer system. When a program is loaded into memory and executed, it becomes a process, and its execution is performed sequentially.
A process can be thought of as an entity representing a unit of work within a system. The memory of a process is typically divided into four main sections:
-
Text Section: This section contains the compiled program code that is loaded into memory when the program is executed. It is typically stored in non-volatile storage and is read into memory during the program's launch.
-
Data Section: This section stores global and static variables that are allocated and initialized prior to the execution of the
main
function. -
Heap: The heap is used for dynamic memory allocation during runtime. Memory is allocated and freed dynamically using functions such as
new
,delete
,malloc
, andfree
. -
Stack: The stack is used for local variables. Space for local variables is reserved when they are declared (such as at function entrance). The stack is also responsible for managing function return values. The stack and heap start at opposite ends of the process’s free space and grow towards each other. If they meet, it may result in a stack overflow error or memory allocation failure.
A thread is a single sequential flow of execution of tasks within a process, also referred to as a thread of execution or thread of control. A process in an operating system can have multiple threads, each with its own program counter, stack of activation records, and control blocks. Threads are considered lightweight processes.
A process can be split into multiple threads. For example:
- Browser: Each tab can be considered a separate thread.
- MS Word: Different threads handle text formatting, input processing, etc.
- Three threads of same process:
User-level threads are managed by the user, and the operating system is unaware of them. They are implemented in user space and operate independently of the kernel.
Examples: Java Threads, POSIX Threads.
- Easy to implement.
- Faster and efficient.
- Shorter context switch time compared to kernel threads.
- No OS modification required.
- Simple representation with registers, program counter, stack, and thread control blocks stored in user space.
- Simplified creation, switching, and synchronization.
- Lack of coordination between user threads and the kernel.
- If a thread causes a page fault, the entire process is blocked.
Kernel threads are managed by the operating system. Each thread and process has a thread control block (TCB) and a process control block (PCB) in the kernel.
Examples: Windows Threads, Solaris.
- The kernel is aware of all threads.
- The scheduler optimizes CPU usage across threads.
- Suitable for applications with frequent blocking operations.
- More complex to implement compared to user threads.
- Slower context switching.
Every thread has the following components:
- Program Counter
- Register Set
- Stack Space