Process Management & Threads
1. Process Concept
Definition
• A process is a program in execution.
• Example:
o A text editor (like MS Word) is a program when stored on
disk.
o When you open it, the OS loads it into memory, allocates
resources (CPU, memory, I/O), and it becomes a process.
Components of a Process
1. Program code (text section): The actual instructions.
o Example: Sorting algorithm instructions.
2. Data section: Variables and constants.
o Example: Array to be sorted.
3. Program counter (PC): Keeps track of the next instruction.
4. Stack: Function calls, parameters, return addresses.
5. Heap: Memory allocated dynamically.
o Example: Creating new objects in Java.
process States
1. New – A process is being created.
o Example: Opening Google Chrome.
2. Ready – Process is waiting for CPU.
o Example: Multiple browser tabs waiting for execution.
3. Running – Instructions are being executed by CPU.
o Example: A tab actively rendering a video on YouTube.
4. Waiting/Blocked – Waiting for I/O or an event.
o Example: File download waiting for server response.
5. Terminated – Process has finished.
o Example: Closing a browser tab.
Process Control Block (PCB)
• The OS uses PCB to keep track of each process.
• Example (PCB contents for a browser process):
o Process ID (PID): 2023.
o Process state: Running.
o Program counter: Next instruction for video rendering.
o Registers: Current CPU values.
o Scheduling info: Priority level.
o Memory info: Address space of Chrome.
o I/O status: Active network sockets.
Process Scheduling
Definition
• Process scheduling decides which process gets CPU when
multiple are ready.
• Example:
o While streaming a YouTube video and downloading a file,
the CPU alternates execution between both tasks.
Types of Schedulers
1. Long-term scheduler (Job scheduler):
o Controls job admission into system.
o Example: In batch systems, decides which jobs enter the
ready queue.
2. Medium-term scheduler:
o Suspends/resumes processes.
o Example: OS suspending a background update to improve
responsiveness.
3. Short-term scheduler (CPU scheduler):
o Chooses the process that runs next.
o Example: Switching between video rendering and music
playback.
Scheduling Criteria
• CPU utilization: Keep CPU busy.
• Throughput: Number of jobs completed per time.
• Turnaround time: Time to finish one process.
• Waiting time: Time spent in ready queue.
• Response time: Time before first output appears.
• Example:
o In interactive systems (like typing), response time is
critical.
o In batch jobs (like payroll processing), throughput is more
important.
Operations on Processes
Process Creation
• Example:
o In Unix/Linux, fork() creates a child process.
o If you run gcc hello.c, the parent shell process forks a
child to compile.
Process Termination
• Example:
o If a parent app crashes, child processes (plugins, helper
processes) may also be terminated.
o Windows Task Manager allows manual termination.
Interprocess Communication (IPC)
1. Shared Memory:
o Example: Two processes (e.g., a web server and database)
share cache memory to speed up queries.
2. Message Passing:
o Example: Client-server communication (browser sends
HTTP requests to web server).
Cooperating Processes
Definition
• Processes that interact and share data/resources.
Examples
• Information sharing:
o Dropbox processes sync files across devices.
• Computation speedup:
o Multiple threads/processes handle parallel tasks in a
simulation (e.g., weather forecasting).
• Modularity:
o Word processors use separate processes for spell-check and
auto-save.
• Convenience:
o Multiple users editing Google Docs simultaneously.
Problems
• Race condition:
o Example: Two banking processes updating the same
account balance at the same time → incorrect balance.
• Solution: Use synchronization tools like locks, semaphores,
monitors.
5. Threads
Definition
• A thread is the smallest unit of execution.
• A process can have one or many threads.
• Example:
o A web browser:
▪ One thread renders the page.
▪ Another downloads images.
▪ Another plays audio/video.
Benefits of Threads
1. Responsiveness:
o Example: A music app can play songs (one thread) while
downloading others (another thread).
2. Resource sharing:
o Example: Threads in MS Word share the same memory for
the document but handle spelling, formatting, and autosave
separately.
3. Economy:
o Creating/switching threads is faster than processes.
4. Scalability:
o Example: On multi-core CPUs, multithreaded programs
like Photoshop can process multiple filters simultaneously.
Types of Threads
1. User-level threads:
o Managed by libraries. Faster but OS unaware.
o Example: Java green threads.
2. Kernel-level threads:
o Managed by OS kernel. Slower but better scheduling.
o Example: Windows NT kernel threads.
Multithreading Models
1. Many-to-One: Multiple user threads mapped to one kernel
thread.
o Example: Some old thread libraries.
2. One-to-One: Each user thread maps to one kernel thread.
o Example: Windows, Linux.
3. Many-to-Many: Many user threads mapped to many kernel
threads.
o Example: Solaris OS.
In Summary
• Processes = running programs managed by OS.
• Schedulers decide CPU allocation to improve efficiency.
• Operations = creation, termination, and IPC for communication.
• Cooperating processes boost performance but need
synchronization to avoid race conditions.
• Threads = lightweight execution units, enabling responsiveness,
resource sharing, and scalability in modern apps.