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

Similar documents
CS 450 Operating System Week 4 Lecture Notes

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

Multithreaded Programming

Chapter 4: Threads. Chapter 4: Threads

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

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

CISC2200 Threads Spring 2015

ECE519 Advanced Operating Systems

Operating Systems: Internals and Design Principles. Chapter 4 Threads Seventh Edition By William Stallings

For use by students enrolled in #71251 CSE430 Fall 2012 at Arizona State University. Do not use if not enrolled.

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

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

Chapter 4: Threads. Operating System Concepts 9 th Edit9on

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

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

OPERATING SYSTEM. Chapter 4: Threads

4. Concurrency via Threads

Chapter 4: Multithreaded Programming

Chapter 3 Process Description and Control

Operating Systems. Engr. Abdul-Rahman Mahmood MS, PMP, MCP, QMR(ISO9001:2000) alphapeeler.sf.net/pubkeys/pkey.htm

Che-Wei Chang Department of Computer Science and Information Engineering, Chang Gung University

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

Chapter 4 Multithreaded Programming

Chapter 4: Threads. Chapter 4: Threads

Process Characteristics. Threads Chapter 4. Process Characteristics. Multithreading vs. Single threading

Threads Chapter 4. Reading: 4.1,4.4, 4.5

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

Lecture Contents. 1. Overview. 2. Multithreading Models 3. Examples of Thread Libraries 4. Summary

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

CSE 306/506 Operating Systems Threads. YoungMin Kwon

Threads. CS3026 Operating Systems Lecture 06

CS307: Operating Systems

Roadmap. Concurrent Programming. Concurrent Programming. Motivation. Why Threads? Tevfik Ko!ar. CSC Systems Programming Fall 2010

Threads Chapter 5 1 Chapter 5

Processes, Threads, SMP, and Microkernels

Threads. Threads (continued)

CS510 Operating System Foundations. Jonathan Walpole

Processes and Threads

Operating System. Chapter 4. Threads. Lynn Choi School of Electrical Engineering

Definition Multithreading Models Threading Issues Pthreads (Unix)

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

CS333 Intro to Operating Systems. Jonathan Walpole

Chapter 4: Multi-Threaded Programming

CS 3305 Intro to Threads. Lecture 6

Chapter 4 Threads, SMP, and

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

Threads. studykorner.org

Chapter 4: Threads. Operating System Concepts 9 th Edition

Process Characteristics

Chapter 4: Threads. Operating System Concepts 9 th Edition

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

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

Threads. Thread Concept Multithreading Models User & Kernel Threads Pthreads Threads in Solaris, Linux, Windows. 2/13/11 CSE325 - Threads 1

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

Motivation. Threads. Multithreaded Server Architecture. Thread of execution. Chapter 4

Outline. Threads. Single and Multithreaded Processes. Benefits of Threads. Eike Ritter 1. Modified: October 16, 2012

CSCE 313 Introduction to Computer Systems. Instructor: Dezhen Song

CSCE 313: Intro to Computer Systems

Chapter 4: Multithreaded

4.8 Summary. Practice Exercises

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

THREADS. Jo, Heeseung

Chapter 5: Threads. Outline

Processes and Threads. Processes and Threads. Processes (2) Processes (1)

CS420: Operating Systems

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

Lecture Topics. Announcements. Today: Threads (Stallings, chapter , 4.6) Next: Concurrency (Stallings, chapter , 5.

Process Description and Control

Chapter 4: Multithreaded Programming

Threads, SMP, and Microkernels

Chapter 4: Multithreaded Programming

Chapter 3: Processes. Operating System Concepts 8th Edition

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

Lecture 4 Threads. (chapter 4)

2017 Pearson Educa2on, Inc., Hoboken, NJ. All rights reserved.

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

CS370 Operating Systems

Operating Systems Prof. Ashok K Agrawala

Chapter 4: Threads. Operating System Concepts 8 th Edition,

Chapter 5: Threads. Single and Multithreaded Processes

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

Threads. Jinkyu Jeong Computer Systems Laboratory Sungkyunkwan University

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

Last class: Today: Thread Background. Thread Systems

CS420: Operating Systems

CS370 Operating Systems

Chapter 4: Multithreaded Programming

Computer Systems Laboratory Sungkyunkwan University

Process Concept Process in Memory Process State new running waiting ready terminated Diagram of Process State

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

CS510 Operating System Foundations. Jonathan Walpole

ECE 7650 Scalable and Secure Internet Services and Architecture ---- A Systems Perspective. Part I: Operating system overview: Processes and threads

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

Threads Assistant Professor DCS Operating System Concepts

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

Lecture 2 Process Management

Semantics of fork() and exec()

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

CS 261 Fall Mike Lam, Professor. Threads

Transcription:

Multithreading Reading: Silberschatz chapter 5 Additional Reading: Stallings chapter 4 Understanding Linux/Unix Programming, Bruce Molay, Prentice-Hall, 2003. EEL 602 1

Outline Process and Threads Multithreading Motivation Advantages RPC using Thread(s) User-Level Threads Kernel-Level Threads Combined Approaches Phtreads Threading Issues System Call Semantics Thread Cancellation Signal Handling Thread Pools Thread Specific Data Introduction: Linux, Win32, Solaris and Java Threads EEL 602 2

Process and Threads Process Management Resource Ownership Memory, I/O channel, I/O devices and file Scheduling/Execution Execution state and priority Independent treatment by OS Unit of dispatching Thread Lightweight Process (LWP, even KLT) Unit of resource ownership Process Task EEL 602 3

Multithreading Multiple threads of execution within single process Single Thread: Traditional approach OS Support for Threads MSDOS - a single user process and a single thread UNIX - multiple user processes but only supports one thread per process Windows, Solaris, Linux, Mach, and OS/2 - multiple threads Thread Basic unit of CPU utilization, consisting of PC Register set Stack EEL 602 4

Multithreading EEL 602 5

Examples - Motivation Web Browser One thread to display images Other thread retrieves data from network Word Processor One thread for responding to keystrokes Other thread for spelling and grammar checking Other thread for displaying graphics File Server on LAN Controller thread accepts file service requests and spawns worker thread for each request Can handle many requests concurrently, thread finishes service - destroyed EEL 602 6

Process Unit of resource allocation and a unit of protection, associated: A virtual address space which holds the process image Protected access to processors, other processes, files, and I/O resources Within the process there can be one or more threads EEL 602 7

Thread An execution state (running, ready, blocked) Saved thread context/state when not running Has an execution stack Some per-thread static storage for local variables Access to the memory and resources of its process Shared by all threads of the process EEL 602 8

Multithreading All of the threads of a process share the state and resources of process They reside in same address space and have access to same data EEL 602 9

Advantage Threads! Takes far less time to create a new thread in existing process than a new process; Factor 10 Less time to terminate a thread than a process Less time to switch between two threads within the same process Since threads within the same process share memory and files, they can communicate with each other without invoking the kernel EEL 602 10

Functionality Key States RRB but no suspend Operations Spawn Spawn another thread Block Unblock Finish EEL 602 11

RPC Using Single Thread EEL 602 12

RPC Using Threads EEL 602 13

Example - Multithreading EEL 602 14

User-Level Threads Thread Management Application New threads within the same process Thread Library Control, Library Thread Context user reg, pc & sp EEL 602 15

User-Level Threads EEL 602 16

ULT Vs KLT Advantages of ULTs Thread switching Kernel mode privileges, less overhead Scheduling can be application specific Thread libraries application utilities, Can run on any OS Disadvantages of ULTs High blocking, OS many system calls are blocking, all threads in process are blocked Pure ULT strategy cannot take advantage of multiprocessing Solutions? Jacketing Writing application as multiple processes rather than threads EEL 602 17

Kernel-Level Threads Thread Management Kernel API to the kernel thread facility Thread based scheduling by kernel Pure ULT strategy cannot take advantage of multiprocessing Transfer of control mode switch EEL 602 18

Kernel-Level Threads EEL 602 19

Combined Approaches Thread creation user space, Solaris Bulk of scheduling and synchronization of threads within application Multiple threads within the same application multiple processors Entire process is not blocked, Design EEL 602 20

Relationship Between Threads and Processes EEL 602 21

Threading Issues Semantics of System Calls fork() exec() Thread Cancellation Asynchronous Cancellation Deferred Cancellation EEL 602 22

Threading Issues Signal Handling To thread to which its applicable To every thread To certain threads To a specific thread assigned Thread Pools Sit & Wait Work, Return to pool Faster than waiting to create a thread Limits # that can exists at any point of time Thread Specific Data EEL 602 23

Pthreads A POSIX standard API for thread creation and sync API implementation dependent on OS Common in UNIX operating systems All programs pthread.h pthread_create() pthread_suspend() pthread_yield() pthread_continue() pthread_join() pthread_exit(); pthread_sigmask(); sigwait() EEL 602 24

Example: Running 2 functions simultaneously Single-threaded version Duration: 10 seconds Source code: /* hello_single.c - a single threaded hello world program */ #include <stdio.h> #define NUM 5 main() { void print_msg(char *); print_msg("hello"); print_msg("world\n"); } void print_msg(char *m) { int i; for(i=0 ; i<num ; i++){ printf("%s", m); fflush(stdout); sleep(1); } } Screen output: [ajaykr@lib ~]$./hello_single hellohellohellohellohelloworld world world world world EEL 602 25

Example: Running 2 functions simultaneously Multi-threaded version Duration: 5 seconds Source code: /* hello_multi.c - a multi-threaded hello world program */ #include <stdio.h> #include <pthread.h> #define NUM 5 main() { pthread_t t1, t2; /* two threads */ void *print_msg(void *); pthread_create(&t1, NULL, print_msg, (void *)"hello"); pthread_create(&t2, NULL, print_msg, (void *)"world\n"); pthread_join(t1, NULL); pthread_join(t2, NULL); } void *print_msg(void *m) { char *cp = (char *) m; int i; for(i=0 ; i<num ; i++){ printf("%s", m); fflush(stdout); sleep(1); } return NULL; } Screen output: [ajaykr@lib ~]$./hello_multi helloworld helloworld helloworld helloworld helloworld EEL 602 26

Linux Threads Linux refers to them as tasks rather than threads Thread creation Clone() system call, Flags CLONE_FS CLONE_VM CLONE_SIGHAND CLONE_FILES All flags thread, No flags fork (no sharing) Clone() allows a child task to share the address space of the parent task (process) EEL 602 27

Win32 Threads Primarily API for Win XP, NT, 2000, 98, 95 windows.h Thread creation CreateThread(), Windows XP application Separate Process Components of thread - a thread id - register set - separate user and kernel stacks - private data storage area 1:1 Mapping, fiber library EEL 602 28

Solaris 2 Threads EEL 602 29

Solaris Process EEL 602 30

Java Threads Java language level support Management JVM, alternative to user/kernel Thread Creation Extending Thread class, new class thread class Implementing the Runnable interface Java threads mapping depends on OS EEL 602 31

Java Thread States EEL 602 32