Concurrent Programming and Threads Threads Blocking a User Interface.
Copyright ©: University of Illinois CS 241 Staff1 Threads Systems Concepts.
-
Upload
erik-farmer -
Category
Documents
-
view
213 -
download
0
Transcript of Copyright ©: University of Illinois CS 241 Staff1 Threads Systems Concepts.
![Page 1: Copyright ©: University of Illinois CS 241 Staff1 Threads Systems Concepts.](https://reader035.fdocuments.in/reader035/viewer/2022081519/56649f1b5503460f94c30030/html5/thumbnails/1.jpg)
Copyright ©: University of Illinois CS 241 Staff 1
Threads Systems Concepts
![Page 2: Copyright ©: University of Illinois CS 241 Staff1 Threads Systems Concepts.](https://reader035.fdocuments.in/reader035/viewer/2022081519/56649f1b5503460f94c30030/html5/thumbnails/2.jpg)
Review: Why Threads?
Processes do not share resources very well Why?
Process context switching cost is very high Why?
Threads: light-weight processes
Copyright ©: University of Illinois CS 241 Staff 2
![Page 3: Copyright ©: University of Illinois CS 241 Staff1 Threads Systems Concepts.](https://reader035.fdocuments.in/reader035/viewer/2022081519/56649f1b5503460f94c30030/html5/thumbnails/3.jpg)
Benefits of Threads
Takes less time To create a new thread To terminate a thread To switch between two threads
Inter-thread communication without invoking the kernel
Copyright ©: University of Illinois CS 241 Staff 3
![Page 4: Copyright ©: University of Illinois CS 241 Staff1 Threads Systems Concepts.](https://reader035.fdocuments.in/reader035/viewer/2022081519/56649f1b5503460f94c30030/html5/thumbnails/4.jpg)
We like our Threads …
Foreground and background work Asynchronous processing Speed of execution Modular program structure
Copyright ©: University of Illinois CS 241 Staff 4
![Page 5: Copyright ©: University of Illinois CS 241 Staff1 Threads Systems Concepts.](https://reader035.fdocuments.in/reader035/viewer/2022081519/56649f1b5503460f94c30030/html5/thumbnails/5.jpg)
Threads: Lightweight Processes
5Copyright ©: University of Illinois CS 241 Staff
a) Three processes each with one threadb) One process with three threads
Env
ironm
ent
(res
ourc
e)
execution
![Page 6: Copyright ©: University of Illinois CS 241 Staff1 Threads Systems Concepts.](https://reader035.fdocuments.in/reader035/viewer/2022081519/56649f1b5503460f94c30030/html5/thumbnails/6.jpg)
Tasks Suitable for Threading
Has multiple parallel sub-tasks Some sub-tasks block for potentially
long waits Some sub-tasks use many CPU cycles Must respond to asynchronous events
Copyright ©: University of Illinois CS 241 Staff 6
![Page 7: Copyright ©: University of Illinois CS 241 Staff1 Threads Systems Concepts.](https://reader035.fdocuments.in/reader035/viewer/2022081519/56649f1b5503460f94c30030/html5/thumbnails/7.jpg)
Questions
What are the similarities between processes and threads?
What are the differences between processes and threads?
7Copyright ©: University of Illinois CS 241 Staff
![Page 8: Copyright ©: University of Illinois CS 241 Staff1 Threads Systems Concepts.](https://reader035.fdocuments.in/reader035/viewer/2022081519/56649f1b5503460f94c30030/html5/thumbnails/8.jpg)
Thread Packages
Kernel thread packages Implemented and supported at kernel
level User-level thread packages
Implemented at user level Kernel perspective: everything is a
single-threaded process
8Copyright ©: University of Illinois CS 241 Staff
![Page 9: Copyright ©: University of Illinois CS 241 Staff1 Threads Systems Concepts.](https://reader035.fdocuments.in/reader035/viewer/2022081519/56649f1b5503460f94c30030/html5/thumbnails/9.jpg)
Threads in User Space (Old Linux)
Copyright ©: University of Illinois CS 241 Staff 9
Collection of procedures that
manages the threads
Keep track of threads in process (analogous
to kernel process table)
![Page 10: Copyright ©: University of Illinois CS 241 Staff1 Threads Systems Concepts.](https://reader035.fdocuments.in/reader035/viewer/2022081519/56649f1b5503460f94c30030/html5/thumbnails/10.jpg)
User-level Threads
Advantages Fast Context Switching: keeps the OS out of it!
User level thread libraries do not require system calls No call to OS and no interrupts to kernel
thread_yield Save the thread information in the thread table Call the thread scheduler to pick another thread to run
Saving local thread state scheduling are local procedures No trap to kernel, low context switch overhead, no memory
switch
Customized Scheduling (at user level)
Copyright ©: University of Illinois CS 241 Staff 10
![Page 11: Copyright ©: University of Illinois CS 241 Staff1 Threads Systems Concepts.](https://reader035.fdocuments.in/reader035/viewer/2022081519/56649f1b5503460f94c30030/html5/thumbnails/11.jpg)
User-level Threads
Disadvantages What happens if one thread makes a blocking
I/O call? Change the system to be non-blocking Always check to see if a system call will block
What happens if one thread never yields? Introduce clocked interrupts
Multi-threaded programs frequently make system calls Causes a trap into the kernel anyway!
Copyright ©: University of Illinois CS 241 Staff 11
![Page 12: Copyright ©: University of Illinois CS 241 Staff1 Threads Systems Concepts.](https://reader035.fdocuments.in/reader035/viewer/2022081519/56649f1b5503460f94c30030/html5/thumbnails/12.jpg)
Kernel Threads
Copyright ©: University of Illinois CS 241 Staff 12
User-level Threads Kernel-level Threads
![Page 13: Copyright ©: University of Illinois CS 241 Staff1 Threads Systems Concepts.](https://reader035.fdocuments.in/reader035/viewer/2022081519/56649f1b5503460f94c30030/html5/thumbnails/13.jpg)
Kernel-level Threads
Advantages Kernel schedules threads in addition to
processes Multiple threads of a process can run
simultaneously Now what happens if one thread blocks on I/O? Kernel-level threads can make blocking I/O calls
without blocking other threads of same process
Good for multicore architectures
Copyright ©: University of Illinois CS 241 Staff 13
![Page 14: Copyright ©: University of Illinois CS 241 Staff1 Threads Systems Concepts.](https://reader035.fdocuments.in/reader035/viewer/2022081519/56649f1b5503460f94c30030/html5/thumbnails/14.jpg)
Kernel-level Threads
Disadvantages Overhead in the kernel… extra data
structures, scheduling, etc. Thread creation is expensive
Have a pool of waiting threads What happens when a multi-threaded
process calls fork()? Which thread should receive a signal?
Copyright ©: University of Illinois CS 241 Staff 14
![Page 15: Copyright ©: University of Illinois CS 241 Staff1 Threads Systems Concepts.](https://reader035.fdocuments.in/reader035/viewer/2022081519/56649f1b5503460f94c30030/html5/thumbnails/15.jpg)
Trade-offs?
Kernel thread packages Each thread can make blocking I/O calls Can run concurrently on multiple
processors Threads in User-level
Fast context switch Customized scheduling No need for kernel support
Copyright ©: University of Illinois CS 241 Staff 15
![Page 16: Copyright ©: University of Illinois CS 241 Staff1 Threads Systems Concepts.](https://reader035.fdocuments.in/reader035/viewer/2022081519/56649f1b5503460f94c30030/html5/thumbnails/16.jpg)
Hybrid Implementations (Solaris)
Multiplexing user-level threads onto kernel-level threads
16Copyright ©: University of Illinois CS 241 Staff
![Page 17: Copyright ©: University of Illinois CS 241 Staff1 Threads Systems Concepts.](https://reader035.fdocuments.in/reader035/viewer/2022081519/56649f1b5503460f94c30030/html5/thumbnails/17.jpg)
When can we add Concurrency?
Work that can be executed, or data that can be operated on, by multiple tasks simultaneously
Block for potentially long I/O waits Use many CPU cycles in some places but
not others Must respond to asynchronous events Some work is more important than other
work (priority interrupts)
Copyright ©: University of Illinois CS 241 Staff 17
![Page 18: Copyright ©: University of Illinois CS 241 Staff1 Threads Systems Concepts.](https://reader035.fdocuments.in/reader035/viewer/2022081519/56649f1b5503460f94c30030/html5/thumbnails/18.jpg)
Concurrent Programming
Assumptions Two or more threads (or processes) Each executes in (pseudo) parallel and can’t predict exact
running speeds The threads can interact via access to a shared variable
Example One thread writes a variable The other thread reads from the same variable
Problem The order of READs and WRITEs can make a
difference!!!
Copyright ©: University of Illinois CS 241 Staff 18
![Page 19: Copyright ©: University of Illinois CS 241 Staff1 Threads Systems Concepts.](https://reader035.fdocuments.in/reader035/viewer/2022081519/56649f1b5503460f94c30030/html5/thumbnails/19.jpg)
Common Ways to Structure Multi-threaded Code
Manager/worker Single thread (manager) assigns work to other threads
(workers) Manager handles all input and parcels out work
Copyright ©: University of Illinois CS 241 Staff 19
![Page 20: Copyright ©: University of Illinois CS 241 Staff1 Threads Systems Concepts.](https://reader035.fdocuments.in/reader035/viewer/2022081519/56649f1b5503460f94c30030/html5/thumbnails/20.jpg)
Manager/Worker Model
Manager: Worker:create N workersforever { forever { get a request wait for request
pick free worker perform task} }
Challenges Not enough/too many worker threads
Copyright ©: University of Illinois CS 241 Staff 20
M
WW
WW
![Page 21: Copyright ©: University of Illinois CS 241 Staff1 Threads Systems Concepts.](https://reader035.fdocuments.in/reader035/viewer/2022081519/56649f1b5503460f94c30030/html5/thumbnails/21.jpg)
Common Ways to Structure Multi-threaded Code
Manager/worker Single thread (manager) assigns work to other threads
(workers) Manager handles all input and parcels out work
Pipeline Task is broken into a series of sub-tasks Each sub-task is handled by a different thread
Copyright ©: University of Illinois CS 241 Staff 21
![Page 22: Copyright ©: University of Illinois CS 241 Staff1 Threads Systems Concepts.](https://reader035.fdocuments.in/reader035/viewer/2022081519/56649f1b5503460f94c30030/html5/thumbnails/22.jpg)
Pipeline Model
Manager: Stage N:create N stages forever {forever { wait for request get a request perform task
pick 1st stage pick stage n+1} }
Challenges Balancing per-stage load/parallelism
Copyright ©: University of Illinois CS 241 Staff 22
M 2 41 3
![Page 23: Copyright ©: University of Illinois CS 241 Staff1 Threads Systems Concepts.](https://reader035.fdocuments.in/reader035/viewer/2022081519/56649f1b5503460f94c30030/html5/thumbnails/23.jpg)
Common Ways to Structure Multi-threaded Code
Manager/worker Single thread (manager) assigns work to other threads
(workers) Manager handles all input and parcels out work
Pipeline Task is broken into a series of sub-tasks Each sub-task is handled by a different thread
Peer Same structure as manager/worker model After the main thread creates other threads, it participates
in the work
Copyright ©: University of Illinois CS 241 Staff 23
![Page 24: Copyright ©: University of Illinois CS 241 Staff1 Threads Systems Concepts.](https://reader035.fdocuments.in/reader035/viewer/2022081519/56649f1b5503460f94c30030/html5/thumbnails/24.jpg)
Race Conditions
What is a race condition? Two or more threads have an inconsistent view of a
shared memory region (i.e., a variable)
Why do race conditions occur? Values of memory locations replicated in registers during
execution Context switches at arbitrary times during execution Threads can see “stale” memory values in registers
Copyright ©: University of Illinois CS 241 Staff 24
![Page 25: Copyright ©: University of Illinois CS 241 Staff1 Threads Systems Concepts.](https://reader035.fdocuments.in/reader035/viewer/2022081519/56649f1b5503460f94c30030/html5/thumbnails/25.jpg)
Remember this code?
int x = 1;
main(…) {
pthread_t tid;
pthread_create( &tid,NULL, func,NULL);
func(NULL);
x = x + 1;
}
void* func(void*p){
x = x + 1;
printf("x is %d\n");
return NULL;
}
Copyright ©: University of Illinois CS 241 Staff
What is the output?
![Page 26: Copyright ©: University of Illinois CS 241 Staff1 Threads Systems Concepts.](https://reader035.fdocuments.in/reader035/viewer/2022081519/56649f1b5503460f94c30030/html5/thumbnails/26.jpg)
Race Conditions
Race condition Whenever the output depends on the precise
execution order of the processes!!! What solutions can we apply?
Prevent context switches by preventing interrupts
Make threads coordinate with each other to ensure mutual exclusion in accessing critical sections of code
Copyright ©: University of Illinois CS 241 Staff 26
![Page 27: Copyright ©: University of Illinois CS 241 Staff1 Threads Systems Concepts.](https://reader035.fdocuments.in/reader035/viewer/2022081519/56649f1b5503460f94c30030/html5/thumbnails/27.jpg)
Threading Pitfalls
Global variables No protection between threads
Disallow all global variables Introduce new thread-specific global variables Introduce new library functions
Are my libraries thread-safe? May use local variables May not be designed to be interrupted
Create wrappers
Copyright ©: University of Illinois CS 241 Staff 27
![Page 28: Copyright ©: University of Illinois CS 241 Staff1 Threads Systems Concepts.](https://reader035.fdocuments.in/reader035/viewer/2022081519/56649f1b5503460f94c30030/html5/thumbnails/28.jpg)
Threadssafe Library Calls
#include <string.h>
char *token;
char *line = "LINE TO BE SEPARATED";
char *search = " ";
/* Token will point to "LINE". */
token = strtok(line, search);
/* Token will point to "TO". */
token = strtok(NULL, search);
#include <string.h>
char *token;
char *line = "LINE TO BE SEPARATED";
char *search = " ";
/* Token will point to "LINE". */
token = strtok_r(line, search);
/* Token will point to "TO". */
token = strtok_r(NULL, search);
Copyright ©: University of Illinois CS 241 Staff
![Page 29: Copyright ©: University of Illinois CS 241 Staff1 Threads Systems Concepts.](https://reader035.fdocuments.in/reader035/viewer/2022081519/56649f1b5503460f94c30030/html5/thumbnails/29.jpg)
Threadssafe Library Calls
#include <string.h>
char *token;
char *line = "LINE TO BE SEPARATED";
char *search = " ";
char *state;
/* Token will point to "LINE". */
token = strtok_r(line, search, &state);
/* Token will point to "TO". */
token = strtok_r(NULL, search, &state);
#include <string.h>
char *token;
char *line = "LINE TO BE SEPARATED";
char *search = " ";
char *state;
/* Token will point to "LINE". */
token = strtok_r(line, search, &state);
/* Token will point to "TO". */
token = strtok_r(NULL, search, &state);
Copyright ©: University of Illinois CS 241 Staff
![Page 30: Copyright ©: University of Illinois CS 241 Staff1 Threads Systems Concepts.](https://reader035.fdocuments.in/reader035/viewer/2022081519/56649f1b5503460f94c30030/html5/thumbnails/30.jpg)
System & library functions that are not required to be thread-safe
Copyright ©: University of Illinois CS 241 Staff
asctime dirname getenv getpwent lgamma readdir
basename dlerror getgrent getpwnam lgammaf setenv
catgets drand48 getgrgid getpwuid lgammal setgrent
crypt ecvt getgrnam getservbyname localeconv setkey
ctime encrypt gethostbyaddr getservbyport localtime setpwent
dbm_clearerr endgrent gethostbyname getservent lrand48 setutxent
dbm_close endpwent gethostent getutxent mrand48 strerror
dbm_delete endutxent getlogin getutxid nftw strtok
dbm_error fcvt getnetbyaddr getutxline nl_langinfo ttyname
dbm_fetch ftw getnetbyname gmtime ptsname unsetenv
dbm_firstkey gcvt getnetent hcreate putc_unlocked wcstombs
dbm_nextkey getc_unlocked getopt hdestroy putchar_unlocked wctomb
dbm_open getchar_unlocked getprotobynumber inet_ntoa pututxline
dbm_store getdate getprotoent l64a rand
![Page 31: Copyright ©: University of Illinois CS 241 Staff1 Threads Systems Concepts.](https://reader035.fdocuments.in/reader035/viewer/2022081519/56649f1b5503460f94c30030/html5/thumbnails/31.jpg)
Things to think about …
Who gets to go next when a thread blocks/yields? Scheduling!
What happens when multiple threads are sharing the same resource? Synchronization!
Copyright ©: University of Illinois CS 241 Staff 31