Carnegie Mellon Performance of Mul.threaded Code

Size: px
Start display at page:

Download "Carnegie Mellon Performance of Mul.threaded Code"

Transcription

1 Performance of Mul.threaded Code Karen L. Karavanic Portland State University CS 410/510 Introduc=on to Performance Spring

2 Using Selected Slides from: Concurrent Programming : Introduc=on to Computer Systems 22 nd Lecture, Nov. 11, 2010 Instructors: Randy Bryant and Dave O Hallaron Modified by: Karen L. Karavanic

3 Tradi.onal View of a Process Process = process context + code, data, and stack Process context Code, data, and stack Program context: Data registers Condition codes Stack pointer (SP) Program counter (PC) Kernel context: VM structures Descriptor table brk pointer SP brk PC 0 stack shared libraries run-time heap read/write data read-only code/data 3

4 Alternate View of a Process Process = thread + code, data, and kernel context Thread (main thread) Code and Data SP stack Thread context: Data registers Condition codes Stack pointer (SP) Program counter (PC) brk PC 0 shared libraries run-time heap read/write data read-only code/data Kernel context: VM structures Descriptor table brk pointer 4

5 A Process With Mul.ple Threads Mul.ple threads can be associated with a process Each thread has its own logical control flow Each thread shares the same code, data, and kernel context Share common virtual address space (inc. stacks) Each thread has its own thread id (TID) Thread 1 (main thread) Shared code and data Thread 2 (peer thread) stack 1 Thread 1 context: Data registers Condition codes SP1 PC1 0 shared libraries run-time heap read/write data read-only code/data Kernel context: VM structures Descriptor table brk pointer stack 2 Thread 2 context: Data registers Condition codes SP2 PC2 5

6 Logical View of Threads Threads associated with process form a pool of peers Unlike processes which form a tree hierarchy Threads associated with process foo Process hierarchy T1 T2 shared code, data and kernel context T4 P0 P1 sh sh sh T5 T3 foo bar 6

7 Thread Execu.on Single Core Processor Simulate concurrency by =me slicing Mul.- Core Processor Can have true concurrency Thread A Thread B Thread C Thread A Thread B Thread C Time Run 3 threads on 2 cores 7

8 Logical Concurrency Two threads are (logically) concurrent if their flows overlap in.me Otherwise, they are sequen.al Examples: Concurrent: A & B, A&C Sequen=al: B & C Thread A Thread B Thread C Time 8

9 Threads vs. Processes How threads and processes are similar Each has its own logical control flow Each can run concurrently with others (possibly on different cores) Each is context switched How threads and processes are different Threads share code and some data Processes (typically) do not Threads are somewhat less expensive than processes Process control (crea=ng and reaping) is twice as expensive as thread control Linux numbers: ~20K cycles to create and reap a process ~10K cycles (or less) to create and reap a thread 9

10 Posix Threads (Pthreads) Interface Pthreads: Standard interface for ~60 func.ons that manipulate threads from C programs Crea=ng and reaping threads pthread_create() pthread_join() Determining your thread ID pthread_self() Termina=ng threads pthread_cancel() pthread_exit() exit() [terminates all threads], RET [terminates current thread] Synchronizing access to shared variables pthread_mutex_init pthread_mutex_[un]lock pthread_cond_init pthread_cond_[timed]wait 10

11 The Pthreads "hello, world" Program /* * hello.c - Pthreads "hello, world" program */ #include "csapp.h" void *thread(void *vargp); Thread attributes (usually NULL) int main() { pthread_t tid; } Pthread_create(&tid, NULL, thread, NULL); Pthread_join(tid, NULL); exit(0); /* thread routine */ void *thread(void *vargp) { printf("hello, world!\n"); return NULL; } Thread arguments (void *p) return value (void **p) 11

12 Execu.on of Threaded hello, world main thread call Pthread_create() Pthread_create() returns call Pthread_join() main thread waits for peer thread to terminate Pthread_join() returns peer thread printf() return NULL; (peer thread terminates) exit() terminates main thread and any peer threads 12

13 Threaded Execu.on Model Connection Requests Listening Server Client 1 data Client 1 Server Client 2 Server Client 2 data Mul=ple threads within single process Some state between them File descriptors 13

14 Approaches to Concurrency Processes Hard to share resources: Easy to avoid unintended sharing High overhead in adding/removing clients Threads Easy to share resources: Perhaps too easy Medium overhead Not much control over scheduling policies Difficult to debug Event orderings not repeatable 14

15 Mul.core Processor Core 0 Core n-1 Regs Regs L1 d-cache L1 i-cache L1 d-cache L1 i-cache L2 unified cache L2 unified cache L3 unified cache (shared by all cores) Main memory Intel Nehalem Processor E.g., Shark machines Mul=ple processors opera=ng with coherent view of memory 15

16 Memory Consistency int a = 1; int b = 100; Thread1: Wa: a = 2; Rb: print(b); Thread2: Wb: b = 200; Ra: print(a); Thread consistency constraints Wa Rb Wb Ra What are the possible values printed? Depends on memory consistency model Abstract model of how hardware handles concurrent accesses Sequen.al consistency Overall effect consistent with each individual thread Otherwise, arbitrary interleaving 16

17 Sequen.al Consistency Example int a = 1; int b = 100; Thread consistency constraints Wa Rb Thread1: Wa: a = 2; Rb: print(b); Thread2: Wb: b = 200; Ra: print(a); Wa Wb Rb Wb Ra Rb Ra Wb Ra Rb Ra Wa Rb Impossible outputs 100, 1 and 1, 100 Would require reaching both Ra and Rb before Wa and Wb Wa Wb Ra Rb Ra Rb Ra 100, 2 200, 2 2, 200 1, 200 2, , 2 17

18 Out- of- Order Processor Structure Instruction Control Instruction Decoder Instruction Cache Registers Op. Queue PC Functional Units Integer Arith Integer Arith FP Arith Load / Store Data Cache Instruc.on control dynamically converts program into stream of opera.ons Opera.ons mapped onto func.onal units to execute in parallel 18

19 Hyperthreading Instruction Control Reg A Op. Queue A Instruction Decoder Instruction Cache Reg B Op. Queue B PC A PC B Functional Units Integer Arith Integer Arith FP Arith Load / Store Data Cache Replicate enough instruc.on control to process K instruc.on streams K copies of all registers Share func.onal units 19

20 Summary: Crea.ng Parallel Machines Mul.core Separate instruc=on logic and func=onal units Some shared, some private caches Must implement cache coherency Hyperthreading Also called simultaneous mul=threading Separate program state Shared func=onal units & caches No special control needed for coherency Combining Shark machines: 8 cores, each with 2- way hyperthreading Theore=cal speedup of 16X Never achieved in our benchmarks 20

21 Summa.on Example Sum numbers 0,, N- 1 Should add up to (N- 1)*N/2 Par..on into K ranges N/K values each Accumulate lelover values serially Method #1: All threads update single global variable 1A: No synchroniza=on 1B: Synchronize with pthread semaphore 1C: Synchronize with pthread mutex Binary semaphore. Only values 0 & 1 21

22 Accumula.ng in Single Global Variable: Declara.ons typedef unsigned long data_t; /* Single accumulator */ volatile data_t global_sum; /* Mutex & semaphore for global sum */ sem_t semaphore; pthread_mutex_t mutex; /* Number of elements summed by each thread */ size_t nelems_per_thread; /* Keep track of thread IDs */ pthread_t tid[maxthreads]; /* Identify each thread */ int myid[maxthreads]; 22

23 Accumula.ng in Single Global Variable: Opera.on nelems_per_thread = nelems / nthreads; /* Set global value */ global_sum = 0; /* Create threads and wait for them to finish */ for (i = 0; i < nthreads; i++) { myid[i] = i; Pthread_create(&tid[i], NULL, thread_fun, &myid[i]); } for (i = 0; i < nthreads; i++) Pthread_join(tid[i], NULL); result = global_sum; /* Add leftover elements */ for (e = nthreads * nelems_per_thread; e < nelems; e++) result += e; 23

24 Thread Func.on: No Synchroniza.on void *sum_race(void *vargp) { int myid = *((int *)vargp); size_t start = myid * nelems_per_thread; size_t end = start + nelems_per_thread; size_t i; } for (i = start; i < end; i++) { global_sum += i; } return NULL; 24

25 Unsynchronized Performance N = 2 30 Best speedup = 2.86X Gets wrong answer when > 1 thread! 25

26 Thread Func.on: Semaphore / Mutex Semaphore void *sum_sem(void *vargp) { int myid = *((int *)vargp); size_t start = myid * nelems_per_thread; size_t end = start + nelems_per_thread; size_t i; } for (i = start; i < end; i++) { sem_wait(&semaphore); global_sum += i; sem_post(&semaphore); } return NULL; Mutex pthread_mutex_lock(&mutex); global_sum += i; pthread_mutex_unlock(&mutex); 26

27 Semaphore / Mutex Performance Terrible Performance 2.5 seconds è ~10 minutes Mutex 3X faster than semaphore Clearly, neither is successful 27

28 Separate Accumula.on Method #2: Each thread accumulates into separate variable 2A: Accumulate in con=guous array elements 2B: Accumulate in spaced- apart array elements 2C: Accumulate in registers /* Partial sum computed by each thread */ data_t psum[maxthreads*maxspacing]; /* Spacing between accumulators */ size_t spacing = 1; 28

29 Separate Accumula.on: Opera.on nelems_per_thread = nelems / nthreads; /* Create threads and wait for them to finish */ for (i = 0; i < nthreads; i++) { myid[i] = i; psum[i*spacing] = 0; Pthread_create(&tid[i], NULL, thread_fun, &myid[i]); } for (i = 0; i < nthreads; i++) Pthread_join(tid[i], NULL); result = 0; /* Add up the partial sums computed by each thread */ for (i = 0; i < nthreads; i++) result += psum[i*spacing]; /* Add leftover elements */ for (e = nthreads * nelems_per_thread; e < nelems; e++) result += e; 29

30 Thread Func.on: Memory Accumula.on void *sum_global(void *vargp) { int myid = *((int *)vargp); size_t start = myid * nelems_per_thread; size_t end = start + nelems_per_thread; size_t i; } size_t index = myid*spacing; psum[index] = 0; for (i = start; i < end; i++) { psum[index] += i; } return NULL; 30

31 Memory Accumula.on Performance Clear threading advantage Adjacent speedup: 5 X Spaced- apart speedup: 13.3 X (Only observed speedup > 8) Why does spacing the accumulators apart mamer? 31

32 False Sharing psum Cache Block m Cache Block m+1 Coherency maintained on cache blocks To update psum[i], thread i must have exclusive access Threads sharing common cache block will keep figh=ng each other for access to block 32

33 False Sharing Performance Best spaced- apart performance 2.8 X beper than best adjacent Demonstrates cache block size = byte values No benefit increasing spacing beyond 8 33

34 Thread Func.on: Register Accumula.on void *sum_local(void *vargp) { int myid = *((int *)vargp); size_t start = myid * nelems_per_thread; size_t end = start + nelems_per_thread; size_t i; size_t index = myid*spacing; data_t sum = 0; for (i = start; i < end; i++) { sum += i; } psum[index] = sum; return NULL; } 34

35 Register Accumula.on Performance Clear threading advantage Speedup = 7.5 X 2X bemer than fastest memory accumula.on 35

36 Performance Implications of Multithreading 1. Scalability Questions What is the speedup over a single-threaded version? How much can we improve the performance? 2. Variance Multiple levels of scheduling Complex memory hierarchy Shared Resources 3. Metrics CPU Utilization? Synchronization Overhead Memory 4. Measurement Tools Tool Scalability overhead to measure individual threads? How to Visualize? Carnegie Mellon 36

37 Scalability Carnegie Mellon 1. Scalability Questions What is the speedup over a single-threaded version? How much can we improve the performance? Amdahl s Law 37

38 Performance Implications of Multithreading 1. Variance Indices of Central Tendency Arithmetic Mean Harmonic Mean Weighted Mean Quantifying Variability Sample Variance how are the values distributed, in relation to the mean? Standard Deviation positive square root of the variance Changes the units to match the mean, easier for comparison Coefficient of Variation Standard deviation divided by mean Now there are no units Carnegie Mellon 38

39 Performance Implications of Multithreading Metrics CPU Utilization Average across the cores Total CPU time sum across the cores What about balance? (max, min, variance) Synchronization Overhead Lock wait time Memory Number of cache misses Better cache miss rate Memory contention Memory access latency Carnegie Mellon 39

40 Performance Implications of Multithreading 1. Measurement Tools Tool Scalability overhead to measure individual threads? How to Visualize? Example: HPC Toolkit Carnegie Mellon 40

Thread-Level Parallelism

Thread-Level Parallelism Thread-Level Parallelism 15-213 / 18-213: Introduction to Computer Systems 26 th Lecture, April 27, 2017 Instructors: Seth Copen Goldstein, Franz Franchetti 1 Today Parallel Computing Hardware Multicore

More information

Thread- Level Parallelism

Thread- Level Parallelism Thread- Level Parallelism 15-213 / 18-213: Introduc on to Computer Systems 26 th Lecture, Nov 26, 2012 Instructors: Randy Bryant, Dave O Hallaron, and Greg Kesden 1 Today Parallel Compu ng Hardware Mul

More information

Thread-Level Parallelism. Instructor: Adam C. Champion, Ph.D. CSE 2431: Introduction to Operating Systems Reading: Section 12.

Thread-Level Parallelism. Instructor: Adam C. Champion, Ph.D. CSE 2431: Introduction to Operating Systems Reading: Section 12. Thread-Level Parallelism Instructor: Adam C. Champion, Ph.D. CSE 2431: Introduction to Operating Systems Reading: Section 12.6, [CSAPP] 1 Contents Parallel Computing Hardware Multicore Multiple separate

More information

Concurrent Programming

Concurrent Programming Concurrent Programming 15-213 / 18-213: Introduc2on to Computer Systems 23 rd Lecture, Nov. 14, 2013 Instructors: Randy Bryant, Dave O Hallaron, and Greg Kesden 1 Concurrent Programming is Hard! The human

More information

Concurrent Programming

Concurrent Programming Concurrent Programming 15-213: Introduc0on to Computer Systems 22 nd Lecture, Nov. 11, 2010 Instructors: Randy Bryant and Dave O Hallaron 1 Concurrent Programming is Hard! The human mind tends to be sequen9al

More information

Concurrent Programming

Concurrent Programming Concurrent Programming CS 485G-006: Systems Programming Lectures 32 33: 18 20 Apr 2016 1 Concurrent Programming is Hard! The human mind tends to be sequential The notion of time is often misleading Thinking

More information

Concurrent Programming

Concurrent Programming Concurrent Programming Prof. Jin-Soo Kim( jinsookim@skku.edu) TA Jinhong Kim( jinhong.kim@csl.skku.edu) Computer Systems Laboratory Sungkyunkwan University http://csl.skku.edu Echo Server Revisited int

More information

Concurrent Programming is Hard! Concurrent Programming. Reminder: Iterative Echo Server. The human mind tends to be sequential

Concurrent Programming is Hard! Concurrent Programming. Reminder: Iterative Echo Server. The human mind tends to be sequential Concurrent Programming is Hard! Concurrent Programming 15 213 / 18 213: Introduction to Computer Systems 23 rd Lecture, April 11, 213 Instructors: Seth Copen Goldstein, Anthony Rowe, and Greg Kesden The

More information

Computer Systems Laboratory Sungkyunkwan University

Computer Systems Laboratory Sungkyunkwan University Concurrent Programming Computer Systems Laboratory Sungkyunkwan University http://csl.skku.edu Echo Server Revisited int main (int argc, char *argv[]) {... listenfd = socket(af_inet, SOCK_STREAM, 0); bzero((char

More information

Total Score is updated. The score of PA4 will be changed! Please check it. Will be changed

Total Score is updated. The score of PA4 will be changed! Please check it. Will be changed Announcement Total Score is updated Please check it Will be changed The score of PA4 will be changed! 1 Concurrent Programming Prof. Jin-Soo Kim( jinsookim@skku.edu) TA Sanghoon Han(sanghoon.han@csl.skku.edu)

More information

Concurrent programming

Concurrent programming Concurrent programming : Introduc2on to Computer Systems 24th Lecture Sec2ons 12.1-12.3 Instructors: Ian Foster Gordon Kindlmann Slides borrow copiously from materials by Randy Bryant and Dave O Hallaron

More information

The course that gives CMU its Zip! Concurrency I: Threads April 10, 2001

The course that gives CMU its Zip! Concurrency I: Threads April 10, 2001 15-213 The course that gives CMU its Zip! Concurrency I: Threads April 10, 2001 Topics Thread concept Posix threads (Pthreads) interface Linux Pthreads implementation Concurrent execution Sharing data

More information

Concurrent Programming

Concurrent Programming Concurrent Programming Prof. Jinkyu Jeong( jinkyu@skku.edu) TA Jinhong Kim( jinhong.kim@csl.skku.edu) TA Seokha Shin(seokha.shin@csl.skku.edu) Computer Systems Laboratory Sungkyunkwan University http://csl.skku.edu

More information

Carnegie Mellon. Bryant and O Hallaron, Computer Systems: A Programmer s Perspective, Third Edition

Carnegie Mellon. Bryant and O Hallaron, Computer Systems: A Programmer s Perspective, Third Edition Carnegie Mellon 1 Concurrent Programming 15-213: Introduction to Computer Systems 23 rd Lecture, Nov. 13, 2018 2 Concurrent Programming is Hard! The human mind tends to be sequential The notion of time

More information

Concurrent Programming

Concurrent Programming Concurrent Programming Andrew Case Slides adapted from Mohamed Zahran, Jinyang Li, Clark Barre=, Randy Bryant and Dave O Hallaron 1 Concurrent vs. Parallelism Concurrency: At least two tasks are making

More information

Lecture #23 Concurrent Programming

Lecture #23 Concurrent Programming Lecture #23 Concurrent Programming Nov. 20, 2017 18-600 Foundations of Computer Systems 1 Concurrent Programming is Hard! The human mind tends to be sequential The notion of time is often misleading Thinking

More information

Concurrent Programming

Concurrent Programming Concurrent Programming is Hard! Concurrent Programming Kai Shen The human mind tends to be sequential Thinking about all possible sequences of events in a computer system is at least error prone and frequently

More information

Threading Language and Support. CS528 Multithreading: Programming with Threads. Programming with Threads

Threading Language and Support. CS528 Multithreading: Programming with Threads. Programming with Threads Threading Language and Support CS528 Multithreading: Programming with Threads A Sahu Dept of CSE, IIT Guwahati Pthread: POSIX thread Popular, Initial and Basic one Improved Constructs for threading c++

More information

Approaches to Concurrency

Approaches to Concurrency PROCESS AND THREADS Approaches to Concurrency Processes Hard to share resources: Easy to avoid unintended sharing High overhead in adding/removing clients Threads Easy to share resources: Perhaps too easy

More information

Concurrent Programming

Concurrent Programming Concurrent Programming is Hard! Concurrent Programming Kai Shen The human mind tends to be sequential Thinking about all possible sequences of events in a computer system is at least error prone and frequently

More information

CS 475. Process = Address space + one thread of control Concurrent program = multiple threads of control

CS 475. Process = Address space + one thread of control Concurrent program = multiple threads of control Processes & Threads Concurrent Programs Process = Address space + one thread of control Concurrent program = multiple threads of control Multiple single-threaded processes Multi-threaded process 2 1 Concurrent

More information

Carnegie Mellon. Bryant and O Hallaron, Computer Systems: A Programmer s Perspective, Third Edition

Carnegie Mellon. Bryant and O Hallaron, Computer Systems: A Programmer s Perspective, Third Edition Synchronization: Basics 53: Introduction to Computer Systems 4 th Lecture, November 6, 7 Instructor: Randy Bryant Today Threads review Sharing Mutual exclusion Semaphores 3 Traditional View of a Process

More information

Threads. CS 475, Spring 2018 Concurrent & Distributed Systems

Threads. CS 475, Spring 2018 Concurrent & Distributed Systems Threads CS 475, Spring 2018 Concurrent & Distributed Systems Review: Signals OS has a mechanism to interrupt regular execution of a process We've discussed this in the context of preemption - when a round-robin

More information

Concurrent Programming. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

Concurrent Programming. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University Concurrent Programming Jin-Soo Kim (jinsookim@skku.edu) Computer Systems Laboratory Sungkyunkwan University http://csl.skku.edu Echo Server Revisited int main (int argc, char *argv[]) {... listenfd = socket(af_inet,

More information

Introduction to Threads

Introduction to Threads Computer Systems Introduction to Threads Race Conditions Single- vs. Multi-Threaded Processes Process Process Thread Thread Thread Thread Memory Memory Heap Stack Heap Stack Stack Stack Data Data Code

More information

CS 5523 Operating Systems: Midterm II - reivew Instructor: Dr. Tongping Liu Department Computer Science The University of Texas at San Antonio

CS 5523 Operating Systems: Midterm II - reivew Instructor: Dr. Tongping Liu Department Computer Science The University of Texas at San Antonio CS 5523 Operating Systems: Midterm II - reivew Instructor: Dr. Tongping Liu Department Computer Science The University of Texas at San Antonio Fall 2017 1 Outline Inter-Process Communication (20) Threads

More information

Carnegie Mellon. Synchroniza+on : Introduc+on to Computer Systems Recita+on 14: November 25, Pra+k Shah (pcshah) Sec+on C

Carnegie Mellon. Synchroniza+on : Introduc+on to Computer Systems Recita+on 14: November 25, Pra+k Shah (pcshah) Sec+on C Synchroniza+on 15-213: Introduc+on to Computer Systems Recita+on 14: November 25, 2013 Pra+k Shah (pcshah) Sec+on C 1 Topics News Shared State Race condi+ons Synchroniza+on Mutex Semaphore Readers- writers

More information

call connect call read call connect ret connect call fgets Client 2 blocks waiting to complete its connection request until after lunch!

call connect call read call connect ret connect call fgets Client 2 blocks waiting to complete its connection request until after lunch! 15-213 The course that gives CMU its Zip! Concurrent Servers December 4, 2001 Topics Limitations of iterative servers Process-based concurrent servers Threads-based concurrent servers Event-based concurrent

More information

Today. Threads review Sharing Mutual exclusion Semaphores

Today. Threads review Sharing Mutual exclusion Semaphores SYNCHRONIZATION Today Threads review Sharing Mutual exclusion Semaphores Process: Traditional View Process = process context + code, data, and stack Process context Program context: Data registers Condition

More information

CS 3723 Operating Systems: Final Review

CS 3723 Operating Systems: Final Review CS 3723 Operating Systems: Final Review Outline Threads Synchronizations Pthread Synchronizations Instructor: Dr. Tongping Liu 1 2 Threads: Outline Context Switches of Processes: Expensive Motivation and

More information

CSE 306/506 Operating Systems Threads. YoungMin Kwon

CSE 306/506 Operating Systems Threads. YoungMin Kwon CSE 306/506 Operating Systems Threads YoungMin Kwon Processes and Threads Two characteristics of a process Resource ownership Virtual address space (program, data, stack, PCB ) Main memory, I/O devices,

More information

Concurrent Programming April 21, 2005

Concurrent Programming April 21, 2005 15-213 The course that gives CMU its Zip! Concurrent Programming April 21, 2005 Topics Limitations of iterative servers Process-based concurrent servers Event-based concurrent servers Threads-based concurrent

More information

Parallel Programming with Threads

Parallel Programming with Threads Thread Programming with Shared Memory Parallel Programming with Threads Program is a collection of threads of control. Can be created dynamically, mid-execution, in some languages Each thread has a set

More information

Concurrent Programming November 28, 2007

Concurrent Programming November 28, 2007 15-213 Concurrent Programming November 28, 2007 Topics Limitations of iterative servers Process-based concurrent servers Event-based concurrent servers Threads-based concurrent servers class25.ppt Concurrent

More information

Synchronization: Basics

Synchronization: Basics Synchronization: Basics 53: Introduction to Computer Systems 4 th Lecture, April 8, 7 Instructor: Seth Copen Goldstein, Franz Franchetti Today Threads review Sharing Mutual exclusion Semaphores Traditional

More information

Three Basic Mechanisms for Creating Concurrent Flows. Systemprogrammering 2009 Föreläsning 10 Concurrent Servers. Process-Based Concurrent Server

Three Basic Mechanisms for Creating Concurrent Flows. Systemprogrammering 2009 Föreläsning 10 Concurrent Servers. Process-Based Concurrent Server Systemprogrammering 2009 Föreläsning 10 Concurrent Servers Topics! Limitations of iterative servers! Process-based concurrent servers! Event-based concurrent servers! Threads-based concurrent servers Three

More information

Proxy: Concurrency & Caches

Proxy: Concurrency & Caches Proxy: Concurrency & Caches Elie Krevat (with wisdom from past terms) Outline Proxy Lab logistics Concurrency and multi-threading Synchronization with semaphores Caching objects in your proxy Proxy Lab

More information

Concurrency on x86-64; Threads Programming Tips

Concurrency on x86-64; Threads Programming Tips Concurrency on x86-64; Threads Programming Tips Brad Karp UCL Computer Science CS 3007 22 nd March 2018 (lecture notes derived from material from Eddie Kohler, David Mazières, Phil Gibbons, Dave O Hallaron,

More information

Process Synchroniza/on

Process Synchroniza/on Process Synchroniza/on Andrew Case Slides adapted from Mohamed Zahran, Clark Barre9, Jinyang Li, Randy Bryant and Dave O Hallaron Topics Sharing data Race condi/ons Mutual exclusion and semaphores Sample

More information

CS 333 Introduction to Operating Systems. Class 3 Threads & Concurrency. Jonathan Walpole Computer Science Portland State University

CS 333 Introduction to Operating Systems. Class 3 Threads & Concurrency. Jonathan Walpole Computer Science Portland State University CS 333 Introduction to Operating Systems Class 3 Threads & Concurrency Jonathan Walpole Computer Science Portland State University 1 Process creation in UNIX All processes have a unique process id getpid(),

More information

CS 333 Introduction to Operating Systems. Class 3 Threads & Concurrency. Jonathan Walpole Computer Science Portland State University

CS 333 Introduction to Operating Systems. Class 3 Threads & Concurrency. Jonathan Walpole Computer Science Portland State University CS 333 Introduction to Operating Systems Class 3 Threads & Concurrency Jonathan Walpole Computer Science Portland State University 1 The Process Concept 2 The Process Concept Process a program in execution

More information

Threads. Jo, Heeseung

Threads. Jo, Heeseung Threads Jo, Heeseung Multi-threaded program 빠른실행 프로세스를새로생성에드는비용을절약 데이터공유 파일, Heap, Static, Code 의많은부분을공유 CPU 를보다효율적으로활용 코어가여러개일경우코어에 thread 를할당하는방식 2 Multi-threaded program Pros. Cons. 대량의데이터처리에적합 - CPU

More information

Iterative Servers The course that gives CMU its Zip! Iterative servers process one request at a time. Concurrent Servers

Iterative Servers The course that gives CMU its Zip! Iterative servers process one request at a time. Concurrent Servers 15-213 The course that gives CMU its Zip! Concurrent Servers Dec 3, 2002 Topics! Limitations of iterative servers! Process-based concurrent servers! Event-based concurrent servers! Threads-based concurrent

More information

Recitation 14: Proxy Lab Part 2

Recitation 14: Proxy Lab Part 2 Recitation 14: Proxy Lab Part 2 Instructor: TA(s) 1 Outline Proxylab Threading Threads and Synchronization 2 ProxyLab ProxyLab is due in 1 week. No grace days Late days allowed (-15%) Make sure to submit

More information

CS 5523 Operating Systems: Thread and Implementation

CS 5523 Operating Systems: Thread and Implementation When in the Course of human events, it becomes necessary for one people to dissolve the political bands which have connected them with another, and to assume among the powers of the earth, the separate

More information

Threaded Programming. Lecture 9: Alternatives to OpenMP

Threaded Programming. Lecture 9: Alternatives to OpenMP Threaded Programming Lecture 9: Alternatives to OpenMP What s wrong with OpenMP? OpenMP is designed for programs where you want a fixed number of threads, and you always want the threads to be consuming

More information

CS333 Intro to Operating Systems. Jonathan Walpole

CS333 Intro to Operating Systems. Jonathan Walpole CS333 Intro to Operating Systems Jonathan Walpole Threads & Concurrency 2 Threads Processes have the following components: - an address space - a collection of operating system state - a CPU context or

More information

Concurrent Servers Dec 2, 2009"

Concurrent Servers Dec 2, 2009 Concurrent Servers Dec 2, 2009" Administrivia" 2! Iterative Servers" client 1! server! client 2! call connect ret connect call read ret read close call accept" ret accept" write close call accept" ret

More information

Recitation 14: Proxy Lab Part 2

Recitation 14: Proxy Lab Part 2 Recitation 14: Proxy Lab Part 2 Instructor: TA(s) 1 Outline Proxylab Threading Threads and Synchronization PXYDRIVE Demo 2 ProxyLab Checkpoint is worth 1%, due Thursday, Nov. 29 th Final is worth 7%, due

More information

CS Operating Systems: Threads (SGG 4)

CS Operating Systems: Threads (SGG 4) When in the Course of human events, it becomes necessary for one people to dissolve the political bands which have connected them with another, and to assume among the powers of the earth, the separate

More information

Synchronization: Basics

Synchronization: Basics Synchronization: Basics CS 485G6: Systems Programming Lecture 34: 5 Apr 6 Shared Variables in Threaded C Programs Question: Which variables in a threaded C program are shared? The answer is not as simple

More information

Threads. Jinkyu Jeong Computer Systems Laboratory Sungkyunkwan University

Threads. Jinkyu Jeong Computer Systems Laboratory Sungkyunkwan University Threads Jinkyu Jeong (jinkyu@skku.edu) Computer Systems Laboratory Sungkyunkwan University http://csl.skku.edu EEE3052: Introduction to Operating Systems, Fall 2017, Jinkyu Jeong (jinkyu@skku.edu) Concurrency

More information

CS 3305 Intro to Threads. Lecture 6

CS 3305 Intro to Threads. Lecture 6 CS 3305 Intro to Threads Lecture 6 Introduction Multiple applications run concurrently! This means that there are multiple processes running on a computer Introduction Applications often need to perform

More information

Synchronization: Advanced

Synchronization: Advanced Synchronization: Advanced CS 485G-006: Systems Programming Lecture 35: 27 Apr 2016 1 Enforcing Mutual Exclusion Question: How can we guarantee a safe trajectory? Answer: We must synchronize the execution

More information

ANSI/IEEE POSIX Standard Thread management

ANSI/IEEE POSIX Standard Thread management Pthread Prof. Jinkyu Jeong( jinkyu@skku.edu) TA Jinhong Kim( jinhong.kim@csl.skku.edu) TA Seokha Shin(seokha.shin@csl.skku.edu) Computer Systems Laboratory Sungkyunkwan University http://csl.skku.edu The

More information

A Thread is an independent stream of instructions that can be schedule to run as such by the OS. Think of a thread as a procedure that runs

A Thread is an independent stream of instructions that can be schedule to run as such by the OS. Think of a thread as a procedure that runs A Thread is an independent stream of instructions that can be schedule to run as such by the OS. Think of a thread as a procedure that runs independently from its main program. Multi-threaded programs

More information

POSIX threads CS 241. February 17, Copyright University of Illinois CS 241 Staff

POSIX threads CS 241. February 17, Copyright University of Illinois CS 241 Staff POSIX threads CS 241 February 17, 2012 Copyright University of Illinois CS 241 Staff 1 Recall: Why threads over processes? Creating a new process can be expensive Time A call into the operating system

More information

CS510 Operating System Foundations. Jonathan Walpole

CS510 Operating System Foundations. Jonathan Walpole CS510 Operating System Foundations Jonathan Walpole Threads & Concurrency 2 Why Use Threads? Utilize multiple CPU s concurrently Low cost communication via shared memory Overlap computation and blocking

More information

pthreads CS449 Fall 2017

pthreads CS449 Fall 2017 pthreads CS449 Fall 2017 POSIX Portable Operating System Interface Standard interface between OS and program UNIX-derived OSes mostly follow POSIX Linux, macos, Android, etc. Windows requires separate

More information

CS Lecture 3! Threads! George Mason University! Spring 2010!

CS Lecture 3! Threads! George Mason University! Spring 2010! CS 571 - Lecture 3! Threads! George Mason University! Spring 2010! Threads! Overview! Multithreading! Example Applications! User-level Threads! Kernel-level Threads! Hybrid Implementation! Observing Threads!

More information

Warm-up question (CS 261 review) What is the primary difference between processes and threads from a developer s perspective?

Warm-up question (CS 261 review) What is the primary difference between processes and threads from a developer s perspective? Warm-up question (CS 261 review) What is the primary difference between processes and threads from a developer s perspective? CS 470 Spring 2018 POSIX Mike Lam, Professor Multithreading & Pthreads MIMD

More information

Warm-up question (CS 261 review) What is the primary difference between processes and threads from a developer s perspective?

Warm-up question (CS 261 review) What is the primary difference between processes and threads from a developer s perspective? Warm-up question (CS 261 review) What is the primary difference between processes and threads from a developer s perspective? CS 470 Spring 2019 POSIX Mike Lam, Professor Multithreading & Pthreads MIMD

More information

Outline. CS4254 Computer Network Architecture and Programming. Introduction 2/4. Introduction 1/4. Dr. Ayman A. Abdel-Hamid.

Outline. CS4254 Computer Network Architecture and Programming. Introduction 2/4. Introduction 1/4. Dr. Ayman A. Abdel-Hamid. Threads Dr. Ayman Abdel-Hamid, CS4254 Spring 2006 1 CS4254 Computer Network Architecture and Programming Dr. Ayman A. Abdel-Hamid Computer Science Department Virginia Tech Threads Outline Threads (Chapter

More information

Carnegie Mellon Concurrency and Synchronization

Carnegie Mellon Concurrency and Synchronization Concurrency and Synchronization CMPSCI 3: Computer Systems Principles int pthread_join (pthread_t thread, void **value_ptr) { int result; ptw3_thread_t * tp = (ptw3_thread_t *) thread.p; if (NULL == tp

More information

POSIX Threads. HUJI Spring 2011

POSIX Threads. HUJI Spring 2011 POSIX Threads HUJI Spring 2011 Why Threads The primary motivation for using threads is to realize potential program performance gains and structuring. Overlapping CPU work with I/O. Priority/real-time

More information

Threads (SGG 4) Outline. Traditional Process: Single Activity. Example: A Text Editor with Multi-Activity. Instructor: Dr.

Threads (SGG 4) Outline. Traditional Process: Single Activity. Example: A Text Editor with Multi-Activity. Instructor: Dr. When in the Course of human events, it becomes necessary for one people to dissolve the political bands which have connected them with another, and to assume among the powers of the earth, the separate

More information

Threads. Jo, Heeseung

Threads. Jo, Heeseung Threads Jo, Heeseung Multi-threaded program 빠른실행 프로세스를새로생성에드는비용을절약 데이터공유 파일, Heap, Static, Code 의많은부분을공유 CPU 를보다효율적으로활용 코어가여러개일경우코어에 thread 를할당하는방식 2 Multi-threaded program Pros. Cons. 대량의데이터처리에적합 - CPU

More information

CSE Opera,ng System Principles

CSE Opera,ng System Principles CSE 30341 Opera,ng System Principles Lecture 5 Processes / Threads Recap Processes What is a process? What is in a process control bloc? Contrast stac, heap, data, text. What are process states? Which

More information

Thread and Synchronization

Thread and Synchronization Thread and Synchronization pthread Programming (Module 19) Yann-Hang Lee Arizona State University yhlee@asu.edu (480) 727-7507 Summer 2014 Real-time Systems Lab, Computer Science and Engineering, ASU Pthread

More information

THREADS. Jo, Heeseung

THREADS. Jo, Heeseung THREADS Jo, Heeseung TODAY'S TOPICS Why threads? Threading issues 2 PROCESSES Heavy-weight A process includes many things: - An address space (all the code and data pages) - OS resources (e.g., open files)

More information

CS3733: Operating Systems

CS3733: Operating Systems Outline CS3733: Operating Systems Topics: Synchronization, Critical Sections and Semaphores (SGG Chapter 6) Instructor: Dr. Tongping Liu 1 Memory Model of Multithreaded Programs Synchronization for coordinated

More information

High Performance Computing Course Notes Shared Memory Parallel Programming

High Performance Computing Course Notes Shared Memory Parallel Programming High Performance Computing Course Notes 2009-2010 2010 Shared Memory Parallel Programming Techniques Multiprocessing User space multithreading Operating system-supported (or kernel) multithreading Distributed

More information

Why do we care about parallel?

Why do we care about parallel? Threads 11/15/16 CS31 teaches you How a computer runs a program. How the hardware performs computations How the compiler translates your code How the operating system connects hardware and software The

More information

CS 261 Fall Mike Lam, Professor. Threads

CS 261 Fall Mike Lam, Professor. Threads CS 261 Fall 2017 Mike Lam, Professor Threads Parallel computing Goal: concurrent or parallel computing Take advantage of multiple hardware units to solve multiple problems simultaneously Motivations: Maintain

More information

Ricardo Rocha. Department of Computer Science Faculty of Sciences University of Porto

Ricardo Rocha. Department of Computer Science Faculty of Sciences University of Porto Ricardo Rocha Department of Computer Science Faculty of Sciences University of Porto For more information please consult Advanced Programming in the UNIX Environment, 3rd Edition, W. Richard Stevens and

More information

CS 5523: Operating Systems

CS 5523: Operating Systems CS 5523: Operating Systems Instructor: Dr. Tongping Liu Midterm Exam: Oct 6, 2015, Tuesday 7:15pm 8:30pm CS5523: Operating Systems @ UTSA 1 Lecture1: OS Overview Operating System: what is it?! Evolution

More information

HPCSE - I. «Introduction to multithreading» Panos Hadjidoukas

HPCSE - I. «Introduction to multithreading» Panos Hadjidoukas HPCSE - I «Introduction to multithreading» Panos Hadjidoukas 1 Processes and Threads POSIX Threads API Outline Thread management Synchronization with mutexes Deadlock and thread safety 2 Terminology -

More information

Preview. What are Pthreads? The Thread ID. The Thread ID. The thread Creation. The thread Creation 10/25/2017

Preview. What are Pthreads? The Thread ID. The Thread ID. The thread Creation. The thread Creation 10/25/2017 Preview What are Pthreads? What is Pthreads The Thread ID The Thread Creation The thread Termination The pthread_join() function Mutex The pthread_cancel function The pthread_cleanup_push() function The

More information

Multithreading Programming II

Multithreading Programming II Multithreading Programming II Content Review Multithreading programming Race conditions Semaphores Thread safety Deadlock Review: Resource Sharing Access to shared resources need to be controlled to ensure

More information

Concurrent Server Design Multiple- vs. Single-Thread

Concurrent Server Design Multiple- vs. Single-Thread Concurrent Server Design Multiple- vs. Single-Thread Chuan-Ming Liu Computer Science and Information Engineering National Taipei University of Technology Fall 2007, TAIWAN NTUT, TAIWAN 1 Examples Using

More information

Introduction to PThreads and Basic Synchronization

Introduction to PThreads and Basic Synchronization Introduction to PThreads and Basic Synchronization Michael Jantz, Dr. Prasad Kulkarni Dr. Douglas Niehaus EECS 678 Pthreads Introduction Lab 1 Introduction In this lab, we will learn about some basic synchronization

More information

LSN 13 Linux Concurrency Mechanisms

LSN 13 Linux Concurrency Mechanisms LSN 13 Linux Concurrency Mechanisms ECT362 Operating Systems Department of Engineering Technology LSN 13 Creating Processes fork() system call Returns PID of the child process created The new process is

More information

CSC 447: Parallel Programming for Multi-Core and Cluster Systems

CSC 447: Parallel Programming for Multi-Core and Cluster Systems CSC 447: Parallel Programming for Multi-Core and Cluster Systems Shared Memory Programming Using POSIX Threads Instructor: Haidar M. Harmanani Spring 2018 Multicore vs. Manycores A multicore processor

More information

CS-345 Operating Systems. Tutorial 2: Grocer-Client Threads, Shared Memory, Synchronization

CS-345 Operating Systems. Tutorial 2: Grocer-Client Threads, Shared Memory, Synchronization CS-345 Operating Systems Tutorial 2: Grocer-Client Threads, Shared Memory, Synchronization Threads A thread is a lightweight process A thread exists within a process and uses the process resources. It

More information

CS345 Opera,ng Systems. Φροντιστήριο Άσκησης 2

CS345 Opera,ng Systems. Φροντιστήριο Άσκησης 2 CS345 Opera,ng Systems Φροντιστήριο Άσκησης 2 Inter- process communica0on Exchange data among processes Methods Signals Pipes Sockets Shared Memory Sockets Endpoint of communica,on link between two programs

More information

Threads. Threads (continued)

Threads. Threads (continued) Threads A thread is an alternative model of program execution A process creates a thread through a system call Thread operates within process context Use of threads effectively splits the process state

More information

CS510 Operating System Foundations. Jonathan Walpole

CS510 Operating System Foundations. Jonathan Walpole CS510 Operating System Foundations Jonathan Walpole The Process Concept 2 The Process Concept Process a program in execution Program - description of how to perform an activity instructions and static

More information

Thread. Disclaimer: some slides are adopted from the book authors slides with permission 1

Thread. Disclaimer: some slides are adopted from the book authors slides with permission 1 Thread Disclaimer: some slides are adopted from the book authors slides with permission 1 IPC Shared memory Recap share a memory region between processes read or write to the shared memory region fast

More information

CPSC 341 OS & Networks. Threads. Dr. Yingwu Zhu

CPSC 341 OS & Networks. Threads. Dr. Yingwu Zhu CPSC 341 OS & Networks Threads Dr. Yingwu Zhu Processes Recall that a process includes many things An address space (defining all the code and data pages) OS resources (e.g., open files) and accounting

More information

CSE 333 SECTION 9. Threads

CSE 333 SECTION 9. Threads CSE 333 SECTION 9 Threads HW4 How s HW4 going? Any Questions? Threads Sequential execution of a program. Contained within a process. Multiple threads can exist within the same process. Every process starts

More information

Threads. lightweight processes

Threads. lightweight processes Threads lightweight processes 1 Motivation Processes are expensive to create. It takes quite a bit of time to switch between processes Communication between processes must be done through an external kernel

More information

CSC 252: Computer Organization Spring 2018: Lecture 26

CSC 252: Computer Organization Spring 2018: Lecture 26 CSC 252: Computer Organization Spring 2018: Lecture 26 Instructor: Yuhao Zhu Department of Computer Science University of Rochester Action Items: Programming Assignment 4 grades out Programming Assignment

More information

Threads. What is a thread? Motivation. Single and Multithreaded Processes. Benefits

Threads. What is a thread? Motivation. Single and Multithreaded Processes. Benefits CS307 What is a thread? Threads A thread is a basic unit of CPU utilization contains a thread ID, a program counter, a register set, and a stack shares with other threads belonging to the same process

More information

ENCM 501 Winter 2019 Assignment 9

ENCM 501 Winter 2019 Assignment 9 page 1 of 6 ENCM 501 Winter 2019 Assignment 9 Steve Norman Department of Electrical & Computer Engineering University of Calgary April 2019 Assignment instructions and other documents for ENCM 501 can

More information

Last class: Today: Thread Background. Thread Systems

Last class: Today: Thread Background. Thread Systems 1 Last class: Thread Background Today: Thread Systems 2 Threading Systems 3 What kind of problems would you solve with threads? Imagine you are building a web server You could allocate a pool of threads,

More information

Computer Systems A Programmer s Perspective 1 (Beta Draft)

Computer Systems A Programmer s Perspective 1 (Beta Draft) Computer Systems A Programmer s Perspective 1 (Beta Draft) Randal E. Bryant David R. O Hallaron August 1, 2001 1 Copyright c 2001, R. E. Bryant, D. R. O Hallaron. All rights reserved. 2 Contents Preface

More information

Pthreads. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

Pthreads. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University Pthreads Jin-Soo Kim (jinsookim@skku.edu) Computer Systems Laboratory Sungkyunkwan University http://csl.skku.edu The Pthreads API ANSI/IEEE POSIX1003.1-1995 Standard Thread management Work directly on

More information

EPL372 Lab Exercise 2: Threads and pthreads. Εργαστήριο 2. Πέτρος Παναγή

EPL372 Lab Exercise 2: Threads and pthreads. Εργαστήριο 2. Πέτρος Παναγή EPL372 Lab Exercise 2: Threads and pthreads Εργαστήριο 2 Πέτρος Παναγή 1 Threads Vs Processes 2 Process A process is created by the operating system, and requires a fair amount of "overhead". Processes

More information

Synchroniza+on II COMS W4118

Synchroniza+on II COMS W4118 Synchroniza+on II COMS W4118 References: Opera+ng Systems Concepts (9e), Linux Kernel Development, previous W4118s Copyright no2ce: care has been taken to use only those web images deemed by the instructor

More information

Unix. Threads & Concurrency. Erick Fredj Computer Science The Jerusalem College of Technology

Unix. Threads & Concurrency. Erick Fredj Computer Science The Jerusalem College of Technology Unix Threads & Concurrency Erick Fredj Computer Science The Jerusalem College of Technology 1 Threads Processes have the following components: an address space a collection of operating system state a

More information