You are on page 1of 6

Intro, Threads & Concurrency

What are the three views of an operating system? Describe them.


- Application view: What services the OS provides applications
- System View: What the OS does as a whole, what problems foes it solve
- Implementation View: How does it work behind the scenes?
Define the kernel of the OS. What things are considered part of the OS but not the
kernel?
- Central core of the OS, handles syscalls, exceptions, and interrupts
What are some advantages of allowing more than one thread to exists
simultaneously?
- Concurrency, threads can interact, multitasking, wastes less CPU time
What is contained in a thread context? What is the thread control block?
- Thread context a threads CPU state (registers etc)
- Thread Control Block struct storing thread context, along with other thread
data
Describe what happens during a context switch? What is dispatching?
- Context Switch: A threads context is backed up, and a different threads
context is restored allowing the OS to switch threads
- Dispatching: the process, choosing a new thread and restoring it
What is the difference between thread_yield() and thread_sleep()?
- Thread_yield, thread gives up control of the processor, goes back to ready
queue
- Thread_sleep thread needs to wait for some other even, blocks on an address
until that event occurs (so another thread can run)
How is an involuntary context switch accomplished? Define quantum.
- When a thread has run for its share of time, it is switched out by the kernel
after a timer interrupt
- Quantum is the length of time a thread gets to run before being switched out
Describe what happens when an interrupt occurs. Where do interrupts come from?
- The processor immediately switched to priveledged more and jumps to a
specified address where the kernel handler is, the handler then handles the
interrupt and returns control to the process
- Interrupts come from hardware devices

Synchronization
-

Can the OS know when a program is in a critical section?


- Yes, if the program says that it is a critical section
When is it appropriate to disable interrupts? What happens when you disable
interrupts on a multi-processor? Give pros and cons of disabling interrupts.
- When you need to do a small amount of work and dont want a context switch
to occur
- Pros easy, not hardware specific, works for any number of threads
- Cons can screw up timing, disables ALL interrupts, not just timer ones,
overkill, doesnt work on multiprocessors
Describe Peterson's Algorithm. What are some restrictions on its application? Is it
starvation free?
- Say you want in the critical section, set it to be the other threads turn, wait
until your turn or the other thread doesnt want in to enter
- Only works for two threads
- Starvation is not possible
How does Test and Set work? How would you use it in a program? What is a potential
problem with Test and Set?

Atomically stores a value in an address and returns the value that was there
Use in a while loop, testAndSet a lock to true, when another thread is done, it
will testAndSet to false, causing the while loop to fail so you can enter the
critical section (ie the lock is true if a thread is in the critical section)
What is a semaphore? Which two operations does it support? Are they atomic?
- P, V, yes theyre atomic
- A variable that is incremented and decremented and can be used to do
synchronization
Describe how you would synchronize producer and consumer threads using a
semaphore.
- Call V each time something is produced
- Call P each time something is consumed
What is the primary difference between a lock and a binary semaphore?
- The threads that gets the lock is the only one that can release it, any thread
can modify a semaphore
Is the implementation of semaphores given in the notes starvation free? Why or why
not?
- Probably not, depends on what its used for
What is a condition variable? How are they used in conjunction with locks?
- A condition that a thread can wait on inside a critical section
- Must be in a lock to use a CV
Describe, at a high level, what a monitor does. How can we simulate their behaviour
in C?
- An object where only one method can be running at a time (mutual exclusion)
- Simulate with a struct and set of functions all covered by a single lock
Define deadlock. How does the system recover from a deadlock?
- When multiple threads need resources that the other(s) hold, no one can
move
- Recover by killing one or more processes
Can deadlock occur with only one thread? Why or why not?
- Probably not, unless a thread is waiting on itself to do something
If there is a cycle in a resource allocation graph, is there a deadlock? What about the
converse of this?
- Not necessarily
- If theres no cycle, theres no deadlock
Give the deadlock detection algorithm, and describe the three deadlock prevention
methods discussed in class.
- Algorithm: U is unallocated resources, for each thread, check if it can be
finished with resources from U, if so add its allocated resources to U and
repeat.
If ever no thread can finish, deadlock
- Prevention Methods:
request all needed resources at the same time (get all or none)
Preemption, take away resources from thread
Resource ordering must acquire resources in a specific order

Processes and the Kernel


-

What information is contained within a process?


- Process contains data, stack, and code
- Address space
What is the difference between a sequential process and a concurrent process?
- Sequential process has one thread, concurrent has many
Can threads from one process access memory from another process?

- No, unless some memory is explicitly shared


What can the kernel do in privileged execution mode that user programs can not
directly accomplish?
- Communicate with hardware, use special instructions, access any memory,
create new threads/processes, anything you need a system call for
Describe what happens when a system call in invoked.
- C library takes care of calling, uses SYSCALL instruction which causes
processor to switch to kernel mode and jump to a specific address where the
handler is. The handler then does whatever the syscall needs
In which registers does a system call expect the system call code? Arguments?
- Code: v0
- Arguments: a0-a3
In which registers will the system call place the return result? Return value?
- Return result (0 success, 1 if failure) in a3
- Return value in v0
Why does OS/161 have two stacks?
- One for the kernel and one for the user so that the user cant access kernel
memory
What are the only three ways for the CPU to enter privileged mode?
- Syscall, interrupt, exception
What is an exception? What is the difference between an exception and a system
call? And exception and a interrupt?
- Interrupt hardware
- Exception CPU creates due to some instruction error
- Syscall called from a program has a specific instruction
The mips trap function is called to handle system calls, exceptions, and interrupts.
How does it differentiate between these three cases?
- From a code in a register from the CPU (in the trapframe)
What does the OS/161 exception handler common exception code do?
When returning from a system call, the program counter is not automatically
advanced by the hard-ware. Why is this?
- Because the software does it, its like a return address
What might an OS want to keep track of in a process table?
- Pid, process state and information, list of resources, etc
Define timesharing. When can the CPU context switch to another thread?
- Sharing CPU among processes, CPU can context switch whenever it is in
kernel mode (ususally with timer interrupts)
What are three aspects of the process model that must be defined by the OS?
- Process Initialization how is a process created and initialized, what is in the
address space, what resources does it have, etc.
- Multithreading Can processes have more than one thread (in OS/161 no)
- Inter-Process Relationships are there parent/child/other relationships between
processes, what are their meanings?

Virtual Memory
-

A system uses physical address and virtual addresses. How many physical address
spaces are there in a computer? How many virtual address spaces?
- One physical address space for the entire system, one VAS for each process
What is a simple method of address translation? How does this work? What are its
disadvantages?
- Dynamic relocation: each VAS has an offset that is simply added to the vaddr
to get the paddr. Cons: need continguous memory, external fragmentation, all
vaddrs must be in physical memory

In dynamic relocation, when does the value of the relocation register in the MMU
change?
- When you switch processes
What are three algorithms that can be used to find an allocation space? Why would
anyone ever want to use the worst fit algorithm?
- N/A
What is the difference between external fragmentation and internal fragmentation?
- External is between allocated blocks in memory, Internal is when allocated
blocks must be a certain size but an allocation doesnt use its entire block
What advantages does paging give over dynamic relocation?
- Pages dont need to be contiguous, removes external fragmentation, allows
for demand paging etc
How does the kernel use a page table to help translate virtual addresses to physical
addresses?
- Looks up vaddr in the page table to get the corresponding paddr and then
puts this entry in the TLB (add offset to paddr)
Given a virtual address and a page table, how do you calculate its physical address?
In reverse?
- Take off the offset and look up in page table, then add back offset to paddr
- Reverse: Scan through paddrs until you find the vaddr???
Describe how the OS uses the valid bit to determine which PTEs contain a valid page
mapping.
- If the valid bit is 1, then the pte contains a valid mapping
In address translation, what (4) roles does the OS serve? What (2) roles does the
MMU serve?
- MMU looks up vaddr in TLB and throws a fault if it doesnt exist or is read only
- OS inserts an entry into the TLB if needed, handles read only faults, manages
the PTs, allocates physical memory
What happens when a process violates a protection rule checked by the MMU?
- An exception is thrown
What is the TLB? What does it stand for?
- Translation Lookaside Buffer, a cache of page table entries
Why might the MMU need to invalidate the TLB? When would this happen?
- When you switch between processes, then the paddrs in the TLB are no longer
relevant and shouldnt be used
Why is it not a good idea to save/restore the TLB on context switches?
- Because the mappings may change between switches, ie something may be
evicted from physical memory
What is the difference between a hardware-controlled TLB and a software-controlled
TLB? Which is the MIPS TLB?
- MIPS is software controlled, the kernel takes care of translating an address
through the page table and putting the entry into the TLB (vs hardware doing
it)
A range addresses starting at 0x00000000 is intentionally left empty. Why is this?
- For null pointer error catching
How does segmentation improve address translation? What is the motivation behind
segmentation?
- Reduces size of the address space, like having a bunch of mini address spaces
since the vaddr space is usually very sparse
How do you translate a segmented and paged virtual address into a physical
address? In reverse?
- Look up the segment number, then the page number and add the offset,
similar in reverse
Why is sharing virtual memory useful? How can this be accomplished? Give an
example of when two processes should share a page.

Saves memory and time, simply map multiple vaddrs to the same paddr, eg
two processes sharing the same code
What are three approaches for locating the kernel's address space? How do OS/161
and Linux do this?
- Part of every vaddr space, its own vaddr space, or just in physical memory
Describe the different parts of an ELF file. Why doesn't the ELF file describe the
stack?
- Headers describe each section
.text (code, read only)
.rodata (read only global data) ^text
.data (initialized global data)
.bss (uninitialized large global data)
.sbss (uninitialized small global data) ^data
No stack because program doesnt care where it is, just needs a
pointer, definied by OS
What are the different sections in an ELF file? What are the different segments?
- See above
Know which kind of variables map to which section.
What functions/system calls can be used to share virtual memory?
Page tables for large address spaces may be very large. One problem is that they
must be in memory, and must be physically contiguous. What are two solutions to
this problem?
- Two Level Page Tables, Inverted Page Tables
What are the key differences between normal and inverted page tables?
- Normal look up vaddr to possibly get paddr, only per address space
- Inverted look up paddr to get vaddr (maps physical memory), only one
What is the difference between a global page replacement policy and a local page
replacement policy?
- Global you can replace any page, local you can only replace pages within the
same process
What steps must the OS take to handle a page fault exception?
- Get the fault addr, look it up in the page table, load it if needed, add the entry
to the TLB
What are the 6 different page replacement policies discussed in lecture? Describe
and compare them.
- FIFO replace page that has been in memory the longest
- LRU track usage and replace the only that hasnt been used in the longest
time
- LFU track frequency of use and replace the only that is used least
- CLOCK cycle around skipping pages where use bit is set and turning it off
- MODIFIED CLOCK (use, modified), look for (0, 0), look for (0, 1) turning off
use, repeat
What is the definition of a compulsory fault?
What are the two types of locality to consider when designing a page replacement
policy?
- Temporal: if a page is used once, its likely to be used again
- Spatial: pages located together are likely to be used together
In practice, frequency based page replacement policies don't work very well. Why is
this?
LRU page replacement is considered impractical in virtual memory systems. Why is
this?
Does the MIPS TLB include a use bit? A modified bit?
Why is simulating a use bit expensive?
Why are modified pages more expensive to replace than a clean page?
Describe how you would simulate a modified bit in software?

Define page cleaning, and explain the difference between synchronous and
asynchronous page cleaning.
What is Belady's Anomaly, and what is a stack policy? What is one way you can tell if
a replacement policy is a stack policy?
Of LRU, FIFO, and CLOCK, which algorithms are stack policies and which are not?
What is prefetching and what is its goal? What are the hazards of prefetching? Which
kind of locality does prefetching try and exploit?

You might also like