31
Thread Thread 15213-S04, Recitation, Section A 15213-S04, Recitation, Section A Thread Memory Model Thread Interfaces (System Calls) Thread Safety (Pitfalls of Using Thread) Racing Semaphore Final & Evaluation Forms

Thread 15213-S04, Recitation, Section A Thread Memory Model Thread Interfaces (System Calls) Thread Safety (Pitfalls of Using Thread) Racing Semaphore

Embed Size (px)

DESCRIPTION

– 3 – , S’04 Why Do We Care About Thread Useful for L7 Part II A very important way to implement modern concurrent systems What’s concurrency? Web Browser Web Server Web Browser Web Browser Web Server Web Server Proxy

Citation preview

Page 1: Thread 15213-S04, Recitation, Section A Thread Memory Model Thread Interfaces (System Calls) Thread Safety (Pitfalls of Using Thread) Racing Semaphore

ThreadThread15213-S04, Recitation, Section A15213-S04, Recitation, Section A

Thread Memory Model Thread Interfaces (System Calls) Thread Safety (Pitfalls of Using Thread) Racing Semaphore

Final & Evaluation Forms

Page 2: Thread 15213-S04, Recitation, Section A Thread Memory Model Thread Interfaces (System Calls) Thread Safety (Pitfalls of Using Thread) Racing Semaphore

– 2 – 15-213, S’04

pthread_createpthread_joinpthread_selfpthread_cancelpthread_exitpthread_mutex_initpthread_mutex_[un]lockpthread_cond_initpthread_cond_[timed]wait

Page 3: Thread 15213-S04, Recitation, Section A Thread Memory Model Thread Interfaces (System Calls) Thread Safety (Pitfalls of Using Thread) Racing Semaphore

– 3 – 15-213, S’04

Why Do We Care About ThreadUseful for L7 Part II

A very important way to implement modern concurrent systems

What’s concurrency?

WebBrowser

WebServer

WebBrowser

WebBrowser

WebServer

WebServer

Proxy

Page 4: Thread 15213-S04, Recitation, Section A Thread Memory Model Thread Interfaces (System Calls) Thread Safety (Pitfalls of Using Thread) Racing Semaphore

– 4 – 15-213, S’04

Three Methods to Implement Concurrency1. Processes

Fork a child process for every incoming client connection Difficult to share data among child processes

2. Threads Create a thread to handle every incoming client connection Our focus today

3. I/O multiplexing with Unix select() Use select() to notice pending socket activity Manually interleave the processing of multiple open

connections More complex!

~ implementing your own app-specific thread package!

Page 5: Thread 15213-S04, Recitation, Section A Thread Memory Model Thread Interfaces (System Calls) Thread Safety (Pitfalls of Using Thread) Racing Semaphore

– 5 – 15-213, S’04

View of ProcessProcess = process context + code, data, and stack

shared libraries

run-time heap

0

read/write data

Program context: Data registers Condition code Stack pointer (SP) Program counter (PC)Kernel context: VM structures Descriptor table

Code, data, and stack

read-only code/data

stackSP

PC

Process context

Page 6: Thread 15213-S04, Recitation, Section A Thread Memory Model Thread Interfaces (System Calls) Thread Safety (Pitfalls of Using Thread) Racing Semaphore

– 6 – 15-213, S’04

View of ThreadMultiple threads can be associated with a process

Each thread has its own logical control flow (instruction flow) Each thread has its own thread ID (TID) Each thread shares the same code, data, and kernel context

shared libraries

run-time heap

0

read/write data

Shared code and data

read-only code/dataThread 1 context: Data registers Condition code SP1 PC1

stack 1

Thread 1 (main thread)

Kernel context: VM structures Descriptor table

Thread 2 context: Data registers Condition code SP2 PC2

stack 2

Thread 2 (peer thread)

Thread memorymodel

Page 7: Thread 15213-S04, Recitation, Section A Thread Memory Model Thread Interfaces (System Calls) Thread Safety (Pitfalls of Using Thread) Racing Semaphore

– 7 – 15-213, S’04

Posix Threads (Pthreads) InterfaceStandard interface for ~60 functions

Creating and reaping threads.pthread_createpthread_join

Determining your thread IDpthread_self

Terminating threadspthread_cancelpthread_exit

Synchronizing access to shared variablespthread_mutex_initpthread_mutex_[un]lockpthread_cond_initpthread_cond_[timed]wait

Page 8: Thread 15213-S04, Recitation, Section A Thread Memory Model Thread Interfaces (System Calls) Thread Safety (Pitfalls of Using Thread) Racing Semaphore

– 8 – 15-213, S’04

The Pthread "hello, world" Program

/* * hello.c - Pthreads "hello, world" program */#include "csapp.h"

/* thread routine */void *thread(void *vargp) { printf("Hello, world!\n"); return NULL;}

int main() { pthread_t tid;

Pthread_create(&tid, NULL, thread, NULL); Pthread_join(tid, NULL); exit(0);}

Page 9: Thread 15213-S04, Recitation, Section A Thread Memory Model Thread Interfaces (System Calls) Thread Safety (Pitfalls of Using Thread) Racing Semaphore

– 9 – 15-213, S’04

Execution of Threaded“hello, world”main thread

main thread waits for peer

thread to terminate

exit() terminates

main thread and any peer threads

peer thread

Call Pthread_create()

call Pthread_join()

Pthread_join() returns

printf()return NULL;(peer threadterminates)

Pthread_create() returns

Page 10: Thread 15213-S04, Recitation, Section A Thread Memory Model Thread Interfaces (System Calls) Thread Safety (Pitfalls of Using Thread) Racing Semaphore

– 10 – 15-213, S’04

PracticesBasic usage of thread

Pthread_exit() & exit() Joinable and detached thread

Thread safety Protecting shared variable Function that returns a static pointer (more) ……

Page 11: Thread 15213-S04, Recitation, Section A Thread Memory Model Thread Interfaces (System Calls) Thread Safety (Pitfalls of Using Thread) Racing Semaphore

– 11 – 15-213, S’04

pthread_exit() & exit()

void *thread(void *vargp){ pthread_exit((void*)42);}

int main(){ int i; pthread_t tid; pthread_create(&tid, NULL, thread, NULL); pthread_join(tid, (void **)&i);

printf("%d\n",i);}

Program 1.1

Page 12: Thread 15213-S04, Recitation, Section A Thread Memory Model Thread Interfaces (System Calls) Thread Safety (Pitfalls of Using Thread) Racing Semaphore

– 12 – 15-213, S’04

pthread_exit() & exit()Program 1.2

void *thread(void *vargp){ exit(42);}

int main(){ int i; pthread_t tid; pthread_create(&tid, NULL, thread, NULL); pthread_join(tid, (void **)&i);

printf("%d\n",i);}

Page 13: Thread 15213-S04, Recitation, Section A Thread Memory Model Thread Interfaces (System Calls) Thread Safety (Pitfalls of Using Thread) Racing Semaphore

– 13 – 15-213, S’04

pthread_exit() & exit()pthread_exit() only terminates the current thread, NOT

the process

Exit() terminates all the threads in the process, i.e., the process itself

Page 14: Thread 15213-S04, Recitation, Section A Thread Memory Model Thread Interfaces (System Calls) Thread Safety (Pitfalls of Using Thread) Racing Semaphore

– 14 – 15-213, S’04

PracticesBasic usage of thread

Pthread_exit() & exit() Joinable and detached thread

Thread safety Protecting shared variable Function that returns a static pointer (more) ……

Page 15: Thread 15213-S04, Recitation, Section A Thread Memory Model Thread Interfaces (System Calls) Thread Safety (Pitfalls of Using Thread) Racing Semaphore

– 15 – 15-213, S’04

Joinable & Detached ThreadsAt any point in time, a thread is either joinable or

detached.

Joinable thread can be reaped and killed by other threads. must be reaped (with pthread_join) to free memory

resources.

Detached thread cannot be reaped or killed by other threads. resources are automatically reaped on termination.

Default state is joinable. use pthread_detach(pthread_self()) to make detached.

Page 16: Thread 15213-S04, Recitation, Section A Thread Memory Model Thread Interfaces (System Calls) Thread Safety (Pitfalls of Using Thread) Racing Semaphore

– 16 – 15-213, S’04

PracticesBasic usage of thread

Pthread_exit() & exit() Joinable and detached thread

Thread safety Protecting shared variable Function that returns a static pointer (more) ……

Page 17: Thread 15213-S04, Recitation, Section A Thread Memory Model Thread Interfaces (System Calls) Thread Safety (Pitfalls of Using Thread) Racing Semaphore

– 17 – 15-213, S’04

Protecting shared variablesProgram 1.5

int i = 42;

void *thread(void *vargp){ printf("%d\n",i); }

void *thread2(void *vargp){ i = 31; }

int main(){ pthread_t tid, tid2; pthread_create(&tid2, NULL, thread2, (void*)&i);

pthread_create(&tid, NULL, thread, (void*)&i); pthread_join(tid, (void**)&i); pthread_join(tid2, NULL);}

Page 18: Thread 15213-S04, Recitation, Section A Thread Memory Model Thread Interfaces (System Calls) Thread Safety (Pitfalls of Using Thread) Racing Semaphore

– 18 – 15-213, S’04

PracticesBasic usage of thread

Pthread_exit() & exit() Joinable and detached thread

Thread safety Protecting shared variable Function that returns a static pointer (more) ……

Page 19: Thread 15213-S04, Recitation, Section A Thread Memory Model Thread Interfaces (System Calls) Thread Safety (Pitfalls of Using Thread) Racing Semaphore

– 19 – 15-213, S’04

Functions that return a pointer to a static value

int main () {struct in_addr a, b;a.s_addr = inet_addr(“1.1.1.1”);b.s_addr = inet_addr(“2.2.2.2”);

printf(“%s %s\n”, inet_ntoa(a),inet_ntoa(b));

}

output: ???

int main () {struct in_addr a;a.s_addr = inet_addr(“1.1.1.1”);printf(“%s\n”, inet_ntoa(a));

}

Output: 1.1.1.1

Page 20: Thread 15213-S04, Recitation, Section A Thread Memory Model Thread Interfaces (System Calls) Thread Safety (Pitfalls of Using Thread) Racing Semaphore

– 20 – 15-213, S’04

Thread SafetyClass 1: Functions that do not protect shared variables

Class 2: Functions that keep state across multiple invocations rand() Textbook P. 886

Class 3: Functions that return a pointer to a static variable

Class 4: Functions that call thread-unsafe functions May or may not be thread-unsafe Textbook P.887

Page 21: Thread 15213-S04, Recitation, Section A Thread Memory Model Thread Interfaces (System Calls) Thread Safety (Pitfalls of Using Thread) Racing Semaphore

– 21 – 15-213, S’04

More Practice ProblemsProgram 1.3 & 1.4 are left for your practice

Page 22: Thread 15213-S04, Recitation, Section A Thread Memory Model Thread Interfaces (System Calls) Thread Safety (Pitfalls of Using Thread) Racing Semaphore

– 22 – 15-213, S’04

Racing

A race occurs when the correctness of a program depends on one thread reaching point x in its control flow before another thread reaches point y. Generally related with the access to the shared variables Need synchronization

Ways to do synchronization: Semaphores Mutual-exclusion

Page 23: Thread 15213-S04, Recitation, Section A Thread Memory Model Thread Interfaces (System Calls) Thread Safety (Pitfalls of Using Thread) Racing Semaphore

– 23 – 15-213, S’04

Program 2.b

void *foo(void *vargp) { int id; id = *((int *)vargp); printf("Thread %d\n", id);}

int main() { pthread_t tid[2]; int i;

for (i = 0; i < 2; i++) Pthread_create(&tid[i], NULL, foo, &i); Pthread_join(tid[0], NULL); Pthread_join(tid[1], NULL);}

Racing!

Page 24: Thread 15213-S04, Recitation, Section A Thread Memory Model Thread Interfaces (System Calls) Thread Safety (Pitfalls of Using Thread) Racing Semaphore

– 24 – 15-213, S’04

Program 2.avoid *foo(void *vargp) { int myid; myid = *((int *)vargp); Free(vargp); printf("Thread %d\n", myid);}

int main() { pthread_t tid[2]; int i, *ptr;

for (i = 0; i < 2; i++) { ptr = Malloc(sizeof(int)); *ptr = i; Pthread_create(&tid[i], 0, foo, ptr); } Pthread_join(tid[0], 0); Pthread_join(tid[1], 0);}

Good!

Page 25: Thread 15213-S04, Recitation, Section A Thread Memory Model Thread Interfaces (System Calls) Thread Safety (Pitfalls of Using Thread) Racing Semaphore

– 25 – 15-213, S’04

Program 2.c

void *foo(void *vargp) { int id; id = (int)vargp; printf("Thread %d\n", id);}

int main() { pthread_t tid[2]; int i;

for (i = 0; i < 2; i++) Pthread_create(&tid[i], 0, foo, i); Pthread_join(tid[0], 0); Pthread_join(tid[1], 0);}

Good!

Page 26: Thread 15213-S04, Recitation, Section A Thread Memory Model Thread Interfaces (System Calls) Thread Safety (Pitfalls of Using Thread) Racing Semaphore

– 26 – 15-213, S’04

The general solution for racingSemaphore & mutual-exclusion

Mutex is a special case for the problems that semaphore targets to solve, and thus can be implemented using semaphore

But mutex seems to be more popularly used (probably because more people understand it)

Classic solution: Dijkstra's P and V operations on semaphores. semaphore: non-negative integer synchronization variable.

P(s): [ while (s == 0) wait(); s--; ] V(s): [ s++; ]

OS guarantees that operations between brackets [ ] are executed indivisibly.

Only one P or V operation at a time can modify s. When while loop in P terminates, only that P can decrements.

Semaphore invariant: (s >= 0)

Page 27: Thread 15213-S04, Recitation, Section A Thread Memory Model Thread Interfaces (System Calls) Thread Safety (Pitfalls of Using Thread) Racing Semaphore

– 27 – 15-213, S’04

Program 2.dsem_t s; /* semaphore s */

void *foo(void *vargp) { int id; id = *((int *)vargp); V(&s); printf("Thread %d\n", id);}

int main() { pthread_t tid[2]; int i;

sem_init(&s, 0, 0); /* S=0 INITIALLY */

for (i = 0; i < 2; i++) { Pthread_create(&tid[i], 0, foo, &i); P(&s); } Pthread_join(tid[0], 0); Pthread_join(tid[1], 0);}

Good!

Page 28: Thread 15213-S04, Recitation, Section A Thread Memory Model Thread Interfaces (System Calls) Thread Safety (Pitfalls of Using Thread) Racing Semaphore

– 28 – 15-213, S’04

Program 2.esem_t s; /* semaphore s */

void *foo(void *vargp) { int id; P(&s); id = *((int *)vargp); V(&s); printf("Thread %d\n", id);}

int main() { pthread_t tid[2]; int i;

sem_init(&s, 0, 1); /* S=1 INITIALLY */

for (i = 0; i < 2; i++) { Pthread_create(&tid[i], 0, foo, &i); } Pthread_join(tid[0], 0); Pthread_join(tid[1], 0);}

Racing!

Page 29: Thread 15213-S04, Recitation, Section A Thread Memory Model Thread Interfaces (System Calls) Thread Safety (Pitfalls of Using Thread) Racing Semaphore

– 29 – 15-213, S’04

Summary So FarThread

Thread memory model How to write thread programs Thread safety (pitfalls of using thread)

Racing Semaphore

Page 30: Thread 15213-S04, Recitation, Section A Thread Memory Model Thread Interfaces (System Calls) Thread Safety (Pitfalls of Using Thread) Racing Semaphore

– 30 – 15-213, S’04

FinalTimes

Exam Time: May 3rd (Monday) 5:30pm – 8:30p UC McConomy

Review Session: May 1st (Saturday), 3:00pm

Page 31: Thread 15213-S04, Recitation, Section A Thread Memory Model Thread Interfaces (System Calls) Thread Safety (Pitfalls of Using Thread) Racing Semaphore

– 31 – 15-213, S’04

Evaluation FormsThere are questions on both sides

One student bring them to WH 5101