260 likes | 465 Views
BASIC PRINCIPLES OF SYNCHRONISATION. MAIN CONCEPTS. SYNCHRONISATION CRITICAL SECTION DEAD LOCK. Multiprogramming created an environment for concurrent classic processes.it also made available for a programmer to create a group of cooperating processes to work concurrently on a single problem.
E N D
MAIN CONCEPTS • SYNCHRONISATION • CRITICAL SECTION • DEAD LOCK
Multiprogramming created an environment for concurrent classic processes.it also made available for a programmer to create a group of cooperating processes to work concurrently on a single problem. • how ever,multiple cooperating processes/threads introduce the potential for new synchronisation problems in software implementations such as: dead lock critical section non deteminacy
What is synchronisation • Synchronisation it refers to the act of ensuring that independent processes/threads begins to execute a designated block of code at the same logical time • Suppose a team has a plan for attacking a fort in which, each member of the team must be prepared to perform a specific task at exactly same time then 1.they must perform their actions at almost exactly at the same time 2.they must synchronize their watches by setting same time
How syncronisation manifests itself in concurrent software Enter loop ANOTHER COMMAND ANOTHER COMMAND ? EXIT LOOP EXIT LOOP NO NO YES YES FORK() CODE CREATEPROCESS () CODE EXCUTE COMMAND EXCUTE COMMAND EXCUTE COMMAND WAIT FOR CHAILD TO TERMINATE Windows command launch Unix shell
UNIX While (TRUE){ … //create a process to execute the command if((chPID =fork()) ==0) { //this is the child Execv(command.name,command.argv); } //wait for the child to terminate thisChPID =wait(&stat); } Windows While fgets(cmdline,MAX_LINE_LEN,FID)!NULL){ //b.create a new process to execute the command If(!create a new process to execute the command If (!createprocess(Null,cmdline,..) {/* error handling code…*/} } program
unix • Parent program creates a child process to execute a command ,then waits for the child to terminate before reading next command. • When this program processes 5 commands ,then the processes that execute the commands will be created sequentially. • Concurrency between the parent and atmost one child at a time. windows • The parent process creates a process to execute a command ,then immediately goes back to the top of the loop to create another process to execute another command • There is concurrency among the parent and all of the child processes
Synchronizing multiple threads with shared variable Thread Work initialize createThread(…) Wait runTime seconds true true true runFlag ? runFlag ? runFlag ? FALSE FALSE FALSE exit terminate
Multiple threads • Parent thread creates N child threads,each running as an iterative loop. • At the end of the loop,each child checks to see if the RUN FLAG has been set FALSE, if not child iterates through the loop again. • If the RUN FLAG has been set FALSE,then the child terminates
Program for multiple threads Static int runFlag =TRUE Void main(…{ … //for 1 to n For (i=0;i<n;i++) { //create a new thread to executes simulated work createThread(…); } //runtime is the number of seconds that the children should run //sleep while children work… Sleep(runtime*1000); RunFlag =FALSE; … } • Sleep(k) causes the thread to sleep for k milliseconds. • While the child threads work for run time seconds ,the parent thread sleeps
CRITICAL SECTION • Intersection is shared between two streets
In software ,there may be certain parts of the two processes that should not be executed concurrently.such parts of the code are the software critical sections. • PROGRAM shared double balance; /* shared variable*/ Code schema for p1 Code schema for p2 …. …… Balance = balance+amount; Balance=balance-amount; ….. ……. This is a critical section Code schema for p1 code schema for p2 Load R1,balance load R1,balance Load R2,amount load R2,amount Add R1,R2 sub R1,R2 Store R1,balance store R1,balance
Critical section execution of p1 execution of p2 …… load R1,balance load R2,amount timer interrupt …. load R1,balance load R2,amount sub R1,R2 store R1,balance …… add R1,R2 store R1, balance ….. the problem occurs because of sharing ,not because of error in the sequential code Timer interrupt
How to avoid critical section • One way of avoiding critical section problem in the program above discussed is by using interrupts. • Now we are going to use TRAFFIC LIGHTS at the intersection,those are the interrupts 1.enable interrupts 2.disable interrupts • The program will DISABLE interrupts when it entered a critical section and then enable when it finished the critical section
Program using interrupts • Shared double amount,balance; /*shared variables*/ Program for p1 program for p2 disableInterrupts(); disableInterrupts(); Balance=balance+amount; balance=balance- amount; enableInterrupts(); enableInterrupts(); • Suppose a program contained an infinite loop inside its critical section .the interrupts would be permanently disabled.USER MODE programs cannot invoke enableInterrupt() and disable interrupt(). • Now we have to find solution which interrupts are not used by which we can avoid long or infinite compute intervals. And to make the two threads coorinate.
Program using a lock • In order to coordinate their processes between p1,p2. SHARED FLAG, LOCK is used instead of Interrupts . shared boolean lock = false; /shared variables*/ shared double amount ,balance /*shared variables*/ program for p1 program for p2 ……. …….. /*Acquire lock */ /*Acquire lock*/ while (lock) {null;}; while(lock) {null;}; lock = TRUE; lock =TRUE; /*execute crit section */ /*execute crit section */ balance = balance+amount; balance = balance_amount; /*release lock */ /*release lock*/ lock = FALSE lock = FALSE; ……. …….
Suppose p1 is interrrupted during the execution of the statement balance = balance +amount; after having set lock to TRUE. • P2 then begins to execute . p2 will wait to obtain the lock at its while statement. Here clock interrupts p2 and resumes p1.which can complete CRITICAL SECTION. • The entire time slice is spent executing WHILE stament.
The execution pattern BLOCKED AT WHILE P2 EXECUTION P1 EXECUTION lock = TRUE interrupt interrupt Lock=FALSE interrupt
The problem is that manipulating the lock variable is ,itself critical section • U have to solve small critical section problem before solving the original one • The lock critical section will be exactly the same code every time a process wants to enter a critical section • Using this knowledge, we recognize that it would generally be acceptable to disable interrupts while we test and set the lock variable , since it will only 3-4 machine instructions.
Since enable interrupts and disable interrupts are privileged ,we can define two new operating system calls 1. enter() 2. exit() • In this case interrupts are disabled only by operating system code (while lock is being manipulated ) • Even when a process is blocked , waiting to enter its critical section, the interrupts are only disabled for a few instructions at a time • It will never be delayed for more than the time taken to execute the while statement
Lock manipulation as a critical section enter (lock){ exit(lock) { disableInterrupts(); disableInterrupts(); /* wait for lock */ lock=FALSE; while (lock)\{ enableInterrupts(); /* let interrupt occur */ enableInterrupts(); disableInterrupts(); } lock=TRUE; enableInterrupts(); }
The same program using enter() and exit() functions shared double amount , balance; /* shared variables */ shared int lock = FALSE; /* synchronization variable */ program for p1 program for p2 enter (lock); enter (lock); balance= balance + amount; balance= balance – amount; exit (lock); exit (lock);
Dead lock • The existence of critical sections creates an environment in which a new , subtle problem can occur : DEAD LOCK • In software , dead locks occur because one process holds resource (such as file A) while requesting another (such as file B). At the same time , another process holds the second resource (file B) while requesting the first one (file A) • So neither process will ever have all its desired resources allocated to it and both will remain in this DEAD LOCK state
Multiple shared variables with disabled interrupts Shared boolean lock1 =FALSE; /* shared variables*/ Shared boolean lock2 = FALSE; Shared list L; program for p1 program for p2 ……………. …………. /* enter crit section to /* enter crit section to delete elt from list */ * update length */ enter (lock1); enter (lock1); <delete element>; <update length>; /* exit crtical section */ /* exit critical section */ exit (lock); exit (lock); < intermediate computation>; <intermediate computation>; /* enter crit section to /* enter crit section to * update length */ * add elt to list */ enter (lock2); enter (lock1); <update length>; <add element>; /* exit critical section */ /* exit critical section */ exit (lock2); exit (lock1); ………. ……………
Ensuring consistency in related values Shared boolean lock1= FALSE; /* shared variables*/ Shared boolean lock2=FALSE; Program for p1 praogran for p2 ………… ……………. /* enter crit section to /* enter crit section to * delete elt from list */ * update length */ enter (lock1); enter (lock2); <delete element>; <update length>; <intermediate computation>; <intermediate computation> /* enter crit section to /* enter crit section to * update length */ * add elt to list */ enter (lock2); enter (lock1); <update length>; <add length>; /* exit both crit sections */ /* exit both crit sections */ exit (lock1); exit (lock2); exit (lock2); exit (lock1); …….. …………..
SYNCHRONIZATION USING FORK(),JOIN(), AND QUIT() (INITIAL PROCESS) (INITIAL PROCESS) FORK FORK A A FORK FORK A B A B JOIN SYNCHRONIZE B WAITING A B FORK SYNCHRONIZE B C A B JOIN JOIN B A QUIT QUIT CREATE AND DESTROY SYNCHRONIZATION