class29 - 15-213 The course that gives CMU its Zip!...

Info iconThis preview shows pages 1–7. Sign up to view the full content.

View Full Document Right Arrow Icon
Programming with Threads Dec 5, 2002 Topics Topics Shared variables The need for synchronization Synchronizing with semaphores Thread safety and reentrancy Races and deadlocks class29.ppt 15-213 “The course that gives CMU its Zip!”
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
– 2 – 15-213, F’02 Shared Variables in Threaded C Programs Question: Which variables in a threaded C program are Question: Which variables in a threaded C program are shared variables? shared variables? The answer is not as simple as “global variables are shared” and “stack variables are private”. Requires answers to the following questions: Requires answers to the following questions: What is the memory model for threads? How are variables mapped to memory instances? How many threads reference each of these instances?
Background image of page 2
– 3 – 15-213, F’02 Threads Memory Model Conceptual model: Conceptual model: Each thread runs in the context of a process. Each thread has its own separate thread context. Thread ID, stack, stack pointer, program counter, condition codes, and general purpose 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 Operationally, this model is not strictly enforced: Operationally, this model is not strictly enforced: While register values are truly separate and protected. ... Any thread can read and write the stack of any other thread. Mismatch between the conceptual and operation model is a source Mismatch between the conceptual and operation model is a source of confusion and errors. of confusion and errors.
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
– 4 – 15-213, F’02 Example of Threads Accessing Another Thread’s Stack char **ptr; /* global */ int main() { int i; pthread_t tid; char *msgs[N] = { "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 svar = 0; printf("[%d]: %s (svar=%d)\n", myid, ptr[myid], ++svar); } Peer threads access main thread’s stack indirectly through global ptr variable
Background image of page 4
– 5 – 15-213, F’02 Mapping Variables to Mem. Instances char **ptr; /* global */ int main() { int i; pthread_t tid; char *msgs[N] = { "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 svar = 0; printf("[%d]: %s (svar=%d)\n", myid, ptr[myid], ++svar); } Global var : 1 instance ( ptr [data]) Local static var : 1 instance ( svar [data]) Local automatic vars : 1 instance ( i.m, msgs.m ) Local automatic var: 2 instances ( myid.p0 [peer thread 0’s stack], myid.p1 [peer thread 1’s stack] )
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
– 6 – 15-213, F’02 Shared Variable Analysis Which variables are shared? Which variables are shared?
Background image of page 6
Image of page 7
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 11/17/2009 for the course CS:APP 422 taught by Professor Randale.bryantanddavidr.o'hallaron during the Spring '02 term at Carnegie Mellon.

Page1 / 33

class29 - 15-213 The course that gives CMU its Zip!...

This preview shows document pages 1 - 7. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online