BASIC PRINCIPLES OF SYNCHRONISATION. MAIN CONCEPTS  SYNCHRONISATION  CRITICAL SECTION  DEAD LOCK.

Slides:



Advertisements
Similar presentations
Operating Systems Part III: Process Management (Process Synchronization)
Advertisements

Practice Session 7 Synchronization Liveness Deadlock Starvation Livelock Guarded Methods Model Thread Timing Busy Wait Sleep and Check Wait and Notify.
Concurrency: Mutual Exclusion and Synchronization Chapter 5.
Operating Systems: A Modern Perspective, Chapter 8 Slide 8-1 Copyright © 2004 Pearson Education, Inc.
Slide 2-1 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 2 Using the Operating System 2.
Chapter 6: Process Synchronization
Background Concurrent access to shared data can lead to inconsistencies Maintaining data consistency among cooperating processes is critical What is wrong.
Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9 th Edition Chapter 5: Process Synchronization.
5.1 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts with Java – 8 th Edition Chapter 5: CPU Scheduling.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 6: Process Synchronization.
Process Synchronization. Module 6: Process Synchronization Background The Critical-Section Problem Peterson’s Solution Synchronization Hardware Semaphores.
1 Operating Systems, 122 Practical Session 5, Synchronization 1.
Mutual Exclusion.
CH7 discussion-review Mahmoud Alhabbash. Q1 What is a Race Condition? How could we prevent that? – Race condition is the situation where several processes.
Chapter 6: Process Synchronization. Outline Background Critical-Section Problem Peterson’s Solution Synchronization Hardware Semaphores Classic Problems.
Inter Process Communication:  It is an essential aspect of process management. By allowing processes to communicate with each other: 1.We can synchronize.
A. Frank - P. Weisberg Operating Systems Introduction to Cooperating Processes.
Instructor: Umar KalimNUST Institute of Information Technology Operating Systems Process Synchronization.
Phones OFF Please Processes Parminder Singh Kang Home:
Adopted from and based on Textbook: Operating System Concepts – 8th Edition, by Silberschatz, Galvin and Gagne Updated and Modified by Dr. Abdullah Basuhail,
Fundamentals of Python: From First Programs Through Data Structures
The Critical Section Problem
Object Oriented Analysis & Design SDL Threads. Contents 2  Processes  Thread Concepts  Creating threads  Critical sections  Synchronizing threads.
Concurrency, Mutual Exclusion and Synchronization.
111 © 2002, Cisco Systems, Inc. All rights reserved.
Copyright © 1997 – 2014 Curt Hill Concurrent Execution of Programs An Overview.
ITEC 502 컴퓨터 시스템 및 실습 Chapter 2-1: Process Mi-Jung Choi DPNM Lab. Dept. of CSE, POSTECH.
Operating Systems ECE344 Ashvin Goel ECE University of Toronto Mutual Exclusion.
Internet Software Development Controlling Threads Paul J Krause.
Concurrency: Mutual Exclusion and Synchronization Chapter 5.
Threaded Programming in Python Adapted from Fundamentals of Python: From First Programs Through Data Structures CPE 401 / 601 Computer Network Systems.
1 Program5 Due Friday, March Prog4 user_thread... amount = … invoke delegate transact (amount)... mainThread... Total + = amount … user_thread...
CY2003 Computer Systems Lecture 04 Interprocess Communication.
1 Computer Systems II Introduction to Processes. 2 First Two Major Computer System Evolution Steps Led to the idea of multiprogramming (multiple concurrent.
Discussion Week 2 TA: Kyle Dewey. Overview Concurrency Process level Thread level MIPS - switch.s Project #1.
1 Lecture 8: Concurrency: Mutual Exclusion and Synchronization Advanced Operating System Fall 2012.
13-1 Chapter 13 Concurrency Topics Introduction Introduction to Subprogram-Level Concurrency Semaphores Monitors Message Passing Java Threads C# Threads.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 6: Process Synchronization.
Operating Systems: A Modern Perspective, Chapter 8 Slide 8-1 Copyright © 2004 Pearson Education, Inc.
Synchronization CSCI 3753 Operating Systems Spring 2005 Prof. Rick Han.
CS4315A. Berrached:CMS:UHD1 Process Synchronization Chapter 8.
1 5-High-Performance Embedded Systems using Concurrent Process (cont.)
Concurrency (Threads) Threads allow you to do tasks in parallel. In an unthreaded program, you code is executed procedurally from start to finish. In a.
Chapter 6 Synchronization Dr. Yingwu Zhu. The Problem with Concurrent Execution Concurrent processes (& threads) often access shared data and resources.
Process Synchronization. Concurrency Definition: Two or more processes execute concurrently when they execute different activities on different devices.
Chapter 4 – Thread Concepts
Multithreading / Concurrency
Threaded Programming in Python
Process concept.
Background on the need for Synchronization
Concurrency.
Chapter 4 – Thread Concepts
Chapter 5: Process Synchronization
5-High-Performance Embedded Systems using Concurrent Process (cont.)
143a discussion session week 3
Multithreading.
Multithreading.
UNIVERSITY of WISCONSIN-MADISON Computer Sciences Department
Lecture 2 Part 2 Process Synchronization
Grades.
Chapter 6: Process Synchronization
Threaded Programming in Python
Basic Synchronization
CS510 Operating System Foundations
CS333 Intro to Operating Systems
Chapter 6: Synchronization Tools
Threads and Multithreading
Steve’s Concurrency Slides
Process/Thread Synchronization (Part 2)
Presentation transcript:

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

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

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

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…*/} }

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

Synchronizing multiple threads with shared variable initialize createThread(…) Wait runTime seconds ThreadWork exitterminate FALSE FALSEFALSE runFlag ? truetruetrue

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

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

CRITICAL SECTION Intersection is shared between two streets 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. 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

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

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

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.

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; ……. ……. ……. …….

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.

The execution pattern P2 EXECUTION P1 EXECUTION lock = TRUE interrupt Lock=FALSE interrupt BLOCKED AT WHILE

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.

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

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

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

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

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

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

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