1 / 37

Programming with Threads

Programming with Threads. Topics Shared variables The need for synchronization Synchronizing with semaphores Thread safety and reentrancy Races and deadlocks. Shared Variables in Threaded C Programs. Question: Which variables in a threaded C program are shared variables?

cassiep
Download Presentation

Programming with Threads

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Programming with Threads • Topics • Shared variables • The need for synchronization • Synchronizing with semaphores • Thread safety and reentrancy • Races and deadlocks

  2. Shared Variables in Threaded C Programs • Question: Which variables in a threaded C program are shared variables? • The answer is not as simple as “global variables are shared” and “stack variables are private”. • 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?

  3. Threads Memory 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 code • Open files and installed handlers • 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.

  4. Practice Problem • Problem 13.6, p. 871 (handout)

  5. Shared Variable Analysis • Which variables are shared? Variable Referenced by Referenced by Referenced by instance main thread? peer thread 0? peer thread 1? ptr yes yes yes cnt no yes yes i.m yes no no msgs.m yes yes yes myid.p0 no yes no myid.p1 no no yes • ptr, cnt, and msgs are shared. • i and myid are not shared (i is passed by value) • demo sharing.c (on cs01)

  6. Synchronization • An improperly synchronized program • badcnt.c(p. 872)

  7. Synchronization • An improperly synchronized program • badcnt.c(p. 872) • examine count function in badcnt.s

  8. Synchronization • An improperly synchronized program • badcnt.c(p. 872) • examine count function in badcnt.s cnt++; movl _cnt,%eax incl %eax movl %eax,_cnt

  9. Synchronization • An improperly synchronized program • badcnt.c(p. 872) • examine count function in badcnt.s • compile without the force-mem option that forces memory operands to be copied to registers before performing arithmetic cnt++; movl _cnt,%eax incl %eax movl %eax,_cnt

  10. Synchronization • An improperly synchronized program • badcnt.c(p. 872) • examine count function in badcnt.s • compile without the force-mem option that forces memory operands to be copied to registers before performing arithmetic cnt++; movl _cnt,%eax incl %eax movl %eax,_cnt cnt++; incl _cnt

  11. Assembly Code for Counter Loop C code for counter loop Corresponding asm code (gcc -fforce-mem) for (i=0; i<NITERS; i++) cnt++; .L9: movl -4(%ebp),%eax cmpl $99999999,%eax jle .L12 jmp .L10 .L12: movl _cnt,%eax # Load incl %eax # Update movl %eax,_cnt # Store .L11: movl -4(%ebp),%eax leal 1(%eax),%edx movl %edx,-4(%ebp) jmp .L9 .L10: Head (Hi) Load cnt (Li) Update cnt (Ui) Store cnt (Si) Tail (Ti)

  12. Concurrent Execution • Any sequential interleaving is possible, but some orderings are incorrect • %eaxi is the contents of %eax in thread i’s context • H=Head T=Tail L=Load U=Update S=Store i (thread) instri %eax1 %eax2 cnt 1 H1 - - 0 1 L1 0 - 0 1 U1 1 - 0 1 S1 1 - 1 2 H2 - - 1 2 L2 - 1 1 2 U2 - 2 1 2 S2 - 2 2 2 T2 - 2 2 1 T1 1 - 2 OK

  13. Concurrent Execution • Incorrect ordering: two threads increment the counter, but the result is 1 instead of 2. i (thread) instri %eax1 %eax2 cnt 1 H1 - - 0 1 L1 0 - 0 1 U1 1 - 0 2 H2 - - 0 2 L2 - 0 0 1 S1 1 - 1 1 T1 1 - 1 2 U2 - 1 1 2 S2 - 1 1 2 T2 - 1 1 Oops!

  14. i (thread) instri %eax1 %eax2 cnt 1 H1 0 1 L1 2 H2 2 L2 2 U2 2 S2 1 U1 1 S1 1 T1 2 T2 Concurrent Execution • How about this ordering?

  15. i (thread) instri %eax1 %eax2 cnt 1 H1 - - 0 1 L1 0 - 0 2 H2 - - 0 2 L2 - 0 0 2 U2 - 1 0 2 S2 - 1 1 1 U1 1 - 1 1 S1 1 - 1 1 T1 1 - 1 2 T2 1 1 Concurrent Execution • How about this ordering? We can clarify our understanding of concurrent execution with the help of the progress graph

  16. Progress Graphs Thread 2 A progress graph depicts the execution state of concurrent threads. Each axis corresponds to the sequential order of instructions in a thread. Each point corresponds to a possible execution state. E.g., (L1, S2) denotes state where thread 1 has completed L1 and thread 2 has completed S2. T2 (L1, S2) S2 U2 L2 H2 Thread 1 H1 L1 U1 S1 T1

  17. Thread 2 T2 S2 U2 L2 H2 Thread 1 H1 L1 U1 S1 T1 Trajectories in Progress Graphs A trajectory is a sequence of legal state transitions that describes one possible concurrent execution of the threads. Example: H1, L1, U1, H2, L2, S1, T1, U2, S2, T2

  18. Critical Sections and Unsafe Regions Thread 2 L, U, and S form a critical sectionwith respect to the shared variable cnt. Instructions in critical sections should not be interleaved. Sets of states where such interleaving occurs form unsafe regions. T2 S2 critical section Unsafe region U2 L2 H2 Thread 1 H1 L1 U1 S1 T1 critical section

  19. Safe and Unsafe Trajectories Thread 2 Def: A trajectory is safe iff it doesn’t cross into the unsafe region. Claim: A trajectory is correct iff it is safe. Safe trajectory T2 S2 Unsafe trajectory Unsafe region critical section U2 L2 H2 Thread 1 H1 L1 U1 S1 T1 critical section

  20. Semaphores • Question: How can we guarantee a safe trajectory? • We must synchronizethe threads so that they never enter an unsafe state • Classic solution: Dijkstra's P and V operations on semaphores. • semaphore: non-negative integer synchronization variable. • Variable “s” indicates the number of resources available • P(s): [ while (s == 0) wait(); s--; ] • P(s) requests resource “s” • Dutch for "Proberen" (test) • V(s): [ s++; ] • V(s) releases resource “s” • Dutch for "Verhogen" (increment) • OS guarantees that operations between brackets [ ] are atomic operations (executed indivisibly). • Only one P or V operation at a time can modify ‘s’ • When while loop in P terminates, only that P can decrement ‘s’

  21. Safe Sharing with Semaphores • Here is how we would use P and V operations to synchronize the threads that update cnt. /* Semaphore s is initially 1 */ /* Thread routine */ void *count(void *arg) { int i; for (i=0; i<NITERS; i++) { P(s); cnt++; V(s); } return NULL; }

  22. 1 1 0 0 0 0 1 1 1 1 0 0 0 0 1 1 1 1 0 0 0 0 1 1 1 1 0 0 0 0 1 1 Safe Sharing With Semaphores Thread 2 Provide mutually exclusive access to shared variable by surrounding critical section with P and V operations on semaphore s (initially set to 1). Semaphore invariant creates a forbidden region that encloses unsafe region and is never touched by any trajectory. T2 V(s) Forbidden region 0 0 0 0 -1 -1 -1 -1 S2 0 0 0 0 -1 -1 -1 -1 U2 Unsafe region 0 0 0 0 -1 -1 -1 -1 L2 -1 -1 -1 -1 0 0 0 0 P(s) H2 H1 P(s) L1 U1 S1 V(s) T1 Thread 1 Initially s = 1

  23. Semaphore Wrappers /* Initialize semaphore sem to value */ /* pshared=0 if thread, pshared=1 if process */ void Sem_init(sem_t *sem, int pshared, unsigned int value) { if (sem_init(sem, pshared, value) < 0) unix_error("Sem_init"); } /* P operation on semaphore sem */ void P(sem_t *sem) { if (sem_wait(sem)) unix_error("P"); } /* V operation on semaphore sem */ void V(sem_t *sem) { if (sem_post(sem)) unix_error("V"); }

  24. Demo: goodcnt.c(demo, cs01, not in text)

  25. Signaling With Semaphores producer thread consumer thread • Common synchronization pattern: • Producer waits for slot, inserts item in buffer, and “signals” consumer. • Consumer waits for item, removes it from buffer, and “signals” producer. • “signals” in this context have nothing to do with Unix signals • Examples • Multimedia processing: • Producer creates MPEG video frames, consumer renders the frames • Event-driven graphical user interfaces • Producer (person) creates mouse clicks, mouse movements, and keyboard hits and inserts corresponding events in buffer. • Consumer (computer) retrieves events from buffer and paints the display. shared buffer

  26. Examples • Demo: Shared FIFO Buffer • sbuf.h/sbuf.c - shared FIFO buffer (p. 880-882) • sbufdriver.c – test driver • Demo: Concurrent Echo Server • echoservert_pre.c(p. 884) • echo_cnt (p. 885)

  27. All functions Thread-safe functions Thread-unsafe functions Reentrant functions Thread-Safe and Reentrant Functions • Functions called from a thread must be thread-safe. • A function is reentrant iff it • Accesses NO shared variables • Does not require synchronization operations (initialization) • A function can be thread-safe, but not reentrant • A thread-safe application can have shared variables • But uses semaphores to access shared variables • Less efficient than reentrant implementation

  28. Thread Safety • Functions that are thread-unsafe include functions that • fail to protect shared variables • rely on persistent state across invocations • return a pointer to a static variable • call thread-unsafe functions

  29. Thread-Unsafe Functions • Failing to protect shared variables. • Fix: Use P and V semaphore operations. • Issue: Synchronization operations will slow down code. • Example: goodcnt.c

  30. Thread-Unsafe Functions • Relying on persistent state across multiple function invocations. • Random number generator relies on static state • Fix: Rewrite function so that caller passes in all necessary state. // rand - return pseudo-random integer on 0..32767 static unsigned int next = 1; int rand(void) { next = (next * 1103515245) + 12345; return (unsigned int)(next/65536) % 32768; } // srand - set seed for rand() void srand(unsigned int seed) { next = seed; } srand(20); int x = rand();

  31. Thread-Unsafe Functions • Relying on persistent state across multiple function invocations. • Random number generator relies on static state • Fix: Rewrite function so that caller passes in the necessary state. // rand - return pseudo-random integer on 0..32767 int rand(unsigned int *next) { *next = (*next * 1103515245) + 12345; return (unsigned int)(*next/65536) % 32768; } int seed = 20; int x = rand(&seed);

  32. Thread-Unsafe Functions • Returning a ptr to a static variable. • Fixes: • Reentrant version, caller passes pointer to buf. • Issue: Requires changes in caller and callee. • Thread-safe version, Lock-and-copy wrapper • Issue: caller must free memory. char *asctime(struct tm *CLOCK) { static char buf[50]; ... return buf; } // see ‘man asctime’ buf = Malloc(...); asctime_r(&tm, buf); char *asctime_t(struct tm *CLOCK) { char *buf = Malloc(...); P(&mutex); char *p = asctime(tm); strcpy(buf, p); V(&mutex); return buf; }

  33. Thread-Unsafe Functions • Calling thread-unsafe functions. • Calling one thread-unsafe function makes an entire function thread-unsafe. • Fix: Modify the function so it calls only thread-safe functions

  34. Thread-Safe Library Functions • Most functions in the Standard C Library (at the back of your K&R text) are thread-safe. • Examples: malloc, free, printf, scanf • Exceptions: asctime, ctime, gmtime, localtime • These four functions return pointers to static objects that may be overwritten by other calls. • Exceptions: rand • Reentrant versions: asctime_r, rand_r • Most Unix system calls are thread-safe, with a few exceptions: Thread-unsafe function Reentrant version gethostbyaddr gethostbyaddr_r gethostbyname gethostbyname_r inet_ntoa (none)

  35. Races • A race occurs when the correctness of the program depends on one thread reaching point ‘x’ before another thread reaches point ‘y’. (demo, run several times) // race between creating next thread and // accessing variable “i” int main() { pthread_t tid[N]; int i; for (i = 0; i < N; i++) Pthread_create(&tid[i], NULL, thread, &i); for (i = 0; i < N; i++) Pthread_join(tid[i], NULL); exit(0); } // thread routine void *thread(void *vargp) { int myid = *((int *)vargp); printf("Hello from thread %d\n", myid); return NULL; }

  36. Deadlocks Locking introduces the potential for deadlock: waiting for a condition that will never be true. Any trajectory that enters the deadlock regionwill eventually reach the deadlock state, waiting for either s or t to become nonzero. Other trajectories luck out and skirt the deadlock region. Thread 2 deadlock state V(a) forbidden region for a V(b) P(a) deadlock region forbidden region for b P(b) P(a) P(b) V(a) V(b) Thread 1 Initially, a = b= 1

  37. Threads Summary • Threads provide another mechanism for writing concurrent programs. • Threads are growing in popularity • Somewhat cheaper than processes • Easy to share data between threads • However, the ease of sharing has a cost: • Easy to introduce subtle synchronization errors • Tread carefully with threads! • For more info: • D. Butenhof, “Programming with Posix Threads”, Addison-Wesley, 1997.

More Related