Title: Thread 15213S04, Recitation, Section A
1Thread15213-S04, Recitation, Section A
- Thread Memory Model
- Thread Interfaces (System Calls)
- Thread Safety (Pitfalls of Using Thread)
- Racing
- Semaphore
- Final Evaluation Forms
2- pthread_create
- pthread_join
- pthread_self
- pthread_cancel
- pthread_exit
- pthread_mutex_init
- pthread_mutex_unlock
- pthread_cond_init
- pthread_cond_timedwait
3Why Do We Care About Thread
- Useful for L7 Part II
- A very important way to implement modern
concurrent systems - Whats concurrency?
4Three Methods to Implement Concurrency
- 1. 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!
5View of Process
- Process process context code, data, and stack
Code, data, and stack
Process context
stack
SP
Program context Data registers Condition
code Stack pointer (SP) Program counter
(PC) Kernel context VM structures
Descriptor table
shared libraries
run-time heap
read/write data
PC
read-only code/data
0
6View of Thread
- Multiple 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 code and data
shared libraries
run-time heap
read/write data
read-only code/data
0
Kernel context VM structures Descriptor
table
7Posix Threads (Pthreads) Interface
- Standard interface for 60 functions
- Creating and reaping threads.
- pthread_create
- pthread_join
- Determining your thread ID
- pthread_self
- Terminating threads
- pthread_cancel
- pthread_exit
- Synchronizing access to shared variables
- pthread_mutex_init
- pthread_mutex_unlock
- pthread_cond_init
- pthread_cond_timedwait
8The 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)
9Execution of Threadedhello, world
call Pthread_join()
printf()
main thread waits for peer thread to terminate
exit() terminates main thread and any peer
threads
10Practices
- Basic usage of thread
- Pthread_exit() exit()
- Joinable and detached thread
- Thread safety
- Protecting shared variable
- Function that returns a static pointer
- (more)
11pthread_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)
12pthread_exit() exit()
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)
13pthread_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
14Practices
- Basic usage of thread
- Pthread_exit() exit()
- Joinable and detached thread
- Thread safety
- Protecting shared variable
- Function that returns a static pointer
- (more)
15Joinable Detached Threads
- At 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.
16Practices
- Basic usage of thread
- Pthread_exit() exit()
- Joinable and detached thread
- Thread safety
- Protecting shared variable
- Function that returns a static pointer
- (more)
17Protecting shared variables
int i 42void 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)
18Practices
- Basic usage of thread
- Pthread_exit() exit()
- Joinable and detached thread
- Thread safety
- Protecting shared variable
- Function that returns a static pointer
- (more)
19Functions that return a pointer to a static value
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
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 ???
20Thread Safety
- Class 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
21More Practice Problems
- Program 1.3 1.4 are left for your practice
22Racing
- 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
23Program 2.b
void foo(void vargp) int id id
((int )vargp) printf("Thread d\n",
id)int main() pthread_t tid2
int i for (i 0 i lt 2 i)
Pthread_create(tidi, NULL, foo, i)
Pthread_join(tid0, NULL)
Pthread_join(tid1, NULL)
Racing!
24Program 2.a
void foo(void vargp) int myid myid
((int )vargp) Free(vargp)
printf("Thread d\n", myid)int main()
pthread_t tid2 int i, ptr for (i
0 i lt 2 i) ptr Malloc(sizeof(int))
ptr i Pthread_create(tidi
, 0, foo, ptr) Pthread_join(tid0,
0) Pthread_join(tid1, 0)
Good!
25Program 2.c
void foo(void vargp) int id id
(int)vargp printf("Thread d\n", id)int
main() pthread_t tid2 int i
for (i 0 i lt 2 i)
Pthread_create(tidi, 0, foo, i)
Pthread_join(tid0, 0) Pthread_join(tid1,
0)
Good!
26The general solution for racing
- Semaphore 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 gt 0)
27Program 2.d
sem_t s / semaphore s /void foo(void
vargp) int id id ((int )vargp)
V(s) printf("Thread d\n", id)int
main() pthread_t tid2 int i
sem_init(s, 0, 0) / S0 INITIALLY / for
(i 0 i lt 2 i) Pthread_create(tid
i, 0, foo, i) P(s)
Pthread_join(tid0, 0) Pthread_join(tid1,
0)
Good!
28Program 2.e
sem_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 tid2
int i sem_init(s, 0, 1) / S1 INITIALLY
/ for (i 0 i lt 2 i)
Pthread_create(tidi, 0, foo, i)
Pthread_join(tid0, 0) Pthread_join(tid1,
0)
Racing!
29Summary So Far
- Thread
- Thread memory model
- How to write thread programs
- Thread safety (pitfalls of using thread)
- Racing
- Semaphore
30Final
- Times
- Exam Time May 3rd (Monday) 530pm
830p UC McConomy - Review Session May 1st (Saturday), 300pm
31Evaluation Forms
- There are questions on both sides
- One student bring them to WH 5101