Systems 2 Final

Réussis tes devoirs et examens dès maintenant avec Quizwiz!

(9.1) Relocation Register

A CPU register whose value is added to every logical address to create a physical address for primitive memory management Renamed base register Derived from PCB of process when context switched by CPU Allows the OS to change size dynamically

(9.1) Stall

A CPU state occurring when the CPU is waiting for data from main memory and must delay operation In these cases the CPU can switch to other hardware threads (logical processors) while the waiting one waits

(6.9) Lock-Free Algorithms

Algorithms without locks using CAS instructions Protect from race conditions with less overhead and more scalability Difficult to develop and test

(8.5) Preventing Mutual Exclusion

Cant deny mutual exclusion

(6.5) Lock Contention

Contended if thread blocks trying to acquire lock. If lock is available when thread attempts to acquire, the lock is uncontended High Contention - lots of threads trying to acquire - decrease performance Low contention - not many threads trying to acquire

(9.1) Dynamic Linking

Linking postponed until runtime. Usually used with system libraries Without this, each program must include language library in the executable, increasing size and wasting main memory Ex: C standard library Requires help from OS to check other process memory for the same library

(9.3) Huge Pages

Linux Architecture-dependent larger page size

(9.1) Binding

Matching a symbolic address to a relocatable address, or a relocatable address to an absolute address Each _______ is a mapping from one address space to another

(7.2) Signaled State

Means this object IS available, and a thread will NOT block when acquiring this object.

(6.3) Peterson's Solution

Not guaranteed to work on modern architecture with reordering Two processes required to share int turn and boolean flag[2] Turn indicates whose turn it is to enter critical section, if turn == i, then process Pi is allowed to execute in critical section flag array indicates if process is ready to enter. If flag[i] true, Pi ready to enter.. Both processes set their flag to true and turn to the other process. Whatever value turn ends up being determines who enters critical section first, and other must wait (mutual exclusion) Set flag to false when leaving critical section so that other process can enter (progress) Each process waits at most one run of the other process's critical section (bounded waiting)

(7.3) POSIX Semaphores

Not part of POSIX standard and belong to extension Named and unnamed variants Linux supports both kinds

(9.3) Convert bits to hex

Number of bits - binary - range in decimal - hex 8 bits of binary = 256 decimal = F9 Hex

(8.2) Livelock

Occurs when a thread continuously attempts an action that fails. Indefinite repetition of the same actions Occurs when threads retry failing operations at the same time. Can be avoided by retrying operation at random times. Less common than deadlock

(9.2) Hole

One large block of available memory Adjacent ones are merged to form one larger, continuous blocks

(6.7) Signal and Wait

P calls x.signal while Q suspended with x P either waits until Q leaves monitor or waits for another condition

(6.7) Monitor implemented in Concurrent Pascal Compromise

P calls x.signal while Q suspended with x P executing signal immediately leaves monitor Q resumed

(6.6.1) Binary Semaphore

Semaphore that can only range between 0 and 1. Behaves similarly to mutex locks

(6.6.1) Counting Semaphore

Semaphore that can range over an unrestricted domain Controll access to finite number of instances of a resource, init to # of resources available. wait to "acquire" (decrements count) signal to "release" (increments count) Block if count is 0

(6.4.2) (T/F) Race conditions are prevented by requiring that critical regions be protected by locks

Trust

(9.1) Absolute Code

Code with bindings to absolute memory addresses

(6.3.2) (T/F) Peterson's Solution is a technique for handling critical sections in operating systems

False

(8.5) Preventing Hold and Wait

Can deny holding multiple resources before releasing first one, but leads to low resource utilization. Possible starvation

(8.8) Deadlock Recovery

Can let happen automatically Can inform operator of deadlock or break it Can abort threads Can preempt resources from deadlocked threads

(6.2) Exit Section

Segment of code implementing a process exiting its critical section

(8.3) Assignment Edge

Edge from resource to thread representing assignment of the resource

(6.2) Bounded Wait

"Everyone gets in" - at some point Limited amount (bounded) of waiting before process enters critical section after requesting

(9.1) Dynamic Loading

A routine is not loaded until it is call. Routines are kept on disk in a relocatable load format Main program loaded into memory and executed, check if other routine loaded before relocating into memory Routines only added if needed, useful when large amounts of code needed to handle special cases (error routines) Doesn't require special support from OS, its up to users

(6.8.2) Priority Inversion

A scheduling challenge arising when a higher-priority process needs to read/modify kernel data currently being accessed by lower priority process Avoided by implementing priority-inheritance protocol

(6.2) Critical Section

A segment of code in which the process may be accessing and updating shared data When a process is executing in this section, no other process is allowed to execute in its respective section Process must request permission to enter

(7.5) Memory Transaction

A sequence of atomic memory read-write operations. If completed, is committed. Otherwise, operations must be aborted and rolled back

(8.6) Safe Sequence

A sequences of threads such that resource requested by a thread can be satisfied by currently available resources plus those held by previous threads If one of these exists, state is safe. If not, state is unsafe

(6.8) Liveness

A set of properties that a system must satisfy to ensure that processes make progress during their execution life cycle. I.e. waiting indefinitely is a liveness failure, infinite loop, busy wait (possible) Failure - poor performance and responsiveness

(9.3) Frame Table

A single, system wide data structure that holds the allocation details of physical memory -Which frames are allocated -How many frames are available -How many total frames there are One entry for each physical page frame, indicating whether the latter is free or allocated -if allocated, to which page of which process(es)

(8) Resource types

CPU Cycles, Memory Space, I/OP devices

(7.4) Java Semaphore

Counting semaphore initialized with Semaphore(int value) value being initial value. Acquire() throws exception if interrupted Try finally around acquire and release sem.acquire() and sem.release()

(7.3) POSIX Unnamed Semaphores

Created and init using sem_init(0 function, passed pointer to semaphore, flag for level of sharing, semaphores initial value flag 0 - can be shared by threads belonging to created process - nonzero means shared memory processes can share it Init to 1 Same sem_wait() and sem_post() Return 0 when successful and nonzero when error

(9.1) Where are Page Tables Stored?

In main memory and backing store

(6.1.1) A race condition _____________________

Results when several threads try to access and modify the same data concurrently

(9.1) Logical Address Space

Set of all logical addresses generated by a program Differs from Physical Address Space in execution-time binding

(9.5) Swapping with Paging

Swapping in/out pages of a process rather than an entire process Less costly than swapping entire processes, and small number of pages swapped Swapping now usually refers to standard swapping and paging refers to swapping with paging Page out moves page from memory to backing store Page in moves page back into memory Used by most OS's

(9.1) Static Linking

System libraries treated like any other object module and combined by loader into binary program

(7.1) Dining Philosophers Problem

Table with five philosophers, each with two chopsticks on either side shared by the person next to them. Pick up one at a time, eats if they have two. If not eating, thinking Example of a large class of concurrency-control problems Represents need to allocate among several processes in a deadlock free and starvation-free manner Deadlock free solution doesn't necessary eliminate possibility of starvation

(9.1) Direct-Access Storage Devices

The only storage that the CPU can access directly Includes Main Memory (RAM) and Registers. Programs and their associated data needed for instructions must be loaded here to be accessed by the CPU

(9.3) TLB

Translation Look-Aside Buffer AKA Associative Memory Small, fast lookup hardware cache Associative, high speed memory Solves problem of needing 2 memory accesses (double) when using a main memory page table. Contains a key(or tag) and value. Item comared to all keys simultaneously, returning value if item found. In modern hardware, part of instruction pipeline, adding no performance penalty Must be kept small; 32 - 1,024 entries

(6.3.3) (T/F) Peterson's Solution is a technique for managing critical sections in operating systems

True

(7.2.1) (T/F) Operations on atomic integers do not require locking.

True

(9.3) Handling Full TLB

What to do if TLB is full of entries Select an existing entry for replacement -Least recently used (LRU) -Round robin -Random

(6.3.1) In Peterson's solution, the variable ___________ indicates if a process is ready to enter its critical section

Flag[i]

(9.1) Registers

Built into CPU, can be accessed directly by it Any data being used by instructions need to be here or in main memory Accessible within one cycle of CPU clock.

(PX 6.2) What is the meaning of the term busy waiting? What other kinds of waiting are there in an operating system? Can busy waiting be avoided altogether? Explain your answer.

Busy waiting is when a process is not in the wait queue but is running through a loop until some condition is met (lock is acquired) This can lead to inefficiency if happens for too long too often, as it is wasting CPU cycles. When overall busy wait time is less than the time of the context switches required to switch process to wait queue and back, busy wait is more efficient Busy waiting can be avoided by sleeping and being woken up, but this would reduce performance depending on the average duration of locks being held (causes context switches)

(7.1.3) How many philosophers may eat simultaneously in the Dining Philosophers problem with 5 philosophers?

2

(9.3) Translation of logical address to physical address using MMu

1. Extract page number p and use it as an index into the page table 2. Extract the corresponding frame number F from the page table 3. Replace page number p with frame number f As offset doesn't change, it isn't replaced, frame number and offset now comprise physical address

(9.3) Accessing Paged Data Process Flow- Main Memory

1. Index into page table in main memory, using value in PTBR offset by the page number for i. (1 memory access) 2. Using frame number we just got, combine with page offset to produce actual address, and find in memory (second memory access)

(8.1) Thread Sequence for Utilizing Resources

1. Request Resource 2. Use Resource 3. Release Resource

(8.6) Unsafe State

A state that may lead to a deadlock Has no safe sequence System cant prevent threads from requesting resources in such a way that deadlock occurs Behavior of threads controls these states

(9.3) TLB Miss

A Translation Look-Aside Buffer lookup that fails to provide the address translation because it is not in the TLB

(6.7.3) Conditional-Wait

A component of the monitor construct that allows for waiting on a variable with a priority number to indicate which process should get the lock next x.wait(c) Smallest priority number resumbed next

(6.4.1) Strongly Ordered Memory Model

A memory modification on one processor is immediately visible to all other processes

(8.2.1) Hold and Wait

A necessary condition for deadlock A process must be holding one resource and waiting to acquire additional resources.

(8.3) Circular Wait

A necessary condition for deadlock A set of waiting threads exist such that all are waiting for a resource held by another in the set such that none will be able to receive their necessary resource

(8.3) Mutual Exclusion

A necessary condition for deadlock Only one thread at a time an use the resource. If another thread requests that resource, the requesting thread must be delayed until the resource has been released.

(8.3) No Preemption

A necessary condition for deadlock Resources cannot be preempted; that is, a resource can be released only voluntarily by the thread holding it, after that thread has completed its task

(7.4) Wait Set

A set of threads, initially empty Used if thread calls wait Thread releases lock, blocks, placed in wait set

(7.5) Imperative Programming Languages

AKA Procedural Programming Languages Includes C, C++, Java, C# Implement state based algorithm, in which flow is crucial to correctness Has variables and data structures with mutable state, and changing variable values

(6.2) Select Progress

AKA Selection Progress Selection of next process to enter critical section can not be delayed indefinitely "Pick One"

(7.3) POSIX Condition Variables

Accessed within mutex lock instead of monitor pthread_cond_t data type, init using pthreaD_cond_init(), Mutex lock associated must be locked before calling wait on condition variable, used to protect data in conditional clause from race condition Thread checks condition after mutex lock acquired. If not true, thread invokes wait passing mutex and condition variable as param, releasing mutex lock and allowing other thread to access Place condition clause in loop so condition is rechecked after signaled Can signal condition variable but doesn't release mutex lock, happens after

(7.1) Reader-Writer Locks

Acquiring requires specifying mode of the lock - read or write access Multiple processes can concurrently acquire in read mode, but only one may acquire for writing - mutex Useful when distinction between readers and writers is clear Useful when there are more readers than writers Requires more overhead than semaphores or mutex locks, more readers = more concurrency = compensate for overhead of lock

(9.1) Absolute Address

Address defined by a real numbered location inside memory Ex: 729405

(9.1) Physical Address

Address seen by the memory unite and loaded into memory-address register of memory Derived by some method from logical address

(9.4) Sparse Address Spaces

Address space in which memory references are noncontiguous and scattered throughout the address space

(9.3) ASIDs

Address-Space Identifiers Uniquely identifies a process in a associated with an entry in the TLB

(9.1) Relocatable Address

Addresses defined by an offset from the beginning of the module Ex: "14 bytes from the beginning of this module"

(8.7) Detection Algorithm Usage

Algorithm invoked more depending on how often deadlock occurs We could invoke whenever resources cant be granted immediately - leads to a lot of overhead Can invoke at intervals of time or whenever CPU utilization drops below a certain percentage

(6.8.2) Priority Inheritance Protocol

All processes accessing resources needed by a higher priority process inherit the higher priority until they are finished with the resources in question After done, revert priority

(9.2) First fit

Allocate first hole that is big enough, can start at beginning of set of holes or location at which previous search ended. Better than worst fit, generally faster than Best fit

(9.2) Worst Fit

Allocate largest hole, search whole list unless sorted by size Produces largest leftover hole Worst storage and time optimization

(9.2) Best Fit

Allocate the smallest hole big enough. Must search entire list unless sorted by size. Produces smallest leftover hole Better than worst fit in terms of time and storage optimization. Generally slower than First Fit

(6.4.2) Hardware Instructions

Allow testing and modifying content of a word or to swap contents of two words atomically Simple test_and_set() and compare_and_swap()

(7.4) Block Synchronization

Allowed by java, synchronizing a block of code within a method

(6.2) Preemptive Kernel

Allows a process to be preempted while it is running in kernel mode Must be carefully designed to ensure shared kernel data are free from race conditions Difficult to design for SMP because of parallel execution with multiple cores May be more responsive More suitable for real time programming (real time preempting)

(6.4.1) Memory Barrier

Also known as Memory Fence Instructions that can force any changes in memory to be propagated to all other processors, ensuring memory modifications are visible to all threads on all processors System ensures all loads/stores completed before any subsequent load/store ops. Memory consistent even with reordering When placed after an instruction, make all prior instructions executed before all following ones - prevents reordering Low level operation, only used when writing specialized code that ensures mutual exclusion

(7.4) Scope

Amount of time between lock acquired and released Synchronized method with small percentage of code manipulating shared data may yield too large of a scope May be better to synchronize only manipulating block of code, leading to smaller lock scope

(9.1) Symbolic Address

An address defined by a symbol or name, with no numbers Ex: %variable, int count, etc.

(9.3) Wired down

An entry that cannot be removed from the TLB ex: important kernel code

(6.6) Semaphores

An integer value that apart from init, is accessed only through atomic ops wait() and signal(). wait() - termed p (to test) - decrements count signal () - termed v (to increment) All modifications in wait and signal must be executed atomically. Should balance wait and signal calls just like mutex, but doesn't need to happen in the same process Wait and signal cant be executed on the same semaphore simultaneously - must be placed in critical section (may now have busy waiting in critical section) -short code, little waiting if rarely occupied More robust than mutex locks, more sophisticated synchronization Can suspend with sleep() instead of busy wait, woken up by signal() op from other process Restarted by wakeup() op to go from waiting to ready Signal op wakes up a process Designed by Dijkstra

(6.4.2) Atomic

An uninterruptable unit Swapping atomically means processing a swap between two variables as one action that cant be reordered or interleaved.

(9.4) Hashed Page Tables

Approach to handle address spaces >32 bits Hash value is virtual page number Entry contains linked list of elements that has to the same location (collisions) Each element has -Virtual Page Number -Value of mapped Page frame -Pointer to next element in linked list

(9.2) Memory Allocation

Assign processes to variably sized partitions in memory, where each can only contain one process OS takes into account memory requirements and available memory in space when determining which processes allocating memory When allocated, process gets a spot and is loaded in, and can compete for CPU time When process terminates, releases its memory When there isn't enough memory, can reject process or put in a wait queue

(EX 6.21) A multithreaded web server wishes to keep track of the number of requests it services (known as hits) Consider the two following strategies to prevent a race condition on the variable hits. The first strategy is to use a basic mutex lock when updating hits. A second strategy is to use an atomic integer. Explain which of these two strategies is more efficient

Atomic integer would be more efficient because since the time duration of the lock being held is so short, just the time to increment hits. In a mutex lock if the lock wasn't available, it would incur heavy cost in context switching to sleeping and then to waking back up when it is available shortly thereafter. With atomic integer, it would just busy wait, using CPU cycles but for a much shorter time than the context switch time with a mutex lock.

(7.3) POSIX Synchronization

Available to programmers at user level Not part of any specific OS kernel Other types limited to kernels Must be implemented using tools provided by host OS Includes mutex locks, semaphores, condition variables, Widely used in Pthreads and POSIX APIs on Unix, Linux, macOS

(8.5) Preventing No Preemption

Can add preemption by release resources implicitly when waiting for another. Can also grab resources from other thread if it is waiting for resources too. Good for registers and databases but not locks

(8.6) Resource Allocation Graph Algorithm

Can use if one instance of each resource type Has Claim Edges Can only grant requests if doesn't result in a cycle. If does result i a cycle, make thread wait Use cycle detection algorithm

(8.6) Banker's Algorithm

Can use if we have multiple instances of each resource type Less efficient than scheme with one instance per Treads declare maximum resources of each type they may need Allocate if will lead to a safe state, otherwise wait Uses Available, Max, Allocation, Need data structures Has Safety Algorithm and Resource Request Algorithm

(9.2) Memory Protection

Check to make sure process logical address is under limit logical address, then add relocation register to map to memory

(9.3) Reentrant Code

Code that supports multiple concurrent threads and can be shared

(9.1) Relocatable code

Code with bindings to memory addresses that are changed at loading time to reflect where the code is located in main memory

(9.3) TLB Usage

Contains only a few page-table entries. MMU first checks if page number is in TLB, if found, instant frame number available and access memory and no performance penalty If not in TLB (TLB miss) - access page table as normal, and add to TLB

(6.2.1) A(n) ____________ refers to where a process is accessing/updating shared data

Critical Section

(7.1) Readers-Writers Problem

Database shared among several conurrent processes, sokme wanbt to read, others want to update Readers- read database Writers -Write to database Writers accessing simultaneously with other processes may lead to race conditions Writers have exclusive access to database when writing Solutions may lead to starvation of writers in first and readers in second

(8) Method for Handling Deadlock

Deadlock Prevention and Avoidance ensure system will never enter deadlock state Allow to enter deadlock and then recover Ignore and pretend deadlocks never occur - used by most OSes

(8.3) Necessary Conditions for Deadlock

Deadlock can occur if all four conditions hold simultaneously Mutual Exclusion Hold and Wait No Preemption Circular Wait

(7.1) Monitor Solution to Philosopher Dining Problem

Deadlock free May pick up chopsticks iff both available, use enum for thinking, hungry, eating Can set state to eating iff two neighbors not eating. Can delay self if hungry but cant get chopsticks Monitor controls distribution of chopsticks, processes call monitor. Monitor checks if two neighbors aren't eating, and allows to eat and signal. Starvation possible

(9.1) Base and Limit Registers

Define the range of legal addresses for a specific process in memory. CPU or hardware checks process addresses against these registers before accessing memory If invalid, sends trap to OS who handles the error. Prevents user program from modifying data of other users/OS Loaded and changed by OS only

(8.6) Resource Allocation State

Defined by number of available and allocated resources and the maximum demands of the threads

(6.2) Critical Section Problem

Design a protocl that processes can use to synchronize their activity so as to cooperatively share data Each process must request permission to enter its critical section Must meet 3 criteria: Mutual Exclusion - Only One at a time Selection Progress - Pick One Bounded waiting - Every one gets in at some point

(8.3) System Resource-Allocation Graph

Directed graph used to describe deadlocks Consists of vertices representing threads and resources, and edges representing allocations and requests If the graph contains no cycles, then no thread in the system is deadlocked If it does contain a cycle, a deadlock may exist If contains a cycle and one instance per type, deadlock

(6.2) Non-Preemptive Kernel

Does not allow a process running in kernel mode to be preempted. Will run until it exits kernel mode, blocks, or voluntarily yields CPU control Free from race conditions on kernel data structures as only one process active in kernel at a time. May be less responsive, a kernel process may run for a long time before letting go of CPU - This risk can be minimized by designing behavior to not work this way Less suitable for real time programming

(9.1) DLL

Dynamically Linked Library AKA Shared Libraries System library linked to user programs when they are run. Reduces size and main memory usage of a program Can be shared among multiple processes with only one instance in main memory Requires help from OS to check other process memory for the same library

(7.1) Semaphore Solution to Dining Philosophers Problem

Each chopstick is a semaphore, philosophers wait on them. Signal to release chopsticks. semaphore chopstick[5] with each chopstick init to 1 Philosopher waits on both, eats then signals both, and goes back to thinking Guarantees mutex, but could create deadlock Solutions to deadlock : -Only 4 philosophers sitting at the time -Only pick up chopsticks if both are available (must pick up in a critical section) -Asymmetric - odd numbered philosopher picks up first her left and then right, even numbered picks up her right and then left

(9.2) Contiguous Memory Allocation

Each process is contained in a single section of memory that is contiguous to the section containing the next process.

(PX 6.5) Illustrate how a binary semaphore can be used to implement mutual exclusion among n processes.

Each process would wait until the semaphore value is 0, and then enter, setting the value to 1 when exiting

(PX 6.5) Illustrate how a binary semaphore can be used to implement bounded waiting among n processes.

Each process would wait until the semaphore value is 0, and then enter, setting the value to 1 when exiting. There could be a second data structure shared such as a queue or array in which the currently executing critical section iterates to the next waiting process in the queue and sets a boolean to true so that that process may run. Each process must check if the semaphore is 0 and their flag is true before they may enter critical section

(8.3) Request Edge

Edge from thread to resource representing a request for the resource

(8.8) Process and Thread Termination

Eliminate deadlocks by aborting process or thread -Abort all deadlocked processes - Breaks deadlocked cycle at great expense -Abort one process at a time until deadlock eliminated - Overhead from checking after each abort for deadlock Should abort processes that incur the minimum cost with factors: -Priority -How long has computed/ will need to compute r further -How many and what type of resources has used -How many more resources needed -How many processes

(8.1) Deadlock

Every thread in a set is waiting for an event that an be caused b another thread in the set Events - Resource acquisition and release. - Typically locks, semaphores, files

(9.5) Backing Store

Fast secondary storage that can hold pages, page tables, swapped processes

(9.3) Pages

Fixed size b locks of logical memory of the same size as frames Loaded when process is executed into any available memory frames from their source (file system/backup store) Backing store divided into same size as these Size defined by hardware, usually a power of 2, between 4KB and 1GB If process size independent of page size, internal fragmentation will average one half page per process, suggesting small page sizes preferred but there's overhead for each page, thus page sizes grow Usually 4 or 8 KB+

(9.3) Frames

Fixed size blocks of physical memory used for implementing paging Filled with pages at execution time

(9.2) 50-percent rule

For First-fit, even with optimization given N allocated blocks of memory, another 0.50 N blocks will be lost to fragmentation One third of memory may be unusable

(6.7) Monitor

Fundamental high-level synchronization construct An abstract data type that includes ops provided with mutual exclusion Can access variables within monitor and parameters Condition variables allow for multiple variables under one monitor Ensures only one process at a time is active w/in monitor Can have overhead and scalability issues

(7.5) HTM

Hardware Transactional Memory Uses hardware cache hierarchies and cache coherency protocols to manage and resolve conflicts involving shared data residing in separate processors' caches. Requires no special code instrumentation and thus has less overhead Requires existing cache hierarchies and cache coherency protocols be modified to support transactional memory

(6.4.2) test_and_set()

Hardware instruction that executes atomically Returns original value of passed parameter (lock) Value of passes parameter set to true Can implement mutual exclusion with a lock

(9.4) Forward-Mapped Page Table

Hierarchical Page Table in which address translation works from outer page table inward

(9.1) Base Register

Holds smallest possible legal physical memory address for a specific process

(6.4.1) Memory Model

How a computer determines what memory guarantees i will provide to an application program Strongly ordered or Weakly ordered Vary by processor type

(9.2) Dynamic Storage-Allocation Problem

How to satisfy a request of size n from a list of three holes First Fit Best Fit Worst Fit

(9.3) TLB Flush

If TLB doesn't support multiple ASIDs, every time a new page table is selected (context switch), TLB must be erased Ensures next executing process does not use wrong translation info (outdated virtual addresses)

(9.1) Compile Time Binding

If memory address known at _________, absolute code can be generated. If starting location changes, then the code will need to be recompiled Generates identical logical and physical addresses

(9.1) Load Time Binding

If memory address not known at compile time, compiler must generate relocatable code. Final binding delayed until _______________ If starting address changes, user code needs to be reloaded with changed value Generates identical logical and physical addresses.

(9.1) Execution Time Bnding

If process can be moved in memory during execution, binding must be delayed until runtime. Special hardware must be available Most OS's use this method

(PX 6.6) Race conditions are possible in many computer systems. Consider a banking system that maintains an account balance with two functions: deposit(amount) and withdraw(amount). These two functions are passed the amount that is to be deposited or withdrawn from the bank account balance. Assume that a husband and wife share a bank account. Concurrently, the husband calls the withdraw() function, and the wife calls deposit(). Describe how a race condition is possible and what might be done to prevent the race condition from occurring.

If there is a temporary changed value and the actions are not atomic, a race condition could exit where both spouses retrieve the current balance, 1000, and the husband withdraws 500, and the wife deposits 500. The update of the bank happens for the husband first, and so it becomes 500, but then 1500 after the wife deposits 500, when the correct value should be 1000 The reverse could happen if the wife's value is updated first, leaving the balance at 500 A race condition could be prevented via locks - in this case the lock hold time would be short so a CAS lock would work well

(9.1) Translate Logical Address to Page Number and Offset

Iff logical address size is 2^m and page size is 2^n m-n bits of a logical address are page number n bits are the offset

(6) Traditional Synchronization

Includes Mutex Locks and Semaphores

(7.5) OpenMP

Includes compiler directives and API Defines parallel regions and handles thread creation and management without needing developers to Specifies critical sections in which only one thread may be active at a time to prevent race conditions Blocks thread if attempts to enter while another thread is active. Each critical section assigned a different name Easier to use than standard mutex locks but must identify possible race conditions still. Deadlock possible when more than one critical section

(7.4) Java Synchronization

Includes monitors (original synch mechanism), reentrant locks, semaphores, and condition variables

(6.8) Strvation

Indefinite blocking A process may never be removed from semaphore queue its suspended in

(8.6) Claim Edge

Indicates thread may request resource at some time in the future Edge with dashed line Used in Resource-Allocation-Graph Algorithm for Deadlock Avoidance Converted to request edge when process requests resource Then converted to assignment edge wen resource allocated When resource released, reconverts to claim edge Resources must be claimed a priori

(9.1) Cache

Intermediary memory between CPU core and main memory Can be within core, on chip between cores ,or on dye itself. Faster than memory bus but slower than register access Bigger than register, smaller than main memory

(7.1.1) What is the purpose of the mutex semaphore in the implementation of the bounded-buffer problem using semaphores?

It ensures mutual exclusion

(6.5) Busy Waiting

Looping continuously When can't acquire, processes may do this. Wastes CPU cycles

(6.5) Mutex Lock

MUtual EXclusion lock Protect critical sections and thus prevent race condition Process must acquire lock before entering critical section and release when exiting acquire() and release() available indicated if lock is available or not Waiting on a lock requires two context switches- thread to waiting and restore waiting thread once available Pessimistic - Assume other thread is updating so acquire lock first Simpler and less overhead than semaphores.

(7.2) Non-Signaled State

Means the object is NOT available, and a thread WILL block when attempting to acquire this object When thread blocks on this, changes from ready to waiting, and thread placed in waiting queue for object When state moves to signaled, kernel checks whether any threads waiting, and moves one or more to ready. Mutex - kernel selects 1. Event - Mutex selects all

(6.4.1) Weakly Ordered Memory Model

Modifications to memory on one processor may not be immediately visible to other processors

(7.4) Java Monitor

Monitor-like concurrency mechanism for thread synchronization. Can set synchronized status for method to require owning lock for that object. If lock owned by another thread, thread blocks and placed in entry set for the object's lock If available, calling thread owns lock and can enter method, releasing when exiting. Uses wait() and notify() Notify picks arbitrary thread from wait set, puts into entry set, and sets it to runnable from blocked

(8.5) Preventing Circular Wait

Most practical method of prevention Impose total ordering of resource types and require requesting in increasing order Must release previous resource before requesting next. Disables circular wait condition Doesn't prvent deadlock, app developers must follow it. Can be difficult with many locks

(9.5) Standard Swapping

Moving entire processes between main memory and backing store All per thread data must be swapped All metadata must be maintained Allows physical memory to be oversubscribed, more processes than there is physical memory to store Idle/mostly idle processes swapped, swapped back in once active again Used in traditional UNIX systems because very slow

(7.4) Java Condition Variables

Must be associated with a reentrant lock (like posix condition variables and mutex locks) init reentrant lock then do .newCondition Can invoke .await() and .signal() Can specify a specifc thread like if (threadNumber != turn) condVars[threadNumber].await(); to busy wait and check until it is false Need a condition variable for each index in array

(6.2) MUTEX

Mutual Exclusion "Only One" Only one process in critical selection at a time

(6.2.2) A solution to the critical section problem must satisfy which requirements?

Mutual Exclusion, Progress, and Bounded Waiting

(6.6) Semaphore Implementation

No locking on wait and signal -can put busy wait in CS implementation -fast if cs is fast -bad if cs takes a long time

(EX 6.22) Consider the code example for allocating and releasing processes shown in Figure E6.21. Could we replace the integer variable int number_of_processes = 0 with the atomic integer atomic_t number_of_processes = 0} to prevent the race condition(s)?

No, because if we retrieve the variable number_of_processes in the if statement and the number_of_processes variable then decrements from a release_process() call right after, the decrement will happen atomically. The number we just retrieved will be wrong for the following increment of number_of_processes, even though both modifications are happening atomically.

(9.2) Variable-Partition

OS keeps a tab indicating which parts of memory are available and which are occupied Each process gets a variably sized partition of memory All memory available for user processes and is considered one block of available memory - hole

(7.1) Second Readers-Writers Problem

Once a writer is reader, writer performs its write ASAP. Opposite of other variation, readers cannot start reading if writer is waiting

(9.3) Shared Pages

One piece of Reentrant code shared by multiple processes One page table for all, not one for each Ex: standard C Library

(6.7) Signal and Continue

P calls x.signal while Q suspended with x Q either waits until P leaves the monitor or waits for another condition

(9.3) d

Page Offset Every address generated by CPU divided into this and page number

(9.3) P

Page number Every address generated by CPU divided into this and page offset

(9.3) PTBR

Page-Table Base Register Points to page table when a very large page table is used and stored in main memory. Changing page tables requires only changing this register, reducing context switch time.

(9.3) PTLR

Page-Table Length Register Indicates size of page table, checked against logical address to verify in valid range Failure causes error trap to OS

(9.3) Hit Ratio

Percentage of times that page number is found in TLB

(9.3) Paging

Permit logical address space of processes to be noncontiguous, allowing process to be allocated physical memory wherever such memory is available Most common memory management technique for computer systems Avoids external fragmentation and the associated need for compaction, two major problems of contiguous memory allocation. Separates logical address space from physical address space Increases context switch time

(8.8) Resource Preemption

Preempt resources from processes and give to other processes until deadlock broken 1. select victim to be preempted -Minimize cost with factors like number of resources holding, time computed thus far 2. Rollback - Rollback process to safe state and restart - Usually means aborting and restarting 3. Starvation - Ensure process can only be victim a small finite number of times (include number of rollbacks in cost factor)

(9.3) Effective Memory Access Time

Probability summation of hit and miss Hit = 1 TLB access + 1 Main Mem access Miss = 1 TLB Access + 2 Main Mem access = hit ratio * hit time + miss percentage * miss time = .80 * 10 + .20 *20 (80% hit ratio) = 12 nanoseconds

(9.5) Swapping

Process moving temporarily out of memory to a backing store and brought back for continued execution Not generally used by mobile systems, asks processes to relinquish memory voluntarily, later reloaded. Terminated if don't give up

(6.5) Spinlock

Process spins while waiting for lock to become available. Busy waiting lock method No context switch required when a process must wait, which reduces overhead. If locks are held for short duration, spinning doesn't reduce performance that much. Short duration = less than two context switches (if more, its more efficient to just switch the process to waiting) Widely used

(7.1) Bounded Buffer Problem

Producer and consumer share a lock, and pointers to n buffers holding 1 item. Semaphore empty init to number of buffers n. Semaphore full init to 0 Producer waits for an empty buffer, waits for mutex, adds, then signals mutex and full Consumer waits for an full buffer, waits for mutex, consumes, then signals mutex and empty

(7.5) Functional Programming Languages

Programming languages that do not maintain state Immutable variable values Aren't concerned with race conditions and deadlocks Include Erlang, Scala

(9.4) Clustered Page Tables

Proposed variation of Hashed Page Tables for 64bit address spaces Every entry refers to several pages instead of one Single entry can store mappings for multiple physical page frames Useful for spare address spaces

(7.2) Window Synchronization

Protects access to global resources using spinlocks to protect short code segments. Kernel ensures thread will never be preempted while holding a spinlock Outside kernel, provides dispatcher objects

(7.2) Dispatcher Objects

Provided by Windows Used to synchronize threads using mutex locks, semaphores, events, and timers. System prevents shared data by requiring a thread to gain ownership of a mutex to access the data and to release ownership when it is finished. May be in signaled or nonsignaled state

(8.4) Deadlock Prevention

Provides a set of method to ensure that at least one of the necessary conditions for deadlock cannot hold Prevent deadlocks by constraining requests for resources Can lead to low utilization and system throughput Ensures system will never enter deadlock state

(7.2) Linux Synchronization

Provides atomic integers, using data type atomic_t - useful with counter without locking overhead, not useful when several variables Mutex locks available for protecting critical sections in kernel. Spinlocks and semaphores (and reader-writer versions of them) provided for locking in kernel. Spinlock only held for short durations. In single processor, replaced with enabling/disabling kernel preemption. - nonpreemptive if a task in kernel is holding a lock, tracks via count Enable preemption for signal, disable for wait Locks are nonrecursive - if a thread has acquired one, cant acquire same lock a second time w/o releasing. Second attempt will block.

(6.4.3) Atomic Variables

Provides atomic operations on data types like ints and booleans. Can ensure mutual exclusion on race for single variable, like a counter Often implemented using hardware instructions (CAS) Very lightweight and more appropriate for counters.

(9.1) Main Memory

RAM CPU Can access this directly Program must be moved to here before being run by CPU Any data being used by instructions must be here or in registers If memory access to this takes many cycles of CPU clock, processor needs to stall

(8.4) Deadlock Avoidance

Requires OS be given additional info in advance concerning which resources a thread will request and use during its lifetime. OS Decides whether thread should wait, depending on resources currently available and allocated, and future requests Declare maximum resources needed Resource Utilization may be lower than otherwise would be If single instance of a type - use graph if multiple - use Banker's algorithm

(7.1) First Readers-Writers Problem

Requires that no reader be kept waiting unless a writer has already obtained permission to use the shared object Readers shouldn't wait for other readers to finish just because a writer is waiting

(9.1) MMU

Runtime mapping from virtual to physical addresses is done by this hardware device Renames base register to relocation register, which is added to every address generated by a process dynamically to map it to memory

(6.2) Entry Section

Section of code implementing the request of a process to enter its critical section

(9.1) Physical Address Space

Set of all physical addresses corresponding to logical addresses in logical address space Differs from Logical Address Space in execution-time binding

(6.1) Race Condition

Several threads try to access and modify the same data concurrently. Concurrent access to shared data may cause data inconsistency The outcome depends on the order in which access takes place, and the order cannot be guaranteed without synchronization/coordination Should be avoided

(7.2) Events

Similar to condition variables May notify a waiting thread when a desired condition occurs

(7.5) STM

Software Transactional Memory Implements transactional memory exclusively in software with no special hardware Inserts instrumentation code inside transaction blocks by compiler Manages transactions by examining where statements can run concurrently and where low-level locking is required

(9.2) Compaction

Solves External Fragmentation Shuffle memory contents so that all free memory is together in one block. Only possible if relocation is dynamic and done at execution time. requires only moving program and data then changing base register. Must determine cost of compaction Simplest way is to move all processes toward one end of memory, with holes to the other side, which can be expensive

(9.4) Inverted Page Table

Solves the problem of lots of entries in page table One entry for each physical memory frame Each Entry: Contains the virtual address of the page stored in it and Contains info about process that owns said page Less memory for page table storage, but slower to search. Can use a hash table to alleviate, but still requires two memory reads Does not support shared pages

(9.1) Limit Register

Specifies the size of the range of addresses that a process can access

(PX 6.3) Explain why spinlocks are not appropriate for single-processor systems yet are often used in multiprocessor systems.

Spinlocks aren't appropriate for single processor systems because a busy wait relies on another process freeing up a lock or resource while the first process is still running on the CPU. No other process can run to stop the busy wait.

(8.6) Safe State

State in which the system can allocate resources to each thread up to its max in some order and avoid a deadlock Happens if exists a safe sequence

(7.5) Transactional Memory

Strategy for process synchronization in which a memory transaction is committed if all operations are completed, otherwise it is aborted and rolled back. Responsible for guaranteeing atomicity, not the developer. No deadlock possible because there are no locks Can determine concurrency instead of programmer having to No widespread implementation but lots of research recently with advent of multicore systems and emphasis on concurrent and parallel programming

(7.4) Entry Set

The set of threads waiting for the lock to become available. If not empty when lock is released, JVM arbitrarily selects a thread to be owner of the lock (unordered, but usually FIFO)

(EX 6.20) Assume that a context switch takes T time. Suggest an upper bound (in terms of T) for holding a spinlock. If the spinlock is held for any longer, a mutex lock where waiting threads are put to sleep) is a better alternative

The maximum time T that a spinlock should be held would be 2T because a mutex lock would incur T cost for putting thread to sleep and T cost for waking it up.

(7.4) Reentrant Locks

The opposite of non-recursive locks Simplest locking mechanism in Java API Owned by a single thread, can set fairness paraemeter to favor granting lock to longest waiting thread lock() and unlock(), but if thread invoking lock already owns it while it is locked, it locks again and owns the lock. Must unlock however many times it is locked. Variant called ReentrantReadWriteLock allows multiple readers but only one writer

(PX 6.4) Show that, if the wait() and signal() semaphore operations are not executed atomically, then mutual exclusion may be violated.

The wait could finish its busy wait and be interrupted by another waiting process that decrements its counter of S and enters its critical section first. Then, the resuming first process would decrement its count and enter the critical section, causing multiple processes to potentially be in their critical sections at once

(EX 6.22) Consider the code example for allocating and releasing processes shown in Figure E6.21. Assume you have a mutex lock named mutex with the operations acquire() and release(). Indicate where the locking needs to be placed to prevent the race condition(s)

There should be an acquire before the line "if (number_of_processes....)"" So that the number isn't corrupted after calling the if statement if another process is released. There should be release after the line "++number_of_processes" and an acquire and release surrounding the line "—number_of_processes;" so that only one modification of the count of processes can happen at once.

(PX 6.1) In Section 6.4, we mentioned that disabling interrupts frequently can affect the system's clock. Explain why this can occur and how such effects can be minimized.

This can occur because some systems update their clocks via interrupts and frequent disabling could cause the clock to lose synchronization. The system clock is also used for scheduling with quanta expressed in clock ticks. This can be minimized by reducing the amount of time for which clock interrupts are disabled

(EX 6.07) The pseudocode of Figure 6.14 illustrates the basic push() and pop() operations of an array-based stack. Assuming that this algorithm could be used in a concurrent environment, what data have a race condition?

Top

(6.1.2) (T/F) Instructions from different processes can be interleaved when interrupts are allowed

True

(6.2.3) (T/F) A non-preemptive kernel is safe from race conditions on kernel data structures

True

(6.8.1) Deadlock

Two or more processes waiting indefinitely for a event that can be caused only by one of the waiting processes. - signal op A set is deadlocked when every process is deadlocked.

(6.9) Contention Performanc

Uncontended : Both are fast, but CAS is faster than traditional synchronization Moderate Contention - CAS is faster, possibly much faster, than traditional - CAS usually succeeds and if not, loops a few times. Mutex makes any attempt more overhead, causing context switches High Contention - Traditional faster

(9.2) Internal Fragmentation

Unused memory that is internal to a partition. Requires help from OS to check other process memory for the same library When allocating with a small number of leftover bits, overhead to track it is larger than the hole itself. Solved by breaking memory in to fixed block sizes. Might be slightly larger than requested memory, which is __________________________

(7.1) Synchronization Testing

Use common problems to test new synchronization primitives and solutions Problems include bounded buffer, reader writer, dining philosopher, etc

(8.6) Safety Algorithm

Used In Deadlock Avoidance Banker's Algorithm Determines whether in a safe state Find index that isn't finished and needs amount of resources <= available If cant find, set to safe state and do nothing. If can find, add allocation to available and set this one to finished. go to next thread

(8.6) Resource-Request Algorithm

Used in Deadlock Avoidance Banker's Algorithm Determine s whether requests can be safely granted 1. If request <= need, go to 2. Else, error, thread exceeded max claim 2. If request <= available, go to 3. Else, wait\ 3. Pretend to allocate resources to thread t available -= request allocation += request need -= request If resulting is safe, transaction is completed and allocate resources. Else, wait for request, restore old state

(7.2) Recursive Locks

Used in Linux for spinlocks and mutex locks If a thread has acquired one of these locks, it cant acquire the same one again without first releasing it. If it tries, it will block.

(7.2) Timers

Used to notify one or more threads that a specified amount of time has expired

(7.3) POSIX Mutex Locks

Used to protect critical sections of code A thread acquires lock upon entering and releases when leaving. Pthreads uses pthread_mutex_t init using pointer to lock and attributes. Controlled w/ lock and unlock calls with pointer to lock as parameter. Thread blocks if unavailable upon lock invoke Return a value of 0 with correct operation, nonzero if error

(9.4) Hierarchical Paging

Used to solve problem of excessively large page tables Page Tables themselves are paged to create layers Outer layers are indexes of inner page tables, or pages of page tables, which then point to memory Each individual layer takes an additional memory access

(7.2) Critical-Section Object

User made mutex that can often be acquired and released without kernel intervention First uses a spinlock while waiting for the object, if spins too long, allocates kernel mutex and yields CPUI Efficient bc doesn't use kernel mutex unless there's contention - rare, so savings are significant.

(9.3) Page Table

Uses Page Number as an index. Contains base address of each frame in physical memory, with page offset being location of said frame. Base address combines with page offset to define physical memory address OS contains copy of one for each process, used to translate logical to physical addresses. Used by CPU dispatcher to define hardware ___________ when a process is to be allocated the CPU. Can vary in size from 256 to 2^20 entries

(9.1) Addresses in Source Program

Usually symbolic, e.g %var or variable count

(8.7) Wait-For Graph

Variant of Resource-Allocation graph used by Deadlock Detection algorithm Remove resource nodes and collapse their edges Single instance of each resource type Edge between threads implies thread is waiting for a resource from that thread Deadlock exists if graph contains a cycle ' To detect deadlocks, system must maintain graph and periodically invoke cycle search algorithm - requires O(n^2) ops

(9.1) Logical Address

Virtual Address Address generated by the CPU

(9.4) Hashed Page Table Algorithm

Virtual page number in virtual address hashed into hash table Compared with field 1 in first element in linked list If a match, corresponding page frame in element is used to form physical address If no match, subsequent entries in linked list searched for match

(8.7) Deadlock Detection with Several Instances of Resource Type

Wait-for graph not applicable Needs Available, Allocation, and Request 1. Find index not finished with request <= available. If none, go to 3 2. available += allocation Finish = true Go back to 1 3. If finish is false for some i, system is deadlocked. Requires O(n^2) ops

(6.6) Semaphore w/ No Busy Wait

Waiting Queue for each semaphore, has a value and pointer to next record block (sleep)- place process invoking on semaphore waiting queue wakeup - remove process from wait and place in ready queue

(6.2) Remainder Section

When processes are in this section they cannot participate in deciding which will enter critical sections

(9.2) External Fragmentation

When processes load and remove from memory, free memory space broken into little pieces Enough total memory space to satisfy a request but the available spaces are not contiguous

(9.3) Valid-Invalid Bit

When set to valid, associated page is in process's logical address space and is legal(valid) When set to invalid, page is not in the address space, trapped. Set by OS to allow/disallow access to page

(9.3) Hardware Implementation of Page Tables

When small (256 entries) stored as high speed registers on CPU When large (2^20 entries) stored in main memory, referenced by Page-Table Base Register (PTBR)

(7.1) Solution to First Readers-Writers Problem

Writers wait for read-write mutex, readers read without waiting unless they're the only one to read, in which case they wait for read-write mutex to make sure writer isn't writing Allows readers to not care if writer is waiting but to wait if writer is writing

(EX 6.10) The compare_and_swap() instruction can be used to design lock-free data structures such as stacks, queues, and lists. The program example shown in Figure E6.18 presents a possible solution to a lock-free stack using CAS instructions, where the stack is represented as a linked list of Node elements with top representing the top of the stack. Is this implementation free from race conditions?

Yes, because each operation, push and pop, are implemented as atomic variables and will be executed atomically without being interrupted.

(EX 6.07) The pseudocode of Figure 6.14 illustrates the basic push() and pop() operations of an array-based stack. Assuming that this algorithm could be used in a concurrent environment, how could the race condition be fixed?

You could implement an atomic variable for top by having a compare_and_swap (CAS approach) in which every push and pop call must wait for the lock to be 0 to execute and retrieve/modify top. When each push or pop enters, the CAS call will set the lock to 1 and only one push or pop will be executing at a time, preventing any race conditions

(6.4.2) CAS

compare_and_Swap() instruction Operates on two words atomically, based on swapping content. Returns original value of passed parameter (lock) Sets parameter to passed parameter new value if value == expected Uses locks for mutual exclusion Optimistic - Update variable then check if another thread is updating, try until successful w/o conflict

(EX 6.22) Consider the code example for allocating and releasing processes shown in Figure E6.21. Identify the race conditions(s)

number_of_processes

(7.3) POSIX Named Semaphores

sem_open() used to create passing a string name O_CREAT flag to create if doesn't exist yet 066 for read-write access to other processes init to 1 Multiple processes can easily use a common semaphore as a synchronization mechanism by referrng to name. Subsequent calls to already created semaphore of that name will return the existing one sem_wait() and sem_post() for signal, passing name as param Provided by Linux and macOS


Ensembles d'études connexes

Operating Systems Homework Questions

View Set

Sterile and Nonsterile Compounding

View Set

macro exam 2 study guide chap 8-12

View Set

maternity exam practice questions

View Set

Foundations of Journalism Final 12/8

View Set