Answer:
The Critical Section Problem is the problem of ensuring that when multiple processes share a common resource, only one process can execute its critical section at a time.
The critical section is the part of a program where a shared resource or data is accessed.
> Critical Section = Shared Data + Only One Process at a Time
Two processes trying to update the same bank account at the same time.
A correct critical-section solution must satisfy:
Only one process can be inside the critical section at a time.
If no process is in the critical section, a waiting process should be allowed to enter without unnecessary delay.
A process should not wait forever to enter the critical section.
> M → P → B
Mutual Exclusion → Progress → Bounded Waiting
Or remember:
"ONE → GO → DON'T WAIT FOREVER"
ONE process at a time
GO when section is free
DON'T WAIT FOREVER
| Binary Semaphore | Counting Semaphore |
|---|---|
| Can have only 0 or 1 value. | Can have multiple non-negative values. |
| Mainly used for mutual exclusion. | Used to manage multiple instances of a resource. |
Binary: One printer → 0/1
Counting: Five identical printers → semaphore can represent available printers.
> Binary = One resource / 0 or 1
Counting = Many resources / 0, 1, 2, 3...
The wait() operation decreases the semaphore value and makes a process wait if the required resource is not available.
The signal() operation increases the semaphore value and allows a waiting process to proceed when the resource becomes available.
> wait() = Take / Wait
signal() = Release / Notify
Think of a bathroom:
wait() → "Wait, it's occupied."
signal() → "Now it's free, you can enter."
The main elements are:
Produces data/items and places them into a shared buffer.
Takes/removes data/items from the buffer.
Temporary storage that holds the produced items.
Ensures that the producer and consumer access the buffer safely.
> Producer → Buffer → Consumer
And:
> Produce → Store → Consume
Important: The buffer is usually limited in size, so synchronization is needed to prevent overflow and underflow.
Answer:
A deadlock is a situation where two or more processes are waiting for resources held by each other, so none of them can continue.
Mutual Exclusion – At least one resource must be held by only one process at a time.
> Deadlock = Everyone waits, nobody moves.
P2 has Scanner → wants Printer
Both wait forever.
All four conditions are:
A resource can be used by only one process at a time.
A process holds one resource while waiting for another.
A resource cannot be forcibly taken away from a process.
Processes form a circular chain, where each process waits for a resource held by the next process.
> M → H → N → C
Mutual Exclusion
Hold and Wait
No Preemption
Circular Wait
Think:
> "My Hands Never Cross"
My → Mutual
Hands → Hold
Never → No Preemption
Cross → Circular
If all four occur together, deadlock can occur.
| Deadlock Prevention | Deadlock Avoidance |
|---|---|
| Prevents deadlock by eliminating at least one necessary condition. | Avoids deadlock by carefully allocating resources. |
| Uses fixed rules/restrictions. | Checks whether allocation keeps the system in a safe state. |
| Example: Prevent Hold and Wait. | Example: Banker's Algorithm. |
Prevention:
> "Don't allow the dangerous situation."
Avoidance:
> "Check before giving the resource."
Prevention = Break the condition
Avoidance = Check the future
A Resource-Allocation Graph (RAG) is used to represent the relationship between processes and resources.
It shows:
Which resources are allocated to processes
Which resources are being requested
Possible deadlocks/circular waits
> RAG = Who has what + Who wants what
Resource → allocated to → Process
If the graph contains a cycle, it can indicate a possible deadlock; with a single instance of each resource type, a cycle indicates deadlock.
0 Comments