Skip to main content

Chapter 03 · Watch, then practise

Process Communication and Synchronization

Watch the related lecture, then read the question and answer below.

10 questions · 5 playlist videos. Matches are based on video titles; broader background matches are labeled.

Open the full chapter playlist ↗

Topic matches: critical sections, race conditions, and semaphores.

1. Critical-section problem and semaphores — 2075 Bhadra

What is the critical-section problem? Why must executing the critical section be mutually exclusive? Describe how semaphores can solve the critical-section problem.

Answer

A critical section is a part of a program that accesses a shared resource, such as memory, a data structure, the CPU, or an I/O resource. More than one process must not execute the critical section at the same time. Processes need a protocol for entering and leaving it so that concurrent changes do not cause a race condition.

Mutual exclusion prevents a second process from entering while the first is inside. Otherwise, changes made by one process can cause errors in another. A semaphore controls entry through wait and releases access through signal. The original pseudocode and accompanying explanation are retained below.

The handwritten explanation repeats process labels in a confusing way. Refer to the original rather than treating those labels as corrected here.

Critical section and semaphore pseudocode

Semaphore explanation and following question

Covers critical sections and race conditions. No dedicated sleep/wakeup lecture is listed in this chapter playlist.

2. Race conditions, sleep, and wakeup — 2074 Bhadra

What is a race condition? Why are sleep() and wakeup() better than busy waiting?

Answer

A race condition occurs when processes read or write shared data and the final result depends on which process runs when. Busy waiting continually checks a condition, wasting CPU time. With priority scheduling, it can also cause progress problems when the waiting process prevents the process that must release the resource from running. sleep() suspends a process; another process calls wakeup() when it can proceed.

Topic matches for test-and-set and semaphore operations. The exact class implementation has not been verified against these videos.

3. TSL and semaphore implementation — 2074 Bhadra

What is TSL? Why is it used? Explain the major operations of a semaphore with a simple implementation as a class.

Answer

TSL means test-and-set lock. It provides synchronization for mutual exclusion. The semaphore operations are wait, used before accessing a resource, and signal, used when the process finishes using it. The source's class implementation is preserved below, including its original notation and incomplete code details.

TSL and semaphore class, first part

Semaphore class, continuation

Related explanation of critical sections and race conditions; not a verified answer to the question about keeping a critical section short.

4. Keeping the critical section short — 2073 Bhadra

Explain the critical-section problem. Why is it important for a thread to execute its critical section as quickly as possible?

Answer

Only one thread may hold the lock and execute the critical section at a time. Other threads must wait until it releases the lock. Finishing quickly lets those threads proceed sooner. The critical-section definition is given in Question 1.

Topic match: semaphores. The scanned pseudocode remains the source for the specific implementation.

5. Semaphore definition and operations — 2073 Bhadra

Define a semaphore and explain its major operations, including pseudocode.

Answer

A semaphore is a variable used to solve critical-section and synchronization problems in multiprocessing. The two major operations are wait and signal. Their exact handwritten pseudocode is retained below:

Semaphore definition and start of pseudocode

Semaphore operations and producer–consumer solution

Background on mutual exclusion and semaphores. No dedicated producer–consumer or message-passing solution is listed in the supplied playlist.

6. Mutual-exclusion requirements and producer–consumer — 2073 Magh

State the requirements for mutual exclusion. Solve the producer–consumer problem using semaphores and message passing.

Answer

  1. No two processes may be inside their critical sections simultaneously.
  2. No assumptions should be made about processor speeds or the number of CPUs.
  3. A process outside its critical section must not block another process.
  4. No process should wait forever to enter its critical section.

The semaphore solution is reproduced below. The source does not supply the requested message-passing solution.

Producer–consumer semaphore solution, first part

Producer–consumer semaphore solution, continuation

Monitor in operating system

Easy Learning with Nisha · 5:15

Topic match: monitors. Coverage of this exact producer–consumer implementation has not been verified.

7. Producer–consumer using monitors — 2072 Ashwin

Solve the producer–consumer problem using monitors.

Answer

The critical operations belong inside a ProducerConsumer monitor, which provides mutual exclusion. A producer or consumer that cannot continue waits on a condition; another operation signals that condition when progress becomes possible. The source uses an item count and conditions for full and empty states. Its partial first attempt and subsequent monitor implementation are both preserved.

Original partial and complete monitor implementation

Monitor producer and consumer procedures

Topic matches: Peterson’s solution and test-and-set synchronization.

8. Synchronization, Peterson's solution, and TSL — 2072 Magh

Why is synchronization needed? Explain Peterson's solution and TSL as busy-waiting solutions.

Answer

Synchronization coordinates processes using shared resources so that concurrent operations do not leave inconsistent data. Peterson's solution coordinates two processes accessing a shared resource. Each calls enter_region before its critical section and leave_region afterward; entry waits until it is safe to proceed.

TSL copies the old lock value into a register and sets the lock atomically. The process compares the old value with zero and repeats if the lock was already held. On leaving the critical section it releases the lock. The original code and explanations follow.

One source sentence says the lock is set back to 1, while the exit code and later explanation use 0. The inconsistency is retained in the scan.

Peterson's algorithm, first part

Peterson's algorithm and TSL assembly

TSL explanation and pipe question

Covers semaphore fundamentals only. No dedicated pipe or distributed-semaphore video is listed in the supplied playlist.

9. Pipes and distributed semaphores — 2071 Magh

Why are pipes needed? Define a semaphore and its operations. Can semaphores be used in a distributed system?

Answer

A pipe passes the output of one process to the input of another. The source describes communication between related processes, such as parent and child processes. Semaphore definitions and operations are given in Question 5.

The source says that distributed processors do not share memory and communicate by messages. It then discusses indirectly implementing semaphore-like coordination using message passing.

Source ambiguity: The answer first denies the use of semaphores in distributed systems and later describes a message-based implementation. It does not clearly distinguish a shared-memory semaphore from distributed emulation.

Original distributed-semaphore discussion

Covers critical sections, Peterson’s solution, and test-and-set. The playlist does not separately cover every approach named in this question.

10. Approaches to mutual exclusion — 2070 Bhadra

Explain approaches that prevent another process from entering its critical section while shared memory is being updated.

Answer

  • Disable interrupts: On a single CPU, preventing interrupts stops a process from being switched out during the critical section. Giving user processes this power is unattractive.
  • Lock variable: A shared variable indicates whether the critical section is free or occupied. The source presents the 0/1 approach without establishing that the check-and-set operation is atomic.
  • Strict alternation: A shared turn variable selects which process may enter. The other process busy-waits until the turn changes.
  • Peterson's solution: Coordinates two processes through shared state; see Question 8.
  • TSL: Atomically reads the old lock value and sets the lock; only the process that finds it free enters.

The strict-alternation code and remaining original discussion are preserved below.

Strict-alternation code and explanation

Peterson and TSL discussion