Threads. CSE Computer Systems November 16, 2001

Similar documents
Threads. CSE 410, Spring 2004 Computer Systems.

Processes & Threads. Process Management. Managing Concurrency in Computer Systems. The Process. What s in a Process?

Yi Shi Fall 2017 Xi an Jiaotong University

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

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

CSE 120 Principles of Operating Systems

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

CSE 120 Principles of Operating Systems

Lecture 4: Threads; weaving control flow

Chapter 4: Threads. Chapter 4: Threads

CSE 120 Principles of Operating Systems

Scheduling (Win 2K) CSE Computer Systems November 21, 2001

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

Threads. Computer Systems. 5/12/2009 cse threads Perkins, DW Johnson and University of Washington 1

Interoperation of tasks

Win32 Programming. Jim Fawcett CSE775 Distributed Objects Spring 2012

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

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

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

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

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

CS 153 Design of Operating Systems Winter 2016

Chapter 4: Multithreaded Programming

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

Four Components of a Computer System. Operating System Concepts 7 th Edition, Jan 12, 2005

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

Threads and Too Much Milk! CS439: Principles of Computer Systems February 6, 2019

CS 450 Operating System Week 4 Lecture Notes

Chapter 4: Multithreaded

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

Threads. ZwCreateThread. Parameters. ThreadHandle Points to a variable that will receive the thread object handle if the call is successful.

CS 31: Intro to Systems Threading & Parallel Applications. Kevin Webb Swarthmore College November 27, 2018

Chapter 4: Threads. Chapter 4: Threads

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

CSE 153 Design of Operating Systems Fall 2018

Threads and Too Much Milk! CS439: Principles of Computer Systems January 31, 2018

CS420: Operating Systems

CS 318 Principles of Operating Systems

CSE 153 Design of Operating Systems

CSE 153 Design of Operating Systems Fall 2018

Process and Thread Management

Chapter 4: Threads. Operating System Concepts 9 th Edit9on

Processes and Threads

Outline. Process and Thread Management. Data Structures (2) Data Structures. Kernel Process Block (PCB)

Threads. Raju Pandey Department of Computer Sciences University of California, Davis Spring 2011

Process Description and Control

Readings and References. Virtual Memory. Virtual Memory. Virtual Memory VPN. Reading. CSE Computer Systems December 5, 2001.

Threads. CS3026 Operating Systems Lecture 06

Processes. 4: Threads. Problem needs > 1 independent sequential process? Example: Web Server. Last Modified: 9/17/2002 2:27:59 PM

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

CSE 4/521 Introduction to Operating Systems. Lecture 29 Windows 7 (History, Design Principles, System Components, Programmer Interface) Summer 2018

Chapter 4 Multithreaded Programming

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

THREADS. Jo, Heeseung

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

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

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

Chapter 7: Deadlocks. Operating System Concepts 9 th Edition

Chapter 4: Multithreaded Programming

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

What s in a process?

Chapter 4: Multi-Threaded Programming

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

What s in a traditional process? Concurrency/Parallelism. What s needed? CSE 451: Operating Systems Autumn 2012

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

CSE 4/521 Introduction to Operating Systems

Chapter 3: Processes. Operating System Concepts 9 th Edit9on

Processes and Threads

CS 318 Principles of Operating Systems

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

CS Lecture 3! Threads! George Mason University! Spring 2010!

Process- Concept &Process Scheduling OPERATING SYSTEMS

Threads (light weight processes) Chester Rebeiro IIT Madras

CS 31: Intro to Systems Operating Systems Overview. Kevin Webb Swarthmore College March 31, 2015

Chapter 4: Threads. Operating System Concepts 9 th Edition

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

Agenda Process Concept Process Scheduling Operations on Processes Interprocess Communication 3.2

CS307: Operating Systems

Today s Topics. u Thread implementation. l Non-preemptive versus preemptive threads. l Kernel vs. user threads

Today s Topics. u Thread implementation. l Non-preemptive versus preemptive threads. l Kernel vs. user threads

Chapter 3: Processes. Operating System Concepts 8 th Edition,

10/10/ Gribble, Lazowska, Levy, Zahorjan 2. 10/10/ Gribble, Lazowska, Levy, Zahorjan 4

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

Page Replacement Algorithms

Processes and Threads

OPERATING SYSTEM. Chapter 4: Threads

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

Chapter 8: Virtual Memory. Operating System Concepts Essentials 2 nd Edition

Processes. CS 475, Spring 2018 Concurrent & Distributed Systems

Chapter 4: Threads. Operating System Concepts 9 th Edition

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

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

Process and Its Image An operating system executes a variety of programs: A program that browses the Web A program that serves Web requests

CSE 4/521 Introduction to Operating Systems

Chapter 9: Virtual Memory. Operating System Concepts 9 th Edition

Lecture 4: Mechanism of process execution. Mythili Vutukuru IIT Bombay

CS 3305 Intro to Threads. Lecture 6

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

CS 153 Design of Operating Systems Spring 18

Processes and More. CSCI 315 Operating Systems Design Department of Computer Science

Transcription:

Threads CSE 410 - Computer Systems November 16, 2001

Readings and References Reading Chapter 5, Operating System Concepts, Silberschatz, Galvin, and Gagne Other References Inside Microsoft Windows 2000, Third Edition, Solomon and Russinovich Pthreads Programming, Nichols, Buttlar and Farrell 16-Nov-01 CSE 410 - Threads 2

A Process A complete process includes numerous things address space (all the code and data pages) OS resources and accounting information a thread of control, which defines where the process is currently executing the Program Counter CPU registers 16-Nov-01 CSE 410 - Threads 3

Processes are heavyweight objects Creating a new process is costly lots of data must be allocated and initialized operating system control data structures memory allocation for the process Communicating between processes is costly most communication goes through the OS need a context switch for each process 16-Nov-01 CSE 410 - Threads 4

Parallelism using Processes Why build a parallel program? responsiveness to user web server handling simultaneous web requests execute faster on a multiprocessor One approach using heavyweight processes create several processes to execute in parallel map each process to the same address space specify starting address and initial parameters 16-Nov-01 CSE 410 - Threads 5

Parallel processes are expensive There s a lot of cost creating these processes coordinating them There s a lot of duplication same program code, protection, etc It may be time for a little refinement and complexity... 16-Nov-01 CSE 410 - Threads 6

What is fundamental in a process? What do our parallel processes share? Same code and data (address space) Same privileges They share almost everything in the process What don t they share? Program Counter, registers, and stack Separate the idea of process from the idea of a thread of control (PC, SP, registers) 16-Nov-01 CSE 410 - Threads 7

Threads are Lightweight Processes Most operating systems now support two entities the process, which defines the address space and general process attributes the thread, which defines one or more execution paths within a process Threads are the unit of scheduling Processes are the containers in which threads execute 16-Nov-01 CSE 410 - Threads 8

Multi-threaded design benefits Separating execution path from address space simplifies design of parallel applications Some benefits of threaded designs improved responsiveness to user actions handling concurrent events (e.g., web requests) simplified program structure (code, data) more efficient and so less impact on system map easily to multi-processor systems 16-Nov-01 CSE 410 - Threads 9

One thread Three threads stack $sp $sp 1 stack 1 stack 2 $sp 2 stack 3 $sp 3 heap heap code PC PC 1 code PC PC 3 2 16-Nov-01 CSE 410 - Threads 10

Cookbook Analogy Think of a busy kitchen over the holiday 3 cooks and 1 cookbook Each cook maintains a pointer to where they are in the cookbook (the Program Counter) Two cooks could both be making the same thing (threads running the same procedure) The cooks must coordinate access to the kitchen appliances (resource access control) 16-Nov-01 CSE 410 - Threads 11

Implementation A thread is bound to the process that provides its address space Each process has one or more threads How are threads actually implemented? In the kernel and user mode libraries combined In user mode libraries alone 16-Nov-01 CSE 410 - Threads 12

Kernel Threads The operating system knows about and manages the threads in every program Thread operations (create, yield,...) all require kernel involvement Major benefit is that threads in a process are scheduled independently one blocked thread does not block the others threads in a process can run on different CPUs 16-Nov-01 CSE 410 - Threads 13

Kernel Thread Performance Kernel threads have performance issues Even though threads avoid process overhead, operations on kernel threads are still slow a thread operation requires a kernel call kernel threads may be overly general, in order to support needs of different users, languages, etc. the kernel doesn t trust the user, so there must be lots of checking on kernel calls 16-Nov-01 CSE 410 - Threads 14

User Threads To make thread operations faster, they can be implemented at the user level Each thread is managed by the run-time system user-mode libraries are linked with your program Each thread is represented simply by a PC, registers, stack and a control block, managed in the user s address space 16-Nov-01 CSE 410 - Threads 15

User Thread Performance All activities happen in user address space so thread operations can be faster But OS scheduling takes place at process level block entire process if a single thread is I/O blocked may run a process that is just running an idle thread Win2K provides fibers as user mode threads application can schedule its own lightweight threads in user mode code 16-Nov-01 CSE 410 - Threads 16

Simplified W2K Process Data Process environment block process address space system address space Thread environment block Process block Win32 process block Handle table Copied frominside Windows 2000 Thread block 16-Nov-01 CSE 410 - Threads 17

Simplified Thread Interface t = thread_create(), thread_start(t) create a new thread of control and start it thread_yield() voluntarily give up the processor for awhile thread_exit() terminate the calling thread 16-Nov-01 CSE 410 - Threads 18

Win2K Thread/Fiber API Thread Functions AttachThreadInput CreateRemoteThread CreateThread ExitThread GetCurrentThread GetCurrentThreadId GetExitCodeThread GetThreadPriority GetThreadPriorityBoost GetThreadTimes ResumeThread SetThreadAffinityMask SetThreadIdealProcessor SetThreadPriority SetThreadPriorityBoost Sleep SleepEx SuspendThread SwitchToThread TerminateThread ThreadProc TlsAlloc TlsFree TlsGetValue TlsSetValue WaitForInputIdle Fiber Functions ConvertThreadToFiber CreateFiber DeleteFiber FiberProc GetCurrentFiber GetFiberData SwitchToFiber 16-Nov-01 CSE 410 - Threads 19