Too Much Milk With Locks

Slides:



Advertisements
Similar presentations
Dynamic Allocation and Linked Lists. Dynamic memory allocation in C C uses the functions malloc() and free() to implement dynamic allocation. malloc is.
Advertisements

For(int i = 1; i
1 Lab 4 Westfield High School APCS LAB 4 Parameters, apvectors, structs.
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",
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.
CS Lecture 4 Programming with Posix Threads and Java Threads George Mason University Fall 2009.
Templated Functions. Overloading vs Templating  Overloaded functions allow multiple functions with the same name.
Pointer to Structure. Structure variable can be access using pointers int a=10,*p; Here p  is an integer type pointer variable, p can hold the address.
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
Union, bitfield, typedef, enum union nama_u{ }; union nama_u{ struct nama_s byte; }; enum{ }; Tipedef var BYTE.
CS1061: C Programming Lecture 21: Dynamic Memory Allocation and Variations on struct A. O’Riordan, 2004, 2007 updated.
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
5.6.2 Thread Synchronization with Semaphores Semaphores can be used to notify other threads that events have occurred –Producer-consumer relationship Producer.
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);
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.
Condition Variables Revisited Copyright ©: University of Illinois CS 241 Staff1.
Recursion Examples Fundamentals of CS Case 1: Code /* Recursion: Case 1 */ #include void count (int index); main () { count (0); getchar(); } void count.
Passing Structure to function.  structure to function structure to function  Passing structure to function in C Passing structure to function in C 
More C++ Bryce Boe 2013/10/23 CS24, Fall Outline Project 1 Review Alignment and Padding Finish C++ Introduction.
CS 139-Programming Fundamentals Lecture 11B - Arrays Adapted from a presentation by Dr. Rahman Fall 2014.
MP2 Discussion Session CS414 Spring 2010 Long Vu 2/16/2010.
Quiz // // The function exchanges the two parameters. // Param: ( ) // Param:
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.
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.
Auburn University COMP 3500 Introduction to Operating Systems Project 3 – Synchronization Cats and Mice: Implementation.
Concurrency 2 CS 2110 – Spring 2016.
Auburn University COMP 3500 Introduction to Operating Systems Project 3 – Synchronization Condition Variables Dr. Xiao.
Structs versus Classes
CS150 Introduction to Computer Science 1
CS 140 Lecture Notes: Lock Implementation
CSC 253 Lecture 8.
Uniprocessor Lock Implementation
CSC 253 Lecture 8.
Too Much Milk With Locks
Structure ការណែនាំអំពី Structure
Condition Variables and Producer/Consumer
Return by Reference CSCE 121 J. Michael Moore.
Condition Variables and Producer/Consumer
CS 140 Lecture Notes: Concurrency
CS 140 Lecture Notes: Concurrency
Too Much Milk With Locks
Too Much Milk With Locks
Too Much Milk With Locks
Introduction to Programming
Local Variables, Global Variables and Variable Scope
Chapter 30 Condition Variables
Default Arguments.
Dynamic Memory A whole heap of fun….
CS 140 Lecture Notes: Concurrency
Programming Language C Language.
CS148 Introduction to Programming II
CS 140 Lecture Notes: Concurrency
The Stack.
Bitwise Operators.
Too Much Milk With Locks
CSCE 206 Lab Structured Programming in C
CS150 Introduction to Computer Science 1
C++ Parse Analysis Introduction
Single-Core Lock Implementation
Presentation transcript:

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

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

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

CS 140 Lecture Notes: Locks and Condition Variables Producer/Consumer, v3 char buffer[SIZE]; int count = 0; int putIndex = 0, getIndex = 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[putIndex] = c; putIndex++; if (putIndex == SIZE) { putIndex = 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[getIndex]; getIndex++; if (getIndex == SIZE) { getIndex = 0; cond_signal(&spaceAvailable, &l); lock_release(&l); return c; T1 T2 T3 CS 140 Lecture Notes: Locks and Condition Variables