Process Synchronization II

Pataasin ang iyong marka sa homework at exams ngayon gamit ang Quizwiz!

Implementing a monitor using semaphores As an alternative to a mutex, processes actively in a monitor may __________

"pause" by signaling mutex and instead waiting on next. next_count is number of waiting processes.

Pthreads mutex syntax

#include <pthread.h> pthread_mutex_t mutex; // create the mutex lock pthread_mutex_init(&mutex, NULL); // acquire the mutex lock pthread_mutex_lock(&mutex); // critical section // release the mutex lock pthread_mutex_unlock(&mutex);

Pthreads semaphore syntax

#include <semaphore.h> sem_t sem; // Create the semaphore and set it to 1 sem_init(&sem, 0, 1); // acquire the semaphore sem_wait(&sem); // critical section // release the semaphore sem_post(&sem);

Semaphores select the appropriate synchronization tool: mutex, semaphore, or none. (a) Sending an email to an email server. (b) Managing a file sharing client's capped connections to peers. (c) A printer connected to a network. (d) Managing a database that can be read by multi-clients.

(a) None. (b) A semaphore. (c) A mutex. (d) A mutex.

explain the errrors in the code that cause the output NOT to be -10. hint: there are 2 errors

- on pthread_create(), the last parameter &changeVal should be changeVal. We should not be passing by reference, we want to pass the value itself which will be dereferenced in function - the pthread_join should happen before the for loop otherwise it will assign the change in min only after having assigned the min value prior to -10

Semaphore priority inversion

A scheduling challenge arising when a higher-priority process needs to read or modify kernel data that are currently being accessed by a lower-priority process.

Semaphore

An integer variable that, apart from initialization, is accessed only through two standard atomic operations: wait() and signal().

Linux There are various mechanisms in the Linux kernel for providing low-level synchronization:

Atomic integer operations (arch/*/atomic.h) Spinlocks Mutex support (kernel/locking/mutex.c)

consider the monitor based solution we had for the dining philosophers problem: how would the functionality of this change if the commented line (THIS LINE) were removed? Justify your answer

Commenting this line out implies that when a philosopher starts eating they never end up putting their chopsticks back down even after they're done eating. This means that up to two philosophers will be able to start eating but once other philosophers try to pick up two chopsticks they will get stuck waiting as the other philosophers try to pick up two chopsticks they will get stuck waiting as the other philosophers never returned them to the table.

Synchronization Examples Synchronization related issues that we have covered so far (readers-writers, critical section, bounded buffer) are issues that can be problematic if not properly dealt with. Consider a functional programming language as opposed to a procedural one like C; would you have to deal with these problems in the same manner or at all? Justfy your answer for all three of the previously mentioned issues.

No, the aforementioned issues in a functional programming language wouldn't exist because they all are state dependent. Functional programming explicitly prohibits changing state, therefore these issues wouldn't carry over. The readers-writers problem goes away because reading data in parallel is ne and writing can never be an issue. The bounded buffer problem isn't an issue anymore because you wouldn't be able to write to a buffer in such an instance. Finally the critical section problem would cease to be an issue as having only non-mutable data means that it shouldn't be possible to have changing state during the execution of a separate process which would otherwise be problematic.

Semaphores The provided code is attempting to guard the data resource so that only one thread can access a row of the 2D data array at a time. Is the semaphore properly guarding this resource as is? If not, how could you x this problem? Justify your answer with regard to how a semaphore guards a resource and/or critical section.

No, the semaphore in its current state will only ensure that no more than three threads can touch the data array at a time. Because you are accessing data with a global variable and then incrementing it, this becomes a critical section problem. The semaphore allows for multiple threads to run on this crtitical section, and thus mutual exlusion is broken. To x this issue you would need to either limit the semaphore's initial value to 1 (so it essentially becomes a mutex lock), or add a mutex lock around the critical section. Only then would the program work as intended.

Solving the critical section ______ also supports marking something as a critical region.

OpenMP

consider the following fragment of code which uses a semaphore to solve the reader-writer problem: explain how the program would act differently if the indicated line was commented out

Readers would read from the file even if a writer was writing to it. It would also allow multiple writers to write at the same time (since reader's signal without having to do a wait, so they can free the rw_mutex that some writer has acquired, and let another writer acquire it.

Consider the monitor based solution we had for the dining philosophers problem How would the functionality of this change if the conditional in test() used || instead of &&? Additional question - how would you change the original code, such that if the philosopher wanted to eat, all he/she would eat with one chopstick?

So with the ANDs - functionality is it is checking to make sure left and right neighbors are not eating and the current philosopher is hungry, and then can eat - which means in affect needs both the chopsticks to eat. By changing to ORs, then the current phil will start eating in 1 of these conditions: > current phil is hungry (so guess eats with his/her hands and/or one chopstick. > however if either of the neighbors chopsticks is avail (and even if curr philopher is not hungry) the curr philosopher will start eating. NOT much of a philosopher then is that person ;)?

Classic Problems of Synchronization Consider the following solution to the producer consumer problem from Silberschatz: This solution to the producer consumer problem with a bounded buer requires three semaphores - can the problem be solved with less? Explain.

Sure. We only need to use one semaphore to guard a critical section that is basically the entire producer or consumer. That is, instead of invidually managing resources with a semaphore, we just need a single one to act as a lock which guards empty/full/buffer/full/use. The downside is that the producer and consumer won't be able to execute at the same time, they will have to take turns obtaining the global lock

the bounded buffer problem is a result of producing and consuming work asynchronously at different and/or variable rates. Provided below are implementations for producer and consumer functions which use semaphores to solve the bounded buffer problem. What would happen if the commented lines in the producer function (LINE 1 and LINE 2) were removed? Would the implementation still solve the bounded buffer problem? If so, explain what effects removing these lines would have on the execution of the functions. If not, why are those lines necessary to solving the bounded buffer problem? Justify your answer.

Yes, with the two lines commented out the code would still solve the bounded buffer problem. The issue with removing the two lines is that now the critical section in the producer section is no longer guarded. Now both the producer and consumer threads could run their critical sections in parallel which means that the consumer can try to print an index in data before the producer has finished computing the result. This also introduces the readers-writers problem as you have multiple threads reading and writing data at the same time.

Classic Problems of Synchronization Consider the following code which uses a semaphore to solve the reader-writer problem: Explain how the program would act differently if the indicated line was commented out.

[Suthar] That line (read_count--) decrements when the number of reads after it is done reading. If you never decrement then after the first read operation (read_count will go to 1 and increase forever), but will never get back to zero. Which means the if () right after it will never get executed which would allow the writing to continue - so no more writing could ever be done again. Also this would be bad as eventually the count for read_count would overflow.

Implementing a monitor using semaphores Each conditional variable x has ____________

a semaphore (_sem) and int (_count).

Semaphore A more mutex is about excluding an action (running code in a second process) while a semaphore is about ________

acquiring some unique resource from a set.

Semaphore Let some integer S be called a semaphore value, and define two operations: wait(S) { while (S <= 0); S--; } signal(S) { S++; } Both wait() and signal() should be _______ operations.

atomic

Semaphore priority-inheritance

basically says if a resource is needed by a high priority process, then other users of that resource should inherit the high priority

Semaphore Let some integer S be called a semaphore value, and define two operations: wait(S) { while (S <= 0); S--; } signal(S) { S++; } Wait():

blocks until S is positive

Consider the following partially implemented code: the following code uses a semaphore in attempt to guard the critical section between.... indicate which lines of code you would change

change the initial value of the semaphore to 1 which essentially turns it into a mutex lock which means only one thread will be able to run the critical section at a time. This means that work couldn't be done in parallel anymore which could negatively impact the code's performance.

The Readers -Writers Problem

considering a single source of data that multiple threads may read or write into. Multiple reads are fine, but when a write is happening, the data should be constant.

Conditional Variable

exists within a monitor, and is acted upon by two functions (wait, and signal) so that processes using that monitor continue in certain way.

Solving the critical section Most of our problems come from mutable state - if we do a way with state, then many problems vanish... so, just use _________! All data will be immutable.

functional style programming

Semaphore Let some integer S be called a semaphore value, and define two operations: wait(S) { while (S <= 0); S--; } signal(S) { S++; } Signal():

increases S by 1

Conditional Variable What happens when a process calls wait?

it will block until the conditional is ready.

Semaphore Semaphores end up working a little like ________

malloc/free.

Monitor Concept Assume that we have some class that is a so-called "monitor". Then, the methods in that class all have access to the instance variables, but _______

only one process at a time is allowed to run within the monitor class.

Implementing a monitor using semaphores Each monitor includes a class wide mutex semaphore that ___________

processes must use to access functions.

Solving the critical section Transactional Memory

the basic idea of transactions emerged from databases (finance) where need to ensure that all of set of operations happens at once (i.e., atomically).

Conditional Variable What happens when a process calls signal?

the monitor will transfer control to one of the processes currently waiting on that condition.

Semaphore starvation

the term used to refer to a process that gets "stuck" on a semaphore and cannot proceed.

Semaphore Consider the case of processes P1 , and P2 . Both of these processes need access to resources controlled by semaphores S1 , and S2 . If the processes lock a (different) semaphore in parallel, then both will be ____________

unable to lock the second semaphore and go into deadlock.

Semaphore Say we want to implement a semaphore S where S ϵ I, and want to avoid busy waiting. What is a solution?

use a list to store process and block them instead of looping. typedef struct { int value; struct process *list; } semaphore; wait(semaphore* S) { S->value--; if (S->value < 0) { //add this process to S->list block(); } } signal(semaphore* S) { S->value++; if (S->value <= 0) { //remove a process P from S->list wakeup(P); } }

The Dining-Philosophers Problem

you have 5 philosophers sitting around a table, on the table are five plates of food, and five chopsticks. • Occasionally, a philosopher gets hungry, in which case they must acquire two chopsticks, eat, and then puts those chopsticks down. Clearly there aren't enough chopsticks for everyone to eat at once...


Kaugnay na mga set ng pag-aaral

Survey of World Religion Ch. 9- 13

View Set

NUR 114 Test 2 (Intracranial Regulation)

View Set

Mastering Anatomy/Physiology: Chapter 1- The Human Body

View Set

NUR 108 Ch40: Fluid, Electrolyte, and Acid-Base Balance

View Set

Financial Accounting - Module 14: Introduction to the Statement of Cash Flows

View Set

Managerial Accounting - Chapter 18 - 20

View Set

ch 22/23 Nursing Care of the Child With an Alteration in Mobility/Neuromuscular or Musculoskeletal Disorder

View Set

Health Asessment Chapters 20,26,27

View Set

Famous People of the Progressive Era

View Set