OS Final Spring'24 Solution
OS Final Spring'24 Solution
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]
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.
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.
See diagram.
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]
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]
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]
CLO # 1 - Describe, discuss, and analyze, services provided by the modern Operating Systems.
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]
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.
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.
Page 6 of 6