CS 140 Lecture Notes: Lock ImplementationSlide 1 Uniprocessor Locks, v1 void lock_acquire(struct lock *l) { while (1) { Disable interrupts; if (!l->locked)

Slides:



Advertisements
Similar presentations
Synchronization NOTE to instructors: it is helpful to walk through an example such as readers/writers locks for illustrating the use of condition variables.
Advertisements

LINKED LIST, STACKS AND QUEUES Saras M Srivastava, PGT – Comp. Sc. Kendriya Vidyalaya TengaValley.
For(int i = 1; i
Introduction to Computer Science Robert Sedgewick and Kevin Wayne Recursive Factorial Demo pubic class Factorial {
Data Structure Lecture-5
1 Lab 4 Westfield High School APCS LAB 4 Parameters, apvectors, structs.
1 Data Structures - CSCI 102 CS102 C++ Operator Overloading Prof Tejada.
PROCESS SYNCHRONIZATION READINGS: CHAPTER 5. ISSUES IN COOPERING PROCESSES AND THREADS – DATA SHARING Shared Memory Two or more processes share a part.
LCS Non-Dynamic Version int function lcs (x, y, i, j) begin if (i = 0) or (j = 0) return 0; else if (x[i] = y[j]) return lcs(x, y, i-1, j-1)+1; else return.
Background Concurrent access to shared data can lead to inconsistencies Maintaining data consistency among cooperating processes is critical What is wrong.
Kernel Synchronization
Homework 6 Sarah Diesburg Operating Systems CS 3430.
U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science Emery Berger University of Massachusetts Amherst Operating Systems CMPSCI 377 Lecture.
5.6 Semaphores Semaphores –Software construct that can be used to enforce mutual exclusion –Contains a protected variable Can be accessed only via wait.
5.6.2 Thread Synchronization with Semaphores Semaphores can be used to notify other threads that events have occurred –Producer-consumer relationship Producer.
Synchronization (other solutions …). Announcements Assignment 2 is graded Project 1 is due today.
Synchronization Solutions
U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science Emery Berger University of Massachusetts, Amherst Operating Systems CMPSCI 377 Lecture.
CS 140 Lecture Notes: Lock ImplementationSlide 1 Uniprocessor Locks void lock_acquire(struct lock *l) { intr_disable(); if (!l->locked) { l->locked = 1;
9/8/2015cse synchronization-p1 © Perkins DW Johnson and University of Washington1 Synchronization Part 1 CSE 410, Spring 2008 Computer Systems.
Intro to Scheduling (+ OS sync wrap) David E. Culler CS162 – Operating Systems and Systems Programming Lecture 10 Sept 17, 2014 Reading: A&D HW 2.
U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science Emery Berger University of Massachusetts, Amherst Operating Systems CMPSCI 377 Lecture.
Implementing Synchronization. Synchronization 101 Synchronization constrains the set of possible interleavings: Threads “agree” to stay out of each other’s.
Semaphores and Bounded Buffer. Semaphores  Semaphore is a type of generalized lock –Defined by Dijkstra in the last 60s –Main synchronization primitives.
COMP 111 Threads and concurrency Sept 28, Tufts University Computer Science2 Who is this guy? I am not Prof. Couch Obvious? Sam Guyer New assistant.
COSC 3407: Operating Systems Lecture 7: Implementing Mutual Exclusion.
CS 4284 Systems Capstone Godmar Back Concurrency & Synchronization.
CS 162 Discussion Section Week 2 (9/16 – 9/20). Who am I? Kevin Klues Office Hours:
CS 3204 Operating Systems Godmar Back Lecture 7. 12/12/2015CS 3204 Fall Announcements Project 1 due on Sep 29, 11:59pm Reading: –Read carefully.
CSCI-375 Operating Systems Lecture Note: Many slides and/or pictures in the following are adapted from: slides ©2005 Silberschatz, Galvin, and Gagne Some.
1 Synchronization Threads communicate to ensure consistency If not: race condition (non-deterministic result) Accomplished by synchronization operations.
Print Row Function void PrintRow(float x[ ][4],int i) { int j; for(j=0;j
Implementing Lock. From the Previous Lecture  The “too much milk” example shows that writing concurrent programs directly with load and store instructions.
Int fact (int n) { If (n == 0) return 1; else return n * fact (n – 1); } 5 void main () { Int Sum; : Sum = fact (5); : } Factorial Program Using Recursion.
Implementing Mutual Exclusion Andy Wang Operating Systems COP 4610 / CGS 5765.
CS162 Section 2. True/False A thread needs to own a semaphore, meaning the thread has called semaphore.P(), before it can call semaphore.V() False: Any.
Operating Systems NWEN 301 Lecture 6 More Concurrency.
Homework 3-4 Sarah Diesburg Operating Systems COP 4610.
Auburn University COMP 3500 Introduction to Operating Systems Project 3 – Synchronization Cats and Mice: Implementation.
CSE 120 Principles of Operating
Concurrency & Synchronization
CS703 – Advanced Operating Systems
ICS143A: Principles of Operating Systems Lecture 15: Locking
Introduction to Operating Systems
CS 140 Lecture Notes: Lock Implementation
Lecture 12: Peterson’s Solution and Hardware Support
January 31, 2011 Ion Stoica CS162 Operating Systems and Systems Programming Lecture 4 Synchronization, Atomic operations,
Uniprocessor Lock Implementation
Too Much Milk With Locks
Too Much Milk With Locks
Sarah Diesburg Operating Systems COP 4610
Stack Memory 2 (also called Call Stack)
الاتفاقية الخاصة بحقوق أصحاب الإعاقة ENABLE (الحاجات الخاصة)
Too Much Milk With Locks
Too Much Milk With Locks
Too Much Milk With Locks
UNIVERSITY of WISCONSIN-MADISON Computer Sciences Department
Anthony D. Joseph and John Canny
Implementing Mutual Exclusion
February 6, 2013 Ion Stoica CS162 Operating Systems and Systems Programming Lecture 5 Semaphores, Conditional Variables.
Implementing Mutual Exclusion
September 12, 2012 Ion Stoica CS162 Operating Systems and Systems Programming Lecture 5 Semaphores, Conditional Variables.
Lecture 12: Peterson’s Solution and Hardware Support
Chapter 6: Synchronization Tools
CSE 153 Design of Operating Systems Winter 19
Main() { int fact; fact = Factorial(4); } main fact.
Too Much Milk With Locks
CSCE 206 Lab Structured Programming in C
Radix Sort Sorted
Single-Core Lock Implementation
Presentation transcript:

CS 140 Lecture Notes: Lock ImplementationSlide 1 Uniprocessor Locks, v1 void lock_acquire(struct lock *l) { while (1) { Disable interrupts; if (!l->locked) { l->locked = 1; Enable interrupts; return; } Enable interrupts; } void lock_release(struct lock *l) { Disable interrupts; l->locked = 0; Enable interrupts; } struct lock { int locked; };

CS 140 Lecture Notes: Lock ImplementationSlide 2 Uniprocessor Locks, v2 void lock_acquire(struct lock *l) { Disable interrupts; if (!l->locked) { l->locked = 1; } else { queue_add(&l->q, thread_current()); thread_block(); } Enable interrupts; } void lock_release(struct lock *l) { Disable interrupts; if (queue_empty(&l->q) { l->locked = 0; } else { thread_unblock(queue_remove(&l->q)); } Enable interrupts; } struct lock { int locked; struct queue q; };

CS 140 Lecture Notes: Lock ImplementationSlide 3 Multiprocessor Locks, v1 struct lock { int locked; }; void lock_acquire( struct lock *l) { while (aswap(&l->locked, 1)) { /* Do nothing */ } void lock_release( struct lock *l) { l->locked = 0; }

CS 140 Lecture Notes: Lock ImplementationSlide 4 Multiprocessor Locks, v2 struct lock { int locked; struct queue q; }; void lock_acquire( struct lock *l) { if (aswap(&l->locked, 1) { queue_add(&l->q, thread_current()); thread_block(); } void lock_release( struct lock *l) { if (queue_empty(&l->q) { l->locked = 0; } else { thread_unblock( queue_remove(&l->q)); }

CS 140 Lecture Notes: Lock ImplementationSlide 5 Multiprocessor Locks, v3 struct lock { int locked; struct queue q; int sync; }; void lock_acquire( struct lock *l) { while (aswap(&l->sync, 1) { /* Do nothing */ } if (!l->locked) { l->locked = 1; l->sync = 0; } else { queue_add(&l->q, thread_current()); l->sync = 0; thread_block(); } void lock_release( struct lock *l) { while (aswap(&l->sync, 1) { /* Do nothing */ } if (queue_empty(&l->q) { l->locked = 0; } else { thread_unblock( queue_remove(&l->q)); } l->sync = 0; }

CS 140 Lecture Notes: Lock ImplementationSlide 6 Multiprocessor Locks, v4 struct lock { int locked; struct queue q; int sync; }; void lock_acquire( struct lock *l) { Disable interrupts; while (aswap(&l->sync, 1) { /* Do nothing */ } if (!l->locked) { l->locked = 1; l->sync = 0; } else { queue_add(&l->q, thread_current()); l->sync = 0; thread_block(); } Enable interrupts; } void lock_release( struct lock *l) { Disable interrupts; while (aswap(&l->sync, 1) { /* Do nothing */ } if (queue_empty(&l->q) { l->locked = 0; } else { thread_unblock( queue_remove(&l->q)); } l->sync = 0; Enable interrupts; }

CS 140 Lecture Notes: Lock ImplementationSlide 7

CS 140 Lecture Notes: Lock ImplementationSlide 8 Multiprocessor Locks, v3 struct lock { int locked; struct queue q; }; void lock_acquire( struct lock *l) { Disable interrupts; if (aswap(&l->locked, 1) { queue_add(&l->q, thread_current()); thread_block(); } Enable interrupts; } void lock_release( struct lock *l) { Disable interrupts; if (queue_empty(&l->q) { l->locked = 0; } else { thread_unblock( queue_remove(&l->q)); } Enable interrupts; }

CS 140 Lecture Notes: Lock ImplementationSlide 9 Multiprocessor Locks, v4 struct lock { int locked; struct queue q; int sync; }; void lock_acquire( struct lock *l) { Disable interrupts; while (aswap(&l->sync, 1) { /* Do nothing */ } if (!l->locked) { l->locked = 1; l->sync = 0; } else { queue_add(&l->q, thread_current()); l->sync = 0; thread_block(); } Enable interrupts; } void lock_release( struct lock *l) { Disable interrupts; while (aswap(&l->sync, 1) { /* Do nothing */ } if (queue_empty(&l->q) { l->locked = 0; } else { thread_unblock( queue_remove(&l->q)); } l->sync = 0; Enable interrupts; }