Hcsci133 Tutorial Questiones
Hcsci133 Tutorial Questiones
a) Describe the two general roles of an operating system, and elaborate why these roles are
important. (4)
The first general role of an operating system is to provide an ABSTRACTION layer for
software to run on a machine without needing to know hardware-specific implementation
details. It is important in order to reduce the burden on application software developers,
extend the basic hardware with added functionality and provided a common base for all
applications. The second general role of an operating system is to provide RESOURCE
MANAGEMENT to the machine’s users, by ensuring progress, fairness and efficient
usage of computing resources.
b) Using a simple system call as an example (e.g. getpid, or uptime), describe what is
generally involved in providing the result, from the point of calling the function in the C
library to the point where that function returns. (6)
A system call is completed as follows: - As the function is called, an interrupt of the type
“software exception” is placed on the processor, causing a Context Switch to take place
between the calling function and the kernel. - The exception handler will clear out and
save user registers to the kernel stack so that control may be passed on to the C function
corresponding to the syscall. - The syscall is executed. - The value(s) returned by the
syscall is placed into the correctly corresponding registers of the CPU (the same ones that
a user function normally places its return values in). - The handler takes this value,
restores user registers and returns said value to the user programme that called it.
c) Why must the operating system be more careful when accessing input to a system call (or
producing the result) when the data is in memory instead of registers? (5)
The operating system may access memory without restriction (as opposed to user mode
where memory access is highly regulated by the OS… we hope). When the data is in
memory the OS must be careful to ensure that it is only accessing data that it needs to,
since carelessness might result in overwriting data pertaining to still-running user mode
functions, breaking their operating when the OS shifts scope from kernel mode back to
user mode.
d) Is putting security checks in the C library a good or a bad idea? Explain your reason.
(5)
QUESTIONS TWO
a) Explain the three state process model, describe what transitions are valid between the
three states, and describe an event that might cause such a transition.
(6)
The three-state process model dictates that a process may take the form of one of three
states, RUNNING, READY and BLOCKED. Valid transitions include: - RUNNING to
READY (timeslice process management) - BLOCKED to READY (when a H/W
peripheral becomes free) - READY to RUNNING (the scheduler decides it should run) -
RUNNING to BLOCKED (the process needs some input)
A process is a 'task' or a 'job' that an individual programme has pushed to the CPU. It is
allotted a set of resources and will encompass one or more threads. Its attributes fall
under the headings of PROCESS MANAGEMENT (including registers, PC, PID etc),
MEMORY MANAGEMENT (pointers to CODE, DATA and STACK segments) and
FILE MANAGEMENT (root directory, CWD, UID, GID etc).
The ready queue is a queue of processes in the READY state of the three state process
model. A process will enter the READY queue when it may be executed without waiting
for a resource. The queue exists to establish a fair and efficient order for processes to be
executed. One way to implement such a queue is in a first-in, first-out (FIFO) round robin
scheme.
QUESTION THREE
User-level thread packages are co-operatively scheduled because generally they form part
of a single kernel-level thread. Most often this means that the process runs on its own
user-level scheduler, separate from the kernel scheduler. This means it does not have
access to the strict timing-based preemptive scheduling that kernel level threads enjoy, so
must use cooperative scheduling.
QUESTION FOUR
b) Describe a sequence the sequence of step that occur when a timer interrupt occurs that
eventually results in a context switch to another application. (7)
- Timer Interrupt is called - Trap into kernel space, switching to kernel stack - Current
application’s registers are saved - Scheduler gives next thread that should be run -
Context switch to other thread (load its stack, flush the TLB) - Load user registers from
the kernel stack - PC register shifts to the beginning of the other application’s instruction
c) Context switching between two threads of execution within the operating system is
usually performed by a small assembly language function. In general terms, what does
this small function do internally?
(3)
Saves the current registers on the stack, then stores the stack pointer in the current
thread’s control block. We then load the stack pointer for the new thread’s control block
and restore the registers it had saved, from its stack.
e) What is a critical region? How do they relate to controlling access to shared resources?
(3)
A critical region is a section of code that essentially contains a variable(s) that can be
accessed by multiple threads, but will affect the operation of the entire program, or
system. Correctness relies on critical regions not being modified by two or more different
processes at the same time.
QUESTION FIVE
a) What are three requirements of any solution to the critical sections problem? Why are the
requirements needed? (6)
Mutual Exclusion – One thread cannot access the critical region while another is inside.
Progress – The critical sections solution must not impede an entire thread’s progress,
halting the CPU.
Boundedness – We shall not starve any process, the resource must be freed so that each
thread can access the critical region fairly.
c) What is deadlock? What is startvation? How do they differ from each other? (8)
Deadlock is the phenomenon that arises when a number of concurrent processes all
become blocked waiting for another thread to make available a resource, but it can only
be released by a thread that is still blocked. Starvation is the phenomenon which arises
when a process does not ever receive the resource it is waiting for, even if it repeatedly
becomes available, as it is always allocated to another waiting process. Processes halt in
deadlock becayse they cannot proceed and the resources are never made available.
Therefore, no progress can be made. With starvation, progress is made overall at the
expense of a particular process or processes, which consistently miss out on being
allocated their requested resource.
QUESTION SIX
a) What are the four conditions required for deadlock to occur? (4)
The four conditions required for deadlock to occur are: Mutual Exclusion – the processes
must be trying to access the same resource at the same time Circular Wait – the processes
exist in a circular chain, where each is waiting for the resource held by the next member
of the chain. Hold & Wait – process holds a resource, DOESN'T GIVE IT BACK, and
blocks because it's waiting for more No Preemption – resource can't be forcibly taken
from the process holding it
Ignorance – If deadlocks are not liable to happen, the effort required to deal with them
outweighs the problem of deadlocks actually occurring. Detection and Recovery – Keep
log of resource ownership and requests. If no progress is made, recover from said
deadlock by pre-emption (steal a resource from another process), rollback (make
checkpoints – but operating systems aren't Halo or Call of Duty, this is difficult), or
crudely killing processes in the deadlock cycle. Deadlock Avoidance – The most difficult
option. We disallow deadlock by setting “safe states”, in which process completion is
always guaranteed. Deadlock Prevention – Negate one of the four deadlock conditions.
Most commonly we deal with the circular wait condition. Attacking mutex is infeasible,
attacking hold and wait is prone to starvation, and attacking no preemption is downright
idiotic.
c) Assuming the operating system detects the system is deadlocked, what can the operating
system do to recover from deadlock? (4)
The operating system can recover from deadlock by: - Killing the deadlocked process
(crude, and requires that it will be restarted) - Rolling Back (going back a few
instructions until it can enter a non-deadlocked state, and retry) - Pre-emption (forcing the
handing over of a resource to another process, at the expense of the one it's currently
deadlocked on)
d) What must the banker's algorithm know a priori in order to prevent deadlock?
(4)
The banker's algorithm must calculate whether giving a process a resource will lead to a
safe state or not. In order to work, it must know how many resources the process in
question may be granted at any one time, and how many resources the OS is able to give.
This is difficult because how can we know what future requests might be? To see if a
state is safe – the algorithm checks to see if enough resources exist to satisfy a process. If
so, the “loans” are assumed to be “repaid”.
QUESTION SEVEN
e) Describe the general strategy behind deadlock prevention, and give an example of a
practical deadlock prevention method. (4)
Deadlock prevention is theorised on the basis that if one nullifies one of the four
conditions for deadlock to occur (mutual exclusion, hold and wait, circular wait and no
preemption), a deadlock cannot occur. Attacking mutual exclusion and no preemption has
no practical basis, so we commonly prevent the circular wait condition. We do this by
globally numbering all resources. (e.g. Blu-Ray drive #1 and USB Hard Drive #2). At
every instant – one of the processes will have the highest numbered resource. The process
holding it will never ask for a lower one, because we only allow processes to access
higher numbered resources. Eventually it will finish and free all its resources. All
processes can finish.
f) What is the maximum file size supported by a file system with 16 direct blocks, single,
double, and triple indirection? The block size is 512 bytes. Disk block numbers can be
stored in 4 bytes. (6)
Number of blocks: - Direct Blocks = 16 blocks - Single Indirect Blocks = 512 / 4 = 128
blocks - Double Indirect Blocks = 128 * 128 = 16384 blocks - Triple Indirect Blocks =
128 * 128 * 128 = 2097152 blocks Total number of blocks = direct + single + double +
triple = 16 + 128 + 16384 + 2097152 = 2113680 Total number of bytes = 2113680 * 512
= 1.08220416 E 9 = 1.08 GB
g) The Berkeley Fast Filesystem (and Linux Ext2fs) use the idea of block groups. Describe
what this idea is and what improvements block groups have over the simple filesystem
layout of the System V file system (s5fs). (6)
The System V file system contained four blocks – the boot block, super block, inode
array and data blocks. This was inefficient, because seek times would be massive –
inodes are at the start of the disk and the actual data could be anywhere from the start to
the end! There was also only a single super block (block containing attributes of the
entire filesystem). If this was corrupted, it's bye-bye file system. The Berkeley Fast
Filesystem (and ext2) extended the System V filesystem by creating block groups – all
equally sized and each somewhat replicating the System V structure (aside from boot
block). The inode array was split into group descriptors, data block bitmap, inode bitmap
and inode table. This solves the major problems with s5fs, as proximity of inode tables
and data blocks is spatial localityfriendly, and you can no longer corrupt the entire
filesystem by way of superblock.
h) List and describe the four memory allocation algorithms covered in lectures. Which two
of the four are more commonly used in practice? (4)
The four memory allocation algorithms (in the scheme of dynamic partitioning
placement) are: First-Fit – in the linked list of available memory addresses, we place the
data in the first entry that will fit its data. Its aim is to minimise the amount of searching,
but leads to external fragmentation later on. Next-Fit – similar to first fit, but instead of
searching from the beginning each time, it searches from the last successful allocation.
Greatly reduces the amount of searching but leaves external fragmentation at the
beginning of memory. Worst-Fit – traverses the memory and gives the partitions as large
spaces as possible – to leave usable fragments left over. Needs to search the complete list
and such is a poor performer. Best-Fit – carefully scours the memory for spaces that
perfectly fit the RAM we want. However, the search is likely to take a very long time.
We most commonly use first-fit and next-fit in practise. They're easier to implement and
are faster to boot.
QUESTION 8
Ans.
The Operating System (OS) acts as an interface between the user and the computer hardware
components. It is system software that provides an environment in which a user can execute
programs easily. Every computer system needs an operating system to be able to run programs
and interact with the user. Without OS, it would be just one dumb hardware machine. The main
functions of Operating System are memory management, file management, I/O management,
processor management, device management, security, networking etc.
b. State any three most popular operating systems at present today. Name the developer
/owner and list any two devices popular with each operating system. [9]
Ans. Operating System has evolved from slow and expensive systems to fast, powerful and
affordable in the modern world. The most popular operating systems at present are as
follows:
i) Android: The Android operating system is the world's most used mobile operating system
with over 41% of the global market share. Android Operating System is developed by Google for
smart phones and tablets. It is free and open-source software.
ii) Windows: Windows is the second most used operating system which accounts for 32% of the
global market share. Windows is a proprietary operating system that is owned by Microsoft.
Windows currently dominates the Personal Computers (PCs) market which includes desktops
and laptops.
iii) iOS: iOS is the operating system of one of the most popular smart phone known as the
iPhone. It is a proprietary mobile operating system that is developed and owned by Apple. It
accounts for over 16% of the global market share.
i) It provides the interface for the user to interact with the computer components where the user
would give commands to the operating system and the operating system would in turn instructs
the hardware components to execute the command and perform a specific task.
ii) It organizes and coordinates the processing times of the various processes and programs
running simultaneously for smooth execution.
iii) It deals with memory management which allocates and de-allocates memory space to
programs in need of memory.
vi) It also maintains security of the system and access rights of the user.
QUESTION 9
Ans.
i) Monolithic kernel such as Unix, Linux etc.
ii) Micro kernel such as L4, Minix, K42 etc.
iii) Hybrid kernel such as Windows NT, Netware etc.
iv) Exo kernel such as Nemesis etc.
v) Nano kernel such as EROS etc.
In a Microkernel, the user services are kept in user address space, and kernel services are
kept under kernel address space. As a result, it reduces the size of kernel and thus,
Microkernel is smaller in size. Since the user service shared a different memory space
with the kernel, this can cause the operation of the kernel to be slower as compared to
monolithic kernels. It is easily extendible, it doesn’t require any modification in kernel
space if new service is added to user address space. Microkernels are very uncommon.
Minix, Symbian etc. are the examples microkernel.
QUESTION 10
What is a process in operating system? Use a state process model to illustrate the various states
and the transitions valid between these states, and describe an event that might cause such a
transition to occur. [20]
In operating system, a process is a program in execution. For example, when we write our
program in C, we save, compile and run it, the moment we run it, it is transferred to the main
memory and becomes a process. When a process executes, it passes through many different
stages like Start, Ready, Running, Waiting and Terminated state.
A process goes through various states during execution and they are as follows:
New/Start: This is the state when a program is picked up by the operating system into the main
memory.
Ready: In the ready state, the process is waiting for the processor to be assigned for it to run.
Running: A process is said to be in a running state when it is being executed by the CPU.
Wait: If a process waits for a certain resource or waits for the input from the user it is said to be
in a wait state and in the meantime the CPU picks up another process.
Terminate: When a process finishes its execution, it is said to be in a termination stage. In the
state, the process is removed from main memory
The state process model dictates that a process may take the form of one of three states transitions,
RUNNING, READY and BLOCKED. Valid transitions include:
- RUNNING to READY (time slice process management)
- BLOCKED to READY (when a H/W peripheral becomes free)
- READY to RUNNING (the scheduler decides it should run)
- RUNNING to BLOCKED (the process needs some input/output)
- RUNNING to TERMINATED (the process has completed execution)
QUESTION 11
Describe the various attributes associated with a process that is kept in the Process Control Block
(PCB) [20]
OS maintains a Process Control Block or PCB. PCB is nothing but a data structure that stores
information about a process. There are various attributes stored in PCB, they are as follows:
ii) Process State: This represents the current state of the process such as ready, running etc.
iii) Priority: Each process has its priority and the one with the higher priority gets the CPU first.
iv) Program counter: Program counter stores the address of the last instruction of the process
when the process was suspended. This will help the CPU to know the exact address when
resuming the process.
v) CPU registers: Each process has its own set of CPU registers which are used to hold the data
during the execution of a process.
vi) List of open files: During execution, there are some files that are needed by the process that
needs to be present in the main memory. OS maintains those open files.
vii) List of open devices: OS also maintains list of all the devices that are opened during the
execution of a process.
QUESTION 12
A Deadlock is a situation where two or more processes are waiting for each other to complete in
order to start their operation. This happens when first process is holding a resource and waiting
for another resource that is currently being held by the second process and the second process is
waiting for the resource being held by the first process. This will cause them to wait forever and
be in a state of deadlock.
b. Describe the necessary conditions that can lead to a deadlock situation in a system [4]
There are four different conditions that result in Deadlock and they are as follows:
i) Mutual Exclusion: A resource cannot be shared between two processes. Only one process can
use a particular resource at a time.
ii) Hold and Wait: A process is holding at least one resource and waiting for other resources.
iii) No Preemption: A resource cannot be taken from a process unless the process releases the
resource.
iv) Circular wait: A group of processes are waiting for each other to release the resource and no
one is releasing their own resource.
It is named as Banker's algorithm because the banks use the same technique to allocate money
and provide loans to their customers so that they never run out of money.
Banker's algorithm checks the following three things before allocation of resources:
QUESTION 14
Ignorance – If deadlocks are not liable to happen, the effort required to deal with them
outweighs the problem of deadlocks actually occurring.
Detection and Recovery – Keep log of resource ownership and requests. If no progress is
made, recover from said deadlock by pre-emption (steal a resource from another process),
rollback (make checkpoints – but operating systems aren't Halo or Call of Duty, this is
difficult), or crudely killing processes in the deadlock cycle.
Deadlock Avoidance – The most difficult option. We disallow deadlock by setting “safe
states”, in which process completion is always guaranteed.
Deadlock Prevention – Negate one of the four deadlock conditions. Most commonly we
deal with the circular wait condition. Attacking hold and wait is prone to starvation, and
attacking no preemption is downright idiotic.
b. Describe the term interrupt and explain the four classes of interrupts. Provide one
example of each class. [12]
Ans.
An interrupt is a method by which other events can cause an interruption of the CPU's normal
execution. An Interrupt is a method by which the normal operation of the CPU can be changed.
Interrupts are a better solution than polling for handling I/O devices. There are many methods to
handle interrupts. Four general classes of interrupts are:
Program, trap instructions, page faults etc.
Timer
I/O devices and
Hardware failure.
The causes of occurring interrupts.
QUESTION 15
Consider the following set of processes, with the length of CPU-burst time given in milliseconds:
(a) Draw Gantt charts illustrating the execution of these processes using FCFS, SJF, a non-preemptive
priority (a smaller priority implies a higher priority) and RR (quantum=1) scheduling. [15]
(b) What is the turnaround time of each process for each of the scheduling algorithms in part (a). [15]
Ans..