January 29, 2004 Adrienne Noble

Slides:



Advertisements
Similar presentations
Operating Systems Semaphores II
Advertisements

Operating Systems ECE344 Midterm review Ding Yuan
More on Semaphores, and Classic Synchronization Problems CSCI 3753 Operating Systems Spring 2005 Prof. Rick Han.
EEE 435 Principles of Operating Systems Interprocess Communication Pt II (Modern Operating Systems 2.3)
1 Threads CSCE 351: Operating System Kernels Witawas Srisa-an Chapter 4-5.
Monitors and Blocking Synchronization By Tal Walter.
1 CSE451 – Section 4. 2 Reminders Project 2 parts 1,2,3 due next Thursday Threads, synchronization Today: Project 2 continued (parts 2,3) Synchronization.
1 CS318 Project #3 Preemptive Kernel. 2 Continuing from Project 2 Project 2 involved: Context Switch Stack Manipulation Saving State Moving between threads,
Reminders Homework 3 due next Monday
Concurrency: Mutual Exclusion, Synchronization, Deadlock, and Starvation in Representative Operating Systems.
Semaphores, mutexes and condition variables. semaphores Two types – Binary – 0 or 1 – Counting 0 to n Wait – decrements > 0 forces a wait Post or signal.
Semaphores CSCI 444/544 Operating Systems Fall 2008.
Race Conditions CS550 Operating Systems. Review So far, we have discussed Processes and Threads and talked about multithreading and MPI processes by example.
CS Introduction to Operating Systems
Discussion Week 3 TA: Kyle Dewey. Overview Concurrency overview Synchronization primitives Semaphores Locks Conditions Project #1.
Parallel Processing (CS526) Spring 2012(Week 8).  Thread Status.  Synchronization in Shared Memory Programming(Java threads ) ◦ Locks ◦ Barriars.
Semaphores. Readings r Silbershatz: Chapter 6 Mutual Exclusion in Critical Sections.
Nachos Phase 1 Code -Hints and Comments
Object Oriented Analysis & Design SDL Threads. Contents 2  Processes  Thread Concepts  Creating threads  Critical sections  Synchronizing threads.
CSE451 NT Synchronization Autumn 2002 Gary Kimura Lecture #9 October 18, 2002.
4061 Session 21 (4/3). Today Thread Synchronization –Condition Variables –Monitors –Read-Write Locks.
Consider the program fragment below left. Assume that the program containing this fragment executes t1() and t2() on separate threads running on separate.
1 CMSC421: Principles of Operating Systems Nilanjan Banerjee Principles of Operating Systems Acknowledgments: Some of the slides are adapted from Prof.
Week 3 January 22, 2004 Adrienne Noble. Today CVS – a great tool to use with your groups Threads – basic thread operations Intro to synchronization Hand.
13/03/07Week 21 CENG334 Introduction to Operating Systems Erol Sahin Dept of Computer Eng. Middle East Technical University Ankara, TURKEY URL:
CSE 451: Operating Systems Section 5 Synchronization.
Problems with Semaphores Used for 2 independent purposes –Mutual exclusion –Condition synchronization Hard to get right –Small mistake easily leads to.
CSE 153 Design of Operating Systems Winter 2015 Midterm Review.
Condition Variables Condition variables support three operations: Wait – add calling thread to the condition variable’s queue and put the thread to sleep.
CSCI1600: Embedded and Real Time Software Lecture 17: Concurrent Programming Steven Reiss, Fall 2015.
Implementing Mutual Exclusion Andy Wang Operating Systems COP 4610 / CGS 5765.
CSE 451 Section 4. 2 Synchronization High-level Monitors Java synchronized method OS-level support Special variables – mutex, semaphor, condition var.
Mutual Exclusion -- Addendum. Mutual Exclusion in Critical Sections.
© 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)
CS703 - Advanced Operating Systems
CPS110: Reader-writer locks
PROCESS MANAGEMENT IN MACH
Semaphores Synchronization tool (provided by the OS) that does not require busy waiting. Logically, a semaphore S is an integer variable that, apart from.
CS703 – Advanced Operating Systems
Process Synchronization: Semaphores
Auburn University COMP 3500 Introduction to Operating Systems Synchronization: Part 4 Classical Synchronization Problems.
PARALLEL PROGRAM CHALLENGES
Advanced OS Concepts (For OCR)
Preemption Set timer interrupts But now, must synchronize Two tools:
Auburn University COMP 3500 Introduction to Operating Systems Project 3 – Synchronization Condition Variables Dr. Xiao.
Lecture 25 More Synchronized Data and Producer/Consumer Relationship
Operating Systems CMPSC 473
Lecture 13: Producer-Consumer and Semaphores
Lecture 14: Pthreads Mutex and Condition Variables
CSE 451 Autumn 2003 Section 3 October 16.
Another Means Of Thread Synchronization
Implementing Mutual Exclusion
CSCI1600: Embedded and Real Time Software
Multithreading Tutorial
Implementing Mutual Exclusion
February 5, 2004 Adrienne Noble
Lecture 14: Pthreads Mutex and Condition Variables
Lecture 13: Producer-Consumer and Semaphores
CSE 153 Design of Operating Systems Winter 19
CSE 153 Design of Operating Systems Winter 2019
January 15, 2004 Adrienne Noble
CSCI1600: Embedded and Real Time Software
CSE 451 Section 1/27/2000.
EE 193: Parallel Computing
CSE 153 Design of Operating Systems Winter 2019
“The Little Book on Semaphores” Allen B. Downey
EECE.4810/EECE.5730 Operating Systems
EECE.4810/EECE.5730 Operating Systems
Presentation transcript:

January 29, 2004 Adrienne Noble Week 4 January 29, 2004 Adrienne Noble

Questions? Important dates: Homework 3 – Due tomorrow, Jan 30 Project 2a – Due Monday, Feb 2 Midterm – Friday, Feb 6 Project 2b – Due Wednesday, Feb 11

Typedef Simple a shortcut name to a type Use Examples: typedef type shortcutName Examples: typedef int myIntType; typedef struct { // struct variables… } structName; Typedef char* pointerToChar; Typedef char *pointerToChar;

Mutexes Just a simple lock on shared data Only allows one thread to access the shared data at a time Two functions: lock and unlock Basic Idea: Get lock (call lock function) Access shared data Give up lock (call unlock function)

Mutex Example You should be able to think of all sorts of uses for a mutex… Withdrawing money from a bank account Reserving a ticket

Semaphores More powerful than mutexes Two functions: wait (or P) and signal (or V) Simplest form: a binary semaphore Works just like a mutex Semaphore counter is initialized as 1 A thread calls wait (or P) if the counter is positive, the thread subtracts 1 from the counter and gets the lock If the counter is 0, the thread is suspended and put in the semaphore’s waiting queue A thread calls signal (or V) If the queue is empty, the thread adds 1 to the counter and continues If the queue has waiting threads, the thread adds 1 to the counter and one of the threads is taken off the queue, then continues You can implement your mutex as a semaphore or as a lock… either way is fine. Why?

Semaphore Example Can also be used to let more than one thread in at once A ferry only allows 100 passengers on board The counter is initialized to 100 Each passenger that gets on the ferry will decrement the counter by 1. After 100 passengers, the counter will be at 0, and all further passengers will be put on the queue. If a passenger gets off the ferry, he takes one person off the queue and lets them get on the ferry. If no one is on the queue, he increments the counter by 1.

Condition Variables Used to let threads wait for a certain condition or event to occur while holding a lock (often a monitor) Three functions: wait, signal, and broadcast Basic Idea: Thread with the lock calls wait The thread gives up the lock, then goes to sleep on the condition variable’s queue Someone signals to say that the event or condition has occurred A thread wakes up from the queue, obtains the lock again, and then continues

Condition Variable Example A server has a bunch of threads waiting to handle incoming requests Those threads wait on a condition variable When an incoming request comes in, the controller thread signals that there is work to do A thread is woken up and goes to handle the request What might you call this architecture? Threadpool

Another Condition Variable Example How would you use broadcast? Alarm clock example from homework? Lots of people waiting on “timer” queue, the shortest time is up but you don’t know whose time it is – just wake everyone up and let them figure it out

Monitors A higher level way to deal with shared data Allows you to make an entire object protected by a mutex A monitor controls all access to the class by letting only one thread access the class instance at once

Monitor Example Usually requires language support In Java, any synchronized class is controlled by a monitor Hidden from the programmer, but anytime a thread wants to access anything in the class, the monitor makes it first obtain a lock Condition variables are often used in monitors to allow other threads to access the monitor while one thread waits for an event to occur

Picture from Lecture Everything goes onto ready queue before actually getting run! What exactly is a ready queue?

Thread Scheduler in Linux Doesn’t use a FIFO queue like we did Measures a thread’s “goodness” (yes, that’s what they really call it…) Goodness algorithm (where p is the thread being checked) if ( p->policy != SCHED_OTHER ) return 1000 + p->rt_priority if ( p->counter == 0 )   return 0; if ( p->mm == prev->mm )   return p->counter + p->priority + 1; return p->counter + p->priority; The thread with the highest goodness is chosen to run next (p->policy != SCHED_OTHER) means that it gets a big bonus if it’s a real-time process (p->counter == 0) means that if it’s used up all it’s time, it must wait to run – it has no priority (p->mm == prev->mm) means if it’s sharing some of the previous processes memory, it gets a little bonus