exam 2 chapter 6

Ace your homework & exams now with Quizwiz!

Monitors

- A high-level abstraction (reducing something to its basic characteristics) that provides a convenient and effective mechanism for process synchronization - Abstract data type, internal variables (encapsulated) only accessible by code (method) within the procedure - Only one process may be active within the monitor at a time (providing mutual exclusion) - But not powerful enough to model some synchronization schemes (so, need condition construct)

Critical Section

- A segment of code in which a thread uses resources (such as certain instance variables) that can be used by other threads, but that must not be used by them at the same time - update variable

Deadlock

- AKA "deadly embrace" - two or more processes are waiting indefinitely for an event that can be caused by only one of the waiting processes

Solution: Priority Inversion

- All processes that are accessing resources needed by a higher priority process inherit the higher priority until they are finished with the resources in question - L inherits high priority from H preventing M from preempting it - When L finished with resource, assumes original priority - H runs next

Semaphore Usage

- Can solve various synchronization problems - Can implement a counting semaphore S as a binary semaphore

Peterson's Solution

- Good algorithmic description of solving the problem - Two-process software-based solution - Assume that the load and store machine-language instructions are atomic; that is, cannot be interrupted - The two processes share two variables: int turn; and Boolean flag[2]

semaphore implementation

- Must guarantee that no two processes can execute the wait( ) and signal( ) on the same semaphore at the same time - Thus, the implementation becomes the critical section problem where the wait and signal code are placed in the critical section --> Could now have busy waiting in critical section implementation -----> But implementation code is short -----> Little busy waiting if critical section rarely occupied - Note that applications may spend lots of time in critical sections and therefore this is not a good solution

mutex lock

- Previous solutions are complicated and generally inaccessible to application programmers (accessible in assembly language) - OS designers build software tools to solve CS problem - Simplest is mutex (mutual exclusion) lock Protect a critical section by first acquire() a lock then release() the lock ---> Boolean variable indicating if lock is available or not - Calls to acquire() and release() must be atomic ---> Usually implemented in OS via H/W atomic instructions - But this solution requires busy waiting (good on multiprogramming single-core system? On multicore system?) ---> This lock therefore called a spinlock ---> Does not require a context switch (why not?) ---> Available in some programming languages as a library call (Pthreads)

semaphore

- Synchronization tool that provides more sophisticated ways (than Mutex locks) for processes to synchronize their activities. - Semaphore S - integer variable - Can only be accessed via two indivisible (atomic) operations ---> wait() and signal() Originally called P() and V() Definition of the wait() operation wait(S) { while (S <= 0) ; // busy wait /while the resource is anavailbe S--; //must be executed atomically } //Decrement count of available resources Definition of the signal() operation signal(S) { S++; //Increment count of available resources }

compare_and_swap Instruction

- The operand value is set to new value only if the expression (*value == expected) is true. Regardless, CAS always returns the original value of the variable value. The important characteristic of this instruction is that it is executed atomically. Thus, if two CAS instructions are executed simultaneously (each on a different core), they will be executed sequentially in some arbitrary order - Compare_and_swap can be used to place an item at the head of a queue or delete it. 1. Executed atomically 2. Returns the original value of passed parameter "value" 3. Set the variable "value" to the value of the passed parameter "new_value" but only if "value" =="expected". That is, the swap takes place only under this condition.

exit section

- The section of code following a critical section

Semaphore Implementation with no Busy waiting

- With each semaphore there is an associated waiting queue - Each entry in a waiting queue has two data items: ---> value (of type integer) ---> pointer to next record in the list - Two operations (implemented in OS): ---> sleep - place the process invoking the operation on the appropriate waiting queue; CPU scheduler chooses another process to execute ---> wakeup - remove one of processes in the waiting queue and place it in the ready queue

Race Condition

- causes a concurrent access to shared data which results in data inconsistency --> the outcome of execution depends on order in which the access takes place

Critical Section Problem

-Each process has critical section segment of code ->Process may be changing common variables (previous example), updating table, writing file, the bakery problem, etc. -> When one process in critical section, no other may be in its critical section -Critical section problem is to design protocol to solve this -Each process must ask permission to enter critical section in entry section, may follow critical section with exit section, then remainder section

Solution to Critical Section Problem

1. Mutual Exclusion 2. Progress 3. Bounded Waiting

Bounded Waiting

A bound must exist on the number of times that other processes are allowed to enter their critical sections after a process has made a request to enter its critical section and before that request is granted -> Assume that each process executes at a nonzero speed -> No assumption concerning relative speed of the n processes

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 means that a process is waiting for a condition to be satisfied in a tight loop without relinquishing the processor. Alternatively, a process could wait by relinquishing the processor, and block on a condition and wait to be awakened at some appropriate time in the future. Busy waiting can be avoided but incurs the overhead associated with putting a process to sleep and having to wake it up when the appropriate program state is reached.

Priority Inversion

Scheduling problem when lower-priority process holds a lock needed by higher-priority process - Process H requires a semaphore representing resource S - Semaphore is currently held by process L, causing H to wait - Process M becomes runnable, preempting process L - M has affected how long H must wait for L to relinquish resource S

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

Spinlocks are not appropriate for single-processor systems because the condition that would break a process out of the spinlock can be obtained only by executing a different process. If the process is not relinquishing the processor, other processes do not get the opportunity to set the program condition required for the first process to make progress. In a multiprocessor system, other processes execute on other processors and thereby modify the program state in order to release the first process from the spinlock.

flag

The flag array is used to indicate if a process is ready to enter the critical section. flag[i] = true implies that process Pi is ready!

Bounded-waiting Mutual Exclusion with test_and_set

The key feature of the above algorithm is that a process blocks on the AND of the critical section being locked and that this process is in the waiting to enter state. When exiting a critical section, the exiting process does not just unlock the critical section and let the other processes have a free-for-all trying to get in. Rather it first looks in an orderly progression (starting with the next process on the list) for a process that has been waiting, and if it finds one, then it releases that particular process from its waiting state, without unlocking the critical section, thereby allowing a specific process into the critical section while continuing to block all the others. Only if there are no other processes currently waiting is the general lock removed, allowing the next process to come along to access to the critical section.

entry section

The section of code implementing a request for permission to enter a critical section.

we mentioned that disabling interrupts frequently can affect the system's clock.

The system clock is updated at every clock interrupt. If interrupts were disabled—particularly for a long period of time—it is possible the system clock could easily lose the correct time. The system clock is also used for scheduling purposes. For example, the time quantum for a process is expressed as a number of clock ticks. At every clock interrupt, the scheduler determines if the time quantum for the currently running process has expired. If clock interrupts were disabled, the scheduler could not accurately assign time quantums. This effect can be minimized by disabling clock interrupts for only very short periods.

turn

The variable turn indicates whose turn it is to enter the critical section

Explain why Windows, Linux, and Solaris implement multiple locking mechanisms. Describe the circumstances under which they use spinlocks, mutex locks, semaphores, adaptive mutex locks, and condition variables. In each case, explain why the mechanism is needed.

These operating systems provide different locking mechanisms depending on the application developers' needs. Spinlocks are useful for multiprocessor systems where a thread can run in a busy-loop (for a short period of time) rather than incurring the overhead of being put in a sleep queue. Mutexes are useful for locking resources. Solaris 2 uses adaptive mutexes, meaning that the mutex is implemented with a spin lock on multiprocessor machines. Semaphores and condition variables are more appropriate tools for synchronization when a resource must be held for a long period of time, since spinning is inefficient for a long duration.

strongly ordered

Where a memory modification on one processor is immediately visible to all other processors.

preemptive

allows preemption of kernel-mode process

Synchronization Hardware: Uniprocessors

could disable interrupts ->Currently running code would execute without preemption ->Generally too inefficient on multiprocessor systems

test_and_set instruction

if two test and set() instructions are executed simultaneously (each on a different core), they will be executed sequentially in some arbitrary order. If the machine supports the test and set() instruction, then we can implement mutual exclusion by declaring a boolean variable lock, initialized to false 1. Executed atomically (serializes processing across processors) 2. Returns the original value of passed parameter 3. Set the new value of passed parameter to "TRUE". Definition: boolean test_and_set (boolean *target) { boolean rv = *target; /* set rv to target's value *target = TRUE; /* set target to true return rv: /* return target }

Starvation

indefinite blocking. A process may never be removed from the semaphore queue in which it is suspended

Binary semaphore

integer value can range only between 0 and 1 --> Same as a mutex lock

Counting Semaphore

integer value can range over an unrestricted domain --> Useful for controlling access to a finite number of resources

Atomic

non-interruptible

Locking

protecting critical regions through the use of locks

memory barriers or memory fences

provide instructions that can force any changes in memory to be propagated to all other processors, thereby ensuring that memory modifications are visible to threads running on other processors.

Modern machines

provide special atomic hardware instructions - Either test memory word and set value - Or swap contents of two memory words

remainder section

release exclusive access

Non-preemptive (cooperative)

runs until exits kernel mode, blocks, or voluntarily yields CPU -> Accomplished by preventing interrupts -> Essentially free of race conditions in kernel mode since it will complete its logic without interruption (on a uniprocessor). On an SMP, a process running on a different processor could interfere by executing simultaneously in the critical section.

Weakly ordered

where a memory modification of one processor may not be immediately visible to all other processors.

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

A wait operation atomically decrements the value associated with a semaphore. If two wait operations are executed on a semaphore when its value is 1, if the two operations are not performed atomically, then it is possible that both operations might proceed to decrement the semaphore value, thereby violating mutual exclusion.

memory model

How a computer architecture determines what memory guarantees it will provide to an application program

progress

If no process is executing in its critical section and there exist some processes that wish to enter their critical sections, then the selection of the processes that will enter the critical section next cannot be postponed indefinitely

Mutual Exclusion

If process Pi is executing in its critical section, then no other processes can be executing in their critical sections


Related study sets

Leadership Final: Quiz Questions

View Set

e: Foster Care, Adoption, and Day Care

View Set

Endocrine Homeostasis - DKA, HHS, DI, SIADH (NCLEX 3000)

View Set

SCM Chapter 17: Sustainability and the Supply Chain

View Set

9.14 - Actions to be Taken by the Administrator

View Set

Discovering Geometry - Conjectures

View Set