Too Much Milk With Locks

Slides:



Advertisements
Similar presentations
For(int i = 1; i
Advertisements

Data Structure Lecture-5
void count_down (int count) { for(i=count; i>1; i--) printf(" %d\t", count); } printf("A%d\n", count); if(count>1) count_down(count-1); printf("B%d\n",
COS Operating System Assignment 4 (Precept 2) Inter-Process Communication and Process management Fall 2004.
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.
Data Structures Lecture 13: QUEUES Azhar Maqsood NUST Institute of Information Technology (NIIT)
Templated Functions. Overloading vs Templating  Overloaded functions allow multiple functions with the same name.
Chapter 8. Operator Overloading Operator overloading gives the opportunity to redefine C++ Operator overloading refers to redefine C++ operators such.
EC-241 Object-Oriented Programming
C Intro.
Union, bitfield, typedef, enum union nama_u{ }; union nama_u{ struct nama_s byte; }; enum{ }; Tipedef var BYTE.
CS 140 Lecture Notes: ConcurrencySlide 1 Too Much Milk Person A 3:00Arrive home: no milk 3:05Leave for store 3:10Arrive at store 3:15Leave store 3:20Arrive.
Character Input and Output C and Data Structures Baojian Hua
Language Support for Concurrency. 2 Common programming errors Process i P(S) CS P(S) Process j V(S) CS V(S) Process k P(S) CS.
5.6.2 Thread Synchronization with Semaphores Semaphores can be used to notify other threads that events have occurred –Producer-consumer relationship Producer.
1 Midterm Review (overview of fall 2005 midterm) Professor Jennifer Rexford COS 217.
CS 140 Lecture Notes: Lock ImplementationSlide 1 Uniprocessor Locks, v1 void lock_acquire(struct lock *l) { while (1) { Disable interrupts; if (!l->locked)
A. Frank - P. Weisberg Operating Systems Introduction to Cooperating Processes.
Character Input and Output
Pointers Example Use int main() { int *x; int y; int z; y = 10; x = &y; y = 11; *x = 12; z = 15; x = &z; *x = 5; z = 8; printf(“%d %d %d\n”, *x, y, z);
1 Chapter 5 Concurrency. 2 Concurrency 3 4 Mutual Exclusion: Hardware Support Test and Set Instruction boolean testset (int *i) { if (*i == 0) { *i.
Dynamic Allocation and Linked Lists. Dynamic memory allocation in C C uses the functions malloc() and free() to implement dynamic allocation. malloc is.
CS 140 Lecture Notes: Lock ImplementationSlide 1 Uniprocessor Locks void lock_acquire(struct lock *l) { intr_disable(); if (!l->locked) { l->locked = 1;
Semaphores Questions answered in this lecture: Why are semaphores necessary? How are semaphores used for mutual exclusion? How are semaphores used for.
Recursion Examples Fundamentals of CS Case 1: Code /* Recursion: Case 1 */ #include void count (int index); main () { count (0); getchar(); } void count.
CS 3214 Computer Systems Godmar Back Lecture 22. Announcements Project 4 due Nov 10 Exercise 9 due Nov 11 CS 3214 Fall 2010.
Data structures for concurrency 1. Ordinary collections are not thread safe Namespaces System.Collections System.Collections.Generics Classes List, LinkedList,
CSC321 Concurrent Programming: §5 Monitors 1 Section 5 Monitors.
Lec 7aOperating Systems1 Operating Systems Lecture 7a: Linux Memory Manager William M. Mongan.
Lecture 12 CV. Last lecture Controlling interrupts Test and set (atomic exchange) Compare and swap Load linked and store conditional Fetch and add and.
MP2 Discussion Session CS414 Spring 2010 Long Vu 2/16/2010.
Print Row Function void PrintRow(float x[ ][4],int i) { int j; for(j=0;j
1 11/30/05CS150 Introduction to Computer Science 1 Structs.
CS 241 Section Week #5 9/22/11. 2 Topics This Section File I/O Advanced C.
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.
 Review building a complete linked list  List traversal in main ( )  List traversal using a function.
CS 3204 Operating Systems Godmar Back Lecture 10.
CSC321 §8 Implementing FSP Models in Java 1 Section 8 Implementing FSP Models in Java.
REEM ALAMER REVISION FOR C LANGUAGE COURSE. OUTPUTS int main (void) { int C1, C2; int *p1, *p2; C1 = 8; p1 = &C1; C2 = *p1 / 2 + 5; p2 = &C2; printf ("C1.
CS 311/350/550 Semaphores. Semaphores – General Idea Allows two or more concurrent threads to coordinate through signaling/waiting Has four main operations.
Semaphores & Condition Variables
Auburn University COMP 3500 Introduction to Operating Systems Project 3 – Synchronization Cats and Mice: Implementation.
Concurrency 2 CS 2110 – Spring 2016.
CS 3214 Computer Systems Lecture 20 Godmar Back.
Auburn University COMP 3500 Introduction to Operating Systems Project 3 – Synchronization Condition Variables Dr. Xiao.
CS 3214 Computer Systems Lecture 22 Godmar Back.
CS 3214 Introduction to Computer Systems
CS 3214 Computer Systems.
CS 3214 Computer Systems.
Dr. Xiao Qin Auburn University
CS-401 Computer Architecture & Assembly Language Programming
CS 140 Lecture Notes: Lock Implementation
Uniprocessor Lock Implementation
Too Much Milk With Locks
Too Much Milk With Locks
كيــف تكتـب خطـة بحـث سيئـة ؟؟
الدكتـور/ عبدالناصـر محمـد عبدالحميـد
كار همراه با آسودگي و امنيت
CS 140 Lecture Notes: Concurrency
CS 140 Lecture Notes: Concurrency
Too Much Milk With Locks
Too Much Milk With Locks
Default Arguments.
CS 140 Lecture Notes: Concurrency
CS148 Introduction to Programming II
CS 140 Lecture Notes: Concurrency
Too Much Milk With Locks
Lecture 12 CV and Semaphores
CSCE 206 Lab Structured Programming in C
Single-Core Lock Implementation
Presentation transcript:

Too Much Milk With Locks Both threads: struct lock l; ... lock_acquire(&l); if (milk == 0) { buyMilk(); } lock_release(&l); CS 140 Lecture Notes: Concurrency

CS 140 Lecture Notes: Locks Producer/Consumer v1 char buffer[SIZE]; int count = 0; int head = 0, tail = 0; struct lock l; lock_init(&l); void put(char c) { lock_acquire(&l); count++; buffer[head] = c; head++; if (head == SIZE) { head = 0; } lock_release(&l); char get() { char c; lock_acquire(&l); count--; c = buffer[tail]; tail++; if (tail == SIZE) { tail = 0; } lock_release(&l); return c; CS 140 Lecture Notes: Locks

CS 140 Lecture Notes: Locks Producer/Consumer v1 char buffer[SIZE]; int count = 0; int head = 0, tail = 0; struct lock l; lock_init(&l); void put(char c) { lock_acquire(&l); count++; buffer[head] = c; head++; if (head == SIZE) { head = 0; } lock_release(&l); char get() { char c; lock_acquire(&l); count--; c = buffer[tail]; tail++; if (tail == SIZE) { tail = 0; } lock_release(&l); return c; put('a'); a count: 0 head: 0 tail: 0 SIZE: 8 count: 1 head: 1 tail: 0 SIZE: 8 CS 140 Lecture Notes: Locks

CS 140 Lecture Notes: Locks Producer/Consumer v1 char buffer[SIZE]; int count = 0; int head = 0, tail = 0; struct lock l; lock_init(&l); void put(char c) { lock_acquire(&l); count++; buffer[head] = c; head++; if (head == SIZE) { head = 0; } lock_release(&l); char get() { char c; lock_acquire(&l); count--; c = buffer[tail]; tail++; if (tail == SIZE) { tail = 0; } lock_release(&l); return c; put('b'); put('c'); a a b c count: 0 head: 0 tail: 0 SIZE: 8 count: 3 head: 3 tail: 0 SIZE: 8 CS 140 Lecture Notes: Locks

CS 140 Lecture Notes: Locks Producer/Consumer v1 char buffer[SIZE]; int count = 0; int head = 0, tail = 0; struct lock l; lock_init(&l); void put(char c) { lock_acquire(&l); count++; buffer[head] = c; head++; if (head == SIZE) { head = 0; } lock_release(&l); char get() { char c; lock_acquire(&l); count--; c = buffer[tail]; tail++; if (tail == SIZE) { tail = 0; } lock_release(&l); return c; get() => 'a' a b c a b c count: 3 head: 3 tail: 0 SIZE: 8 count: 2 head: 3 tail: 1 SIZE: 8 CS 140 Lecture Notes: Locks

CS 140 Lecture Notes: Locks Producer/Consumer v1 char buffer[SIZE]; int count = 0; int head = 0, tail = 0; struct lock l; lock_init(&l); void put(char c) { lock_acquire(&l); count++; buffer[head] = c; head++; if (head == SIZE) { head = 0; } lock_release(&l); char get() { char c; lock_acquire(&l); count--; c = buffer[tail]; tail++; if (tail == SIZE) { tail = 0; } lock_release(&l); return c; get() => 'b' a b c a b c count: 2 head: 3 tail: 1 SIZE: 8 count: 1 head: 3 tail: 2 SIZE: 8 CS 140 Lecture Notes: Locks

CS 140 Lecture Notes: Locks Producer/Consumer v1 char buffer[SIZE]; int count = 0; int head = 0, tail = 0; struct lock l; lock_init(&l); void put(char c) { lock_acquire(&l); count++; buffer[head] = c; head++; if (head == SIZE) { head = 0; } lock_release(&l); char get() { char c; lock_acquire(&l); count--; c = buffer[tail]; tail++; if (tail == SIZE) { tail = 0; } lock_release(&l); return c; put('h'); put('i'); a b c d e f g i b c d e f g h count: 5 head: 7 tail: 2 SIZE: 8 count: 7 head: 1 tail: 2 SIZE: 8 CS 140 Lecture Notes: Locks

CS 140 Lecture Notes: Locks Producer/Consumer v2 char buffer[SIZE]; int count = 0; int head = 0, tail = 0; struct lock l; lock_init(&l); void put(char c) { lock_acquire(&l); while (count == SIZE) { lock_release(&l); } count++; buffer[head] = c; head++; if (head == SIZE) { head = 0; char get() { char c; lock_acquire(&l); while (count == 0) { lock_release(&l); } count--; c = buffer[tail]; tail++; if (tail == SIZE) { tail = 0; return c; CS 140 Lecture Notes: Locks

CS 140 Lecture Notes: Locks Producer/Consumer v3 char buffer[SIZE]; int count = 0; int head = 0, tail = 0; struct lock l; struct condition dataAvailable; struct condition spaceAvailable; lock_init(&l); cond_init(&dataAvailable); cond_init(&spaceAvailable); void put(char c) { lock_acquire(&l); while (count == SIZE) { cond_wait(&spaceAvailable, &l); } count++; buffer[head] = c; head++; if (head == SIZE) { head = 0; cond_signal(&dataAvailable, &l); lock_release(&l); char get() { char c; lock_acquire(&l); while (count == 0) { cond_wait(&dataAvailable, &l); } count--; c = buffer[tail]; tail++; if (tail == SIZE) { tail = 0; cond_signal(&spaceAvailable, &l); lock_release(&l); return c; T1 T2 T3 CS 140 Lecture Notes: Locks

CS 140 Lecture Notes: Locks