CS-3013 Operating Systems Hugh C. Lauer

Slides:



Advertisements
Similar presentations
Chapter 4: Threads. 4.2 Silberschatz, Galvin and Gagne ©2005 AE4B33OSS Chapter 4: Threads Overview Multithreading Models Threading Issues Pthreads Windows.
Advertisements

Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 4: Multithreaded Programming.
Threads. Objectives To introduce the notion of a thread — a fundamental unit of CPU utilization that forms the basis of multithreaded computer systems.
Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8 th Edition Chapter 4: Threads.
Threads.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Lecture 6: Threads Chapter 4.
Chapter 4: Threads. 4.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th edition, Jan 23, 2005 Chapter 4: Threads Overview Multithreading.
Chapter 4: Threads. 4.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th edition, Jan 23, 2005 Chapter 4: Threads Overview Multithreading.
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 5: Threads Overview Multithreading Models Threading Issues Pthreads Solaris.
Threads 1 CS502 Spring 2006 Threads CS-502 Spring 2006.
ThreadsCS-502 Fall Threads CS-502 Operating Systems (Slides include materials from Operating System Concepts, 7 th ed., by Silbershatz, Galvin, &
ThreadsCS-3013 C-term Threads CS-3013, Operating Systems C-Term 2008 (Slides include materials from Operating System Concepts, 7 th ed., by Silbershatz,
Process in Unix, Linux and Windows CS-3013 C-term Processes in Unix, Linux, and Windows CS-3013 Operating Systems (Slides include materials from.
ThreadsCS-3013 A-term Threads CS-3013, Operating Systems A-term 2009 (Slides include materials from Modern Operating Systems, 3 rd ed., by Andrew.
Chapter 4: Threads. 4.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Objectives Thread definitions and relationship to process Multithreading.
02/02/2004CSCI 315 Operating Systems Design1 Threads Notice: The slides for this lecture have been largely based on those accompanying the textbook Operating.
Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8 th Edition Chapter 4: Threads.
CS 3013 & CS 502 Summer 2006 Threads1 CS-3013 & CS-502 Summer 2006.
Chapter 4: Threads READ 4.1 & 4.2 NOT RESPONSIBLE FOR 4.3 &
Processes in Unix, Linux, and Windows CS-502 Fall Processes in Unix, Linux, and Windows CS502 Operating Systems (Slides include materials from Operating.
Chapter 4: Threads. 4.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Chapter 4: Threads Overview Multithreading Models Threading Issues.
Chapter 4: Threads Adapted to COP4610 by Robert van Engelen.
14.1 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts with Java – 8 th Edition Chapter 4: Threads.
Process in Unix, Linux, and Windows CS-3013 A-term Processes in Unix, Linux, and Windows CS-3013 Operating Systems (Slides include materials from.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 4: Threads.
Chapter 4: Threads. 4.2CSCI 380 Operating Systems Chapter 4: Threads Overview Multithreading Models Threading Issues Pthreads Windows XP Threads Linux.
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 5: Threads Overview Multithreading Models Threading Issues Pthreads Solaris.
Chapter 4: Multithreaded Programming. 4.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Chapter 4: Multithreaded Programming Overview.
Copyright © 2006 by The McGraw-Hill Companies, Inc. All rights reserved. McGraw-Hill Technology Education Lecture 4 Operating Systems.
Chapter 4: Threads. 4.2 Chapter 4: Threads Overview Multithreading Models Threading Issues Pthreads Windows XP Threads Linux Threads Java Threads.
14.1 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts with Java – 8 th Edition Chapter 4: Threads.
Silberschatz, Galvin and Gagne ©2011Operating System Concepts Essentials – 8 th Edition Chapter 4: Threads.
Chapter 4: Threads. 4.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Threads A thread (or lightweight process) is a basic unit of CPU.
Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8 th Edition Chapter 4: Threads.
Source: Operating System Concepts by Silberschatz, Galvin and Gagne.
ThreadsCS-3013 C-term Threads (continued) CS-3013, Operating Systems C-Term 2008 (Slides include materials from Operating System Concepts, 7 th ed.,
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Chapter 4: Threads Modified from the slides of the text book. TY, Sept 2010.
Silberschatz, Galvin and Gagne  2002 Modified for CSCI 399, Royden, Operating System Concepts Operating Systems Lecture 14 Threads 2 Read Ch.
Chapter 4: Multithreaded Programming. 4.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts What is Thread “Thread is a part of a program.
Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9 th Edition Chapter 4: Threads.
Chapter 4: Threads. 4.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th edition, Jan 23, 2005 Chapter 4: Threads Overview Multithreading.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 4: Threads.
Lecturer 3: Processes multithreaded Operating System Concepts Process Concept Process Scheduling Operation on Processes Cooperating Processes Interprocess.
Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8 th Edition Chapter 4: Threads.
CMSC 421 Spring 2004 Section 0202 Part II: Process Management Chapter 5 Threads.
OPERATING SYSTEM CONCEPT AND PRACTISE
Chapter 4: Threads.
Chapter 4: Threads.
Chapter 5: Threads Overview Multithreading Models Threading Issues
Chapter 4: Multithreaded Programming
Processes in Unix, Linux, and Windows
CS-502, Operating Systems Fall 2007
Nadeem MajeedChoudhary.
Chapter 4: Threads.
Threads & multithreading
Processes in Unix, Linux, and Windows
Chapter 4: Threads.
Chapter 4: Threads.
OPERATING SYSTEMS Threads
Processes in Unix, Linux, and Windows
Modified by H. Schulzrinne 02/15/10 Chapter 4: Threads.
Chapter 4: Threads.
Chapter 4: Threads.
Chapter 4: Threads.
Processes in Unix, Linux, and Windows
Chapter 4: Threads.
Chapter 4: Threads.
Chapter 4: Threads.
Presentation transcript:

CS-3013 Operating Systems Hugh C. Lauer Threads CS-3013 Operating Systems Hugh C. Lauer (Slides include materials from Slides include materials from Modern Operating Systems, 3rd ed., by Andrew Tanenbaum and from Operating System Concepts, 7th ed., by Silbershatz, Galvin, & Gagne) CS-3013, C-Term 2012 Threads

Review Process (the generic concept) A particular execution of a program Separate from all other executions of that same program Separate from all executions of other programs CS-3013, C-Term 2012 Threads

Review — Processes in Unix, Linux, and Windows a particular execution of a program PLUS an address space – usually protected and virtual – mapped into memory the code for the running program the data for the running program an execution stack and stack pointer (SP); also heap the program counter (PC) a set of processor registers – general purpose and status a set of system resources files, network connections, pipes, … privileges, (human) user association, … … CS-3013, C-Term 2012 Threads 3

Processes in Unix, Linux, and Windows (continued) Evolutionary direction:– Isolation I.e., main goal was to keep processes separate from each other Multi-user computer systems (time-sharing) Independent applications Communication and cooperation among processes was secondary & limited CS-3013, C-Term 2012 Threads

Processes in Unix, Linux, and Windows (continued) Processes in Unix, Linux, and Windows are “heavyweight” Lots of resources Expensive context switches Inflexible … CS-3013, C-Term 2012 Threads 5

OS-Centric View of Problem Lots of data in PCB & other data structures Even more when we study memory management More than that when we study file systems, etc. Processor caches a lot of information Memory Management information Caches of active pages Costly context switches and traps Many 10s of microseconds (even 100s of microseconds) CS-3013, C-Term 2012 Threads 6

Application-Centric View of Problem Separate processes have separate address spaces Shared memory is limited or nonexistent Applications with internal concurrency are difficult Isolation between independent processes vs. cooperating activities Fundamentally different goals CS-3013, C-Term 2012 Threads 7

Example Web Server One solution:– Clumsy and inefficient Need to support multiple concurrent requests pertaining to common data One solution:– create several processes that execute in parallel Use shared memory — shmget() — to map to the same address space into multiple processes have the OS schedule them in parallel Clumsy and inefficient Space and time: PCB, page tables, cloning entire process, etc. programming: shmget()is really hard to program! CS-3013, C-Term 2012 Threads 8

Example 2 Transaction processing systems E.g, airline reservations or bank ATM transactions 1000’s of transactions per second Very small computation per transaction Long wait times for data base access Separate processes per transaction are too costly Other techniques (e.g., message passing) are much more complex CS-3013, C-Term 2012 Threads 9

Example 3 Games have multiple active characters Independent behaviors Common context or environment Need “real-time” response to user For interactive gaming experience Programming all characters in separate processes is really, really hard! Programming them in a single process is much harder without concurrency support. CS-3013, C-Term 2012 Threads 10

This problem … … is partly an artifact of Unix, Linux, and Windows and of Big, powerful processors (e.g., Pentium, Athlon) … partly a consequence of history Shared computers rather than personal computers … tends to occur in most large systems … is infrequent in small-scale systems PDAs, cell phones Closed systems (i.e., controlled applications) CS-3013, C-Term 2012 Threads 11

Solution:– Threads A thread is a particular execution of a program, function, or procedure within the context of a Linux or Windows process I.e., a specialization of the concept of process A thread has its own Program counter, registers, PSW Stack A thread shares Address space, heap, global and static data, program code Files, privileges, all other resources with all other threads of the same process CS-3013, C-Term 2012 Threads 12

Reading Assignment Robert Love, Linux Kernel Development Tanenbaum Chapter 3 – “Process Management” Tanenbaum §2.2 – “Threads” Chapters 1 & 2 are useful, too! easy reading! CS-3013, C-Term 2012 Threads 13

Definition:– Thread A thread is a particular execution of a program or procedure within the context of a Unix, Linux, or Windows process I.e., a specialization of the concept of process A thread has its own Program counter, registers, PSW Stack A thread shares Address space, heap, global and static data, program code Files, privileges, all other resources with all other threads of the same process CS-3013, C-Term 2012 Threads 14

Illustration void main(int argc, char** argv); char *f(char *c, int i); main( … ) f( … ) int j = …; char *msg = malloc(…); thread_t T; T = new_thread f(msg,j); … while (…) { /* loop */ } char *result = join T; char *d = malloc(…); for (i=0;i<j;i++){ … d[i] = c[i]; } return d; Same heap CS-3013, C-Term 2012 Threads

Address Space Linux-Windows process From previous lesson Address Space Linux-Windows process 0x00000000 0xFFFFFFFF Virtual address space program code (text) global and static data heap (dynamically allocated) stack PC SP CS-3013, C-Term 2012 Threads 16

Address Space for Multiple Threads 0x00000000 0xFFFFFFFF Virtual address space code (text) global and static data heap thread 1 stack PC (T2) SP (T2) thread 2 stack thread 3 stack SP (T1) SP (T3) PC (T1) PC (T3) CS-3013, C-Term 2012 Threads 17

Basic Thread Functions Create a thread Make a function call that executes concurrently with caller Exit a thread I.e., thread terminates or returns from its function Join a thread Wait until the designated thread terminates, capture its return value, delete its stack … CS-3013, C-Term 2012 Threads

Basic Thread Functions (continued) … Detach a thread Separate it from its creators Becomes a standalone, independent execution Not “joinable”; no value returned Stack goes away upon termination Get own thread ID I.e., get the number of my own thread CS-3013, C-Term 2012 Threads

Another Example // web server while (true) { listen for web req. allocate priv. socket create & detach new thread to serve req. … } // individual request // handler interpret request while (!done) { interact with client over private socket … } exit thread CS-3013, C-Term 2012 Threads

Benefits Responsiveness Resource Sharing Economy Better utilization of multi-processor architectures than achievable with just processes CS-3013, C-Term 2012 Threads 21

Using Threads Everyone uses threads nowadays Many purposes Many languages Programming applications with concurrent activity within them has become an essential skill I.e., some form of threads CS-3013, C-Term 2012 Threads

Questions? CS-3013, C-Term 2012 Threads

Tools for using Threads Three primary thread libraries: – POSIX pthreads Win32 threads Java threads CS-3013, C-Term 2012 Threads

Thread Interface E.g., POSIX pthreads API: See the man pages for these functions Thread Interface E.g., POSIX pthreads API: int pthread_create(pthread_t *thread, const pthread_attr_t *attr, void*(*start_routine) (void), void *arg) creates a new thread of control new thread begins executing the function start_routine(arg) pthread_exit(void *value_ptr) terminates the calling thread pthread_join(pthread_t thread, void **value_ptr) blocks the calling thread until the specified thread terminates pthread_t pthread_self() Returns the calling thread's identifier CS-3013, C-Term 2012 Threads 25

Project 3 will use pthreads CS-3013, C-Term 2012 Threads

Implementing Threads In User space In Kernel User-space function library Runtime system – similar to process management except in user space Windows NT – fibers: a user-level thread mechanism In Kernel Primitive objects known to and scheduled by kernel Linux: lightweight process (LWP) Windows NT, XP, Vista, 7:– threads CS-3013, C-Term 2012 Threads 27

Implementing Threads In User space In Kernel Obsolete User-space function library Runtime system – similar to process management except in user space Windows NT – fibers: a user-level thread mechanism In Kernel Primitive objects known to and scheduled by kernel Linux: lightweight process (LWP) Windows NT, XP, Vista, 7:– threads CS-3013, C-Term 2012 Threads 28

Threads Implemented in User Space Thread management done by non-privileged threads library Runs in process address space CS-3013, C-Term 2012 Threads 29

User-Space Threads (continued) Can be implemented without kernel support … or knowledge! Program links with a runtime system that does thread management Operations are very efficient (function calls) Space efficient and all in user space (TCB) Task switching is very fast Since kernel not aware of threads, there can be scheduling inefficiencies or issues E.g., blocking I/O calls Non-concurrency of threads on multiple processors CS-3013, C-Term 2012 Threads 30

User-Space Threads Obsolete because all modern kernels support threads at kernel level Still (somewhat of) a performance issue Research focused on how to avoid system calls for thread synchronization, creation, deletion, etc. CS-3013, C-Term 2012 Threads

Questions? CS-3013, C-Term 2012 Threads 32

Threads Implemented in Kernel Supported by the Kernel OS maintains data structures for thread state and does all of the work of thread implementation. Examples Windows XP/2000/Vista/7 Solaris Linux version 2.6 Tru64 UNIX Mac OS X CS-3013, C-Term 2012 Threads 33

Kernel-level Threads (continued) OS schedules threads instead of processes Benefits Overlap I/O and computing in a process Creation is cheaper than processes Context switch can be faster than processes Negatives System calls needed for thread operations High overhead Additional OS data space for each thread CS-3013, C-Term 2012 Threads 34

Threads Supported by Processor E.g., Pentium 4 with Hyperthreading™ www.intel.com/products/ht/hyperthreading_more.htm Multiple processor cores on a single chip True concurrent execution within a single process Requires kernel support Exposes many issues Critical section management of synchronization primitives at kernel level Multiple threads scheduling from same ready queue Multiple interrupts in progress at one time CS-3013, C-Term 2012 Threads 35

Unix Processes vs. Threads On a 700 Mhz Pentium running Linux Processes: fork()/exit(): 250 microsec Kernel threads: pthread_create()/pthread_join(): 90 microsec User-level threads: pthread_create()/pthread_join(): 5 microsec CS-3013, C-Term 2012 Threads 36

Some Issues Pertaining to Threads Process global variables E.g., ERRNO in Unix — a global variable set by system calls Semantics of fork() and exec() system calls for processes Thread cancellation Signal handling Thread pools Thread specific data Scheduler activations CS-3013, C-Term 2012 Threads 37

Semantics of fork() and exec() Does fork() duplicate only the calling thread or all threads? Easy if user-level threads All threads are duplicated, unbeknownst to kernel Not so easy with kernel-level threads Linux has special clone() operation Windows XP/Vista has something similar CS-3013, C-Term 2012 Threads 38

Thread Cancellation Terminating a thread before it has finished Two general approaches: Asynchronous cancellation terminates the target thread immediately Deferred cancellation allows the target thread to periodically check if it should be cancelled CS-3013, C-Term 2012 Threads 39

Signal Handling Signals are used in UNIX/Linux systems to notify a process that a particular event has occurred AKA software interrupts A signal handler is used to process a class of signals Signal is generated by particular event Signal is delivered to a process Signal handling implemented as forced function call by process Options for multi-threaded process:– Deliver the signal to the thread to which the signal applies How does system know which one? Deliver the signal to every thread in the process Assign specific threads to receive specific signals for the process CS-3013, C-Term 2012 Threads 40

Models for Kernel Implementation Many-to-One One-to-One Many-to-Many This was a big issue a decade ago Still discussed in textbooks Hardly relevant today Windows, Linux, and most Unix systems provide kernel threads See extra slides on course web site at end of this topic CS-3013, C-Term 2012 Threads 41

Modern Linux Threads Implemented in kernel “A thread is just a special kind of process.” Robert Love, Linux Kernel Development, p.23 The primary unit of scheduling and computation implemented by Linux 2.6 kernel Every thread has its own task_struct in kernel … CS-3013, C-Term 2012 Threads 42

Modern Linux Threads (continued) Process task_struct has pointers to own memory & resources Thread task_struct has pointer to process’s memory & resources fork() and thread_create() are library functions implemented by clone() kernel call … CS-3013, C-Term 2012 Threads 43

Modern Linux Threads (continued) Threads are scheduled independently of each other Threads can block independently of each other Even threads of same process Threads can make their own kernel calls Kernel maintains a small kernel stack per thread During kernel call, kernel is in process context CS-3013, C-Term 2012 Threads 44

Digression – Process Address Space Linux includes (parts of) kernel in every address space Protected Easy to access Allows kernel to see into client processes Transferring data Examining state … Also many other operating systems CS-3013, C-Term 2012 Threads 45

Kernel Code and Data 0xFFFFFFFF thread 1 stack SP (T1) thread 2 stack Virtual address space code (text) global and static data heap thread 1 stack PC (T2) SP (T2) thread 2 stack thread 3 stack SP (T1) SP (T3) PC (T1) PC (T3) Kernel Code and Data CS-3013, C-Term 2012 Threads 46

Linux Kernel Implementation Kernel may execute in either Process context vs. Interrupt context In Process context, kernel has access to Virtual memory, files, other process resources May sleep, take page faults, etc., on behalf of process In Interrupt context, no assumption about what process was executing (if any) No access to virtual memory, files, resources May not sleep, take page faults, etc. CS-3013, C-Term 2012 Threads 47

Modern Linux Threads (continued) Multiple threads can be executing in kernel at same time When in process context, kernel can sleep on behalf of its thread take pages faults on behalf of its thread move data between kernel and process or thread … CS-3013, C-Term 2012 Threads 48

Implications of Threads in Kernel Kernel maintains a small stack for each thread in kernel space Large enough for one system call 4 kBytes (32 bit architectures) 8 kBytes (64-bit architectures) Limit on total number of threads supportable by kernel Bound on number of function calls within system call CS-3013, C-Term 2012 Threads 49

Threads in Linux Kernel Kernel has its own threads No associated process context Supports concurrent activity within kernel Multiple devices operating at one time Multiple application activities at one time Multiple processors in kernel at one time A useful tool Special kernel thread packages, synchronization primitives, etc. Useful for complex OS environments CS-3013, C-Term 2012 Threads 50

Windows NT/XP/Vista Threads Much like Linux 2.6 threads Primitive unit of scheduling defined by kernel Threads can block independently of each other Threads can make kernel calls … Process A higher level (non-kernel) abstraction A container See Tanenbaum, §11.4 CS-3013, C-Term 2012 Threads 51

Threads – Summary Threads were invented to counteract the heavyweight nature of Processes in Unix, Windows, etc. Provide lightweight concurrency within a single address space Have evolved to become the primitive abstraction defined by kernel Fundamental unit of scheduling in Linux, Windows, etc CS-3013, C-Term 2012 Threads 52

Reading Assignment Robert Love, Linux Kernel Development Tanenbaum Chapter 3 – “Process Management” Tanenbaum §2.2 – “Threads” CS-3013, C-Term 2012 Threads 53

Questions? CS-3013, C-Term 2012 Threads 54

Single and Multithreaded Processes CS-3013, C-Term 2012 Threads 64