110 likes | 272 Views
Operating Systems 10/2/2014. 11CS10031 Naman Dasot. Topics Covered: Producer Consumer Problem Race Condition Critical Section Problem. Producer Consumer Pg-1. Producer Consumer Problem. It is a classic example of a multi process synchronization problem
E N D
Operating Systems 10/2/2014 11CS10031Naman Dasot Topics Covered: Producer Consumer Problem Race Condition Critical Section Problem
Producer Consumer Pg-1 Producer Consumer Problem • It is a classic example of a multi process synchronization problem • Both Processes share common fixed-size buffer, used as a queue • The producer's job is to generate a piece of data, put it into the buffer. • Consumer’s job is to consume the piece of data produced by producer, from the buffer. • Consumer should not consume from empty buffer and producer should not produce when buffer is full.
Race Condition Pg-1 Race Condition • It is a behavior of system when output is dependent on the sequence or timing of other uncontrollable event. • Two or more processes are reading or writing some shared data then the final result depends on who runs precisely when.
Critical Section Problem Pg-1 Critical Section Problem • Critical Section: Part of program where shared memory is accessed. Some synchronization technique must be used at the entry and exit of critical section. • Mutual exclusion: • Prohibit more than one process from reading and writing the shared data at the same time
Critical Section Problem Pg-2 Critical Section Problem • Consider system of n processes {p0, p1, … pn-1} • Each process has critical section segment of code • Process may be changing common variables, updating table, writing file, 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
Critical Section Problem Pg-3 Critical Section Problem • Entry Section • Ensures safe entry to the critical section. • Critical Section • Uses/modifies shared variable • Exit Section • Informs other processes that the shared variable is now accessible. do { Entry Section critical section Exit Section remainder section } while(true)
Critical Section Problem Pg-4 Solution To Critical Section Problem • Mutual Exclusion: • P0 and P1 can never be in the critical section at the same time Four conditions to provide mutual Exclusion: • No two processes simultaneously in critical region. • No assumptions made about speeds or number of CPUs • No process running outside its critical region may block another process. • No process must wait forever to enter its critical region.
Critical Section Problem Pg-5 Solution To Critical Section Problem • Progress • If no process is executing in its critical section and some processes wish to enter their critical sections, then only those processes that are not executing in their remainder sections can participate in making the decision as to which process will enter its critical section next. This selection cannot be postponed indefinitely.
Critical Section Problem Pg-6 Solution To Critical Section Problem • Bounded waiting • Bounded waiting means that "there exists a bound or limit 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"
Critical Section Problem Pg-7 Critical Section Problem Mutual exclusion using critical regions
Race condition for Kernel Pg-1 Race Condition for kernel processes • Several Kernel processes try to access shared data structures like • - File descriptor • - Process list (ready queue etc.) • There can be two solutions: • Disable Interrupt : Non-Preemptive • Process cannot be interrupted till its completion. • Busy Waiting: Using lock variable • 0 -> CS is available • 1 -> CS is occupied • Problem: Lock is itself shared