Download presentation
Presentation is loading. Please wait.
1
BASIC PRINCIPLES OF SYNCHRONISATION
2
MAIN CONCEPTS SYNCHRONISATION CRITICAL SECTION DEAD LOCK
3
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. 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: how ever,multiple cooperating processes/threads introduce the potential for new synchronisation problems in software implementations such as: dead lock dead lock critical section critical section non deteminacy non deteminacy
4
What is synchronisation 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 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 1.they must perform their actions at almost exactly at the same time exactly at the same time 2.they must synchronize their watches by setting same time 2.they must synchronize their watches by setting same time
5
How syncronisation manifests itself in concurrent software Enter loop ANOTHER COMMAND ? ANOTHER COMMAND EXIT LOOP EXCUTE COMMAND WAIT FOR CHAILD TO TERMINATE YES NO FORK() CODECREATEPROCESS () CODE Unix shell Windows command launch
6
program UNIX While (TRUE){ … //create a process to execute the command if((chPID =fork()) ==0) { if((chPID =fork()) ==0) { //this is the child Execv(command.name,command.argv); } //wait for the child to terminate thisChPID =wait(&stat); } Windows 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…*/} }
7
unix Parent program creates a child process to execute a command,then waits for the child to terminate before reading next command. 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. 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. 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 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 There is concurrency among the parent and all of the child processes
8
Synchronizing multiple threads with shared variable initialize createThread(…) Wait runTime seconds ThreadWork exitterminate FALSE FALSEFALSE runFlag ? truetruetrue
9
Multiple threads Parent thread creates N child threads,each running as an iterative loop. 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. 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 If the RUN FLAG has been set FALSE,then the child terminates
10
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(…); 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. Sleep(k) causes the thread to sleep for k milliseconds. While the child threads work for run time seconds,the parent thread sleeps While the child threads work for run time seconds,the parent thread sleeps
11
CRITICAL SECTION Intersection is shared between two streets Intersection is shared between two streets
12
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. 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 PROGRAM shared double balance; /* shared variable*/ 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
13
Critical section Critical section execution of p1 execution of p2 execution of p1 execution of p2 …… …… load R1,balance load R1,balance load R2,amount load R2,amount timer interrupt timer interrupt …. …. load R1,balance load R1,balance load R2,amount load R2,amount sub R1,R2 sub R1,R2 store R1,balance store R1,balance …… …… add R1,R2 add R1,R2 store R1, balance store R1, balance ….. ….. the problem occurs because of sharing,not because of error in the sequential code the problem occurs because of sharing,not because of error in the sequential code Timer interrupt
14
How to avoid critical section One way of avoiding critical section problem in the program above discussed is by using interrupts. 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 Now we are going to use TRAFFIC LIGHTS at the intersection,those are the interrupts 1.enable interrupts 1.enable interrupts 2.disable interrupts 2.disable interrupts The program will DISABLE interrupts when it entered a critical section and then enable when it finished the critical section The program will DISABLE interrupts when it entered a critical section and then enable when it finished the critical section
15
Program using interrupts Shared double amount,balance; /*shared variables*/ Shared double amount,balance; /*shared variables*/ Program for p1 program for p2 Program for p1 program for p2 disableInterrupts(); disableInterrupts(); disableInterrupts(); disableInterrupts(); Balance=balance+amount; balance=balance- amount; Balance=balance+amount; balance=balance- amount; enableInterrupts(); enableInterrupts(); 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(). 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. 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.
16
Program using a lock In order to coordinate their processes between p1,p2. SHARED FLAG, LOCK is used instead of Interrupts. In order to coordinate their processes between p1,p2. SHARED FLAG, LOCK is used instead of Interrupts. shared boolean lock = false; /shared variables*/ shared boolean lock = false; /shared variables*/ shared double amount,balance /*shared variables*/ shared double amount,balance /*shared variables*/ program for p1 program for p2 program for p1 program for p2 ……. …….. ……. …….. /*Acquire lock */ /*Acquire lock*/ /*Acquire lock */ /*Acquire lock*/ while (lock) {null;}; while(lock) {null;}; while (lock) {null;}; while(lock) {null;}; lock = TRUE; lock =TRUE; lock = TRUE; lock =TRUE; /*execute crit section */ /*execute crit section */ /*execute crit section */ /*execute crit section */ balance = balance+amount; balance = balance_amount; balance = balance+amount; balance = balance_amount; /*release lock */ /*release lock*/ /*release lock */ /*release lock*/ lock = FALSE lock = FALSE; lock = FALSE lock = FALSE; ……. ……. ……. …….
17
Suppose p1 is interrrupted during the execution of the statement Suppose p1 is interrrupted during the execution of the statement balance = balance +amount; balance = balance +amount; after having set lock to TRUE. 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. 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 entire time slice is spent executing WHILE stament.
18
The execution pattern P2 EXECUTION P1 EXECUTION lock = TRUE interrupt Lock=FALSE interrupt BLOCKED AT WHILE
19
The problem is that manipulating the lock variable is,itself critical section 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 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 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. 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.
20
Since enable interrupts and disable interrupts are privileged,we can define two new operating system calls Since enable interrupts and disable interrupts are privileged,we can define two new operating system calls 1. enter() 1. enter() 2. exit() 2. exit() In this case interrupts are disabled only by operating system code (while lock is being manipulated ) 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 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 It will never be delayed for more than the time taken to execute the while statement
21
enter (lock){ exit(lock) { disableInterrupts(); disableInterrupts(); disableInterrupts(); disableInterrupts(); /* wait for lock */ lock=FALSE; /* wait for lock */ lock=FALSE; while (lock)\{ enableInterrupts(); while (lock)\{ enableInterrupts(); /* let interrupt occur */ /* let interrupt occur */ enableInterrupts(); enableInterrupts(); disableInterrupts(); } disableInterrupts(); } lock=TRUE; lock=TRUE; enableInterrupts(); enableInterrupts();} Lock manipulation as a critical section
22
The same program using enter() and exit() functions The same program using enter() and exit() functions shared double amount, balance; /* shared variables */ shared double amount, balance; /* shared variables */ shared int lock = FALSE; /* synchronization variable */ shared int lock = FALSE; /* synchronization variable */ program for p1 program for p2 program for p1 program for p2 enter (lock); enter (lock); enter (lock); enter (lock); balance= balance + amount; balance= balance – amount; balance= balance + amount; balance= balance – amount; exit (lock); exit (lock); exit (lock); exit (lock);
23
Dead lock The existence of critical sections creates an environment in which a new, subtle problem can occur : 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) 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 So neither process will ever have all its desired resources allocated to it and both will remain in this DEAD LOCK state
24
Shared boolean lock1 =FALSE; /* shared variables*/ Shared boolean lock1 =FALSE; /* shared variables*/ Shared boolean lock2 = FALSE; Shared boolean lock2 = FALSE; Shared list L; Shared list L; program for p1 program for p2 program for p1 program for p2 ……………. …………. ……………. …………. /* enter crit section to /* enter crit section to delete elt from list */ * update length */ delete elt from list */ * update length */ enter (lock1); enter (lock1); enter (lock1); enter (lock1); ; ; ; ; /* exit crtical section */ /* exit critical section */ exit (lock); exit (lock); exit (lock); exit (lock); ; ; ; ; /* enter crit section to /* enter crit section to * update length */ * add elt to list */ * update length */ * add elt to list */ enter (lock2); enter (lock1); enter (lock2); enter (lock1); ; ; ; ; /* exit critical section */ /* exit critical section */ exit (lock2); exit (lock1); exit (lock2); exit (lock1); ………. …………… ………. …………… Multiple shared variables with disabled interrupts
25
Ensuring consistency in related values Shared boolean lock1= FALSE; /* shared variables*/ Shared boolean lock1= FALSE; /* shared variables*/ Shared boolean lock2=FALSE; Shared boolean lock2=FALSE; Program for p1 praogran for p2 Program for p1 praogran for p2 ………… ……………. ………… ……………. /* enter crit section to /* enter crit section to * delete elt from list */ * update length */ * delete elt from list */ * update length */ enter (lock1); enter (lock2); enter (lock1); enter (lock2); ; ; ; ; ; ; /* enter crit section to /* enter crit section to * update length */ * add elt to list */ * update length */ * add elt to list */ enter (lock2); enter (lock1); enter (lock2); enter (lock1); ; ; ; ; /* exit both crit sections */ /* exit both crit sections */ exit (lock1); exit (lock2); exit (lock1); exit (lock2); exit (lock2); exit (lock1); exit (lock2); exit (lock1); …….. ………….. …….. …………..
26
SYNCHRONIZATION USING FORK(),JOIN(), AND QUIT() FORK JOIN SYNCHRONIZE QUIT A A A A A A A B B B B B B BC (INITIAL PROCESS) WAITING SYNCHRONIZATION CREATE AND DESTROY
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.