Lab assignment 1: Synchronization

Slides:



Advertisements
Similar presentations
Operating Systems Semaphores II
Advertisements

Practice Session 7 Synchronization Liveness Deadlock Starvation Livelock Guarded Methods Model Thread Timing Busy Wait Sleep and Check Wait and Notify.
1 C OMP 346 – W INTER 2015 Tutorial # 5. Semaphores for Barrier Sync A barrier is a type of synchronization method. A barrier for a group of threads or.
CS Lecture 4 Programming with Posix Threads and Java Threads George Mason University Fall 2009.
1 Operating Systems, 122 Practical Session 5, Synchronization 1.
Operating Systems ECE344 Ding Yuan Synchronization (I) -- Critical region and lock Lecture 5: Synchronization (I) -- Critical region and lock.
Big Picture Lab 4 Operating Systems Csaba Andras Moritz.
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 Principles. Race Conditions Race Conditions: An Example spooler directory out in 4 7 somefile.txt list.c scores.txt Process.
Semaphores CSCI 444/544 Operating Systems Fall 2008.
A. Frank - P. Weisberg Operating Systems Introduction to Cooperating Processes.
U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science Emery Berger University of Massachusetts, Amherst Operating Systems CMPSCI 377 Lecture.
Synchronization CSCI 444/544 Operating Systems Fall 2008.
5.4 Which of the following scheduling algorithms could result in starvation? a. First-come, first-served b. Shortest job first c. Round robin d. Priority.
Discussion Week 3 TA: Kyle Dewey. Overview Concurrency overview Synchronization primitives Semaphores Locks Conditions Project #1.
CS4231 Parallel and Distributed Algorithms AY 2006/2007 Semester 2 Lecture 2 (19/01/2006) Instructor: Haifeng YU.
Nachos Phase 1 Code -Hints and Comments
U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science Emery Berger University of Massachusetts, Amherst Operating Systems CMPSCI 377 Lecture.
Operating Systems CMPSC 473 Mutual Exclusion Lecture 14: October 14, 2010 Instructor: Bhuvan Urgaonkar.
1 Announcements The fixing the bug part of Lab 4’s assignment 2 is now considered extra credit. Comments for the code should be on the parts you wrote.
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.
Operating Systems ECE344 Ashvin Goel ECE University of Toronto Mutual Exclusion.
® 7-2 Semaphores 7.1Overview Binary Semaphores and Synchronization Mutual Exclusion.
Synchronized and Monitors. synchronized is a Java keyword to denote a block of code which must be executed atomically (uninterrupted). It can be applied.
More on Semaphores Andy Wang Operating Systems COP 4610 / CGS 5765.
Kernel Locking Techniques by Robert Love presented by Scott Price.
Lecture 6: Synchronization (II) – Semaphores and Monitors
Chapter 6 – Process Synchronisation (Pgs 225 – 267)
Synchronizing Threads with Semaphores
Operating Systems ECE344 Ashvin Goel ECE University of Toronto Synchronization.
Discussion Week 2 TA: Kyle Dewey. Overview Concurrency Process level Thread level MIPS - switch.s Project #1.
U NIVERSITY OF M ASSACHUSETTS A MHERST Department of Computer Science Software Systems Advanced Synchronization Emery Berger and Mark Corner University.
Background Concurrent access to shared data may result in data inconsistency Maintaining data consistency requires mechanisms to ensure the orderly execution.
Computer Architecture and Operating Systems CS 3230: Operating System Section Lecture OS-5 Process Synchronization Department of Computer Science and Software.
1 Synchronization Threads communicate to ensure consistency If not: race condition (non-deterministic result) Accomplished by synchronization operations.
Problems with Semaphores Used for 2 independent purposes –Mutual exclusion –Condition synchronization Hard to get right –Small mistake easily leads to.
U NIVERSITY OF M ASSACHUSETTS A MHERST Department of Computer Science Computer Systems Principles Synchronization Emery Berger and Mark Corner University.
Processes Creation and Threads. Shared Resource Example The console is just a place where text can be printed by the application currently running. Program.
1 Processes and Threads Part II Chapter Processes 2.2 Threads 2.3 Interprocess communication 2.4 Classical IPC problems 2.5 Scheduling.
Implementing Lock. From the Previous Lecture  The “too much milk” example shows that writing concurrent programs directly with load and store instructions.
CSCI1600: Embedded and Real Time Software Lecture 17: Concurrent Programming Steven Reiss, Fall 2015.
CSC 660: Advanced Operating SystemsSlide #1 CSC 660: Advanced OS Synchronization.
Implementing Mutual Exclusion Andy Wang Operating Systems COP 4610 / CGS 5765.
Working with Pthreads. Operations on Threads int pthread_create (pthread_t *thread, const pthread_attr_t *attr, void * (*routine)(void*), void* arg) Creates.
Mutual Exclusion -- Addendum. Mutual Exclusion in Critical Sections.
Synchronization Questions answered in this lecture: Why is synchronization necessary? What are race conditions, critical sections, and atomic operations?
1 COMP 3500 Introduction to Operating Systems Project 3 – Synchronization Overview Dr. Xiao Qin Auburn University
Big Picture Lab 4 Operating Systems C Andras Moritz
Web Server Architecture Client Main Thread for(j=0;j
© Janice Regan, CMPT 300, May CMPT 300 Introduction to Operating Systems Mutual Exclusion Mutexes, Semaphores.
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.
1 Section 5 Synchronization primitives (Many slides taken from Winter 2006)
Kernel Synchronization David Ferry, Chris Gill CSE 522S - Advanced Operating Systems Washington University in St. Louis St. Louis, MO
Auburn University COMP 3500 Introduction to Operating Systems Project 3 – Synchronization Cats and Mice: Implementation.
Auburn University COMP 3500 Introduction to Operating Systems Project 3 – Synchronization Cats and Mice Dr. Xiao Qin.
Multi Threading.
Chapter 5: Process Synchronization – Part 3
Background on the need for Synchronization
143a discussion session week 3
Definitions Concurrent program – Program that executes multiple instructions at the same time. Process – An executing program (the running JVM for Java.
Synchronization and Semaphores
Chapter 7: Synchronization Examples
Implementing Mutual Exclusion
Implementing Mutual Exclusion
CSE 451 Section 1/27/2000.
Process/Thread Synchronization (Part 2)
More concurrency issues
Synchronization and liveness
Presentation transcript:

Lab assignment 1: Synchronization Operating Systems ECE344 Lab assignment 1: Synchronization Question: java thread? Ding Yuan

Overview of the lab assignment Task 1: implementing synchronization primitives 1a: implement lock 1b: implement condition variable Task 2: use synchronization primitives to solve problems 2a: Mice and cats 2b: traffic lights Ding Yuan, ECE344 Operating System

Ding Yuan, ECE344 Operating System Lock and cond. var. P(sem) { Disable interrupts; while (sem->count == 0) { thread_sleep(sem); /* current thread will sleep on this sem */ } sem->count--; Enable interrupts; V(sem) { Disable interrupts; sem->count++; thread_wakeup (sem); /* this will wake up all the threads waiting on this sem. Why wake up all threads? */ Enable interrupts; } why wake up all threads: this is to make the threads runnable. Then it’s up to the scheduler to pick one. all these thread will “return” from thread_sleep. only one can pass P. --- this is also why we need a while (sem->count != 0) instead of “if (sem->count != 0)”. sleep with interrupts disabled: thread sleep will eventually call mips_switch to swtich to a new thread. when that thread is waken up, the first thing is to enable interrupt. Needs atomic region Atomic region can be done in a similar way to semaphore If you understand how semaphore is implemented, should be trivial! Cannot use semaphore to implement lock or cond. var. Ding Yuan, ECE344 Operating System

Synchronization problems How to start? First: write operation code Next: carefully reason about all the possible interleaving and timing scenarios Add synchronization Ding Yuan, ECE344 Operating System

Ding Yuan, ECE344 Operating System Mice and cats Two bowls, multiple cats and mice Safety criteria: If a cat is eating at either dishes, no mouse can eat If a mouse is eating at either dishes, no cat can eat Ding Yuan, ECE344 Operating System

Ding Yuan, ECE344 Operating System Operation code void sem_eat(const char *who, int num, int bowl, int iteration) { kprintf("%s: %d starts eating: bowl %d, iteration %d\n", who, num, bowl, iteration); clocksleep(1); kprintf("%s: %d ends eating: bowl %d, iteration %d\n", who, num, } void mousesem(void * p, unsigned long mousenumber) { int bowl, iteration; for (iteration = 0; iteration < 4; iteration++) { sem_eat (”mouse", mousenumber, bowl, iteration); int catmousesem(.. ..) { for (index = 0; index < NCATS; index++) thread_fork("catsem Thread”, NULL, index, catsem, NULL); for (index = 0; index < NMICE; index++) thread_fork(”mousesem Thread”, NULL, index, mousesem, NULL); Ding Yuan, ECE344 Operating System

Ding Yuan, ECE344 Operating System About starvation You do not need to consider priority or starvation e.g., mice can prevent cat from eating Since cats/mice will eventually finish eating, won’t starve forever Ding Yuan, ECE344 Operating System