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.
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).
| Processes | CPU Burst Time | Arrival Time |
|---|---|---|
| A | 9 | 2 |
| B | 11 | 1 |
| C | 1 | 0 |
| D | 2 | 3 |
| E | 7 | 4 |
[2+8]
Show solved answerHide answer
User Thread vs Kernel Thread
| Feature | User Thread | Kernel Thread |
|---|---|---|
| Management | Managed by a user-level thread library | Managed directly by the OS kernel |
| Kernel awareness | Kernel is unaware of them | Kernel is aware and schedules them |
| Context switch | Fast, no kernel mode switch | Slower, requires kernel mode switch |
| Blocking | One blocking thread blocks whole process | Other threads continue running |
| Portability | Portable, runs on any OS | OS dependent |
| Example | POSIX Pthreads (user), Java green threads | Windows threads, Linux kernel threads |
Given Data
| Process | Burst | Arrival |
|---|---|---|
| A | 9 | 2 |
| B | 11 | 1 |
| C | 1 | 0 |
| D | 2 | 3 |
| E | 7 | 4 |
Quantum $= 2$.
(a) FCFS
Order by arrival: C(0), B(1), A(2), D(3), E(4).
| Process | Arr | Burst | Start | Finish | TAT | WT |
|---|---|---|---|---|---|---|
| C | 0 | 1 | 0 | 1 | 1 | 0 |
| B | 1 | 11 | 1 | 12 | 11 | 0 |
| A | 2 | 9 | 12 | 21 | 19 | 10 |
| D | 3 | 2 | 21 | 23 | 20 | 18 |
| E | 4 | 7 | 23 | 30 | 26 | 19 |
$$\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
| Process | Arr | Burst | Finish | TAT = FโA | WT = TATโB |
|---|---|---|---|---|---|
| C | 0 | 1 | 1 | 1 | 0 |
| B | 1 | 11 | 30 | 29 | 18 |
| A | 2 | 9 | 29 | 27 | 18 |
| D | 3 | 2 | 7 | 4 | 2 |
| E | 4 | 7 | 28 | 24 | 17 |
$$\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
| Metric | FCFS | Round Robin (Q=2) |
|---|---|---|
| Avg Turnaround Time | 15.4 | 17.0 |
| Avg Waiting Time | 9.4 | 11.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.
Producer consumer problem with message passing
2080 ยท 5 marks
Write down the Solving technique of the producer consumer problem with Message passing? [5]
Lock variable solution
2079 ยท 5 marks
What is race condition? Explain critical problem solution using lock variable. [5]
Thread definition and characteristics
2079 ยท 5 marks
How threads differ from processes? Explain user level thread and kernel level thread. [5]
2078 ยท 5 marks
What is thread? Explain user level thread and kernel level thread. [5]
Show solved answerHide 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
| Feature | User Level Thread | Kernel Level Thread |
|---|---|---|
| Managed by | User-space library | Operating System Kernel |
| Kernel awareness | Not aware | Fully aware |
| Speed | Faster (no mode switch) | Slower (mode switch needed) |
| Blocking | Blocks entire process | Only the calling thread blocks |
| Multiprocessor support | No | Yes |
| Example | POSIX 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.
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]
Critical section problem
0 ยท 10 marks
Define critical section problem. Explain race condition with suitable example.[10]
Multithreading models
0 ยท 5 marks
How does process differ with thread? Discuss at least one multithreading model with its advantages and disadvantages. [5]
Make Unit 5 stick
Practice BIT204 with flashcards & quizzes