Chapter 5 Operating Systems

Lakukan tugas rumah & ujian kamu dengan baik sekarang menggunakan Quizwiz!

How does Eisenburg & McGuire algorithm prove mutual exclusion

A process enters into the critical section only when flag variable of any other process is not set to "in_cs" The process sets its flag to in_cs before entering the critical section Then the process checks if there is any other process whose flag variable is in_cs. Thus, it can be ensured that no two processes will be in the critical section at once.

What does the following code do? do { acquire lock critical section release lock remainder section } while (TRUE);

A process is acquiring a lock

What are condition variables?

A condition variable is basically a container of threads that are waiting for a certain condition

hence, spin locks are useful for _____ locks.

short

If lock is held by a thread currently running on the CPU, the thread _____ until the lock becomes _______

spins, available.

What is being implemented here? do { key = TRUE; while(key==TRUE) Swap(&lock,&key); //critical section lock = FALSE } while(TRUE);

swap() instruction

Can busy waiting be avoided altogether?

yes

What do monitors provide to threads regarding their exclusive accesses?

Monitors provide a mechanism for threads to temporarily give up exclusive access in order to wait for some condition to be met, before regaining exclusive access and resuming their task.

Describe mutex.

Mutex: 1. It is a synchronization mechanism. 2. It is an environment for applying boundaries on access to a resource. 3. It works with environment where many thread of execution works together.

What is a semaphore?

A (non-negative) integer representing the quantity of a resource available

Describe the circumstances under which they use semaphores (Explain why the mechanism is needed) in linux

When a process requires a lock for long duration, it must use semaphores.

When is the wait() operation performed?

When a process wants a resource, then it performs the wait() operation to increment the semaphore S value.

What is starvation?

When low-priority jobs never get run because there are always higher priority jobs running. Also in concurrent computing where a process is perpetually denied necessary resources to process its work.[1] Starvation may be caused by errors in a scheduling or mutual exclusion algorithm, but can also be caused by resource leaks

The deadlock issue

When more than one process wants to enter the critical section at the same time, then a deadlock will occur. To avoid the deadlock, only one process must execute its critical section at a time

The timeline of processes P1 and P2 ?

T0: P1 determines that value of S = 1. T1: P2 determines that value of S = 1. T2: P1 decrements S by 1 and enters the critical section. T3: P2 decrements S by 1 and enters the critical section.

What is being implemented here? do { While(TestAndSet(&lock)); lock = FALSE; } while(TRUE);

TestAndSet() instruction

What is a lock meant to do?

A lock is designed to enforce a mutual exclusion concurrency control policy.

What is a f.u.c.k.i.n.g. spinlock?

A lock that makes threads of a process enter into wait condition, while searching for availability of desired lock

What do monitors consist of?

A monitor consists of a mutex (lock) object and condition variables.

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

A process must do both the operations concurrently to achieve the mutual exclusion. If the number of available resources S is 1, and the two processes P1 and P2 executes only wait () operation concurrently, then the mutual exclusion may be violated. When P1 and P2 determine that value of S is 1 and both performs wait() operation, then P1 decrements S by 1 and enters critical section and P2 decrements S by 1 and enters the critical section. But the processes are not performing the signal() operation after the wait() operation. This condition leads to the violation of the mutual exclusion.

Example of multiple locking

A process p1 acquired a lock to critical section for its execution. At the same time, a process p2 wants to use the critical section but, it will be locked by some other process. p2 must wait until the lock releases by p1. here, no chance to occurrence of inconsistency

What is a binary semaphore?

A semaphore that only takes the value 0 or 1 hence its functional operations are similar to that of a mutex lock

What is a race condition?

A situation where several processes access and manipulate the same data concurrently and the outcome of the execution depends on the particular order in which the access takes place.

Would an atomic integer or a basic mutex lock be more effective in helping a multithreaded web server keep track of the number of hits, or requests, it services?

An atomic integer would be more efficient of a method to apply for hits on a website, as the atomic integer updates the variable allocated for hits to ensure the system does not exceed the race condition on hits.

How does Eisenburg & McGuire algorithm prove Progress requirement?

Assume that more than one process sets its flag variable to in_cs at the same time. After each sets its flag, they check if there is any other process whose flag variable is in_cs. After realizing that there is another in_cs process, each process move to the next iteration of the while() loop. Then, they set their flag variable to in_want. Among all the processes, the process whose index value is near to turn variable will set its flag variable to in_cs. At each iteration, the index of the processes who set their variable to in_cs move closer to turn. Thus, it is ensured that only once process can set its flag variable to in_cs.

Describe busy waiting.

Busy Waiting: 1. Suppose a process is executing in its critical section. 2. If any process wanted to get into its critical section, then it goes into the continuous loop in the entry section.

What two functions can overcome busy waiting?

Busy waiting can be overcome by a wait() and signal() semaphore operation. When a process executes the wait() operation and find that the semaphore value is not positive, it must wait. however, rather than engaging in busy waiting, process can block itself.

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

Busy waiting wastes CPU cycles that some other process might be able to use productively. This type of semaphore is called spin lock because the process "spins" while waiting for the lock. When locks are expected to be held for short times, spin locks are useful, they are employed on multiprocessor system where one thread can "spin" on one process while another thread performs its critical section on another processor.

What is busy waiting?

Busy-waiting, busy-looping or spinning is a technique in which a process repeatedly checks to see if a condition is true, such as whether keyboard input or a lock is available.

OS defines two types of semaphores. name them

Counting semaphore, binary semaphore

To avoid busy waiting, what must be done

Create resources that enforce mutual exclusion and make them accessible by through the OS. Processes waiting to enter a critical section will be put in the blocked date, just like they were waiting for I/O, so that other processes can continue doing useful work.

What does busy waiting do to the CPU cycles?

Degrades the CPU cycles

Explain why interrupts are not appropriate for implementing synchronization primitives in multiprocessor systems.

Disabling interrupts on every processor can be a difficult task and furthermore can seriously diminish performance. Disabling interrupts on a multiprocessor can be time consuming, as the message is passed to all the processors. This message passing delays entry into each critical section and system efficiency decreases. Also, consider the effect on a systems clock, if the clock is kept updated by interrupts.

What is bounded waiting?

If a thread wishes to enter a critical section, then there exists a bound on the number of other threads that may enter the critical section before that thread does

Explain why implementing synchronization primitives by disabling interrupts is not appropriate in single-processor systems if the synchronization primitives are to be used in user-level programs.

If a user-level program is given the ability to disable interrupts, then it can disable the timer interrupt and prevent context switching from taking place, thereby allowing it to use the processor without letting other processes to execute.

Describe the circumstances under which they use spin-locks (Explain why the mechanism is needed) in Solaris

If lock is held by a thread currently running on the CPU, the thread spins until the lock becomes available.

How is the systems clock affected by disabling interrupts?

If the interrupts need to be updated by the system clock, then the system clock is affected in updating the clock cycles by frequent interrupts having occurred.

How does Dekker's solution solve progress?

If two processes want toe execute their critical section at the same time, the process in the remainder section decides which process can enter into the critical section.

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).

If we have a mutex lock in the given program, with acquire() and release() operations, the programmer needs to place the two operations at the very first and end of a function call respectively. The acquire() function should be placed in the beginning of function call. Whereas release() operation call should be placed just before the end of the function.

Disabling interrupts frequently can affect the system's clock. Explain why this can occur and how such effects can be minimized. (explain multiprocessor)

In case of multiprocessor,single processor tactics are not a feasible solution because disabling interrupts on a multiprocessor is most time consuming as the message is passed to all processors. Each time, a process needs to acquire a lock before entering the critical section.

what is deadlock?

In concurrent computing, a deadlock is a state in which each member of a group is waiting for another member, including itself, to take action, such as sending a message or more commonly releasing a lock

What is progress (liveness)?

In concurrent computing, liveness refers to a set of properties of concurrent systems, that require a system to make progress despite the fact that its concurrently executing components ("processes") may have to "take turns" in critical sections, parts of the program that cannot be simultaneously run by multiple processes.[1] Liveness guarantees are important properties in operating systems and distributed systems.

What are monitors and what are they used for

In concurrent programming, a monitor is a synchronization construct that allows threads to have both mutual exclusion and the ability to wait (block) for a certain condition to become true. Monitors also have a mechanism for signaling other threads that their condition has been met.

What is a critical section (lol)

In concurrent programming, concurrent accesses to shared resources can lead to unexpected or erroneous behavior, so parts of the program where the shared resource is accessed are protected. This protected section is the critical section or critical region.

Assume that a system has multiple processing cores. For each of the following scenarios, describe which is a better locking mechanism—a spinlock or a mutex lock where waiting processes sleep while waiting for the lock to become available: • The lock is to be held for a short duration.

In first condition, the lock is to be held for a short duration and hence "spinlock" will be the best option for the processes. As the spinlock mechanism locks the threads for shortest possible time and will not go into long looped mechanisms.

How does the signal() operation associated with monitors differ from the corresponding operation defined for semaphores?

In monitor x, signal() operation resumes exactly one suspended process. if not process is suspended, then the signal() operation has no effect; that is the state of x is the same as if the operation had never been executed contrast this operation with the signal() operation associated with semaphores, which always affect the state of the semaphore.

Assume that a system has multiple processing cores. For each of the following scenarios, describe which is a better locking mechanism—a spinlock or a mutex lock where waiting processes sleep while waiting for the lock to become available: The lock is to be held for a long duration

In second condition, the lock is to be held for a long duration and hence in this scenario mutex locks would be the best choice. As mutex locks may sometimes the undergo long time duration and are far apart from spinlocks short duration holds.

How does Dekker's solution solve Bounded waiting?

In the algorithm, one process updates the value of the variable "turn" of the other process. if the process P(i) wants to enter its critical section, then flag(i) = true. After completion of the P(i) execution, it sets the turn variable to j and allows the process P(j) to execute its critical section. Hence, both processes need not wait for indefinite time.

Disabling interrupts frequently can affect the system's clock. Explain why this can occur and how such effects can be minimized. (explain Single processor)

In the case of a single processor, critical section problems are often solved by preventing interrupts while shared variable is being modified.

Assume that a system has multiple processing cores. For each of the following scenarios, describe which is a better locking mechanism—a spinlock or a mutex lock where waiting processes sleep while waiting for the lock to become available: A thread may be put to sleep while holding the lock.

In third condition, the thread may be put to sleep while holding the lock; and hence once again a mutex lock would be the better one among two available lock choices. the main reason behind this is that spinlocks do not allow threads to go in sleep mode while holding the lock.

What is noticeable about this piece of code?

In this piece of code we can see a "do-while" condition. Here, the wait function lies under critical section of mutual exclusion; whereas signal() function lies under the remainder section of the same.

The signal() operation does what to the value of the semaphore?

Increases the value of semaphore S. When the process does not need the resource anymore, then it performs the signal() operation to increment the semaphore S value.

What does the message passing technique do to critical sections?

It delays getting entry into critical section. Therefore, system efficiency decreases.

What is signal() used for

It is a system call or a software interrupt generated by the operating system and sent to the process with some number. That number is called signal ID. The signal is generated when the process is aborted, quit, trace, or hang while the process is executing.

Describe two kernel data structures in which race conditions are possible.

Kernel process table: The kernel table also triggers a race for two processes created at the same time for allocation of space in the kernel table. Scheduling Queue: The scheduling queues in the kernel data structure, receive the process. One process in queue has been waiting for the I/O resources, and the resource is free now. The resource is allocated to the process. Second process waiting for the same I/O resource is still pending in the queue. These two processes in the queue are in a race with each other for the allocation of the I/O resources to move to the runnable queue for execution. Hence, processes create a race condition in the runnable queue.

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

Let the 'n' number of processes be described as "pi". Now, for the mutual exclusion between these n processes we take a semaphore mutex which is initialized to '1'. hence, each Pi process would undergo the below condition: do { wait(mutex); //critical section signal(mutex); //remainder section } while(true);

Describe the circumstances under which they use spin-locks (Explain why the mechanism is needed) in linux

Linux uses Spin locks only for short duration processes.

Describe the circumstances under which they use mutex locks (Explain why the mechanism is needed) in linux

Linux uses mutex_lock() and mutex_unlock() function to acquire and release the locks.

What are locks used to do to interrupts in a multi-threaded system?

Locks are basically used to destroy interrupts in multi-threaded systems. Locks prevent threads from entering a deadlock state.

What is the dining philosophers problem?

No two philosophers can use the same fork at the same time (mutual exclusion), No philosopher must starve to death (avoid deadlock and starvation)

What is the test_and_set() function used for?

Performs tow operations individually and cannot be interrupted in the middle of the process execution. this function used for handling the synchronization problem of two processes. it takes a shared lock variable as an input parameter which is either 0 (unlock) or 1 (lock)

Give an example (case) on the use of multiple locking systems

Processes perform many operations like read, write, update on the critical section. If OS doesn't use any locking mechanism, every process uses the critical section and will change the code & it will result in inconsistency. Multiple locking mitigates this

What is the meaning of the term "busy waiting"?

Processes waiting to enter the critical section use the CPU to keep checking if they can enter their critical section. The act of using the CPU to repeatedly check for entry to the critical section is called busy waiting.

Describe Semaphore

Semaphore: 1.It is a variable 2.It is used to control access. 3. It controls access of common resources. 4. It works in multiple processes where OS support multiprogramming.

Servers can be designed to limit the number of open connections. For example, a server may wish to have only N socket connections at any point in time. As soon as N connections are made, the server will not accept another incoming connection until an existing connection is released. Explain how semaphores can be used by a server to limit the number of concurrent connections.

Semaphores are defined as interprocess communication. -it is used to control and monitor the open socket connections. - it allows the process to create the open socket connections that communicate with the other processes. -It describes the open connection's existence and properties among the sockets. -The server deals with each connection's existence and properties among the sockets. -The server deals with each connection via semaphore communication, it instructs new connections are done until the limit is reached. -It makes the individual processes exit to make a way to new connections. Here, the acquire() function is called if the connection is accepted. the release() function is called if the connection is released. The repeated number of calls to acquire () will block the existing connection, where the number of allowable socket connections to the system is reached. The release() method is invoked where the existing connection is terminated. (IGNORE THIS CARD IF NEED BE)

The linux kernel has a policy that a process cannot hold a spinlock while attempting to acquire a semaphore. Explain why this policy is in place. (couple of reasons)

Semaphores are sleeping lock in Linux, therefore, if a task wants to retrieve the semaphore which is already help then the task should be put on the waiting queue to sleep. Spinlocks are just another form of interrupt disabler and hence they cannot go in a sleeping state. As the wait duration is usually very small, no thread goes into sleep mode. Rather, the threads remain active but still do not perform and useful work. As the wait time is very small, no process can sleep while holding a spinlock. The process will keep attempting until it gets the lock. Trying to acquire a semaphore variable may take longer time than the wait duration of a spinlock. hence, one cannot use a semaphore while being in a spinlock condition. Sleeping while holding spinlocks may force rest of the threads in the process to enter infinite loops until and unless the thread becomes active again.

Describe the circumstances under which they use adaptive mutex locks (Explain why the mechanism is needed) in solaris

Solaris uses adaptive mutex locks to protect the access of each data item in the critical section. If a data is locked, adaptive mutex lock follows two things

Describe the circumstances under which they use condition variables and semaphores. (Explain why the mechanism is needed) in solaris

Solaris uses condition variables and semaphores for longer code segments.

Describe the circumstances under which they use mutex locks (Explain why the mechanism is needed) in solaris

Solaris uses mutex locks when a thread contains less than 500 instructions

Explain why Windows, Linux, and Solaris implement multiple locking mechanisms.

Solaris, Linux and Windows XP all three OS implements multiple locking mechanism to protect the access of critical section code.

How to minimize interrupts?

Special hardware instructions like TestAndSet() and swap() instructions can be used to test and modify the content of a word or to swap the contents of two words automatically. By using these instructions, the affect can be minimized.

How do mutex and spinlocks differ

Spinglocks can be used for short periods only, they typically do not allow threads to sleep. Whereas a mutex lock is totally opposite of a spinlock and they allow long spinning loops, which means threads are allowed to sleep in the wait queue until the lock gets released by current thread.

How does Eisenburg & McGuire algorithm prove bounded-waiting?

Suppose a process k wants to enter the critical section. Then its flag variable to in_cs. If the index of any process that wants to enter the critical section is not in between turn and k, then it cannot enter the critical section. If the index of any process is between turn and k, then they can enter the critical section and the turn value will come closer to k. So, turn will become k at some point and can enter the critical section. Thus, the condition of bounded-waiting is satisfied.

How does Dekker's solution solve Mutual Exclusion?

The algorithm allows only one process to execute its critical section at a time

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)?

The answer is no. Doing so would not help either, as the race condition occurs in the allocate_process() function. the race condition is initially tested in the "if" condition for the first time. The number of processes is calculated to be equal to 254 at the first test, but due to the loop process it is set to 255 by increment performed by other thread

Describe how the compare_and_swap() instruction can be used to provide mutual exclusion that satisfies the bounded-waiting requirement.

The execution of compare_and_swap() instruction takes place automatically. Declare a global variable named as lock and initialize it to 0. Pass the value 1 to the lock in compare_and_swap() as the first process. The expected value 0 is not equal to the lock that takes it to the critical section. Another process will start to enter its critical section.

Where is the race condition in the following code? #define MAX PROCESSES 255 int number of processes = 0; /* the implementation of fork() calls this function */ int allocate process() { int new pid; if (number of processes == MAX PROCESSES) return -1; else { /* allocate necessary process resources */ ++number of processes; return new pid; } } /* the implementation of exit() calls this function */ void release process() { /* release process resources */ --number of processes; }

The given code illustrates only one race condition; that is on the variable 'number_of_processes'

What is 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.

Which type of processors are spin locks often found in and why?

They are often used in multiprocessor systems where one thread spin while another thread performs its critical section.

What is the main disadvantage of mutual exclusion?

They require all processes to be busy waiting.

The implementation of mutex locks provided in section 5.5 suffers from busy waiting. Describe what changes would be necessary so that a process waiting to acquire a mutex lock would be blocked and placed into a waiting queue until the lock became available.

This might make fundamentally the same of the progressions made in the portrayal of the semaphore. Connected with each mutex lock might a chance to be queue of waiting processes. When a procedure determines those locks are unavailable, they need aid put under those queue. At a transform discharges the lock, it removes and also awakens the procedure for sitting tight procedures.

True or false A lock usually requires a system call for performing context switch which one or two threads in a program tend to sleep.

True

What is the wait() system call used for?

Used to wait for another process to complete its execution in the operating system and it may wait until any of its child processes to exit in the operating system.

Main advantage of a spin lock?

When the process must wait on a lock situation and the context switch takes much time. Therefore, no context switch is required.

Describe the circumstances under which they use mutex locks (Explain why the mechanism is needed) in windows

Windows uses a mutex lock, when the number of threads waits in a queue. Thread acquires mutex lock on a non signaled dispatcher and releases the lock on a signed dispatcher.

Describe the circumstances under which they use spin-locks (Explain why the mechanism is needed) in windows

Windows: critical section uses spin lock when a thread waits for another thread to release the lock.

What is a lock?

a lock or mutex (from mutual exclusion) is a synchronization mechanism for enforcing limits on access to a resource in an environment where there are many threads of execution.

What is a resource leak?

a resource leak is a particular type of resource consumption by a computer program where the program does not release resources it has acquired.

What is needed in order for a process to switch to a waiting queue instead of busy waiting?

a semaphore

What does the wait() operation do to the value of a semaphore?

decreases the value of semaphore S until the value of S become 1.


Set pelajaran terkait

Common Issues Associated with the Elbow, Wrist, and Hand

View Set

Chapter 42 Drugs for Bowel Disorders & GI conditions

View Set

Comprehensive Physical Assessment of a Child

View Set

DI 251 Advanced Abdomen and Small Parts III

View Set

Anatomy and Physiology Unit 4 Quiz (Tissues)

View Set

CSIS 110 - Chapter 2 /3 LearnSmart

View Set

Anatomy Review: Skeletal muscle Tissue

View Set

APUSH Units 1 and 2 SAQ Practice

View Set