Unit 5
Unit 5
Explain Semaphore in
detail.
What is a Semaphore?
Types of Semaphores:
1. Binary Semaphore:
2. Counting Semaphore:
Working of Semaphore:
Wait(S):
S=S-1
Signal(S):
S=S+1
2. Process Behavior:
Advantages of Semaphores:
Disadvantages of Semaphores:
detail.
1. Process Management
Functions:
2. Memory Management
Functions:
4. Device Management
Functions:
Functions:
6. User Interface
Types:
7. Networking Services
Functions:
o Manages network connections and protocols.
Functions:
1. Preemptive Scheduling
Key Characteristics:
A process can lose the CPU even if it hasn't completed its execution.
How it Works:
1. A process is running.
Advantages:
Disadvantages:
Examples of Algorithms:
2. Non-Preemptive Scheduling
Key Characteristics:
How it Works:
Disadvantages:
Examples of Algorithms:
Scheduling Algorithms
Description:
Processes are executed in the order they arrive in the ready queue.
It works like a queue: the process that arrives first is executed first
(FIFO).
Steps to Implement FCFS:
Advantages:
Disadvantages:
Example:
P1 0 4
P2 1 3
P3 2 1
Step-by-Step Execution:
Gantt Chart:
| P1 | P2 | P3 |
0 4 7 8
Calculations:
P1 4 4-0=4 4-4=0
P2 7 7-1=6 6-3=3
P3 8 8-2=6 6-1=5
Description:
Advantages:
Disadvantages:
P1 0 4
P2 1 3
P3 2 1
Step-by-Step Execution:
Gantt Chart:
| P1 | P2 | P3 | P1 | P2 |
0 2 4 5 7 8
Calculations:
P1 7 7-0=7 7-4=3
P2 8 8-1=7 7-3=4
P3 5 5-2=3 3-1=2
i) Semaphore
2.
3. +process if necessary.
Types of Semaphores:
Example:
Wait(S);
// Critical Section
Signal(S);
ii) Monitor
Example:
class MonitorExample {
try {
} catch (InterruptedException e) {}
count++;
try {
} catch (InterruptedException e) {}
count--;
Key Properties:
1. Ownership: Only the thread that locks the mutex can unlock it.
Example:
// Critical Section
Q.6 What is deadlock? State and explain the conditions for deadlock,
Explain
What is Deadlock?
1. Mutual Exclusion
3. No Preemption
4. Circular Wait
Example:
Example of Deadlock
Scenario:
P1 R1 R2
P2 R2 R1
Deadlock Cycle:
Circular wait occurs as neither process can proceed until the other
releases its resource.
Avoidance:
solution?
LATER REVISIT
1. Deadlock Prevention
Definition:
Deadlock prevention ensures that at least one of the necessary conditions
for deadlock cannot occur.
2. Hold and Wait: A process holds one resource and waits for
another.
Mutual
Share resources where possible (e.g., read-only resources).
Exclusion
Hold and Require processes to request all resources at once (but this
Condition Prevention Strategy
No
Allow resources to be forcibly taken away from processes.
Preemption
Example:
2. Deadlock Avoidance
Definition:
Deadlock avoidance dynamically checks whether allocating a resource to
a process will cause a deadlock.
Assumptions:
3. If the system is still in a safe state (no deadlock), grant the request;
otherwise, deny it.
Example:
Consider:
Total resources: 10
Allocated: 7
3. Deadlock Detection
Definition:
Deadlock detection involves allowing deadlocks to occur and then using
algorithms to detect the circular wait among processes.
Example:
If P1 holds R1 and waits for R2, and P2 holds R2 and waits for R1, a
circular wait exists, signaling a deadlock.
4. Deadlock Recovery
Definition:
After detecting a deadlock, recovery methods are used to break the
deadlock and resume process execution.
Recovery Methods:
1. Process Termination:
Example: