CLO # 4 - Understand synchronization and deadlocks. Implement synchronization using Mutex/Semaphores.
Q1. [15 marks] Instruction: Write answers as bullets points.
a) Consider a tcke booking sysem where he available number o tckes or
each program mus be mainained. A person who wishes o book a tcke calls
he bookTicke() uncton, which checks i tckes are available and hen
decreases he available tckes coun by one. This is illusraed in he code
shown. Now explain how a race conditon is possible in his siuaton. Also,
show a Phread library-based C program ha avoids race conditon using a muex. [1 + 3]
Race condition is possible as follows:
• This function code has no mutual exclusion.
• Threads, running on multiple cores, will update the variable “availableTickets” – which is a single memory
location – in random order resulting in race condition.
• *** full program needs to be written transforming given bookTicket() function given.
• No marks if semaphores are used. 1.5 marks if syntax is 100% correct + 1.5 marks if 100% logic is correct.
b) Explain synchronizaton issue(s) in producer and consumer codes in a producer-consumer problem. Wrie and explain
C code snippes ha use wo ypes o semaphores as a soluton or his problem. [2+2]
• Both have the same synchronization issue: i.e. ensuring that the producer and consumer access the shared
buffer in a mutually exclusive manner, and
• coordinating their activities so that the buffer does not overflow or underflow.
• 1 wt. for correct code and 1wt. for explanation of code.
Page 1 of 6
c) Wrie answers o he ollowing questons:
i. Why hardware aomic insructons are necessary in he implemenaton o locking primitves? [1]
These instructions ensure mutual exclusion even if threads run on different cores in parallel.
ii. How do wo hreads become deadlocked while accessing resources? Wrie C code snippe using semaphores and
a sep-by-sep explanaton or any scenario. [1.5+1.5]
• 1.5 wt. will be awarded only if out-of-order pairs of waits are used in
the code.
• Textual answer will get no awards.
• 1.5 wt. will be awarded if step-by-step explanation given.
Page 2 of 6
d) How busy waitng is implemened during synchronizaton? Wrie wo C code snippes showing wo dieren mehods.
Also, explain he working o any one snippe using a dry run wih variable values. Assume wo processes where one
is executng in is critcal secton and oher is waitng [2 + 1].
2 wt.
• The busy term (a.k.a spinning) is used as waiting involves a loop that
continuously checks the condition, consuming CPU cycles until the
condition is met.
• Two Option A and Option B (or similar) is given with dry run.
1wt.
• Only if explicit dry run done by the student.
CLO # 5 - Understand virtual memory and its management.
Q2. [15 marks] Instruction: Write answers as bullets points. Make suitable assumptions, if needed.
a) Explain exernal ragmenaton in main memory using a labeled diagram. Wha causes inernal ragmenaton in a
page? Explain wih an example. [2 + 1]
2wt.
1 wt.
Internal fragmentation occurs when memory allocated for a process is slightly larger than what the process
requires. This "extra" allocated memory within a fixed-size block (page) is wasted because it cannot be used by
other processes. Suppose total size of executable code is 3.5k and page allocated to text segment of the process
will always be 4k in a system where page size is fixed to 4k. Therefore, 0.5k is waster due to internal
fragmentation.
b) Page auls are critcal in a virual memory sysem.
i. Explain Page aul processing using a labelled diagram. [2]
See diagram.
ii. In a compuer sysem, memory access tme is 40 nanoseconds
and hard disk access tme is 1.25 milliseconds. Calculae
eectve memory access tme i page aul probabiliy is
0.000025. [2]
effective access time = (1 − p) × (40) + p (1.25 milliseconds)
= (1 − p) × 40 + p × 1,250,000
= 40 + 1249960 × p
= 40 + 1249960 (0.000025)
= 40 + 31.249 ➔ 71.249
Page 3 of 6
c) LRU is one o he many page replacemen algorihms. Explain is wo
implemenatons as ollows:
i. Use a labelled diagram and sepwise hins o describe clock (second
chance) algorihm. Show i does approximae LRU unctonaliy. [2]
ii. Now, enhance he above algorihm o ensure ha diry victm pages
are ushed o memory beore reuse. [2]
• The reference bits are used to approximate the LRU policy.
• Pages that are used frequently will have their reference bit set
to 1, giving them a second chance and making it less likely for
them to be replaced immediately, much like how LRU would
prefer recently used pages over those used a long time ago.
d) Suppose a program is accessing main memory in a virual memory sysem having a page size o 2K.
i. Sae he dierence beween paged and demand paged memory sysem? [1]
Paged Memory System Demand Paged Memory System
All pages that are part of process are loaded into Pages are loaded only when accessed. Pages are
available frames at execution start. loaded on page fault occurrence
ii. Show a labelled diagram which shows how a virual memory address generaed by a program is ranslaed ino a
physical address. Now, in your diagram, assume suiable values or page number and page ose and show how he
processor calculaes he physical address. [3]
• Virtual address (logical address) is generated by the
executing program.
• Page table lookup is done to read the frame number.
• Displacement within page/frame remains same.
• ----
• Suppose page number is 26 and offset is 87 so the logical
address is 2687. The page table lookup for page number
26 resulted in a frame number 890. So the physical
address becomes 89087.
CLO # 1 - Describe, discuss, and analyze, services provided by the modern Operating Systems.
Q3. [6 marks] Instruction: Avoid unnecessary details in your answers.
Page 4 of 6
a) Creae a diagram illusratng he inerrup handling process in a ypical operatng sysem. Wha is he purpose o
tmer in operatng sysems and how i is relaed o inerrups. [1+2]
1 wt. for diagram and 2wt. for the answer below.
• OS needs to be reminded to perform periodic tasks.
• This can be done using a time, which when reaches a pre-set value, it generates a
hardware interrupt known as a timer interrupt.
• This is used system wide to facilitate process scheduling, time-sharing, and
system resource management.
• For example, a timed interrupt (say 5 milliseconds) in process scheduling
preempts the currently executing process and brings in new one (known as
context switching) thus allowing fair CPU allocation.
b) Describe executon seps o a (generic) sysem call using wo modes in a modern operatng sysem wih he help o
a labelled diagram. [2]
• When a system call is executed, it is treated as a software interruption.
• Control is transferred through the interrupt vector to a
service routine in the operating system.
• Switching the mode to kernel mode.
• The kernel identifies the system call type and retrieves
necessary parameters, which may be passed via
registers, stack, or memory.
• After verifying the parameters, the kernel executes the request and returns control to the user program.
Wha problems do you expec i OS execues his sysem call in user mode only? Explain. [1]
• Privileged instructions (switching to kernel mode, I/O control, and timer management.) can only be executed
in kernel mode. These facilitate managing resources that are kept private from users. The dual mode of
operation protects the operating system and users from errant actions by designating certain harmful
instructions as privileged.
• If privilege mode instructions are allowed in user mode (i.e. there is only one mode of operation) a bug in the
user program will crash the whole OS.
• Multitasking and Multiprogramming will be difficult as in old DISK Operating System (DOS).
CLO # 2 - Understand, design, and implement solutions employing concepts of Processes and Threads
Q4. [6 marks]
a) Consider Google’s Chrome browser as a mult-process archiecure. Wha bene would his implemenaton provide
o he user? [2]
• If each tab is a separate process, then this provides protection and security as each process memory will be
sperate and unaccusable by other processes. can be accessed by other processes.
• Each process can be scheduled on a different core of the processes. Thus, response time will be improved.
b) Show how many processors are needed o gain a 3.5 tmes speedup i he serial porton is 20%. [2]
speedup = 1 / (serial portion + (parallel portion /N) where N is number of processors. Putting values
3.5 = 1 / (0.2 + (0.8/N))
3.5 (0.2 + (0.8/N)) = 1 ➔ 0.7 + (2.8/N) = 1 ➔ 2.8 / N = 0.3 ➔ N = 2.8 / 0.3 ➔ N = 9.33 ~ 10 processors.
c) Explain dieren ypes o mapping beween user-level and kernel-level hreads. [2]
• One-to-One: True parallelism as each user threads gets its own kernel threads which can be scheduled
independently.
• Many-to-One: Thread library (like Pthread) can manage user threads and maps then to a single kernel
thread.
• Many-to-Many: Flexible mapping where many user threads are mapped equal number and less kernel threads.
Page 5 of 6
CLO # 3 – Understand mechanisms for scheduling of tasks in modern operating systems.
Q5. [8 marks] Instruction: Write answers as bullets points.
a) Consider Linux Compleely Fair Scheduler. Explain: i) he compuatons done o akes scheduling decisions, and ii)
give a diagram o show how CFS ecienly compues he nex runnable ask. [2+2]
b) Assume an Operatng Sysem uses FCFS scheduler. Suppose here are only wo processes: rs is running wih fy
hreads o compue he value o Pi using 5000K ieratons, and he second process is an Inerne browser where a
single ab is shown o he user. Explain how you change he code o he frs process such ha he FCFS scheduler
run boh processes alernatvely i.e. beore nishing he rs process. Write precise answer in 2-3 lines only [2].
Cooperative or non-preemptive scheduling. FCFS is non-preemptive, so once it starts execution of the first
process the 2nd process must wait till the completion of the 1st.
2wt. To achieve alternating execution with an FCFS scheduler, the programming has to code either sleep () or
other system calls to relinquish the CPU within each thread's computation loop. This allows the second process
(the Internet browser) to run.
c) Compare and conras pre-emptve and non-preemptve scheduling [2].
Pre-emptive Scheduling Non-preemptive Scheduling
OS can interrupt processes at any time Processes run until they voluntarily relinquish or finish
Overhead due to frequent context switches Lower due to minimal context switches
More complex to implement and manage Simple (process must relinquish CPU)
Fair CPU utilization May lead to starvation
Highly responsive Depend upon
Page 6 of 6