CS 33. Multithreaded Programming V. CS33 Intro to Computer Systems XXXVI 1 Copyright 2018 Thomas W. Doeppner. All rights reserved.
|
|
- Logan Pitts
- 5 years ago
- Views:
Transcription
1 CS 33 Multithreaded Programming V CS33 Intro to Computer Systems XXXVI 1 Copyright 2018 Thomas W. Doeppner. All rights reserved.
2 Alternatives to Mutexes: Atomic Instructions Read-modify-write performed atomically Lock prefix may be used with certain IA32 and x86-64 instructions to make this happen lock incr x lock add $2, x It s expensive It s not portable no POSIX-threads way of doing it Windows supports» InterlockedIncrement» InterlockedDecrement CS33 Intro to Computer Systems XXXVI 2 Copyright 2018 Thomas W. Doeppner. All rights reserved.
3 Alternatives to Mutexes: Spin Locks Consider pthread_mutex_lock(&mutex); new->next = list_ele->next; list_ele->next = new; pthread_mutex_unlock(&mutex); A lot of overhead is required to put thread to sleep, then wake it up Rather than do that, repeatedly test mutex until it s unlocked, then lock it makes sense only on multiprocessor system CS33 Intro to Computer Systems XXXVI 3 Copyright 2018 Thomas W. Doeppner. All rights reserved.
4 Quiz 1 pthread_mutex_lock(&mutex); new->next = list_ele->next; list_ele->next = new; pthread_mutex_unlock(&mutex); The thread that has locked the mutex might yield the processor to another thread due to time slicing. This will necessarily cause threads waiting for the mutex to wait longer. Will this result in those threads using significant additional processor time? a) yes b) no CS33 Intro to Computer Systems XXXVI 4 Copyright 2018 Thomas W. Doeppner. All rights reserved.
5 Compare and Exchange cmpxchg src_reg, dest compare contents of %rax with contents of dest» if equal, then dest = src_reg (and ZF = 1)» otherwise %rax = dest (and ZF = 0) CS33 Intro to Computer Systems XXXVI 5 Copyright 2018 Thomas W. Doeppner. All rights reserved.
6 Spin Lock the spin lock is pointed to by the first arg (%rdi) locked is 1, unlocked is 0.text.globl slock, sunlock slock: loop: movq $0, %rax movq $1, %r10 lock cmpxchg %r10, 0(%rdi) jne loop ret sunlock: movq $0, 0(%rdi) ret CS33 Intro to Computer Systems XXXVI 6 Copyright 2018 Thomas W. Doeppner. All rights reserved.
7 Improved Spin Lock slock: loop:.text.globl slock, sunlock cmp $0, 0(%rdi) # compare using normal instructions jne loop movq $0, %rax movq $1, %r10 lock cmpxchg %r10, 0(%rdi) # verify w/ cmpxchg jne loop ret sunlock: movq $0, 0(%rdi) ret CS33 Intro to Computer Systems XXXVI 7 Copyright 2018 Thomas W. Doeppner. All rights reserved.
8 Yet More From POSIX int pthread_spin_init(pthread_spin_t *s, int pshared); int pthread_spin_destroy(pthread_spin_t *s); int pthread_spin_lock(pthread_spin_t *s); int pthread_spin_trylock(pthread_spin_t *s); int pthread_spin_unlock(pthread_spin_t *s); CS33 Intro to Computer Systems XXXVI 8 Copyright 2018 Thomas W. Doeppner. All rights reserved.
9 Using Spin Locks pthread_spin_lock(&spinlock); new->next = list_ele->next; list_ele->next = new; pthread_spin_unlock(&spinlock); CS33 Intro to Computer Systems XXXVI 9 Copyright 2018 Thomas W. Doeppner. All rights reserved.
10 Quiz 2 pthread_spin_lock(&spinlock); new->next = list_ele->next; list_ele->next = new; pthread_spin_unlock(&spinlock); The thread that has locked the spin lock might yield the processor to another thread due to time slicing. This will necessarily cause threads waiting for the spin lock to wait longer. Will this result in those threads using significant additional processor time? a) yes b) no CS33 Intro to Computer Systems XXXVI 10 Copyright 2018 Thomas W. Doeppner. All rights reserved.
11 A Problem... In thread 1: In thread 2: if ((ret = open(path, if ((ret = socket(af_inet, O_RDWR) == -1) { SOCK_STREAM, 0)) { if (errno == EINTR) { if (errno == ENOMEM) { There s only one errno! However, somehow it works. What s done??? CS33 Intro to Computer Systems XXXVI 11 Copyright 2018 Thomas W. Doeppner. All rights reserved.
12 A Solution... #define errno (* errno_location()) errno_location returns an int * that s different for each thread thus each thread has, effectively, its own copy of errno CS33 Intro to Computer Systems XXXVI 12 Copyright 2018 Thomas W. Doeppner. All rights reserved.
13 Process Address Space errno Stack, etc. Thread 1 errno Stack, etc. Thread 2 errno Stack, etc. Thread 3 Dynamic Data Text CS33 Intro to Computer Systems XXXVI 13 Copyright 2018 Thomas W. Doeppner. All rights reserved.
14 Generalizing Thread-specific data (sometimes called thread-local storage) data that s referred to by global variables, but each thread has its own private copy thread 1 tsd[0] tsd[1] tsd[2] tsd[3] tsd[4] tsd[5] tsd[6] tsd[7] thread 2 tsd[0] tsd[1] tsd[2] tsd[3] tsd[4] tsd[5] tsd[6] tsd[7] CS33 Intro to Computer Systems XXXVI 14 Copyright 2018 Thomas W. Doeppner. All rights reserved.
15 Some Machinery pthread_key_create(&key, cleanup_routine) allocates a slot in the TSD arrays provides a function to cleanup when threads terminate value = pthread_getspecific(key) fetches from the calling thread s array pthread_setspecific(key, value) stores into the calling thread s array CS33 Intro to Computer Systems XXXVI 15 Copyright 2018 Thomas W. Doeppner. All rights reserved.
16 Beyond POSIX TLS Extensions for ELF and gcc Thread Local Storage (TLS) thread int x=6; // Each thread has its own copy of x, // each initialized to 6. // Linker and compiler do the setup. // May be combined with static or extern. // Doesn t make sense for local variables! CS33 Intro to Computer Systems XXXVI 16 Copyright 2018 Thomas W. Doeppner. All rights reserved.
17 Example: Per-Thread Windows typedef struct { wcontext_t win_context; int file_descriptor; win_t; thread static win_t my_win; void getwindow() { my_win.win_context = ; my_win.file_decriptor = ; int threadwrite(char *buf) { int status = write_to_window( &my_win, buf); return(status); void *tfunc(void * arg) { getwindow(); threadwrite("started"); func2( ); void func2( ) { threadwrite( "important msg"); CS33 Intro to Computer Systems XXXVI 17 Copyright 2018 Thomas W. Doeppner. All rights reserved.
18 Static Local Storage char *strtok(char *str, const char *delim) { static char *saveptr;... // find next token starting at either... // str or saveptr... // update saveptr return(&token); CS33 Intro to Computer Systems XXXVI 18 Copyright 2018 Thomas W. Doeppner. All rights reserved.
19 Coping Use thread local storage Allocate storage internally; caller frees it Redesign the interface CS33 Intro to Computer Systems XXXVI 19 Copyright 2018 Thomas W. Doeppner. All rights reserved.
20 Thread-Safe Version char *strtok_r(char *str, const char *delim, char **saveptr) {... // find next token starting at either... // str or *saveptr... // update *saveptr return(&token); CS33 Intro to Computer Systems XXXVI 20 Copyright 2018 Thomas W. Doeppner. All rights reserved.
21 Shared Data Thread 1: printf("goto statement reached"); Thread 2: printf("hello World\n"); Printed on display: go to Hell CS33 Intro to Computer Systems XXXVI 21 Copyright 2018 Thomas W. Doeppner. All rights reserved.
22 Coping Wrap library calls with synchronization constructs Fix the libraries CS33 Intro to Computer Systems XXXVI 22 Copyright 2018 Thomas W. Doeppner. All rights reserved.
23 Efficiency Standard I/O example getc() and putc()» expensive and thread-safe?» cheap and not thread-safe? two versions» getc() and putc() expensive and thread-safe» getc_unlocked() and putc_unlocked() cheap and not thread-safe made thread-safe with flockfile() and funlockfile() CS33 Intro to Computer Systems XXXVI 23 Copyright 2018 Thomas W. Doeppner. All rights reserved.
24 Efficiency Naive Efficient for(i=0; i<lim; i++) putc(out[i]); flockfile(stdout); for(i=0; i<lim; i++) putc_unlocked(out[i]); funlockfile(stdout); CS33 Intro to Computer Systems XXXVI 24 Copyright 2018 Thomas W. Doeppner. All rights reserved.
25 What s Thread-Safe? Everything except asctime() basename() catgets() crypt() ctime() dbm_clearerr() dbm_close() dbm_delete() dbm_error() dbm_fetch() dbm_firstkey() dbm_nextkey() dbm_open() dbm_store() dirname() dlerror() drand48() ecvt() encrypt() endgrent() endpwent() endutxent() fcvt() ftw() gcvt() getc_unlocked() getchar_unlocked() getdate() getenv() getgrent() getgrgid() getgrnam() gethostbyaddr() gethostbyname() gethostent() getlogin() getnetbyaddr() getnetbyname() getnetent() getopt() getprotobyname() getprotobynumber() getprotoent() getpwent() getpwnam() getpwuid() getservbyname() getservbyport() getservent() getutxent() getutxid() getutxline() gmtime() hcreate() hdestroy() hsearch() inet_ntoa() l64a() lgamma() lgammaf() lgammal() localeconv() localtime() lrand48() mrand48() nftw() nl_langinfo() ptsname() putc_unlocked() putchar_unlocked() putenv() pututxline() rand() readdir() setenv() setgrent() setkey() setpwent() setutxent() strerror() strtok() ttyname() unsetenv() wcstombs() wctomb() CS33 Intro to Computer Systems XXXVI 25 Copyright 2018 Thomas W. Doeppner. All rights reserved.
26 Fork and Threads fork Or fork CS33 Intro to Computer Systems XXXVI 26 Copyright 2018 Thomas W. Doeppner. All rights reserved.
27 Processes vs. Threads Process 1 Process 2 Process 3 CS33 Intro to Computer Systems XXXVI 27 Copyright 2018 Thomas W. Doeppner. All rights reserved.
28 Communicating via Shared Memory Shared Memory Process 1 Process 2 CS33 Intro to Computer Systems XXXVI 28 Copyright 2018 Thomas W. Doeppner. All rights reserved.
29 Cross-Process Synchronization pthread_mutexattr_t mattr; pthread_condattr_t cattr; pthread_mutex_t mut; pthread_cond_t cond; pthread_mutexattr_init(&mattr); pthread_condattr_init(&cattr); pthread_mutexattr_setpshared(&mattr, PTHREAD_PROCESS_SHARED); pthread_condattr_setpshared(&cattr, PTHREAD_PROCESS_SHARED); pthread_mutex_init(&mut, &mattr); pthread_cond_init(&cond, &cattr); CS33 Intro to Computer Systems XXXVI 29 Copyright 2018 Thomas W. Doeppner. All rights reserved.
30 Cross-Process Producer-Consumer (1) typedef struct buffer { char buf[bsize]; sem_t filled; sem_t empty; int nextin; int nextout; buffer_t; CS33 Intro to Computer Systems XXXVI 30 Copyright 2018 Thomas W. Doeppner. All rights reserved.
31 Cross-Process Producer-Consumer (2) int main() { buffer_t *buffer; if ((buffer = mmap(0, sizeof(buffer_t), PROT_READ PROT_WRITE, MAP_SHARED MAP_ANONYMOUS, -1, 0)) == (void *)-1) { perror("mmap"); exit(1); CS33 Intro to Computer Systems XXXVI 31 Copyright 2018 Thomas W. Doeppner. All rights reserved.
32 Cross-Process Producer-Consumer (3) buffer->nextin = buffer->nextout = 0; sem_init(&buffer->filled, 1, 0); sem_init(&buffer->empty, 1, BSIZE); if (fork() == 0) consumer_driver(buffer); else producer_driver(buffer); return 0; CS33 Intro to Computer Systems XXXVI 32 Copyright 2018 Thomas W. Doeppner. All rights reserved.
33 Cross-Process Producer-Consumer (4) void producer_driver( buffer_t *b) { int item; while (1) { item = getchar(); if (item == EOF) { produce(b, '\0'); break; else { produce(b, (char)item); void consumer_driver( buffer_t *b) { char item; while (1) { if ((item = consume(b)) == '\0') break; putchar(item); CS33 Intro to Computer Systems XXXVI 33 Copyright 2018 Thomas W. Doeppner. All rights reserved.
34 Cross-Process Producer-Consumer (5) void produce(buffer_t *b, char item) { sem_wait(&b->empty); b->buf[b->nextin] = item; b->nextin++; b->nextin %= BSIZE; sem_post(&b->filled); char consume(buffer_t *b) { char item; sem_wait(&b->filled); item = b->buf[b->nextout]; b->nextout++; b->nextout %= BSIZE; sem_post(&b->empty); return item; CS33 Intro to Computer Systems XXXVI 34 Copyright 2018 Thomas W. Doeppner. All rights reserved.
35 Multi-Core Processor: Simple View Cores Memory CS33 Intro to Computer Systems XXXVI 35 Copyright 2018 Thomas W. Doeppner. All rights reserved.
36 Multi-Core Processor: More Realistic View Cores L1Caches Bus Memory CS33 Intro to Computer Systems XXXVI 36 Copyright 2018 Thomas W. Doeppner. All rights reserved.
37 Multi-Core Processor: Even More Realistic buffer buffer buffer buffer Cores L1 Caches Bus Memory CS33 Intro to Computer Systems XXXVI 37 Copyright 2018 Thomas W. Doeppner. All rights reserved.
38 Concurrent Reading and Writing Thread 1: Thread 2: i = shared_counter; shared_counter++; CS33 Intro to Computer Systems XXXVI 38 Copyright 2018 Thomas W. Doeppner. All rights reserved.
39 Mutual Exclusion w/o Mutexes void peterson(long me) { static long loser; // shared static long active[2] = {0, 0; // shared long other = 1 me; // private active[me] = 1; loser = me; while (loser == me && active[other]) ; // critical section active[me] = 0; CS33 Intro to Computer Systems XXXVI 39 Copyright 2018 Thomas W. Doeppner. All rights reserved.
40 Quiz 3 void peterson(long me) { static long loser; // shared static long active[2] = {0, 0; // shared long other = 1 me; // private active[me] = 1; loser = me; while (loser == me && active[other]) ; // critical section active[me] = 0; This works on sunlab machines. a) true b) false CS33 Intro to Computer Systems XXXVI 40 Copyright 2018 Thomas W. Doeppner. All rights reserved.
41 Busy-Waiting Producer/Consumer void producer(char item) { while(in out == BSIZE) ; char consumer( ) { char item; while(in out == 0) ; buf[in%bsize] = item; item = buf[out%bsize]; in++; out++; return(item); CS33 Intro to Computer Systems XXXVI 41 Copyright 2018 Thomas W. Doeppner. All rights reserved.
42 Quiz 4 void producer(char item) { while(in out == BSIZE) ; char consumer( ) { char item; while(in out == 0) ; buf[in%bsize] = item; item = buf[out%bsize]; in++; out++; This works on sunlab machines. a) true b) false return(item); CS33 Intro to Computer Systems XXXVI 42 Copyright 2018 Thomas W. Doeppner. All rights reserved.
43 Coping Don t rely on shared memory for synchronization Use the synchronization primitives CS33 Intro to Computer Systems XXXVI 43 Copyright 2018 Thomas W. Doeppner. All rights reserved.
44 Which Runs Faster? volatile int a, b; void *thread1(void *arg) { int i; for (i=0; i<reps; i++) { a = 1; void *thread2(void *arg) { int i; for (i=0; i<reps; i++) { b = 1; volatile int a, padding[128], b; void *thread1(void *arg) { int i; for (i=0; i<reps; i++) { a = 1; void *thread2(void *arg) { int i; for (i=0; i<reps; i++) { b = 1; CS33 Intro to Computer Systems XXXVI 44 Copyright 2018 Thomas W. Doeppner. All rights reserved.
45 Cache Lines Address Tag Index Offset L1 Cache Tag Data Cache Line CS33 Intro to Computer Systems XXXVI 45 Copyright 2018 Thomas W. Doeppner. All rights reserved.
46 False Sharing L1 Cache L1 Cache Tag a b Cache Line Tag a b Cache Line CS33 Intro to Computer Systems XXXVI 46 Copyright 2018 Thomas W. Doeppner. All rights reserved.
CS 33. Multithreaded Programming III. CS33 Intro to Computer Systems XXXIV 1 Copyright 2017 Thomas W. Doeppner. All rights reserved.
CS 33 Multithreaded Programming III CS33 Intro to Computer Systems XXXIV 1 Copyright 2017 Thomas W. Doeppner. All rights reserved. Start/Stop Start/Stop interface void wait_for_start(state_t *s); void
More informationLocks and semaphores. Johan Montelius KTH
Locks and semaphores Johan Montelius KTH 2017 1 / 41 recap, what s the problem : # include < pthread.h> volatile int count = 0; void * hello ( void * arg ) { for ( int i = 0; i < 10; i ++) { count ++;
More informationLocks and semaphores. Johan Montelius KTH
Locks and semaphores Johan Montelius KTH 2018 1 / 40 recap, what s the problem : # include < pthread.h> volatile int count = 0; void * hello ( void * arg ) { for ( int i = 0; i < 10; i ++) { count ++;
More informationTHREADS. 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 informationSynchronization and Semaphores. Copyright : University of Illinois CS 241 Staff 1
Synchronization and Semaphores Copyright : University of Illinois CS 241 Staff 1 Synchronization Primatives Counting Semaphores Permit a limited number of threads to execute a section of the code Binary
More informationCS 153 Lab6. Kishore Kumar Pusukuri
Outline Mutex vs Condition Variables Unlocking and locking mutex leads spinning or polling, wastes CPU time. We could sleep for some amount of time, but we do not know how long to sleep. A mutex is for
More informationrecap, what s the problem Locks and semaphores Total Store Order Peterson s algorithm Johan Montelius 0 0 a = 1 b = 1 read b read a
recap, what s the problem Locks and semaphores Johan Montelius KTH 2017 # include < pthread.h> volatile int count = 0; void * hello ( void * arg ) { for ( int i = 0; i < 10; i ++) { count ++; int main
More informationComputer Systems Laboratory Sungkyunkwan University
Threads Jin-Soo Kim (jinsookim@skku.edu) Computer Systems Laboratory Sungkyunkwan University http://csl.skku.edu Today s Topics Why threads? Threading issues 2 Processes Heavy-weight A process includes
More informationCS 470 Spring Mike Lam, Professor. Semaphores and Conditions
CS 470 Spring 2018 Mike Lam, Professor Semaphores and Conditions Synchronization mechanisms Busy-waiting (wasteful!) Atomic instructions (e.g., LOCK prefix in x86) Pthreads Mutex: simple mutual exclusion
More informationThreads. 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 informationConcurrency 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 informationCS5460: Operating Systems
CS5460: Operating Systems Lecture 9: Implementing Synchronization (Chapter 6) Multiprocessor Memory Models Uniprocessor memory is simple Every load from a location retrieves the last value stored to that
More informationMultithreading 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 informationWarm-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 informationPERL - FUNCTIONS REFERENCES
PERL - FUNCTIONS REFERENCES http://www.tutorialspoint.com/perl/perl_function_references.htm Copyright tutorialspoint.com Here is the list of all the important functions supported by standard Perl. abs
More informationSynchronization and Semaphores. Copyright : University of Illinois CS 241 Staff 1
Synchronization and Semaphores Copyright : University of Illinois CS 241 Staff 1 Synchronization Primatives Counting Semaphores Permit a limited number of threads to execute a section of the code Binary
More informationThreads. Threads are an alternative to multiple processes. Different flows (or sequences) of execution operate on the same data (heap).
Threads 1/66 Threads Threads are an alternative to multiple processes. Different flows (or sequences) of execution operate on the same data (heap). 2/66 Thread (Solaris) Model Traditional processes User
More informationCS-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 informationOperating Systems CMPSCI 377 Spring Mark Corner University of Massachusetts Amherst
Operating Systems CMPSCI 377 Spring 2017 Mark Corner University of Massachusetts Amherst What is a Monitor? Ties data and the synchronization operations together Monitors guarantee mutual exclusion, i.e.,
More informationThreads. Jo, Heeseung
Threads Jo, Heeseung Multi-threaded program 빠른실행 프로세스를새로생성에드는비용을절약 데이터공유 파일, Heap, Static, Code 의많은부분을공유 CPU 를보다효율적으로활용 코어가여러개일경우코어에 thread 를할당하는방식 2 Multi-threaded program Pros. Cons. 대량의데이터처리에적합 - CPU
More informationCS 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 informationThreads. 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 informationConcurrent 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 informationSystèmes d Exploitation Avancés
Systèmes d Exploitation Avancés Instructor: Pablo Oliveira ISTY Instructor: Pablo Oliveira (ISTY) Systèmes d Exploitation Avancés 1 / 32 Review : Thread package API tid thread create (void (*fn) (void
More informationCSci 4061 Introduction to Operating Systems. Synchronization Basics: Locks
CSci 4061 Introduction to Operating Systems Synchronization Basics: Locks Synchronization Outline Basics Locks Condition Variables Semaphores Basics Race condition: threads + shared data Outcome (data
More informationCOMP 3430 Robert Guderian
Operating Systems COMP 3430 Robert Guderian file:///users/robg/dropbox/teaching/3430-2018/slides/04_threads/index.html?print-pdf#/ 1/58 1 Threads Last week: Processes This week: Lesser processes! file:///users/robg/dropbox/teaching/3430-2018/slides/04_threads/index.html?print-pdf#/
More informationPosix Threads (Pthreads)
Posix Threads (Pthreads) Reference: Programming with POSIX Threads by David R. Butenhof, Addison Wesley, 1997 Threads: Introduction main: startthread( funk1 ) startthread( funk1 ) startthread( funk2 )
More informationParallel Programming: Background Information
1 Parallel Programming: Background Information Mike Bailey mjb@cs.oregonstate.edu parallel.background.pptx Three Reasons to Study Parallel Programming 2 1. Increase performance: do more work in the same
More informationParallel Programming: Background Information
1 Parallel Programming: Background Information Mike Bailey mjb@cs.oregonstate.edu parallel.background.pptx Three Reasons to Study Parallel Programming 2 1. Increase performance: do more work in the same
More informationData Races and Deadlocks! (or The Dangers of Threading) CS449 Fall 2017
Data Races and Deadlocks! (or The Dangers of Threading) CS449 Fall 2017 Data Race Shared Data: 465 1 8 5 6 209? tail A[] thread switch Enqueue(): A[tail] = 20; tail++; A[tail] = 9; tail++; Thread 0 Thread
More informationPthreads (2) Dong-kun Shin Embedded Software Laboratory Sungkyunkwan University Embedded Software Lab.
1 Pthreads (2) Dong-kun Shin Embedded Software Laboratory Sungkyunkwan University http://nyx.skku.ac.kr Last Class Review 2 ANSI/IEEE POSIX1003.1-95 Standard Thread management Work directly on threads
More informationFinal Exam. 12 December 2018, 120 minutes, 26 questions, 100 points
Name: CS520 Final Exam 12 December 2018, 120 minutes, 26 questions, 100 points The exam is closed book and notes. Please keep all electronic devices turned off and out of reach. Note that a question may
More informationCOSC 6374 Parallel Computation. Shared memory programming with POSIX Threads. Edgar Gabriel. Fall References
COSC 6374 Parallel Computation Shared memory programming with POSIX Threads Fall 2012 References Some of the slides in this lecture is based on the following references: http://www.cobweb.ecn.purdue.edu/~eigenman/ece563/h
More informationCS 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 informationLecture Outline. CS 5523 Operating Systems: Concurrency and Synchronization. a = 0; b = 0; // Initial state Thread 1. Objectives.
CS 5523 Operating Systems: Concurrency and Synchronization Thank Dr. Dakai Zhu and Dr. Palden Lama for providing their slides. Lecture Outline Problems with concurrent access to shared data Ø Race condition
More informationProxy: 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 informationWarm-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 informationPOSIX Threads. Paolo Burgio
POSIX Threads Paolo Burgio paolo.burgio@unimore.it The POSIX IEEE standard Specifies an operating system interface similar to most UNIX systems It extends the C language with primitives that allows the
More informationCondition Variables. Dongkun Shin, SKKU
Condition Variables 1 Why Condition? cases where a thread wishes to check whether a condition is true before continuing its execution 1 void *child(void *arg) { 2 printf("child\n"); 3 // XXX how to indicate
More informationCS 471 Operating Systems. Yue Cheng. George Mason University Fall 2017
CS 471 Operating Systems Yue Cheng George Mason University Fall 2017 1 Review: Sync Terminology Worksheet 2 Review: Semaphores 3 Semaphores o Motivation: Avoid busy waiting by blocking a process execution
More informationCS3733: 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 informationPRINCIPLES OF OPERATING SYSTEMS
Department of Computer Science, University of Calgary PRINCIPLES OF OPERATING SYSTEMS Tutorial 9/10: Concurrency CPSC 457, Spring 2015 June 1-2, 2015 Announcement Midterm exam is rescheduled. The exam
More informationW4118 Operating Systems. Instructor: Junfeng Yang
W4118 Operating Systems Instructor: Junfeng Yang Outline Semaphores Producer-consumer problem Monitors and condition variables 2 Semaphore motivation Problem with lock: mutual exclusion, but no ordering
More informationSynchroniza+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 informationCS 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 informationLecture 6. Process Synchronization
Lecture 6 Process Synchronization 1 Lecture Contents 1. Principles of Concurrency 2. Hardware Support 3. Semaphores 4. Monitors 5. Readers/Writers Problem 2 1. Principles of Concurrency OS design issue
More informationSection 5: Thread Synchronization
CS162 February 16, 2018 Contents 1 Warmup 2 1.1 Thread safety........................................... 2 2 Vocabulary 2 3 Problems 3 3.1 The Central Galactic Floopy Corporation...........................
More informationreal time operating systems course
real time operating systems course 4 introduction to POSIX pthread programming introduction thread creation, join, end thread scheduling thread cancellation semaphores thread mutexes and condition variables
More informationProgramming with Threads Dec 7, 2009"
Programming with Threads Dec 7, 2009" Administrivia" 2! Shared Variables in Threaded C Programs" 3! Threads Memory Model" 4! Example of Threads Accessing Another Threadʼs Stack" char **ptr; /* global */
More informationCS 31: Intro to Systems Misc. Threading. Kevin Webb Swarthmore College December 6, 2018
CS 31: Intro to Systems Misc. Threading Kevin Webb Swarthmore College December 6, 2018 Classic thread patterns Agenda Pthreads primitives and examples of other forms of synchronization: Condition variables
More informationHPCSE - 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 informationSynchronization: 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 informationConcurrent Programming Lecture 10
Concurrent Programming Lecture 10 25th September 2003 Monitors & P/V Notion of a process being not runnable : implicit in much of what we have said about P/V and monitors is the notion that a process may
More informationCS 333 Introduction to Operating Systems Class 4 Concurrent Programming and Synchronization Primitives
CS 333 Introduction to Operating Systems Class 4 Concurrent Programming and Synchronization Primitives Jonathan Walpole Computer Science Portland State University 1 What does a typical thread API look
More informationPthreads. 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 informationRecap: Thread. What is it? What does it need (thread private)? What for? How to implement? Independent flow of control. Stack
What is it? Recap: Thread Independent flow of control What does it need (thread private)? Stack What for? Lightweight programming construct for concurrent activities How to implement? Kernel thread vs.
More informationChapter 10. Threads. OS Processes: Control and Resources. A process as managed by the operating system groups together:
SFWR ENG 3BB4 Software Design 3 Concurrent System Design 2 SFWR ENG 3BB4 Software Design 3 Concurrent System Design 10.11 13 OS Processes: Control and Resources Chapter 10 Threads A process as managed
More informationCarnegie 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 informationSemaphores. To avoid busy waiting: when a process has to wait, it will be put in a blocked queue of processes waiting for the same event
Semaphores Synchronization tool (provided by the OS) that do not require busy waiting A semaphore S is an integer variable that, apart from initialization, can only be accessed through 2 atomic and mutually
More informationSemaphores. Semaphores. Semaphore s operations. Semaphores: observations
Semaphores Synchronization tool (provided by the OS) that do not require busy waiting A semaphore S is an integer variable that, apart from initialization, can only be accessed through 2 atomic and mutually
More informationProblem 1: Concepts (Please be concise)
Problem 1: Concepts (Please be concise) 1) Consider the use of threads vs. the select() system call for asynchronous I/O. Name one advantage of each approach. 2) Consider a dynamically allocated linked-list
More informationCondition Variables. Dongkun Shin, SKKU
Condition Variables 1 Why Condition? cases where a thread wishes to check whether a condition is true before continuing its execution 1 void *child(void *arg) { 2 printf("child\n"); 3 // XXX how to indicate
More informationOverview. CMSC 330: Organization of Programming Languages. Concurrency. Multiprocessors. Processes vs. Threads. Computation Abstractions
CMSC 330: Organization of Programming Languages Multithreaded Programming Patterns in Java CMSC 330 2 Multiprocessors Description Multiple processing units (multiprocessor) From single microprocessor to
More informationConcurrency, Thread. Dongkun Shin, SKKU
Concurrency, Thread 1 Thread Classic view a single point of execution within a program a single PC where instructions are being fetched from and executed), Multi-threaded program Has more than one point
More informationOutline. 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 informationHigh Performance Computing Lecture 21. Matthew Jacob Indian Institute of Science
High Performance Computing Lecture 21 Matthew Jacob Indian Institute of Science Semaphore Examples Semaphores can do more than mutex locks Example: Consider our concurrent program where process P1 reads
More informationFinal Exam. 11 May 2018, 120 minutes, 26 questions, 100 points
Name: CS520 Final Exam 11 May 2018, 120 minutes, 26 questions, 100 points The exam is closed book and notes. Please keep all electronic devices turned off and out of reach. Note that a question may require
More information10/17/ Gribble, Lazowska, Levy, Zahorjan 2. 10/17/ Gribble, Lazowska, Levy, Zahorjan 4
Temporal relations CSE 451: Operating Systems Autumn 2010 Module 7 Synchronization Instructions executed by a single thread are totally ordered A < B < C < Absent synchronization, instructions executed
More informationLecture 4. Threads vs. Processes. fork() Threads. Pthreads. Threads in C. Thread Programming January 21, 2005
Threads vs. Processes Lecture 4 Thread Programming January 21, 2005 fork() is expensive (time, memory) Interprocess communication is hard. Threads are lightweight processes: one process can contain several
More informationCS 33. Architecture and Optimization (3) CS33 Intro to Computer Systems XVI 1 Copyright 2018 Thomas W. Doeppner. All rights reserved.
CS 33 Architecture and Optimization (3) CS33 Intro to Computer Systems XVI 1 Copyright 2018 Thomas W. Doeppner. All rights reserved. Hyper Threading Instruction Control Instruction Control Retirement Unit
More informationThreads. Jo, Heeseung
Threads Jo, Heeseung Multi-threaded program 빠른실행 프로세스를새로생성에드는비용을절약 데이터공유 파일, Heap, Static, Code 의많은부분을공유 CPU 를보다효율적으로활용 코어가여러개일경우코어에 thread 를할당하는방식 2 Multi-threaded program Pros. Cons. 대량의데이터처리에적합 - CPU
More informationThreads and Synchronization. Kevin Webb Swarthmore College February 15, 2018
Threads and Synchronization Kevin Webb Swarthmore College February 15, 2018 Today s Goals Extend processes to allow for multiple execution contexts (threads) Benefits and challenges of concurrency Race
More informationSynchronization Mechanisms
Synchronization Mechanisms CSCI 4061 Introduction to Operating Systems Instructor: Abhishek Chandra Mutex Locks Enforce protection and mutual exclusion Condition variables Allow atomic checking of conditions
More informationCarnegie 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 informationThread. 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 informationANSI/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 informationGlobal variables and resources are shared between threads within the same process. Each thread has its own stack and program counter (see Fig 2-7).
Threads One way to reduce context switch time is to run lightweight processes within the same address space. This abstraction is often called a thread. Look at Figure 2-6. Global variables and resources
More informationParallel 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 informationPOSIX 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 informationConcurrent 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 informationLocks. Dongkun Shin, SKKU
Locks 1 Locks: The Basic Idea To implement a critical section A lock variable must be declared A lock variable holds the state of the lock Available (unlocked, free) Acquired (locked, held) Exactly one
More informationCMSC 330: Organization of Programming Languages
CMSC 330: Organization of Programming Languages Multithreading Multiprocessors Description Multiple processing units (multiprocessor) From single microprocessor to large compute clusters Can perform multiple
More informationHigh 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 informationCarnegie Mellon Synchronization: Advanced
1 Synchronization: Advanced 15-213 / 18-213: Introduction to Computer Systems 25 th Lecture, Nov. 20, 2018 2 Reminder: Semaphores Semaphore: non-negative global integer synchronization variable Manipulated
More informationAchieving Synchronization or How to Build a Semaphore
Achieving Synchronization or How to Build a Semaphore CS 241 March 12, 2012 Copyright University of Illinois CS 241 Staff 1 Announcements MP5 due tomorrow Jelly beans... Today Building a Semaphore If time:
More informationCS 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 informationECEN 449 Microprocessor System Design. Review of C Programming
ECEN 449 Microprocessor System Design Review of C Programming 1 Objectives of this Lecture Unit Review C programming basics Refresh es programming g skills s 2 1 Basic C program structure # include
More informationpthreads 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 informationCondition Variables CS 241. Prof. Brighten Godfrey. March 16, University of Illinois
Condition Variables CS 241 Prof. Brighten Godfrey March 16, 2012 University of Illinois 1 Synchronization primitives Mutex locks Used for exclusive access to a shared resource (critical section) Operations:
More informationCSE 451: Operating Systems Winter Lecture 7 Synchronization. Steve Gribble. Synchronization. Threads cooperate in multithreaded programs
CSE 451: Operating Systems Winter 2005 Lecture 7 Synchronization Steve Gribble Synchronization Threads cooperate in multithreaded programs to share resources, access shared data structures e.g., threads
More informationOperating Systems. Thread Synchronization Primitives. Thomas Ropars.
1 Operating Systems Thread Synchronization Primitives Thomas Ropars thomas.ropars@univ-grenoble-alpes.fr 2017 2 Agenda Week 42/43: Synchronization primitives Week 44: Vacation Week 45: Synchronization
More informationCopyright 2013 Thomas W. Doeppner. IX 1
Copyright 2013 Thomas W. Doeppner. IX 1 If we have only one thread, then, no matter how many processors we have, we can do only one thing at a time. Thus multiple threads allow us to multiplex the handling
More informationConcurrent Programming
Indian Institute of Science Bangalore, India भ रत य व ज ञ न स स थ न ब गल र, भ रत SE 292: High Performance Computing [3:0][Aug:2014] Concurrent Programming Yogesh Simmhan Adapted from Intro to Concurrent
More informationCS 333 Introduction to Operating Systems. Class 4 Concurrent Programming and Synchronization Primitives
CS 333 Introduction to Operating Systems Class 4 Concurrent Programming and Synchronization Primitives Jonathan Walpole Computer Science Portland State University 1 Concurrent programming Assumptions:
More informationThreads Cannot Be Implemented As a Library
Threads Cannot Be Implemented As a Library Authored by Hans J. Boehm Presented by Sarah Sharp February 18, 2008 Outline POSIX Thread Library Operation Vocab Problems with pthreads POSIX Thread Library
More informationProcess Synchronization
Process Synchronization Part III, Modified by M.Rebaudengo - 2013 Silberschatz, Galvin and Gagne 2009 POSIX Synchronization POSIX.1b standard was adopted in 1993 Pthreads API is OS-independent It provides:
More informationPolitecnico di Milano FACOLTÀ DI INGEGNERIA DELL INFORMAZIONE. Advanced Operating Systems A.A Exam date: 18 December 2014
Politecnico di Milano FACOLTÀ DI INGEGNERIA DELL INFORMAZIONE Advanced Operating Systems A.A. 2014-2015 Exam date: 18 December 2014 Prof. William FORNACIARI Surname (readable)... Matr... Name (readable)...
More informationInterprocess Communication and Synchronization
Chapter 2 (Second Part) Interprocess Communication and Synchronization Slide Credits: Jonathan Walpole Andrew Tanenbaum 1 Outline Race Conditions Mutual Exclusion and Critical Regions Mutex s Test-And-Set
More informationCS333 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 informationCS 220: Introduction to Parallel Computing. Condition Variables. Lecture 24
CS 220: Introduction to Parallel Computing Condition Variables Lecture 24 Remember: Creating a Thread int pthread_create( pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine)(void *),
More information