5 Concurrency And Synchronization

Operating Systems ยท Unit 5

Concurrency and Synchronization

Exam-focused notes for Concurrency and Synchronization (Operating Systems, BIT204): what the TU syllabus asks and how it has actually been tested, with 8 solved past questions from this unit.

Concurrency and Synchronization in short

When many processes or threads share data, their order of execution can change the result. This chapter explains how to keep shared data correct (synchronization) and introduces threads.

Concurrency and Synchronization: notes

Critical section problem

A critical section is the part of a program that uses shared data or resources. Only one process should be inside its critical section at any time.

A process has this general form:

do {
    entry section        // ask for permission
        critical section // use shared data
    exit section         // release permission
        remainder section
} while (true);

A correct solution must satisfy three rules:

  • Mutual exclusion: if one process is in its critical section, no other can be.
  • Progress: if no process is inside, only processes that want to enter decide who goes next, and the choice cannot wait forever.
  • Bounded waiting: there is a limit on how many times others can enter before a waiting process gets in.

Race condition definition and examples

A race condition happens when two or more processes use shared data at the same time. The result then depends on the order in which they run.

Example: two threads add 1 to a shared counter that holds 5. The statement counter++ is really three machine steps: load, add, store.

$$\begin{aligned} \text{T1 loads} &: 5 \ \text{T2 loads} &: 5 \ \text{T1 stores} &: 6 \ \text{T2 stores} &: 6 \end{aligned}$$

The answer should be 7, but it is 6. One update was lost. Race conditions are hard to find because they do not happen on every run. The cure is mutual exclusion around the shared update.

Lock variable solution

A lock variable is a shared flag. The value 0 means the critical section is free, and 1 means it is in use.

while (lock == 1)
    ;              // wait
lock = 1;          // take the lock
/* critical section */
lock = 0;          // release the lock

This looks right but it does not work. Suppose process A reads lock as 0. Before A sets it to 1, a context switch lets process B read 0 as well. Both then enter the critical section. Mutual exclusion fails because testing and setting the lock are two separate steps.

A working lock needs one atomic step, which cannot be broken. Hardware provides this as a TestAndSet or Swap instruction. Such a lock still wastes CPU time because the waiting process loops. This is called busy waiting, and the lock is a spinlock.

What this unit covers

  • Critical section problem
  • Race condition definition and examples
  • Lock variable solution
  • Semaphore operations
  • Producer consumer problem with semaphore
  • Producer consumer problem with message passing
  • Thread definition and characteristics
  • User level threads
  • Kernel level threads
  • Multithreading models

User level threads

2082 ยท 10 marks

Question

Differentiate between user thread and kernel thread. Given the following information about processes, compute the average waiting time and turnaround time for FCFS and Round Robin (Quantum = 2).

ProcessesCPU Burst TimeArrival Time
A92
B111
C10
D23
E74

[2+8]

Show solved answer

User Thread vs Kernel Thread

FeatureUser ThreadKernel Thread
ManagementManaged by a user-level thread libraryManaged directly by the OS kernel
Kernel awarenessKernel is unaware of themKernel is aware and schedules them
Context switchFast, no kernel mode switchSlower, requires kernel mode switch
BlockingOne blocking thread blocks whole processOther threads continue running
PortabilityPortable, runs on any OSOS dependent
ExamplePOSIX Pthreads (user), Java green threadsWindows threads, Linux kernel threads

Given Data

ProcessBurstArrival
A92
B111
C10
D23
E74

Quantum $= 2$.


(a) FCFS

Order by arrival: C(0), B(1), A(2), D(3), E(4).

ProcessArrBurstStartFinishTATWT
C010110
B111112110
A2912211910
D3221232018
E4723302619

$$\text{Avg TAT} = \frac{1+11+19+20+26}{5} = \frac{77}{5} = 15.4$$

$$\text{Avg WT} = \frac{0+0+10+18+19}{5} = \frac{47}{5} = 9.4$$


(b) Round Robin (Quantum = 2)

Enqueue rule: a newly arriving process is added to the ready queue before the just-preempted process is re-added.

Tracing:

  • t=0: only C ready. Run C(1) โ†’ t=1, C done.
  • t=1: B arrives, queue [B]. Run B โ†’ t=3, B rem 9. During run, A arrives at 2, D arrives at 3. Queue after adding arrivals then B: [A, D, B].
  • t=3: Run A โ†’ t=5, A rem 7. E arrives at 4. Queue: [D, B, E, A].
  • t=5: Run D(2) โ†’ t=7, D done. Queue: [B, E, A].
  • t=7: Run B โ†’ t=9, B rem 7. Queue: [E, A, B].
  • t=9: Run E โ†’ t=11, E rem 5. Queue: [A, B, E].
  • t=11: Run A โ†’ t=13, A rem 5. Queue: [B, E, A].
  • t=13: Run B โ†’ t=15, B rem 5. Queue: [E, A, B].
  • t=15: Run E โ†’ t=17, E rem 3. Queue: [A, B, E].
  • t=17: Run A โ†’ t=19, A rem 3. Queue: [B, E, A].
  • t=19: Run B โ†’ t=21, B rem 3. Queue: [E, A, B].
  • t=21: Run E โ†’ t=23, E rem 1. Queue: [A, B, E].
  • t=23: Run A โ†’ t=25, A rem 1. Queue: [B, E, A].
  • t=25: Run B โ†’ t=27, B rem 1. Queue: [E, A, B].
  • t=27: Run E(1) โ†’ t=28, E done. Queue: [A, B].
  • t=28: Run A(1) โ†’ t=29, A done. Queue: [B].
  • t=29: Run B(1) โ†’ t=30, B done.

Gantt Chart:

C | B | A | D | B | E | A | B | E | A | B | E | A | B | E | A | B
0 1   3   5   7   9  11  13  15  17  19  21  23  25  27 28 29 30

Completion / TAT / WT

ProcessArrBurstFinishTAT = Fโˆ’AWT = TATโˆ’B
C01110
B111302918
A29292718
D32742
E47282417

$$\text{Avg TAT} = \frac{1+29+27+4+24}{5} = \frac{85}{5} = 17.0$$

$$\text{Avg WT} = \frac{0+18+18+2+17}{5} = \frac{55}{5} = 11.0$$


Summary

MetricFCFSRound Robin (Q=2)
Avg Turnaround Time15.417.0
Avg Waiting Time9.411.0

Watch the Round Robin queue order: using the standard enqueue convention (new arrivals join the ready queue before the preempted process), B finishes at 30 and A at 29, giving Avg TAT = 17.0 and Avg WT = 11.0. A different enqueue order, for example running D at t = 6 to 8, gives finish times B = 29 and A = 27 instead. FCFS is unaffected.

Open this answer on its own page โ†’

Producer consumer problem with message passing

2080 ยท 5 marks

Write down the Solving technique of the producer consumer problem with Message passing? [5]

Message passing is an inter-process communication (IPC) technique where processes communicate by sending and receiving messages rather than using shared memory. This eliminates the need for explicit mutual exclusion (no shared variables, no semaphores). --- Primitive Description -----------------...

Open this answer on its own page โ†’

Lock variable solution

2079 ยท 5 marks

What is race condition? Explain critical problem solution using lock variable. [5]

A race condition occurs when two or more processes access shared data concurrently, and the final result depends on the order or timing of their execution. Because the processes "race" against each other, the outcome is unpredictable and may lead to data inconsistency. Example: Suppose two proces...

Open this answer on its own page โ†’

Thread definition and characteristics

2079 ยท 5 marks

How threads differ from processes? Explain user level thread and kernel level thread. [5]

--- Aspect Process Thread --------- Definition An independent program in execution with its own address space A lightweight unit of execution within a process Memory Has its own separate memory space Shares memory (code, data, heap) with other threads of the same process Creation overhead Heavy; ...

Open this answer on its own page โ†’

2078 ยท 5 marks

What is thread? Explain user level thread and kernel level thread. [5]

Show solved answer

Thread, User Level Thread, and Kernel Level Thread

What is a Thread?

A thread is the smallest unit of CPU execution within a process. It is also called a lightweight process (LWP). A thread shares the code section, data section, and OS resources (like open files and signals) with other threads belonging to the same process, but has its own:

  • Thread ID
  • Program counter (PC)
  • Register set
  • Stack

A process can have multiple threads running concurrently, which improves performance and responsiveness.


User Level Threads (ULT)

User level threads are managed entirely by a user-space thread library (e.g., POSIX Pthreads) without kernel involvement.

Characteristics:

  • The kernel is not aware of the existence of these threads.
  • Thread management (creation, scheduling, synchronization) is done by the user-level library.
  • From the kernel's perspective, the entire process appears as a single-threaded process.

Advantages:

  • Thread switching does not require kernel mode, so it is faster.
  • Can be implemented on any OS, even those that do not support threads.
  • More portable and flexible scheduling.

Disadvantages:

  • If one thread makes a blocking system call, the entire process is blocked.
  • Cannot take advantage of multiprocessor systems (all threads map to one kernel thread).

Kernel Level Threads (KLT)

Kernel level threads are managed directly by the operating system kernel.

Characteristics:

  • The kernel maintains information about each thread.
  • Thread creation, scheduling, and management are performed by the kernel.
  • Each user thread maps to a kernel thread.

Advantages:

  • If one thread is blocked, the kernel can schedule another thread of the same process.
  • Can run on multiple processors simultaneously (true parallelism).
  • Better support for multiprocessor environments.

Disadvantages:

  • Thread switching requires a mode switch to kernel, making it slower than ULT.
  • More overhead in thread management.

Comparison Table

FeatureUser Level ThreadKernel Level Thread
Managed byUser-space libraryOperating System Kernel
Kernel awarenessNot awareFully aware
SpeedFaster (no mode switch)Slower (mode switch needed)
BlockingBlocks entire processOnly the calling thread blocks
Multiprocessor supportNoYes
ExamplePOSIX Pthreads (user mode)Windows threads, Linux threads

Note: Many modern operating systems use a combined (hybrid) approach where multiple user threads are mapped to multiple kernel threads to get the benefits of both models.

Open this answer on its own page โ†’

Producer consumer problem with semaphore

2078 ยท 10 marks

What is critical section? What are the operations that can be performed on the semaphore? Explain the solution to producer consumer problem using semaphore.[10]

--- A critical section is a segment of code in a process where the process accesses and manipulates shared resources (such as shared variables, files, or data structures) that must not be accessed by more than one process at the same time. Property Description ------ Mutual Exclusion Only one pro...

Open this answer on its own page โ†’

Critical section problem

0 ยท 10 marks

Define critical section problem. Explain race condition with suitable example.[10]

A critical section is a segment of code in which a process accesses shared resources (such as shared variables, files, or data structures) that must not be accessed by more than one process at the same time. The Critical Section Problem is the problem of designing a protocol that processes can us...

Open this answer on its own page โ†’

Multithreading models

0 ยท 5 marks

How does process differ with thread? Discuss at least one multithreading model with its advantages and disadvantages. [5]

Aspect Process Thread --------- Definition An independent program in execution with its own memory space A lightweight unit of execution within a process Memory Has its own separate address space Shares address space with other threads of the same process Resource Heavy resource consumption Light...

Open this answer on its own page โ†’