1. Parallel Programming and Synchronization P&H Chapter 2.11 Guest Lecture: Kevin Walsh CS 3410, Spring 2011 Computer Science Cornell University.

Slides:



Advertisements
Similar presentations
Operating Systems ECE344 Midterm review Ding Yuan
Advertisements

More on Processes Chapter 3. Process image _the physical representation of a process in the OS _an address space consisting of code, data and stack segments.
Slide 2-1 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 2 Using the Operating System 2.
Exceptional Control Flow Processes Today. Control Flow Processors do only one thing: From startup to shutdown, a CPU simply reads and executes (interprets)
Silberschatz, Galvin and Gagne ©2007 Operating System Concepts with Java – 7 th Edition, Nov 15, 2006 Chapter 6 (a): Synchronization.
Synchronization P&H Chapter 2.11 Han Wang CS 3410, Spring 2012 Computer Science Cornell University.
Processes and Threads Prof. Sirer CS 4410 Cornell University.
Synchronization Hakim Weatherspoon CS 3410, Spring 2013 Computer Science Cornell University P&H Chapter 2.11 and 5.8.
Review: Chapters 1 – Chapter 1: OS is a layer between user and hardware to make life easier for user and use hardware efficiently Control program.
Synchronization and Prelim 2 Review Hakim Weatherspoon CS 3410, Spring 2011 Computer Science Cornell University.
Kevin Walsh CS 3410, Spring 2010 Computer Science Cornell University Parallel Programming and Synchronization P&H Chapter 2.11.
Race Conditions Critical Sections Deker’s Algorithm.
1 Processes and Pipes COS 217 Professor Jennifer Rexford.
Processes CSCI 444/544 Operating Systems Fall 2008.
OS Fall ’ 02 Introduction Operating Systems Fall 2002.
Avishai Wool lecture Priority Scheduling Idea: Jobs are assigned priorities. Always, the job with the highest priority runs. Note: All scheduling.
1 CS 333 Introduction to Operating Systems Class 3 – Threads & Concurrency Jonathan Walpole Computer Science Portland State University.
Threads 1 CS502 Spring 2006 Threads CS-502 Spring 2006.
OS Spring’03 Introduction Operating Systems Spring 2003.
CS-502 Fall 2006Processes in Unix, Linux, & Windows 1 Processes in Unix, Linux, and Windows CS502 Operating Systems.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 3: Processes.
Unix & Windows Processes 1 CS502 Spring 2006 Unix/Windows Processes.
Lecture 1: Introduction CS170 Spring 2015 Chapter 1, the text book. T. Yang.
OS Spring’04 Introduction Operating Systems Spring 2004.
Processes in Unix, Linux, and Windows CS-502 Fall Processes in Unix, Linux, and Windows CS502 Operating Systems (Slides include materials from Operating.
Phones OFF Please Processes Parminder Singh Kang Home:
Process. Process Concept Process – a program in execution Textbook uses the terms job and process almost interchangeably A process includes: – program.
CSE 451: Operating Systems Autumn 2013 Module 6 Review of Processes, Kernel Threads, User-Level Threads Ed Lazowska 570 Allen.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 2: System Structures.
CS 3410, Spring 2014 Computer Science Cornell University See P&H Chapter: 6.7.
Threads, Thread management & Resource Management.
Today’s Topics Introducing process: the basic mechanism for concurrent programming –Process management related system calls Process creation Process termination.
Recall: Three I/O Methods Synchronous: Wait for I/O operation to complete. Asynchronous: Post I/O request and switch to other work. DMA (Direct Memory.
Processes and Threads CS550 Operating Systems. Processes and Threads These exist only at execution time They have fast state changes -> in memory and.
Lecture 3 Process Concepts. What is a Process? A process is the dynamic execution context of an executing program. Several processes may run concurrently,
ECE200 – Computer Organization Chapter 9 – Multiprocessors.
Background: Operating Systems Brad Karp UCL Computer Science CS GZ03 / M th November, 2008.
CS333 Intro to Operating Systems Jonathan Walpole.
Hakim Weatherspoon CS 3410, Spring 2015 Computer Science Cornell University P&H Chapter 2.11, 5.10, and 6.5.
Synchronization Hakim Weatherspoon CS 3410, Spring 2013 Computer Science Cornell University P&H Chapter 2.11 and 5.8.
CS 153 Design of Operating Systems Spring 2015 Lecture 5: Processes and Threads.
1 Computer Systems II Introduction to Processes. 2 First Two Major Computer System Evolution Steps Led to the idea of multiprogramming (multiple concurrent.
Synchronization P&H Chapter 2.11 Han Wang CS 3410, Spring 2012 Computer Science Cornell University.
1 Process Description and Control Chapter 3. 2 Process A program in execution An instance of a program running on a computer The entity that can be assigned.
Major OS Components CS 416: Operating Systems Design, Spring 2001 Department of Computer Science Rutgers University
CSCI/CMPE 4334 Operating Systems Review: Exam 1 1.
S ALVATORE DI G IROLAMO (TA) Networks and Operating Systems: Exercise Session 1.
CS 162 Discussion Section Week 2. Who am I? Prashanth Mohan Office Hours: 11-12pm Tu W at.
Introduction to Operating Systems Concepts
Introduction to threads
Andy Wang COP 5611 Advanced Operating Systems
Andy Wang COP 5611 Advanced Operating Systems
Chapter 4: Threads.
Parallel Programming and Synchronization
Operating Systems and Systems Programming
Chapter 3: Processes.
Chapter 15, Exploring the Digital Domain
Jonathan Walpole Computer Science Portland State University
Processes in Unix, Linux, and Windows
Multicore, Parallelism, and Synchronization
CSE 451: Operating Systems Spring 2012 Module 6 Review of Processes, Kernel Threads, User-Level Threads Ed Lazowska 570 Allen.
Chapter 3: Processes.
Jonathan Walpole Computer Science Portland State University
Andy Wang COP 5611 Advanced Operating Systems
CSE 451: Operating Systems Winter 2003 Lecture 4 Processes
Processes in Unix and Windows
CS510 Operating System Foundations
CSE 451: Operating Systems Autumn 2004 Module 4 Processes
CSE 153 Design of Operating Systems Winter 2019
Andy Wang COP 5611 Advanced Operating Systems
Presentation transcript:

1

Parallel Programming and Synchronization P&H Chapter 2.11 Guest Lecture: Kevin Walsh CS 3410, Spring 2011 Computer Science Cornell University

3 Multi-core is a reality… … but how do we write multi-core safe code?

4 Cache Coherence: Necessary, but not Sufficient

5 NUMA

6 Shared Memory Multiprocessors Shared Memory Multiprocessor (SMP) Typical (today): 2 – 4 processor dies, 2 – 8 cores each Assume physical addresses (ignore virtual memory) Assume uniform memory access (ignore NUMA) Core0Core1CoreN Cache MemoryI/O Interconnect...

7 Cache Coherence Problem Shared Memory Multiprocessor (SMP) What could possibly go wrong?... x = x+1... while (x==5) { // wait }... Core0Core1Core3 I/O Interconnect...

8 Coherence Defined Cache coherence defined... Informal: Reads return most recently written value Formal: For concurrent processes P 1 and P 2 P writes X before P reads X (with no intervening writes)  read returns written value P 1 writes X before P 2 reads X  read returns written value P 1 writes X and P 2 writes X  all processors see writes in the same order –all see the same final value for X

9 Snooping Recall: Snooping for Hardware Cache Coherence All caches monitor bus and all other caches Bus read: respond if you have dirty data Bus write: update/invalidate your copy of data Core0 Cache MemoryI/O Interconnect Core1 Cache CoreN Cache...

10 Is cache coherence sufficient? Example with cache coherence: P 1 P 2 x = x +1while (x==5) ;

11 Is cache coherence sufficient? Example with cache coherence: P 1 P 2 x = x +1x = x + 1

12 Software Support for Synchronization and Coordination: Programs and Processes

13 Processes How do we cope with lots of activity? Simplicity? Separation into processes Reliability? Isolation Speed? Program-level parallelism gcc emacs nfsd lpr ls www emacs nfsdlpr ls www OS

14 Process and Program Process OS abstraction of a running computation The unit of execution The unit of scheduling Execution state + address space From process perspective a virtual CPU some virtual memory a virtual keyboard, screen, … Program “Blueprint” for a process Passive entity (bits on disk) Code + static data

15 Role of the OS Context Switching Provides illusion that every process owns a CPU Virtual Memory Provides illusion that process owns some memory Device drivers & system calls Provides illusion that process owns a keyboard, … To do: How to start a process? How do processes communicate / coordinate?

16 Role of the OS

17 Creating Processes: Fork

18 How to create a process? Q: How to create a process? A: Double click After boot, OS starts the first process …which in turn creates other processes parent / child  the process tree

19 pstree example $ pstree | view - init-+-NetworkManager-+-dhclient |-apache2 |-chrome-+-chrome | `-chrome |-chrome---chrome |-clementine |-clock-applet |-cron |-cupsd |-firefox---run-mozilla.sh---firefox-bin-+-plugin-cont |-gnome-screensaver |-grep |-in.tftpd |-ntpd `-sshd---sshd---sshd---bash-+-gcc---gcc---cc1 |-pstree |-vim `-view

20 Processes Under UNIX Init is a special case. For others… Q: How does parent process create child process? A: fork() system call Wait. what? int fork() returns TWICE!

21 Example main(int ac, char **av) { int x = getpid(); // get current process ID from OS char *hi = av[1]; // get greeting from command line printf(“I’m process %d\n”, x); int id = fork(); if (id == 0) printf(“%s from %d\n”, hi, getpid()); else printf(“%s from %d, child is %d\n”, hi, getpid(), id); } $ gcc -o strange strange.c $./strange “Hey” I’m process Hey from Hey from 23511, child is 23512

22 Inter-process Communication Parent can pass information to child In fact, all parent data is passed to child But isolated after (C-O-W ensures changes are invisible) Q: How to continue communicating? A: Invent OS “IPC channels” : send(msg), recv(), …

23 Inter-process Communication Parent can pass information to child In fact, all parent data is passed to child But isolated after (C-O-W ensures changes are invisible) Q: How to continue communicating? A: Shared (Virtual) Memory!

24 Processes and Threads

25 Processes are heavyweight Parallel programming with processes: They share almost everything code, shared mem, open files, filesystem privileges, … Pagetables will be almost identical Differences: PC, registers, stack Recall: process = execution context + address space

26 Processes and Threads Process OS abstraction of a running computation The unit of execution The unit of scheduling Execution state + address space From process perspective a virtual CPU some virtual memory a virtual keyboard, screen, … Thread OS abstraction of a single thread of control The unit of scheduling Lives in one single process From thread perspective one virtual CPU core on a virtual multi-core machine

27 Multithreaded Processes

28 Threads #include int counter = 0; void PrintHello(int arg) { printf(“I’m thread %d, counter is %d\n”, arg, counter++);... do some work... pthread_exit(NULL); } int main () { for (t = 0; t < 4; t++) { printf(“in main: creating thread %d\n", t); pthread_create(NULL, NULL, PrintHello, t); } pthread_exit(NULL); }

29 Threads versus Fork in main: creating thread 0 I’m thread 0, counter is 0 in main: creating thread 1 I’m thread 1, counter is 1 in main: creating thread 2 in main: creating thread 3 I’m thread 3, counter is 2 I’m thread 2, counter is 3 If processes?

30 Example Multi-Threaded Program Example: Apache web server void main() { setup(); while (c = accept_connection()) { req = read_request(c); hits[req]++; send_response(c, req); } cleanup(); }

31 Race Conditions Example: Apache web server Each client request handled by a separate thread (in parallel) Some shared state: hit counter,... (look familiar?) Timing-dependent failure  race condition hard to reproduce  hard to debug Thread hits = hits + 1;... Thread hits = hits + 1;... Thread 52 read hits addi write hits Thread 205 read hits addi write hits

32 Programming with threads Within a thread: execution is sequential Between threads? No ordering or timing guarantees Might even run on different cores at the same time Problem: hard to program, hard to reason about Behavior can depend on subtle timing differences Bugs may be impossible to reproduce Cache coherency isn’t sufficient… Need explicit synchronization to make sense of concurrency!

33 Managing Concurrency Races, Critical Sections, and Mutexes

34 Goals Concurrency Goals Liveness Make forward progress Efficiency Make good use of resources Fairness Fair allocation of resources between threads Correctness Threads are isolated (except when they aren’t)

35 Race conditions Race Condition Timing-dependent error when accessing shared state Depends on scheduling happenstance … e.g. who wins “race” to the store instruction? Concurrent Program Correctness = all possible schedules are safe Must consider every possible permutation In other words… … the scheduler is your adversary

36 Critical sections What if we can designate parts of the execution as critical sections Rule: only one thread can be “inside” Thread 52 read hits addi write hits Thread 205 read hits addi write hits

37 Interrupt Disable Q: How to implement critical section in code? A: Lots of approaches…. Disable interrupts? CSEnter() = disable interrupts (including clock) CSExit() = re-enable interrupts Works for some kernel data-structures Very bad idea for user code read hits addi write hits

38 Preemption Disable Q: How to implement critical section in code? A: Lots of approaches…. Modify OS scheduler? CSEnter() = syscall to disable context switches CSExit() = syscall to re-enable context switches Doesn’t work if interrupts are part of the problem Usually a bad idea anyway read hits addi write hits

39 Mutexes Q: How to implement critical section in code? A: Lots of approaches…. Mutual Exclusion Lock (mutex) acquire(m): wait till it becomes free, then lock it release(m): unlock it apache_got_hit() { pthread_mutex_lock(m); hits = hits + 1; pthread_mutex_unlock(m) }

40 Q: How to implement mutexes?