Concurrency I: Threads Nov 9, 2000 Topics Thread concept Posix threads (Pthreads) interface Linux Pthreads implementation Concurrent execution Sharing.

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

Carnegie Mellon 1 Synchronization: Basics : Introduction to Computer Systems 23 rd Lecture, Nov. 16, 2010 Instructors: Randy Bryant and Dave O’Hallaron.
Processes Topics Process context switches Creating and destroying processes CS 105 “Tour of the Black Holes of Computing!”
Lecture 4: Concurrency and Threads CS 170 T Yang, 2015 Chapter 4 of AD textbook.
– 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.
Pthreads Operating Systems Hebrew University of Jerusalem Spring 2004.
Unix Threads operating systems. User Thread Packages pthread package mach c-threads Sun Solaris3 UI threads Kernel Threads Windows NT, XP operating systems.
Concurrent HTTP Proxy with Caching Ashwin Bharambe Monday, Dec 4, 2006.
Concurrent Servers April 25, 2002 Topics Limitations of iterative servers Process-based concurrent servers Threads-based concurrent servers Event-based.
1 Concurrent Programming. 2 Outline Concurrent programming –Processes –Threads Suggested reading: –12.1, 12.3.
Processes Topics Process context switches Creating and destroying processes CS 105 “Tour of the Black Holes of Computing!”
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 Chapter 26. Threads Light-weight processes Each process can have multiple threads of concurrent control. What’s wrong with processes? fork() is.
PROCESS AND THREADS. Three ways for supporting concurrency with socket programming  I/O multiplexing  Select  Epoll: man epoll  Libevent 
Silberschatz, Galvin and Gagne ©2005 Modified by Dimitris Margaritis, Spring 2007 Chapter 4: Threads.
12/22/ Thread Model for Realizing Concurrency B. Ramamurthy.
Copyright ©: Nahrstedt, Angrave, Abdelzaher
Threads A thread is an alternative model of program execution
Thread S04, Recitation, Section A Thread Memory Model Thread Interfaces (System Calls) Thread Safety (Pitfalls of Using Thread) Racing Semaphore.
Carnegie Mellon 1 Concurrent Programming / : Introduction to Computer Systems 22 nd Lecture, Nov. 11, 2014 Instructors: Greg Ganger, Greg.
1 Exceptional Control Flow Ⅱ. 2 Outline Kernel Mode and User Mode Process context switches Three States of Processes Context Switch System Calls and Error.
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.
7/9/ Realizing Concurrency using Posix Threads (pthreads) B. Ramamurthy.
Tutorial 4. In this tutorial session we’ll see Threads.
A thread is a basic unit of CPU utilization within a process Each thread has its own – thread ID – program counter – register set – stack It shares the.
CS703 – Advanced Operating Systems By Mr. Farhan Zaidi.
Threads Some of these slides were originally made by Dr. Roger deBry. They include text, figures, and information from this class’s textbook, Operating.
Instructor: Haryadi Gunawi
Threads Synchronization Thread-safety of Library Functions
Realizing Concurrency using the thread model
Threads in C Caryl Rahn.
CS399 New Beginnings Jonathan Walpole.
CENG334 Introduction to Operating Systems
Chapter 4: Threads.
Linux Processes & Threads
Concurrent Programming November 13, 2008
Concurrency I: Threads April 10, 2001
Concurrent Servers Topics Limitations of iterative servers
Realizing Concurrency using Posix Threads (pthreads)
Operating Systems Lecture 13.
Instructor: Randy Bryant
Concurrent Programming
Realizing Concurrency using the thread model
CS 105 “Tour of the Black Holes of Computing!”
CS510 Operating System Foundations
Operating System Concepts
CS 105 “Tour of the Black Holes of Computing!”
CS 105 “Tour of the Black Holes of Computing!”
Jonathan Walpole Computer Science Portland State University
CS 105 “Tour of the Black Holes of Computing!”
Realizing Concurrency using the thread model
Realizing Concurrency using Posix Threads (pthreads)
Concurrent Programming : Introduction to Computer Systems 23rd Lecture, Nov. 13, 2018
Realizing Concurrency using the thread model
Concurrent Programming CSCI 380: Operating Systems
Realizing Concurrency using Posix Threads (pthreads)
CS510 Operating System Foundations
Tutorial 4.
Concurrent Programming November 13, 2008
Instructor: Brian Railing
Lecture 19: Threads CS April 3, 2019.
Instructor: Brian Railing
Shared Memory Programming with Pthreads
Concurrent Programming
Presentation transcript:

Concurrency I: Threads Nov 9, 2000 Topics Thread concept Posix threads (Pthreads) interface Linux Pthreads implementation Concurrent execution Sharing data class22.ppt “The course that gives CMU its Zip!”

CS 213 F’00– 2 – class22.ppt Traditional view of a process shared libraries run-time heap 0 read/write data Process = process context + code, data, and stack Program context: Data registers Condition codes Stack pointer (SP) Program counter (PC) Kernel context: VM structures Open files Signal handlers brk pointer Code, data, and stack read-only code/data stack SP PC brk Process context

CS 213 F’00– 3 – class22.ppt Modern view of a process shared libraries run-time heap 0 read/write data Process = thread + code, data, and kernel context Thread context: Data registers Condition codes Stack pointer (SP) Program counter (PC) Code and Data read-only code/data stack SP PC brk Thread (main thread) Kernel context: VM structures Open files Signal handlers brk pointer

CS 213 F’00– 4 – class22.ppt A process with multiple threads shared libraries run-time heap 0 read/write data Thread 1 context: Data registers Condition codes SP1 PC1 Shared code and data read-only code/data stack 1 Thread 1 (main thread) Kernel context: VM structures Open files Signal handlers brk pointer Multiple threads can be associated with a process Each thread has its own logical control flow (sequence of PC values) Each thread shares the same code, data, and kernel context Each thread has its own thread id (tid) Thread 2 context: Data registers Condition codes SP2 PC2 stack 2 Thread 2 (peer thread)

CS 213 F’00– 5 – class22.ppt Logical view of threads Threads associated with a process form a pool of peers. unlike processes which form a tree hierarchy P0 P1 sh foo bar T1 Process hierarchy Threads associated with process foo T2 T4 T5 T3 shared code, data and kernel context

CS 213 F’00– 6 – class22.ppt Concurrent thread execution Two threads run concurrently (are concurrent) if their logical flows overlap in time. Otherwise, they are sequential. Examples: Concurrent: A & B, A&C Sequential: B & C Time Thread AThread BThread C

CS 213 F’00– 7 – class22.ppt Threads vs processes How threads and processes are similar Each has its own logical control flow. Each can run concurrently. Each is context switched. How threads and processes are different Threads share code and data, processes (typically) do not. Threads are somewhat less expensive than processes. –process control (creating and reaping) is twice as expensive as thread control. –Linux/Pentium III numbers: »20K cycles to create and reap a process. »10K cycles to create and reap a thread.

CS 213 F’00– 8 – class22.ppt Threads are a unifying abstraction for exceptional control flow Exception handler A handler can be viewed as a thread Waits for a "signal" from CPU Upon receipt, executes some code, then waits for next "signal" Process A process is a thread + shared code, data, and kernel context. Signal handler A signal handler can be viewed as a thread Waits for a signal from the kernel or another process Upon receipt, executes some code, then waits for next signal.

CS 213 F’00– 9 – class22.ppt Posix threads (Pthreads) interface Pthreads: Standard interface for ~60 functions that manipulate threads from C programs. Creating and reaping threads. –pthread_create –pthread_join Determining your thread ID –pthread_self Terminating threads –pthread_cancel –pthread_exit –exit() [terminates all threads], ret [terminates current thread] Synchronizing access to shared variables –pthread_mutex_init –pthread_mutex_[un]lock –pthread_cond_init –pthread_cond_[timed]wait

CS 213 F’00– 10 – class22.ppt The Pthreads "hello, world" program /* * hello.c - Pthreads "hello, world" program */ #include void *thread(void *vargp); int main() { pthread_t tid; Pthread_create(&tid, NULL, thread, NULL); Pthread_join(tid, NULL); exit(0); } /* thread routine */ void *thread(void *vargp) { printf("Hello, world!\n"); return NULL; } Thread attributes (usually NULL) Thread arguments (void *p) return value (void **p)

CS 213 F’00– 11 – class22.ppt Execution of “hello, world” main threadpeer thread create peer thread print output terminate thread via ret wait for peer thread to terminate exit() terminates main thread and any peer threads

CS 213 F’00– 12 – class22.ppt Unix vs Posix error handling Unix-style error handling (Unix syscalls) if error: return -1 and set errno variable to error code. if OK: return useful result as value >= 0. Posix-style error handling (newer Posix functions) if error: return nonzero error code, zero if OK useful results are passed back in an argument. if ((pid = wait(NULL)) < 0) { perror("wait"); exit(0); } if ((rc = pthread_join(tid, &retvalp)) != 0) { printf(”pthread_create: %s\n", strerror(rc)); exit(0); }

CS 213 F’00– 13 – class22.ppt Suggested error handling macros Error checking crucial, but cluttered. Use these to simplify your error checking: /* * macro for posix-style error handling */ #define posix_error(code,msg) do {\ printf("%s: %s\n", msg, strerror(code));\ exit(0);\ } while (0) /* * macro for unix-style error handling */ #define unix_error(msg) do {\ printf("%s: %s\n", msg, strerror(errno));\ exit(0);\ } while (0)

CS 213 F’00– 14 – class22.ppt Pthreads wrappers We advocate Steven’s convention of providing wrappers for each system-level function call. wrapper is denoted by capitalizing first letter of function name. wrapper has identical interface as the original function. each wrapper does appropriate unix or posix style error checking. wrapper typically return nothing. declutters code without compromising safety. /* * wrapper function for pthread_join */ void Pthread_join(pthread_t tid, void **thread_return) { int rc = pthread_join(tid, thread_return); if (rc != 0) posix_error(rc, "Pthread_join"); }

CS 213 F’00– 15 – class22.ppt Basic thread control: create a thread Creates a new peer thread tidp : thread id attrp : thread attributes (usually NULL) routine: thread routine argp : input parameters to routine Akin to fork() but without the confusing “call once return twice” semantics. peer thread has local stack variables, but shares all global variables. int pthread_create(pthread_t *tidp, pthread_attr_t *attrp, void *(*routine)(void *), void *argp);

CS 213 F’00– 16 – class22.ppt Basic thread control: join Waits for a specific peer thread to terminate, and then reaps it. tid : thread ID of thread to wait for. thread_return : object returned by peer thread via ret stmt Akin to wait and wait_pid but unlike wait... Any thread can reap any other thread (not just children) Must wait for a *specific* thread –no way to wait for *any* thread. –perceived by some as a flaw in the Pthreads design int pthread_join(pthread_t tid, void **thread_return);

CS 213 F’00– 17 – class22.ppt Linux implementation of Pthreads Linux implements threads in an elegant way: Threads are just processes that share the same kernel context. fork() : creates a child process with a new kernel context clone() : creates a child process that shares some or all of the parent’s kernel context. int __clone(int (*fn)(void *arg),void *child_stack, int flags, void *arg); Creates a new process and executes function fn with argument arg in that process using the stack space pointed to by child_stack. Returns pid of new process. flags determine the degree of kernel context sharing: e.g., CLONE_VM: share virtual address space CLONE_FS: share file system information CLONE_FILES: share open file descriptors

CS 213 F’00– 18 – class22.ppt hellopid.c The following routine will show us the process hierarchy of a Linux thread pool: #include void *thread(void *vargp); int main() { pthread_t tid; printf("Hello from main thread! tid:%ld pid:%d\n", pthread_self(), getpid()); Pthread_create(&tid, NULL, thread, NULL); Pthread_join(tid, NULL); exit(0); } void *thread(void *vargp) { printf("Hello from child thread! tid:%ld pid:%d ppid:%d\n", pthread_self(), getpid(), getppid()); return NULL; }

CS 213 F’00– 19 – class22.ppt Linux process hierarchy for threads bass> hellopid Hello from main thread! tid:1024 pid:6024 Hello from child thread! tid:1025 pid:6026 ppid:6025 thread mgr pid=6025 main pid=6024 child pid=6026 other peer thread other peer thread Thread manager supports thread abstraction using signals: exit(): kills all threads, regardless where it is called from slow system calls such as sleep() or read() block only the calling thread.

CS 213 F’00– 20 – class22.ppt beep.c: Performing concurrent tasks /* * beeps until the user hits a key */ #include void *thread(void *vargp); /* shared by both threads */ char shared = '\0'; int main() { pthread_t tid; Pthread_create(&tid, NULL, thread, NULL); while (shared == '\0') { printf("BEEP\n"); sleep(1); } Pthread_join(tid, NULL); printf("DONE\n"); exit(0); } /* thread routine */ void *thread(void *vargp) { shared = getchar(); return NULL; }

CS 213 F’00– 21 – class22.ppt badcnt.c: Sharing data between threads /* bad sharing */ #include #define NITERS 1000 void *count(void *arg); struct { int counter; } shared; int main() { pthread_t tid1, tid2; Pthread_create(&tid1, NULL, count, NULL); Pthread_create(&tid2, NULL, count, NULL); if (shared.counter != NITERS*2) printf("BOOM! counter=%d\n", shared.counter); else printf("OK counter=%d\n", shared.counter); } /* thread routine */ void *count(void *arg) { int i, val; for (i=0; i<NITERS; i++) { val = shared.counter; printf("%d: %d\n", (int)pthread_self(), val); shared.counter = val + 1; } return NULL; } Key point: “struct shared” is visible to all threads. “i” and “val” are visible only to the count thread.

CS 213 F’00– 22 – class22.ppt Running badcnt.c 1025: : : : : : : : : : : : 1968 BOOM! counter= : : : : : : : : : : : : 1711 BOOM! counter= : : : : : : : : : : : : 1999 OK counter=2000 Output of run 1 Output of run 2Output of run 3 So what’s the deal? We must synchronize concurrent accesses to shared thread data (the topic of our next lecture)