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

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

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

CS 450 Operating System Week 4 Lecture Notes

OPERATING SYSTEM. Chapter 4: Threads

Chapter 4: Multi-Threaded Programming

Chapter 4: Threads. Chapter 4: Threads

CSE 4/521 Introduction to Operating Systems

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

Chapter 4: Threads. Chapter 4: Threads

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

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

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

Threads. CS3026 Operating Systems Lecture 06

CS307: Operating Systems

Chapter 3 Process Description and Control

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

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

Threads Chapter 5 1 Chapter 5

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

CS420: Operating Systems

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

Chapter 4: Multithreaded

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

Threads Implementation. Jo, Heeseung

Chapter 4: Threads. Operating System Concepts 9 th Edition

Chapter 4: Threads. Operating System Concepts 9 th Edit9on

Chapter 4: Multithreaded Programming

Preview. The Thread Model Motivation of Threads Benefits of Threads Implementation of Thread

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

ECE519 Advanced Operating Systems

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

Chapter 4: Threads. Operating System Concepts 9 th Edition

Multithreaded Programming

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

Threads Chapter 4. Reading: 4.1,4.4, 4.5

Processes and Threads

Threads, SMP, and Microkernels

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

Processes and Threads

Chapter 5: Threads. Outline

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

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

Definition Multithreading Models Threading Issues Pthreads (Unix)

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

CSCE 313 Introduction to Computer Systems. Instructor: Dezhen Song

CSCE 313: Intro to Computer Systems

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

Chapter 4: Multithreaded Programming

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

Concurrency, Thread. Dongkun Shin, SKKU

Chapter 4: Multithreaded Programming

Chapter 4: Multithreaded Programming

Threads. studykorner.org

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

Threads Implementation. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

Semantics of fork() and exec()

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

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

POSIX Threads: a first step toward parallel programming. George Bosilca

Page 1. Analogy: Problems: Operating Systems Lecture 7. Operating Systems Lecture 7

Chapter 5: Threads. Single and Multithreaded Processes

Processes & Threads. Recap of the Last Class. Microkernel System Architecture. Layered Structure

Chapter 4 Threads, SMP, and

4. Concurrency via Threads

THREADS. Jo, Heeseung

Process Characteristics

Chapter 4 Multithreaded Programming

Operating Systems Prof. Ashok K Agrawala

Motivation of Threads. Preview. Motivation of Threads. Motivation of Threads. Motivation of Threads. Motivation of Threads 9/12/2018.

Lecture 2 Process Management

4.8 Summary. Practice Exercises

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

CSE 153 Design of Operating Systems Fall 2018

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

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

Operating System Architecture. CS3026 Operating Systems Lecture 03

Lecture 17: Threads and Scheduling. Thursday, 05 Nov 2009

Process Description and Control

CHAPTER 2: PROCESS MANAGEMENT

Chapter 5: Processes & Process Concept. Objectives. Process Concept Process Scheduling Operations on Processes. Communication in Client-Server Systems

Threads, SMP, and Microkernels. Chapter 4

ICS Principles of Operating Systems

Agenda Process Concept Process Scheduling Operations on Processes Interprocess Communication 3.2

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

Threads. Jinkyu Jeong Computer Systems Laboratory Sungkyunkwan University

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

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

Chapter 3: Processes. Operating System Concepts 8th Edition

OPERATING SYSTEMS. UNIT II Sections A, B & D. An operating system executes a variety of programs:

Distributed Systems Operation System Support

CSE Opera,ng System Principles

CSE 306/506 Operating Systems Threads. YoungMin Kwon

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

Threads. Threads (continued)

CS 5523 Operating Systems: Midterm II - reivew Instructor: Dr. Tongping Liu Department Computer Science The University of Texas at San Antonio

CISC2200 Threads Spring 2015

LINUX INTERNALS & NETWORKING Weekend Workshop

Last Class: CPU Scheduling. Pre-emptive versus non-preemptive schedulers Goals for Scheduling: CS377: Operating Systems.

Questions from last time

Computer Systems Laboratory Sungkyunkwan University

Transcription:

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

Threads The process concept incorporates two abstractions: a virtual processor (an execution context) a resource ownership (an address space, open files etc..) Within an address space, we can have more units of execution: threads All the threads of a process share the same address space and the same resources 2/13/11 CSE325 - Threads 2

Multithreaded Environment Process all resources allocated: IPC channels, files etc.. a virtual address space that holds the process image Threads a dispatchable unit of work an execution state: running, ready, etc.. an execution context: PC, SP, other registers a per-thread stack 2/13/11 CSE325 - Threads 3

Single and Multithreaded Processes 2/13/11 CSE325 - Threads 4

2/13/11 CSE325 - Threads 5

Process vs. Thread Process Have a virtual address space, which holds the process image Protected access to processors, other processes, I/O resources, and files Thread An execution state (running, ready, etc.) Saved thread context (when not running) Has an execution stack Per-thread static storage for local variables Access to the memory and resources of the process it belongs to all threads of the same process share this 2/13/11 CSE325 - Threads 6

Threads vs. Processes Advantages operations on threads (creation, termination, scheduling, etc..) are cheaper than the corresponding operations on processes inter-thread communication is supported through shared memory without kernel intervention Responsiveness, Resource Sharing, Utilization of MP Architectures Disadvantages easy to introduce race conditions synchronization is necessary 2/13/11 CSE325 - Threads 7

User Threads All thread management is done by the application. A user thread maintains all its state in user space. The kernel is not aware of the existence of user threads Examples POSIX Pthreads Mach C-threads Solaris threads 2/13/11 CSE325 - Threads 8

Kernel Threads OS Kernel maintains context information for the process and the threads (LWP) Scheduling is done on a thread basis Examples Windows NT/XP/Vista/7 Solaris Tru64 UNIX Linux 2/13/11 CSE325 - Threads 9

Thread Implementation User-level threads Implemented as a thread library, which contains the code for thread creation, termination, scheduling, and switching Kernel sees one process and it is unaware of its thread activity can be preemptive or not (co-routines) Kernel-level threads Thread management done by the kernel 2/13/11 CSE325 - Threads 10

User-Level Thread Implementation thread 1 sp pc data sp pc thread stacks thread 2 code process kernel 2/13/11 CSE325 - Threads 11

User-Level vs. Kernel-Level Threads Advantages of the user-level threads Performance: low-cost thread operations (do not require crossing protection domains), fast context switching Flexibility: scheduling can be application specific Portability: user-level thread library easy to port Disadvantages of the user-level threads If a user-level thread is blocked in the kernel, the entire process (all threads of that process) are blocked cannot take advantage of multiprocessing (the kernel assigns one process to only one processor) 2/13/11 CSE325 - Threads 12

User-Level vs. Kernel-Level Threads user-level threads thread scheduling process process scheduling processor user kernel kernel-level threads thread scheduling processor 2/13/11 CSE325 - Threads 13

Multithreading Models Many-to-One One-to-One Many-to-Many 2/13/11 CSE325 - Threads 14

Many-to-One Model Many user-level threads mapped to single kernel thread. Used on systems that do not support multiple kernel threads. E.g., Solaris green threads; GNU Portable Threads. 2/13/11 CSE325 - Threads 15

One-to-One Model Each user-level thread maps to kernel thread. Examples Windows NT/XP/Vista/7 Linux, Solaris 9 2/13/11 CSE325 - Threads 16

Many-to-Many Model Allows many user level threads to be mapped to many kernel threads. Allows the operating system to create a sufficient number of kernel threads. Solaris 2 HP-UX, Tru64 UNIX 2/13/11 CSE325 - Threads 17

POSIX Thread (Pthread) a POSIX standard (IEEE 1003.1c) API for thread creation and synchronization. API specifies behavior of the thread library, implementation is up to developer of the library. Common in UNIX operating systems. 2/13/11 CSE325 - Threads 18

Pthread API thread creation and termination pthread_create(&tid,null,start_fn,arg); pthread_exit(status); thread join pthread_join(tid, &status); mutual exclusion pthread_mutex_lock(&lock); pthread_mutex_unlock(&lock); condition variable pthread_cond_wait(&c,&lock); pthread_cond_signal(&c); 2/13/11 CSE325 - Threads 19

Condition Variables (Example) thread 1 pthread_mutex_lock(&lock); while (!my-condition) pthread_cond_wait(&c,&lock); do_critical_section(); pthread_mutex_unlock(&lock); thread 2 pthread_mutex_lock(&lock); my-condition = true; pthread_mutex_unlock(&lock); pthread_cond_signal(&c); 2/13/11 CSE325 - Threads 20

Solaris 2 Threads 2/13/11 CSE325 - Threads 21

Solaris Process 2/13/11 CSE325 - Threads 22

Linux Threads Linux refers to them as tasks rather than threads. Thread creation is done through clone() system call. Clone() allows a child task to share the address space of the parent task (process) 2/13/11 CSE325 - Threads 23

Windows NT Threads Implements the one-to-one mapping. Each thread contains a thread ID register set separate user and kernel stacks private data storage area 2/13/11 CSE325 - Threads 24