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
Senem Kumova Metin Spring2009 STACKS AND QUEUES Chapter 10 in A Book on C.
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.
Automated Verification with HIP and SLEEK Asankhaya Sharma.
SORTING AND ASYMPTOTIC COMPLEXITY Lecture 12 CS2110 – Spring 2014 File searchSortAlgorithms.zip on course website (lecture notes for lectures 12, 13) contains.
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
Lecture # 21 Chapter 6 Uptill 6.4. Type System A type system is a collection of rules for assigning type expressions to the various parts of the program.
1 Class Vehicle #include #define N 10../.. 2 Class Vehicle class vehicle { public: float speed; char colour[N+1]; char make[N+1];
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
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);
02/14/2007CSCI 315 Operating Systems Design1 Process Synchronization Notice: The slides for this lecture have been largely based on those accompanying.
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.
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.
 Review structures  Program to demonstrate a structure containing a pointer.
Data structures for concurrency 1. Ordinary collections are not thread safe Namespaces System.Collections System.Collections.Generics Classes List, LinkedList,
Passing Structure to function.  structure to function structure to function  Passing structure to function in C Passing structure to function in C 
Quiz // // The function exchanges the two parameters. // Param: ( ) // Param:
Java Producer-Consumer Monitor From: Concurrent Programming: The Java Programming Language By Steven J. Hartley Oxford University Press, 1998.
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.
CS150 Introduction to Computer Science 1
CS 140 Lecture Notes: Lock Implementation
Uniprocessor Lock Implementation
Too Much Milk With Locks
Too Much Milk With Locks
Condition Variables and Producer/Consumer
Introduction to Programming
CS 140 Lecture Notes: Concurrency
CS 140 Lecture Notes: Concurrency
Too Much Milk With Locks
Too Much Milk With Locks
Chapter 30 Condition Variables
Grades.
Default Arguments.
CS 140 Lecture Notes: Introduction
Introduction to Programming
CS 140 Lecture Notes: Concurrency
CS148 Introduction to Programming II
CS 140 Lecture Notes: Introduction
CS 140 Lecture Notes: Concurrency
Running Time Exercises
Outline Chapter 3: Processes Chapter 4: Threads So far - Next -
Lecture 20: Synchronization
Too Much Milk With Locks
Structures in c By Anand George.
CSCE 206 Lab Structured Programming in C
Lecture 24.
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: Concurrency

CS 140 Lecture Notes: Locks 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

CS 140 Lecture Notes: Locks 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

CS 140 Lecture Notes: Locks 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