CMSC621: Advanced Operating Systems Advanced Operating Systems

Slides:



Advertisements
Similar presentations
Operating Systems Semaphores II
Advertisements

1 Chapter 5 Concurrency: Mutual Exclusion and Synchronization Principals of Concurrency Mutual Exclusion: Hardware Support Semaphores Readers/Writers Problem.
Ch 7 B.
Chapter 6: Process Synchronization
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 6: Process Synchronization.
1 CMSC421: Principles of Operating Systems Nilanjan Banerjee Principles of Operating Systems Assistant Professor, University of Maryland Baltimore County.
Concurrency: Mutual Exclusion, Synchronization, Deadlock, and Starvation in Representative Operating Systems.
Threads© Dr. Ayman Abdel-Hamid, CS4254 Spring CS4254 Computer Network Architecture and Programming Dr. Ayman A. Abdel-Hamid Computer Science Department.
Race Conditions CS550 Operating Systems. Review So far, we have discussed Processes and Threads and talked about multithreading and MPI processes by example.
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.
Pthread (continue) General pthread program structure –Encapsulate parallel parts (can be almost the whole program) in functions. –Use function arguments.
CS510 Concurrent Systems Introduction to Concurrency.
Object Oriented Analysis & Design SDL Threads. Contents 2  Processes  Thread Concepts  Creating threads  Critical sections  Synchronizing threads.
U NIVERSITY OF M ASSACHUSETTS A MHERST Department of Computer Science Computer Systems Principles Processes & Threads Emery Berger and Mark Corner University.
Lecture 3 Process Concepts. What is a Process? A process is the dynamic execution context of an executing program. Several processes may run concurrently,
1 CMSC421: Principles of Operating Systems Nilanjan Banerjee Principles of Operating Systems Acknowledgments: Some of the slides are adapted from Prof.
Programming with POSIX* Threads Intel Software College.
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.
4061 Session 21 (4/3). Today Thread Synchronization –Condition Variables –Monitors –Read-Write Locks.
Threads Chapter 26. Threads Light-weight processes Each process can have multiple threads of concurrent control. What’s wrong with processes? fork() is.
1 CMSC421: Principles of Operating Systems Nilanjan Banerjee Principles of Operating Systems Acknowledgments: Some of the slides are adapted from Prof.
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.
CS399 New Beginnings Jonathan Walpole. 2 Concurrent Programming & Synchronization Primitives.
1 Condition Variables CS 241 Prof. Brighten Godfrey March 16, 2012 University of Illinois.
U NIVERSITY OF M ASSACHUSETTS A MHERST Department of Computer Science Computer Systems Principles Synchronization Emery Berger and Mark Corner University.
1 Previous Lecture Overview  semaphores provide the first high-level synchronization abstraction that is possible to implement efficiently in OS. This.
Operating Systems CMPSC 473 Signals, Introduction to mutual exclusion September 28, Lecture 9 Instructor: Bhuvan Urgaonkar.
CS 153 Design of Operating Systems Winter 2016 Lecture 7: Synchronization.
CSCI1600: Embedded and Real Time Software Lecture 17: Concurrent Programming Steven Reiss, Fall 2015.
Mutual Exclusion -- Addendum. Mutual Exclusion in Critical Sections.
pThread synchronization
Case Study: Pthread Synchronization Dr. Yingwu Zhu.
Chapter 5 Concurrency: Mutual Exclusion and Synchronization Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee.
CS703 - Advanced Operating Systems
CS 6560: Operating Systems Design
CS703 – Advanced Operating Systems
Background on the need for Synchronization
PThreads.
Threads Threads.
Multithreading Tutorial
Chapter 3: Process Concept
CSE451 Basic Synchronization Spring 2001
HW1 and Synchronization & Queuing
Threads and Cooperation
Multithreading Tutorial
COT 5611 Operating Systems Design Principles Spring 2014
Process Synchronization
COT 5611 Operating Systems Design Principles Spring 2012
Background and Motivation
CSE 451 Autumn 2003 Section 3 October 16.
Multithreading Tutorial
CSCI1600: Embedded and Real Time Software
CSE 451: Operating Systems Autumn Lecture 8 Semaphores and Monitors
Multithreading Tutorial
Kernel Synchronization II
CSE 451: Operating Systems Autumn 2003 Lecture 7 Synchronization
CSE 451: Operating Systems Autumn 2005 Lecture 7 Synchronization
CSE 451: Operating Systems Winter 2003 Lecture 7 Synchronization
CS510 Operating System Foundations
CSE 153 Design of Operating Systems Winter 19
CSE 153 Design of Operating Systems Winter 2019
CS333 Intro to Operating Systems
CSCI1600: Embedded and Real Time Software
CSE 451 Section 1/27/2000.
CS 144 Advanced C++ Programming May 7 Class Meeting
EECE.4810/EECE.5730 Operating Systems
Don Porter Portions courtesy Emmett Witchel
Presentation transcript:

CMSC621: Advanced Operating Systems Advanced Operating Systems Nilanjan Banerjee Associate Professor, University of Maryland Baltimore County nilanb@umbc.edu http://www.csee.umbc.edu/~nilanb/teaching/621/ Advanced Operating Systems

Announcements Project 1 will be given out next week. How did the homework go?

Three processes are executed. Short homework Assume that a mmapped file has four numbers (1) a (2) b (3) c (4) sum = a+b+c Three processes are executed. First process increments a Second process increments b Third process increments c Each time one of the above happens sum is updated Add proper mutual exclusion to ensure that sum is always equal to a+b+c.

Message Passing Using Sockets A socket is defined as an endpoint for communication Concatenation of IP address and port The socket 161.25.19.8:1625 refers to port 1625 on host 161.25.19.8 Communication consists between a pair of sockets

Message Passing Using Sockets

Linux kernel Process 1 Process 2 POSIX Signals deliver Register a a signal Name Description Default Action SIGINT Interrupt character typed terminate process SIGQUIT Quit character typed (^\) create core image SIGKILL kill -9 terminate process SIGSEGV Invalid memory reference create core image SIGPIPE Write on pipe but no reader terminate process SIGALRM alarm() clock ‘rings’ terminate process SIGUSR1 user-defined signal type terminate process SIGUSR2 user-defined signal type terminate process

int kill( pid_t pid, int signo ); signals int kill( pid_t pid, int signo ); Send a signal to a process with a process id signal(<signal name>, <pointer to handler>) Handle a maskable signal in your code

Why do we need processes? A process (with a single thread) supports a serial flow of execution Is that good enough for all purposes? What if something goes wrong in one process? What if something takes a long amount of time in one process? What if we have more than one user?

What is difference between Process and Threads The execution context of the process are (Program Counter, registers), address space, files, mmaped regions etc.

What is difference between Process and Threads Threads share an address space. They have same files, sockets etc. They have their own stack, program counters, registers, and stack specific data

Threads Vs Processes Processes or Threads Performance? Flexibility/Ease-of-use Robustness Simple Scheduling OS has a list of Threads and schedules them similar to Processes. In Linux, threads/processes treated similarly Chooses one of the threads and loads them to be run Runs them for a while, runs another.

Flexibility/Ease of use? Process are more flexible Easy to spawn processes, I can ssh into a server and spawn a process Can communicate over sockets= distributes across machines Threads Communicate using memory – must be on the same machine Requires thread-safe code

Robustness Process are more robust Processes are separate or sandboxed from other processes If one process dies, no effect on other processes Threads If one thread crashes, whole process terminates Since there is sharing, there are problems with using the stack efficiently

Creating Threads UNIX Pthreads (POSIX threads) Pthread_create() --- creating a thread Pthread_join() --- wait for thread to finish Lets see a demonstration of using pthreads.

Process switches more expensive, or require long quanta Context Switch cost Threads – much cheaper Stash registers, PC (“IP”), stack pointer Processes – above plus Process context TLB shootdown Process switches more expensive, or require long quanta

Synchronization and Concurrency Probably the MOST important concept in OS Lets look at a demonstration

Thread 1 executes the first two statements What really happened? Count = count + 1 load X R1 add Y Z R1 store Y in Mem Thread 1 executes the first two statements and is preempted… Thread 2 executes all the three statements Thread 1 returns and executes the third statement

Concurrency Terminology Mutual exclusion (“mutex”) prevents multiple threads from entering Critical section code only one thread can execute at a time Lock mechanism for mutual exclusion Lock entering critical section, accessing shared data Unlock when complete Wait if locked Invariant Something that must be true

Why do we need concurrency? Synchronization serves two purposes: Ensure safety for shared updates Avoid race conditions Coordinate actions of threads Parallel computation Event notification ALL interleavings must be correct there are lots of interleavings of events also constrain as little as possible

Provide mutual exclusion to shared data Two routines Locks/mutexes Provide mutual exclusion to shared data Two routines acquire – wait for lock, then take it release – unlock, wake up waiters Rules: Acquire lock before accessing shared data Release lock afterwards Lock initially released

Locks and Queueing Acquire: Release: if unlocked, go in; otherwise wait in line Release: Unlock & leave

Synchronization and Concurrency Demonstration on using pthread locks/mutexes

Locks can enforce mutual exclusion, but notorious source of errors Safety Locks can enforce mutual exclusion, but notorious source of errors Failure to unlock Double locking Deadlock (its own lecture) Priority inversion not an “error” per se pthread_mutex_t l; void square (void) { pthread_mutex_lock (&l); // acquires lock // do stuff if (x == 0) { return; } else { x = x * x; } pthread_mutex_unlock (&l);

Bounded Buffer or Producer Consumer Problem Suppose we have a thread-safe queue insert(item), remove(), empty() must protect access with locks Consumer Consumes items in the queue Only if queue has items Producer: Produces items Adds them only if the queue is not full A simple case: max size of queue is 1

Sleep = What about this? Solution (sleep?) “don’t run me until something happens” What about this? Dequeue(){ lock(); if (queue empty) { sleep(); } take one item; unlock(); Enqueue(){ lock(); insert item; if (thread waiting) wake up dequeuer(); unlock(); }

Does this work? Another solution Dequeue(){ lock(); if (queue empty){ unlock(); sleep(); remove item; } else unlock; Enqueue(){ lock(); insert item; if (thread waiting) wake up dequeuer(); unlock(); }

Conditional variables Make it possible/easy to go to sleep Atomically: release lock put thread on wait queue go to sleep Each cv has a queue of waiting threads Worry about threads that have been put on the wait queue but have NOT gone to sleep yet? no, because those two actions are atomic Each condition variable associated with one lock

Conditional variables Wait for 1 event, atomically release lock wait(Lock& l, CV& c) If queue is empty, wait Atomically releases lock, goes to sleep You must be holding lock! May reacquire lock when awakened (pthreads do) signal(CV& c) Insert item in queue Wakes up one waiting thread, if any broadcast(CV& c) Wakes up all waiting threads Monitors = locks + condition variables Sometimes combined with data structures Lets take a look at a demo

Producer-consumer problem using pthread conditionals void * consumer (void *){ while (true) { pthread_mutex_lock(&l); while (q.empty()){ pthread_cond_wait(&nempty, &l); } cout << q.pop_back() << endl; pthread_mutex_unlock(&l); void * producer(void *){ while (true) { pthread_mutex_lock(&l); q.push_front (1); pthread_cond_signal(&nempty); pthread_mutex_unlock(&l); }