Download Synchronization Basics, Lecture Slide - Computer Science and more Slides Computer System Design and Architecture in PDF only on Docsity!
Synchroniza+on: Basics
15-‐213: Introduc0on to Computer Systems
rd
Lecture, Nov. 16, 2010
Instructors:
Randy Bryant and Dave O’Hallaron
Today
Threads review
Sharing
Mutual exclusion
Semaphores
Process: Alterna+ve View
Process = thread + code, data, and kernel context
shared libraries
run-‐0me heap
read/write data
Program context:
Data registers
Condi0on codes
Stack pointer (SP)
Program counter (PC)
Code, data, and kernel context
read-‐only code/data
stack
SP
PC
brk
Thread
Kernel context:
VM structures
Descriptor table
brk pointer
Process with Two Threads
shared libraries
run-‐0me heap
read/write data
Program context:
Data registers
Condi0on codes
Stack pointer (SP)
Program counter (PC)
Code, data, and kernel context
read-‐only code/data
stack
SP PC
brk
Thread 1
Kernel context:
VM structures
Descriptor table
brk pointer
Program context:
Data registers
Condi0on codes
Stack pointer (SP)
Program counter (PC)
stack
SP
Thread 2
Threads vs. Processes (cont.)
Processes form a tree hierarchy
Threads form a pool of peers
Each thread can kill any other
Each thread can wait for any other thread to terminate
Main thread: first thread to run in a process
P
P
sh sh sh
foo
T
Process hierarchy Thread pool
T
T
T
T
shared code, data
and kernel context
Posix Threads (Pthreads) Interface
Pthreads: Standard interface for ~60 func+ons that manipulate
threads from C programs
Threads run thread rou0nes:
void *threadroutine(void *vargp)
Crea0ng and reaping threads
pthread_create(pthread_t *tid, …, func *f, void *arg)
pthread_join(pthread_t tid, void **thread_return)
Determining your thread ID
pthread_self()
Termina0ng threads
pthread_cancel(pthread_t tid)
pthread_exit(void *tread_return)
return (in primary thread rou0ne terminates the thread)
exit (terminates all threads)
Pros and Cons of Thread-‐Based Designs
+ Easy to share data structures between threads
e.g., logging informa0on, file cache
+ Threads are more efficient than processes
– Uninten+onal sharing can introduce subtle and hard-‐to-‐
reproduce errors!
Today
Threads review
Sharing
Mutual exclusion
Semaphores
Threads Memory Model
Conceptual model:
Mul0ple threads run within the context of a single process
Each thread has its own separate thread context
Thread ID, stack, stack pointer, PC, condi0on codes, and GP registers
All threads share the remaining process context
Code, data, heap, and shared library segments of the process virtual address space Open files and installed handlers
Opera+onally, this model is not strictly enforced:
Register values are truly separate and protected, but…
Any thread can read and write the stack of any other thread
The mismatch between the conceptual and opera?on model
is a source of confusion and errors
Example Program to Illustrate Sharing
char *ptr; / global */ int main() { int i; pthread_t tid; char *msgs[2] = { "Hello from foo", "Hello from bar" }; ptr = msgs; for (i = 0; i < 2; i++) Pthread_create(&tid, NULL, thread, (void )i); Pthread_exit(NULL); } / thread routine */ void *thread(void *vargp) { int myid = (int) vargp; static int cnt = 0; printf("[%d]: %s (svar=%d)\n", myid, ptr[myid], ++cnt); }
Peer threads reference main thread’s stack
indirectly through global ptr variable
Mapping Variable Instances to Memory
char *ptr; / global */ int main() { int i; pthread_t tid; char *msgs[2] = { "Hello from foo", "Hello from bar" }; ptr = msgs; for (i = 0; i < 2; i++) Pthread_create(&tid, NULL, thread, (void )i); Pthread_exit(NULL); } / thread routine */ void *thread(void *vargp) { int myid = (int)vargp; static int cnt = 0; printf("[%d]: %s (svar=%d)\n", myid, ptr[myid], ++cnt); }
Global var : 1 instance ( ptr [data])
Local sta?c var : 1 instance ( cnt [data])
Local vars : 1 instance ( i.m, msgs.m )
Local var: 2 instances (
myid.p0 [peer thread 0’s stack],
myid.p1 [peer thread 1’s stack]
Shared Variable Analysis
Which variables are shared?
Answer: A variable x is shared iff mul+ple threads
reference at least one instance of x. Thus:
ptr , cnt , and msgs are shared
i and myid are not shared
Variable Referenced by Referenced by Referenced by
instance main thread? peer thread 0? peer thread 1?
ptr
cnt
i.m
msgs.m
myid.p
myid.p
yes yes yes
no yes yes
yes no no
yes yes yes
no yes no
no no yes
badcnt.c : Improper Synchroniza+on
volatile int cnt = 0; /* global */ int main(int argc, char *argv) { int niters = atoi(argv[1]); pthread_t tid1, tid2; Pthread_create(&tid1, NULL, thread, &niters); Pthread_create(&tid2, NULL, thread, &niters); Pthread_join(tid1, NULL); Pthread_join(tid2, NULL); / Check result / if (cnt != (2 * niters)) printf("BOOM! cnt=%d\n”, cnt); else printf("OK cnt=%d\n", cnt); exit(0); } / Thread routine */ void *thread(void *vargp) { int i, niters = *((int *)vargp); for (i = 0; i < niters; i++) cnt++; return NULL; } linux> ./badcnt 10000 OK cnt= linux> ./badcnt 10000 BOOM! cnt= linux>
cnt should equal 20,000.
What went wrong?
Assembly Code for Counter Loop
movl (%rdi),%ecx
movl $0,%edx
cmpl %ecx,%edx
jge .L
.L11:
movl cnt(%rip),%eax
incl %eax
movl %eax,cnt(%rip)
incl %edx
cmpl %ecx,%edx
jl .L
.L13:
Corresponding assembly code
for (i=0; i < niters; i++)
cnt++;
C code for counter loop in thread i
Head (Hi)
Tail (Ti)
Load cn t (Li)
Update cn t (Ui)
Store cnt (Si)