OS exam 2
Once the operating system identifies a page to remove from memory, what must be done before the frame in which the page is stored can be reused?
1) Write the content of the frame back to swap space if modified (dirty bit is 1). 2) Mark the entry in the page table corresponding to the page that was stored in the frame as invalid.
compare-and-swap instruction
1) test whether the value at the address specified by ptr is equal to expected 2) if so, update the memory location pointed to by ptr with the new value. 3) If not, do nothing. 4) In either case, return the actual value at that memory location, thus allowing the code calling compare-and-swap to know whether it succeeded or not
Lock States
Available (unlocked, free) or Acquired (locked or held)
the test-and-set instruction does as follows
It returns the old value pointed to by the ptr, and simultaneously updates said value to new.
Why do we need locks?
Locks provide some amount of control over scheduling to programmers. Locks yield some of the OS's control back to the programmer
4 conditions which must happen for a deadlock to occur are
Mutual exclusion, hold-and-wait, non-preemption, circular wait
The _________ page replacement policy results in the fewest number of page faults.
OPT
Threads
Separate, independent tasks within a job. Each thread is like a separate process, except they share the same address space and can access the same data
Are spin locks fair?
Simple spin locks (as discussed thus far) are not fair and may lead to starvation.
context switch
State save of the current process and state restore of a different process
What is the difference between the context switch we perform between threads as compared to processes?
The address space remains the same. There is no need to switch which page table we are using.
At one time, virtual memory system designers were advised to bias page replacement algorithms against modified pages in favor of those that have not been modified. Describe the rationale for the original suggestion of paging unmodified pages first.
The attempt was to save on write-to-disk operations: the first step listed above(i.e., writing the dirty pages to the disk)would not be needed.
Mutual Exclusion
The requirement that when one process is in a critical section that accesses shared resources, no other process may be in a critical section that accesses any of those shared resources.
When do you use a thread vs a process?
Threads share an address space, make it easy to share data. Processes are a more sound choice for logically separate tasks where little sharing of data structures in memory is needed.
What is an example of threads' usefulness?
To have your program work efficiently on multiple CPUs. Using a thread per CPU to do this work (parallelization) is a natural and typical way to make programs run faster on modern hardware.
T/F: A process that is waiting for access to a critical section protected by semaphores does not consume processor time
True
T/F: it is possible for one process to lock the mutex and another to unlock it
True
Assume we want to allow "preemption", and thus get out of deadlocks; in other words, if a deadlock is detected, we will forcibly take a lock away from a thread; by repeatedly doing this, we will eventually undo the deadlock. What new problems (if any) are introduced by this preemptive approach?
Various problems may be introduced: 1)livelock, by repeatedly taking away and allocating resources; 2)race conditions, if the resources preempted are locks for imposing mutual exclusion; 3)incorrect execution, if the resources taken away are used for ordering tasks; 4)incorrect execution if the resource cannot be preempted (e.g., a printer)
Thrashing
When the system spends most of its time swapping pieces rather than executing instructions.
Are spin locks correct?
Yes, a spin lock provides mutual exclusion
If there are two threads that are running on a single processor, when switching from running one (T1) to running the other (T2) _____________
a context switch must take place. The context switch between threads is quite similar to the context switch between processes. The register state of T1 must be saved and the register state of T2 restored before running T2
translation-lookaside buffer, or TLB
a hardware cache of popular virtual-to-physical address translations; an address-translation cache
Critical Section
a piece of code that accesses a shared resource and must not be concurrently executed by more than one thread
Critical Section
a piece of code that accesses a shared resource, usually a variable or data structure.
A thread has
a program counter and its own private set of registers it uses for computation
Critical section
a section of code within a process that requires access to shared resources and that must not be executed while another process is in a corresponding section of code.
Atomically
all or nothing.
Race Condition
arises if multiple threads of execution enter the critical section at roughly the same time; both attempt to update the shared data structure, leading to a surprising (and perhaps undesirable) outcome.
The term _______________________ refers to a technique in which a process can do nothing until it gets permission to enter a critical section but continues to execute an instruction/set of instructions that tests the appropriate value to gain entrance.
busy waiting
indeterminate program
consists of one or more race conditions. The output of the program varies from run to run, depending on which threads run when. The outcome is not deterministic.
Circular wait
each car waits for a resource held by the car to the right which leads to a circle of processes waiting for each other
Acquired (or locked or held):
exactly one thread holds the lock and presumably is in a critical section
T/F: Hardware support is always needed to synchronize more than two processes
false
Mutual Exclusion Primitives
guarantee that only a single thread ever enters a critical section, thus avoiding races, and resulting in deterministic program outputs.
Lock Variable
holds the state of the lock at any instant in time
Why is it called "test and set" ?
it enables you to "test" the old value (which is what is returned) while simultaneously "setting" the memory location to a new value. This is enough to build a simple spin lock
examples of mutual exclusion primitives
lock
A multi-threaded program has ___________
more than one point of execution, multiple PCs, each of which is being fetched and executed from
The name that the POSIX library uses for a lock is a ____________
mutex, mutual exclusion
Non-preemption
no car frees its partition before it reaches its objective of crossing the intersection
Available (unlocked, free):
no thread holds the lock.
What are the costs of using a spin lock on multiple CPUs?
on multiple CPUs, spin locks work reasonably well if the number of threads roughly equals the number of CPUs. Spinning to wait for a lock held on another processor doesn't waste many cycles and can be effective
hold-and-wait
once a it has entered the intersection, a car holds the resources it has acquired and waits for access to other resource(s) it needs
coarse-grained locking strategy
one big lock that is used any time any critical section is accessed
In a multi-threaded process, each thread runs independently and may call routines to do whatever work it is doing. Instead of a single stack in the address space, there will be _____________
one stack per thread
Atomically
one uninterrupted unit
The _____________ shows the frame location for each page of the process.
page table
Why use threads?
parallelism, to avoid blocking program progress due to slow I/O
fine-grained locking strategy
protect different data and data structures with different locks, thus allowing more threads to be in locked code at once
A situation in which multiple threads or processes read and write a shared data item and the final result depends on the relative timing of their execution
race condition
Address space
the range of memory addresses available to a process.
Atomicity
the sequence of instructions is guaranteed to execute as a unit or not execute at all
With processes, we saved state to a process control block (PCB); now, we'll need one or more ____________________ to store the state of each thread of a process.
thread control blocks (TCBs)
Threads belonging to a process share the data of that process. Through this, it provides benefits of multithreaded programming. However, in some circumstances, each thread might need its own copy of certain data. We call such data ____________
thread-local storage, the stack of the relevant thread.
Paging
transparent to the programmer and eliminates external fragmentation, providing efficient use of main memory.
Which of the following will NOT guarantee that deadlock is avoided? a. Acquire all resources (locks) all at once, atomically b. Use locks sparingly c. Acquire resources (locks) in a fixed order d. Be willing to release a held lock if another lock you want is held, and then try the whole thing over again
use locks sparingly
Bits used in memory
valid, protection, present, dirty, accessed
page fault
An event that occurs when an accessed page is not present in main memory.
T/F: A process that is waiting for access to a critical section does not consume processor time when mutual exclusion is implemented with TestAndSet-based locks.
False
T/F: A process that is waiting for access to a critical section does not consume processor when mutual exclusion is implemented with CompareAndSwap based locks
False
T/F: Deadlock avoidance is more restrictive than deadlock prevention
False
T/F: Multiple threads in the same process share all code and data resources
False
T/F: Race condition is a situation in which two or more processes continuously change their states in response to changes in the other process(es) without doing any useful work
False
T/F: Threads of the same process that share some memory always need to be synchronized to enforce mutual exclusion, no matter what the code does.
False
T/F: Using locks based on hardware primitives such as TestAndSet are better than using semaphores because they cannot ever lead to deadlock.
False
What are the costs of using a spin lock on a single CPU?
For a single CPU, performance overhead is high; if the thread holding the lock is preempted within a critical section. The scheduler might then run every other thread, each of which tries to acquire the lock.