Last class: Today: Thread Background. Thread Systems

Similar documents
Threads. What is a thread? Motivation. Single and Multithreaded Processes. Benefits

Thread Concept. Thread. No. 3. Multiple single-threaded Process. One single-threaded Process. Process vs. Thread. One multi-threaded Process

I.-C. Lin, Assistant Professor. Textbook: Operating System Concepts 8ed CHAPTER 4: MULTITHREADED PROGRAMMING

Multithreaded Programming

Threads. CS3026 Operating Systems Lecture 06

Chapter 4: Threads. Operating System Concepts 9 th Edit9on

Chapter 4: Threads. Operating System Concepts. Silberschatz, Galvin and Gagne

CS370 Operating Systems

Chapter 4: Threads. Overview Multithreading Models Thread Libraries Threading Issues Operating System Examples Windows XP Threads Linux Threads

CS 475. Process = Address space + one thread of control Concurrent program = multiple threads of control

The course that gives CMU its Zip! Concurrency I: Threads April 10, 2001

CS333 Intro to Operating Systems. Jonathan Walpole

Chapter 4: Threads. Chapter 4: Threads

Chapter 4 Multithreaded Programming

OPERATING SYSTEM. Chapter 4: Threads

Processes and Threads

Computer Systems Laboratory Sungkyunkwan University

CS510 Operating System Foundations. Jonathan Walpole

Ricardo Rocha. Department of Computer Science Faculty of Sciences University of Porto

THREADS. Jo, Heeseung

Operating Systems 2 nd semester 2016/2017. Chapter 4: Threads

Thread. Operating Systems (Fall/Winter 2018) Yajin Zhou ( Zhejiang University

Chapter 4: Threads. Chapter 4: Threads

Introduction to PThreads and Basic Synchronization

POSIX threads CS 241. February 17, Copyright University of Illinois CS 241 Staff

COP 4610: Introduction to Operating Systems (Spring 2015) Chapter 4: Threads. Zhi Wang Florida State University

Chapter 4: Multi-Threaded Programming

CS 450 Operating System Week 4 Lecture Notes

CS370 Operating Systems

CS 326: Operating Systems. Process Execution. Lecture 5

Introduction to pthreads

Lecture 4 Threads. (chapter 4)

Chapter 4: Threads. Overview Multithreading Models Threading Issues Pthreads Windows XP Threads Linux Threads Java Threads. Operating System Concepts

CS370 Operating Systems

Questions answered in this lecture: CS 537 Lecture 19 Threads and Cooperation. What s in a process? Organizing a Process

CSci 4061 Introduction to Operating Systems. (Threads-POSIX)

4.8 Summary. Practice Exercises

Threads. studykorner.org

Chapter 4: Threads. Operating System Concepts with Java 8 th Edition

Computer Systems Laboratory Sungkyunkwan University

Chapter 4: Multithreaded Programming. Operating System Concepts 8 th Edition,

Notes. CS 537 Lecture 5 Threads and Cooperation. Questions answered in this lecture: What s in a process?

Threads. CS-3013 Operating Systems Hugh C. Lauer. CS-3013, C-Term 2012 Threads 1

CS510 Operating System Foundations. Jonathan Walpole

Yi Shi Fall 2017 Xi an Jiaotong University

Outline. CS4254 Computer Network Architecture and Programming. Introduction 2/4. Introduction 1/4. Dr. Ayman A. Abdel-Hamid.

CS307: Operating Systems

CS 333 Introduction to Operating Systems. Class 3 Threads & Concurrency. Jonathan Walpole Computer Science Portland State University

CS 261 Fall Mike Lam, Professor. Threads

Chapter 4: Multithreaded Programming

4. Concurrency via Threads

Thread and Synchronization

Semantics of fork() and exec()

CSC209H Lecture 11. Dan Zingaro. March 25, 2015

EI 338: Computer Systems Engineering (Operating Systems & Computer Architecture)

Operating Systems. Lab. Class Project #5

Concurrent Programming

Chapter 5: Threads. Overview Multithreading Models Threading Issues Pthreads Windows XP Threads Linux Threads Java Threads

Processes. CS 416: Operating Systems Design, Spring 2011 Department of Computer Science Rutgers University

CISC2200 Threads Spring 2015

CS 333 Introduction to Operating Systems. Class 3 Threads & Concurrency. Jonathan Walpole Computer Science Portland State University

Chapter 4: Threads. Chapter 4: Threads. Overview Multicore Programming Multithreading Models Thread Libraries Implicit Threading Threading Issues

Multithreading and Interactive Programs

!! How is a thread different from a process? !! Why are threads useful? !! How can POSIX threads be useful?

Definition Multithreading Models Threading Issues Pthreads (Unix)

! How is a thread different from a process? ! Why are threads useful? ! How can POSIX threads be useful?

CS 3305 Intro to Threads. Lecture 6

ECE 598 Advanced Operating Systems Lecture 23

pthreads Announcement Reminder: SMP1 due today Reminder: Please keep up with the reading assignments (see class webpage)

Chapter 3: Processes. Operating System Concepts 8th Edition

CSE 153 Design of Operating Systems Fall 2018

Multithreading. Reading: Silberschatz chapter 5 Additional Reading: Stallings chapter 4

CS420: Operating Systems

LSN 13 Linux Concurrency Mechanisms

Concurrent Programming

Total Score is updated. The score of PA4 will be changed! Please check it. Will be changed

Threads. Still Chapter 2 (Based on Silberchatz s text and Nachos Roadmap.) 3/9/2003 B.Ramamurthy 1

Chapter 4: Multithreaded

CSE 333 SECTION 9. Threads

CS370 Operating Systems

What is concurrency? Concurrency. What is parallelism? concurrency vs parallelism. Concurrency: (the illusion of) happening at the same time.

Exercise (could be a quiz) Solution. Concurrent Programming. Roadmap. Tevfik Koşar. CSE 421/521 - Operating Systems Fall Lecture - IV Threads

Concurrency. Johan Montelius KTH

EPL372 Lab Exercise 2: Threads and pthreads. Εργαστήριο 2. Πέτρος Παναγή

Overview. Administrative. * HW 2 Grades. * HW 3 Due. Topics: * What are Threads? * Motivating Example : Async. Read() * POSIX Threads

CPSC 313: Intro to Computer Systems. POSIX Threads. Latency Hiding / Multiprogramming (covered earlier) Ease of Programming (covered now)

Chapter 4: Threads. Operating System Concepts 9 th Edition

Concurrent Programming

CPSC 341 OS & Networks. Threads. Dr. Yingwu Zhu

CSMC 412. Operating Systems Prof. Ashok K Agrawala Ashok Agrawala Set 4. September 2006 CMSC 412 Set 4

CSCE 313 Introduction to Computer Systems. Instructor: Dr. Dezhen Song

Agenda. Threads. Single and Multi-threaded Processes. What is Thread. CSCI 444/544 Operating Systems Fall 2008

CSCE 313 Introduction to Computer Systems. Instructor: Dezhen Song

Processes. CS3026 Operating Systems Lecture 05

CSCE 313: Intro to Computer Systems

Proxy: Concurrency & Caches

Concurrent Programming

Concurrency, Thread. Dongkun Shin, SKKU

CSCE 313: Intro to Computer Systems

Chapter 10. Threads. OS Processes: Control and Resources. A process as managed by the operating system groups together:

Transcription:

1

Last class: Thread Background Today: Thread Systems 2

Threading Systems 3

What kind of problems would you solve with threads? Imagine you are building a web server You could allocate a pool of threads, one for each client Thread would wait for a request, get content file, return it How would the different thread models impact this? Imagine you are building a web browser You could allocate a pool of threads Some for user interface Some for retrieving content Some for rendering content What happens if the user decided to stop the request? Mouse click on the stop button 4

Linux Threads Linux uses a one-to-one thread model Threads are calls tasks Linux views threads are contexts of execution Threads are defined separately from processes I.e., a thread is assigned an address space 5

Linux system call Linux Threads clone(int (*fn)(), void **stack, int flags, int argc, /*args */) Create a new thread (Linux task) May be created in the same address space or not Flags: Clone VM, Clone Filesystem, Clone Files, Clone Signal Handlers If clone with all these flags off, what system call is clone equal to? 6

Linux Threads Linux system call clone(int (*fn)(), void **stack, int flags, int argc, /*args */) Create a new thread (Linux task) clone(start, stack, 0, 1) What are start and stack for? So, clone provides significant flexibility over fork Compare to fork variants in Solaris Example: http://tldp.org/faq/threads-faq/clone.c 7

POSIX Threads POSIX Threads or Pthreads is a thread API specification Not directly an implementation Could be mapped to libraries or system calls Supported by Solaris and Linux 8

POSIX Threads Interface pthread_create() thread ID {of the new thread} Set on return attributes {of the new thread} stack size, scheduling information, etc. function {one arg only} the start function arg {for function} the start function Return value, status {of the system call} 9

POSIX Threads pthread_self() return thread ID pthread_equal() for comparisons of thread ID's pthread_exit() or just return from the start function pthread_join() wait for another thread to terminate retrieve value from pthread_exit() pthread_cancel() terminate a thread, by TID pthread_detach() thread is immune to join or cancel runs independently until it terminates pthread_attr_init() thread attribute modifiers 10

POSIX Threads Some example code http://www.cse.psu.edu/~dheller/cse411/programs/thread_sample.c 11

POSIX Threads FAQ How do we pass multiple arguments to start a thread? Build a struct and pass a pointer to it Is the pthreads id unique to the system? No, just process -- Linux task ids are system-wide After pthread_create, which thread is running? Like fork How many threads does exit terminate? pthread_exit? All in process; only caller How are variables shared by threads? Globals, local static, dynamic data (heap) 12

Inter-Thread Communication Can you use shared memory? Already have it Just need to allocate memory in the address space No need for shm Programming to pipes provides abstraction Can you use message passing? Sure Would have to build infrastructure An advantage of using kernel threads (e.g., Linux) is that system IPC can be used between threads Would want optimized paths for common address space 13

Threading Issues 14

Fork/Exec Issues Semantics are ambiguous for multithreaded processes fork() How does it interact with threads? exec() What happens to the other threads? fork, then exec Should all threads be copied? 15

Thread Cancellation So, you want to stop a thread from executing Don t need it anymore Remember the browser stop example Two choices Asynchronous cancellation Terminate it now What about resources allocated to the thread? Deferred cancellation Terminate when the thread is ready Thread checks for termination periodically pthread_cancel(thread_id) 16

Signal Handling What s a signal? A form of IPC Send a particular signal to another process The receiver s signal handler processes the signal on receipt Example Tell the Internet daemon (inetd) to reread its config file Send signal to inetd: kill -SIGHUP <pid> inetd s signal handler for the SIGHUP signal re-reads the config file Note: some signals cannot be handled by the receiving process, so they cause default action (kill the process) 17

Signal Handling Synchronous Signals Generated by the kernel for the process E.g., due to an exception -- divide by 0 Events caused by the thread receiving the signal Asynchronous Signals Generated by another process Asynchronous signals are more difficult for multithreading 18

Signal Handling and Threads So, you send a signal to a process Which thread should it be delivered to? Choices Thread to which the signal applies Every thread in the process Certain threads in the process A specific signal receiving thread It depends 19

Signal Handling and Threads Synchronous vs. Asynchronous Cases Synchronous Signal is delivered to the same process that caused the signal Which thread(s) would you deliver the signal to? Asynchonous Signal generated by another process Which thread(s) in this case? 20

Thread Pools Problem: setup time Faster than setting up a process, but what is necessary? How do we improve performance? 21

Thread Pools Pool of threads Create (all) at initialization time Assign task to a waiting thread It s already made Use all available threads What about when that task is done? Suppose another request is in the queue Should we use running thread or another thread? 22

Scheduling So how many kernel threads should be available for a process? In M:N model Suppose the last kernel thread for an application is to be blocked Recall the relationship between kernel and user threads What happens? 23

Scheduling Wouldn t it be nice if the kernel told the application and the application had a way to get more kernel threads? Scheduler activation At thread block, the kernel tells the application via an upcall Aside: An upcall is a general term for an invocation of application function from the kernel Application can then get a new thread created See lightweight threads in Section 4.4.6 24

Reentrance and Thread-Safety Terms that you might hear Reentrant Code Code that can be run by multiple threads concurrently Thread-safe Libraries Library code that permits multiple threads to invoke the safe function Requirements Rely only on input data Or some thread-specific data 25

Why not threads? Threads can interfere with one another Impact of more threads on caches Impact of more threads on TLB Execution of multiple may slow them down Impact of single thread vs. switching among threads Harder to program a multithreaded program Multitasking hides context switching Multithreading introduces concurrency issues 26

Threads Summary Programming systems Multi-threaded design issues Useful, but not a panacea Slow down system in some cases Can be difficult to program 27

Next time: CPU Scheduling 28