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

Slides:



Advertisements
Similar presentations
Carnegie Mellon 1 Concurrent Programming / : Introduction to Computer Systems 23 rd Lecture, Nov. 15, 2012 Instructors: Dave O’Hallaron, Greg.
Advertisements

1 Carnegie Mellon Proxylab and stuff : Introduction to Computer Systems Recitation 13: November 19, 2012 Donald Huang (donaldh) Section M.
Carnegie Mellon 1 Synchronization: Basics : Introduction to Computer Systems 23 rd Lecture, Nov. 16, 2010 Instructors: Randy Bryant and Dave O’Hallaron.
Lecture 4: Concurrency and Threads CS 170 T Yang, 2015 Chapter 4 of AD textbook.
Threads By Dr. Yingwu Zhu. Review Multithreading Models Many-to-one One-to-one Many-to-many.
– 1 – , F’02 Traditional View of a Process Process = process context + code, data, and stack shared libraries run-time heap 0 read/write data Program.
Fork Fork is used to create a child process. Most network servers under Unix are written this way Concurrent server: parent accepts the connection, forks.
Concurrent Servers Dec 3, 2002 Topics Limitations of iterative servers Process-based concurrent servers Event-based concurrent servers Threads-based concurrent.
Pthreads Operating Systems Hebrew University of Jerusalem Spring 2004.
Lecture 18 Threaded Programming CPE 401 / 601 Computer Network Systems slides are modified from Dave Hollinger.
Threads© Dr. Ayman Abdel-Hamid, CS4254 Spring CS4254 Computer Network Architecture and Programming Dr. Ayman A. Abdel-Hamid Computer Science Department.
Concurrent HTTP Proxy with Caching Ashwin Bharambe Monday, Dec 4, 2006.
Carnegie Mellon 1 Synchronization: Basics / : Introduction to Computer Systems 23 rd Lecture, Jul 21, 2015 Instructors: nwf and Greg Kesden.
1 Concurrent Programming Andrew Case Slides adapted from Mohamed Zahran, Jinyang Li, Clark Barrett, Randy Bryant and Dave O’Hallaron.
Concurrent Servers April 25, 2002 Topics Limitations of iterative servers Process-based concurrent servers Threads-based concurrent servers Event-based.
Programming with POSIX* Threads Intel Software College.
ECE 297 Concurrent Servers Process, fork & threads ECE 297.
1 Concurrent Programming. 2 Outline Concurrent programming –Processes –Threads Suggested reading: –12.1, 12.3.
Carnegie Mellon 1 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Concurrent Programming : Introduction to Computer.
CS333 Intro to Operating Systems Jonathan Walpole.
Week 16 (April 25 th ) Outline Thread Synchronization Lab 7: part 2 & 3 TA evaluation form Reminders Lab 7: due this Thursday Final review session Final.
Threads CSCE Thread Motivation Processes are expensive to create. Context switch between processes is expensive Communication between processes.
Threads Chapter 26. Threads Light-weight processes Each process can have multiple threads of concurrent control. What’s wrong with processes? fork() is.
1 Pthread Programming CIS450 Winter 2003 Professor Jinhua Guo.
Pthreads.
PROCESS AND THREADS. Three ways for supporting concurrency with socket programming  I/O multiplexing  Select  Epoll: man epoll  Libevent 
Carnegie Mellon Introduction to Computer Systems /18-243, fall nd Lecture, Nov. 17 Instructors: Roger B. Dannenberg and Greg Ganger.
Copyright ©: Nahrstedt, Angrave, Abdelzaher
Carnegie Mellon 1 Concurrent Programming / : Introduction to Computer Systems 22 nd Lecture, Nov. 11, 2014 Instructors: Greg Ganger, Greg.
Concurrency I: Threads Nov 9, 2000 Topics Thread concept Posix threads (Pthreads) interface Linux Pthreads implementation Concurrent execution Sharing.
Thread Basic Thread operations include thread creation, termination, synchronization, data management Threads in the same process share:  Process address.
1 Carnegie Mellon Synchronization : Introduction to Computer Systems Recitation 14: November 25, 2013 Pratik Shah (pcshah) Section C.
Concurrency Threads Synchronization Thread-safety of Library Functions Anubhav Gupta Dec. 2, 2002 Outline.
1 Introduction to Threads Race Conditions. 2 Process Address Space Revisited Code Data OS Stack (a)Process with Single Thread (b) Process with Two Threads.
Carnegie Mellon Recitation 11: ProxyLab Fall 2009 November 16, 2009 Including Material from Previous Semesters' Recitations.
CS703 – Advanced Operating Systems By Mr. Farhan Zaidi.
Instructor: Haryadi Gunawi
Threads Synchronization Thread-safety of Library Functions
Synchronization: Basics
Programming with Threads
Programming with Threads
Threads in C Caryl Rahn.
Threads Threads.
Copyright ©: Nahrstedt, Angrave, Abdelzaher
CS399 New Beginnings Jonathan Walpole.
CENG334 Introduction to Operating Systems
Concurrent Programming November 13, 2008
Concurrency I: Threads April 10, 2001
Concurrent Servers Topics Limitations of iterative servers
Instructor: Randy Bryant
Programming with Threads
Concurrent Programming
CS510 Operating System Foundations
Pthread Prof. Ikjun Yeom TA – Mugyo
Jonathan Walpole Computer Science Portland State University
Concurrent Programming : Introduction to Computer Systems 23rd Lecture, Nov. 13, 2018
Concurrent Programming CSCI 380: Operating Systems
Programming with Threads
Programming with Threads
Concurrent Programming November 13, 2008
Programming with Threads
Concurrent Programming December 2, 2004
Instructor: Brian Railing
Lecture 19: Threads CS April 3, 2019.
Instructor: Brian Railing
Shared Memory Programming with Pthreads
Programming with Threads
Threads CSE 2431: Introduction to Operating Systems
Concurrent Programming
Presentation transcript:

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

– 2 – , S’04 pthread_create pthread_join pthread_self pthread_cancel pthread_exit pthread_mutex_init pthread_mutex_[un]lock pthread_cond_init pthread_cond_[timed]wait

– 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

– 4 – , S’04 Three 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!

– 5 – , S’04 View of Process Process = 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 stack SP PC Process context

– 6 – , S’04 View 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 libraries run-time heap 0 read/write data Shared code and data read-only code/data Thread 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 memory model

– 7 – , S’04 Posix 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_[un]lock pthread_cond_init pthread_cond_[timed]wait

– 8 – , 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); }

– 9 – , 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 thread terminates) Pthread_create() returns

– 10 – , S’04 Practices Basic usage of thread Pthread_exit() & exit() Joinable and detached thread Thread safety Protecting shared variable Function that returns a static pointer (more) ……

– 11 – , 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

– 12 – , 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); }

– 13 – , 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

– 14 – , S’04 Practices Basic usage of thread Pthread_exit() & exit() Joinable and detached thread Thread safety Protecting shared variable Function that returns a static pointer (more) ……

– 15 – , S’04 Joinable & 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.

– 16 – , S’04 Practices Basic usage of thread Pthread_exit() & exit() Joinable and detached thread Thread safety Protecting shared variable Function that returns a static pointer (more) ……

– 17 – , S’04 Protecting shared variables Program 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); }

– 18 – , S’04 Practices Basic usage of thread Pthread_exit() & exit() Joinable and detached thread Thread safety Protecting shared variable Function that returns a static pointer (more) ……

– 19 – , S’04 Functions that return a pointer to a static value int main () { struct in_addr a, b; a.s_addr = inet_addr(“ ”); b.s_addr = inet_addr(“ ”); printf(“%s %s\n”, inet_ntoa(a), inet_ntoa(b)); } output: ??? int main () { struct in_addr a; a.s_addr = inet_addr(“ ”); printf(“%s\n”, inet_ntoa(a)); } Output:

– 20 – , S’04 Thread 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

– 21 – , S’04 More Practice Problems Program 1.3 & 1.4 are left for your practice

– 22 – , 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

– 23 – , 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!

– 24 – , S’04 Program 2.a void *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!

– 25 – , 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!

– 26 – , S’04 The 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 >= 0)

– 27 – , S’04 Program 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 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!

– 28 – , S’04 Program 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 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!

– 29 – , S’04 Summary So Far Thread Thread memory model How to write thread programs Thread safety (pitfalls of using thread) Racing Semaphore

– 30 – , S’04 Final Times Exam Time: May 3rd (Monday) 5:30pm – 8:30p UC McConomy Review Session: May 1 st (Saturday), 3:00pm

– 31 – , S’04 Evaluation Forms There are questions on both sides One student bring them to WH 5101