Section 1: Critical Section & Semaphores
1. Define the Critical Section Problem. [2 Marks]

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.

🧠 Easy to remember:

> Critical Section = Shared Data + Only One Process at a Time
Example:

Two processes trying to update the same bank account at the same time.

2. List Three Requirements of a Critical-Section Solution. [2 Marks]

A correct critical-section solution must satisfy:

1. Mutual Exclusion

Only one process can be inside the critical section at a time.

2. Progress

If no process is in the critical section, a waiting process should be allowed to enter without unnecessary delay.

3. Bounded Waiting

A process should not wait forever to enter the critical section.

🧠 Super-easy memory:

> 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
3. Differentiate Between Binary Semaphore and Counting Semaphore. [2 Marks]
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.
Example:

Binary: One printer → 0/1

Counting: Five identical printers → semaphore can represent available printers.

🧠 Remember:

> Binary = One resource / 0 or 1
Counting = Many resources / 0, 1, 2, 3...
4. State the Purpose of wait() and signal() Operations on a Semaphore. [2 Marks]
wait()

The wait() operation decreases the semaphore value and makes a process wait if the required resource is not available.

signal()

The signal() operation increases the semaphore value and allows a waiting process to proceed when the resource becomes available.

🧠 Easy memory:

> wait() = Take / Wait
signal() = Release / Notify

Think of a bathroom:

wait() → "Wait, it's occupied."
signal() → "Now it's free, you can enter."
5. Identify the Elements of the Producer–Consumer Problem. [2 Marks]

The main elements are:

1. Producer

Produces data/items and places them into a shared buffer.

2. Consumer

Takes/removes data/items from the buffer.

3. Shared Buffer

Temporary storage that holds the produced items.

4. Synchronization

Ensures that the producer and consumer access the buffer safely.

🧠 Easy memory:

> Producer → Buffer → Consumer

And:

> Produce → Store → Consume

Important: The buffer is usually limited in size, so synchronization is needed to prevent overflow and underflow.

Section 2: Deadlocks
6. Define Deadlock and State One Necessary Condition for It. [2 Marks]

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.

One necessary condition:

Mutual Exclusion – At least one resource must be held by only one process at a time.

🧠 Easy memory:

> Deadlock = Everyone waits, nobody moves.
Example:
P1 has Printer → wants Scanner
P2 has Scanner → wants Printer

Both wait forever.
7. List the Four Necessary Conditions for a Deadlock to Occur. [2 Marks]

All four conditions are:

1. Mutual Exclusion

A resource can be used by only one process at a time.

2. Hold and Wait

A process holds one resource while waiting for another.

3. No Preemption

A resource cannot be forcibly taken away from a process.

4. Circular Wait

Processes form a circular chain, where each process waits for a resource held by the next process.

🧠 VERY IMPORTANT MEMORY TRICK:

> 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.
8. Distinguish Between Deadlock Prevention and Deadlock Avoidance. [2 Marks]
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.
🧠 Easy memory:

Prevention:
> "Don't allow the dangerous situation."

Avoidance:
> "Check before giving the resource."

Prevention = Break the condition
Avoidance = Check the future
9. State the Purpose of a Resource-Allocation Graph (RAG). [2 Marks]

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

🧠 Easy memory:

> RAG = Who has what + Who wants what
Simple idea:
Process → wants → Resource

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.