Operating System
OS Part-1
OS Part-2
- File Concepts and Access methods
- Free Space Management and Allocation methods
- Directory Systems and Protection
- File Organization, Sharing and Implementation issues
- Disk and Drum Scheduling
- I/O Devices Organisation & I/O Buffering
- I/O Hardware, Kernel I/O subsystem and Transforming I/O Requests to Hardware Operations
- Device Drivers and Path Management
- Device Driver Sub Modules and Procedure
- Device Scheduler and Handler
- Interrupt Service Routine (ISR)
- File System in Linux and Windows
OS Part-3
- Process and Process Control Block(PCB)
- Process Scheduling( Preemptive and Non Preemptive)
- Scheduling Algorithms
- Algorithm Evaluation
- Multiple Processor Scheduling
- Real Time Scheduling
- Operations on Processes
- Threads
- Inter-Process Communication
- Precedence Graphs
- Critical Section Problem
- Semaphores
- Classical Problems of Synchronization
- DeadLock
- Deadlock Prevention and Avoidance
- Deadlock Detection and Recovery
- Process Management in Linux
OS Part-4
- Memory Hierarchy in OS
- Concepts of Memory Management
- MFT and MVT
- Logical and Physical Address Space
- Swapping
- Contiguous and Non Contiguous Memory Allocation
- Paging
- Segmentation
- Paging Combined with Segmentation
- Structure and Implementation of Page Table
- Virtual Memory in OS
- Cache Memory Organization
- Demand Paging
- Page Replacement Algorithms
- Allocation of Frames and Thrashing
- Demand Segmentation
OS Part-5
- Distributed Operating System: Introduction and Types
- Distributed OS: Design Issues
- Distributed OS: File System
- Distributed OS: Remote File Access
- Remote Procedure Call(RPC)
- Remote Method Invocation(RMI)
- Distributed Shared Memory
- Parallel Processing and Concurrent Programming
- Security and Threats Protection in Distributed OS
- Security Design Principles and Authentication in Distributed OS
- Sensor Network and Parallel OS
Semaphores in Operating System
Introduction to Semaphores
Semaphore ek synchronization tool hai jo multiprocessing aur multithreading environments me shared resources ka access manage karne ke liye use hota hai. Yeh race conditions aur critical section problems ko avoid karne me help karta hai.
Semaphore ek integer variable hota hai jo shared resources ki availability ko control karta hai.
Types of Semaphores
Semaphores do tareeke ke hote hain:
1. Binary Semaphore (Mutex)
-
Iska value sirf 0 ya 1 ho sakta hai.
-
0: Resource occupied hai (koi use kar raha hai).
-
1: Resource free hai (koi bhi use kar sakta hai).
-
Ek baar me sirf ek process hi resource ko access kar sakti hai.
-
Example: Agar ek file ko ek process access kar rahi hai, to dusri process tab tak wait karegi jab tak pehli process ka kaam complete nahi ho jata.
2. Counting Semaphore
-
Iska value 0 se zyada ho sakta hai (1, 2, 3,…).
-
Multiple instances wale resources (e.g., printers, database connections) ka access control karta hai.
-
Example: Agar system me 3 printers available hain, to initially semaphore ki value 3 hogi, aur har baar jab ek printer use hoga, value 1 se decrement ho jayegi.
Semaphores ka Working Mechanism
Semaphore do atomic operations ka use karta hai:
1. wait() Operation (P operation / Down operation)
Agar resource available hai, to process use kar sakti hai, warna wait karegi.
wait(S) {
while (S <= 0); // Jab tak S > 0 na ho, tab tak wait karo
S = S - 1; // Resource occupy karne ke baad value reduce kar do
}
2. signal() Operation (V operation / Up operation)
Jab ek process resource use kar ke free kar deti hai, tab signal operation execute hota hai.
In C language
signal(S) {
S = S + 1; // Resource free hone ke baad value increase kar do
}
Example: Semaphore ka use Process Synchronization ke liye
Scenario: Do Processes Shared Resource Use Kar Rahe Hain
Maan lo ki do processes (P1 aur P2) ek shared resource ko access kar rahe hain. Semaphore ensure karega ki ek samay par sirf ek process hi resource ko access kare.
Initial State:
Semaphore S = 1 (Resource free hai)
Execution Steps:
-
1. P1 enters critical section → wait(S) execute hota hai → S = 0 ho jata hai
-
2. P2 bhi enter karna chahta hai → lekin S = 0 hai → P2 wait karega
-
3. P1 ka kaam complete hota hai → signal(S) execute hota hai → S = 1 ho jata hai
-
4. P2 ab enter kar sakta hai → wait(S) execute hota hai → S = 0 ho jata hai
Diagram Representation:
Process P1 Process P2
----------------------------------
wait(S) wait(S) (Blocked)
Enter CS
Modify Resource
signal(S) Enter CS (After P1 exits)
Exit CS Modify Resource
signal(S)
Exit CS
Real-World Use Cases of Semaphores
Process Synchronization: Multiple processes ke beech orderly execution ensure karta hai.
Race Condition Avoidance: Shared resources par multiple processes ke simultaneous access se hone wale issues ko prevent karta hai.
Producer-Consumer Problem: Buffer management me producer aur consumer ka synchronization maintain karta hai.
Readers-Writers Problem: Multiple processes ke read aur write operations ko synchronize karta hai.
Conclusion
Semaphores ek synchronization mechanism hai jo processes aur threads ke beech coordination establish karta hai.
Binary semaphore sirf ek process ko resource access dene me madad karta hai, jabki counting semaphore multiple instances wale resources ka access control karta hai.
wait() aur signal() operations ke through process synchronization achieve kiya jata hai.