Chapter 4 – Threads (Pgs 153 – 174). Threads  A "Basic Unit of CPU Utilization"  A technique that assists in performing parallel computation by setting.

Slides:



Advertisements
Similar presentations
OPERATING SYSTEMS Threads
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.
Modified from Silberschatz, Galvin and Gagne ©2009 Lecture 7 Chapter 4: Threads (cont)
Day 10 Threads. Threads and Processes  Process is seen as two entities Unit of resource allocation (process or task) Unit of dispatch or scheduling (thread.
Based on Silberschatz, Galvin and Gagne  2009 Threads Definition and motivation Multithreading Models Threading Issues Examples.
Threads CSCI 444/544 Operating Systems Fall 2008.
 2004 Deitel & Associates, Inc. All rights reserved. Chapter 4 – Thread Concepts Outline 4.1 Introduction 4.2Definition of Thread 4.3Motivation for Threads.
Chapter 4: Threads. 4.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Objectives Thread definitions and relationship to process Multithreading.
Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8 th Edition Chapter 4: Threads.
Chapter 4: Threads READ 4.1 & 4.2 NOT RESPONSIBLE FOR 4.3 &
Chapter 4: Threads. 4.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Chapter 4: Threads Overview Multithreading Models Threading Issues.
ThreadsThreads operating systems. ThreadsThreads A Thread, or thread of execution, is the sequence of instructions being executed. A process may have.
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.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 4: Threads.
Chapter 4: Threads. From Processes to Threads 4.3 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th edition, Jan 23, 2005 Threads.
Chapter 4: Threads. 4.2CSCI 380 Operating Systems Chapter 4: Threads Overview Multithreading Models Threading Issues Pthreads Windows XP Threads Linux.
1 Lecture 4: Threads Operating System Fall Contents Overview: Processes & Threads Benefits of Threads Thread State and Operations User Thread.
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 5: Threads Overview Multithreading Models Threading Issues Pthreads Solaris.
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.
Threads, Thread management & Resource Management.
Chapter 4: Threads.
 2004 Deitel & Associates, Inc. All rights reserved. 1 Chapter 4 – Thread Concepts Outline 4.1 Introduction 4.2Definition of Thread 4.3Motivation for.
CS 346 – Chapter 4 Threads –How they differ from processes –Definition, purpose Threads of the same process share: code, data, open files –Types –Support.
Source: Operating System Concepts by Silberschatz, Galvin and Gagne.
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.
Multithreaded Programing. Outline Overview of threads Threads Multithreaded Models  Many-to-One  One-to-One  Many-to-Many Thread Libraries  Pthread.
Chapter 4: Multithreaded Programming. 4.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts What is Thread “Thread is a part of a program.
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.
Lecture 3 Threads Erick Pranata © Sekolah Tinggi Teknik Surabaya 1.
CS307 Operating Systems Threads Fan Wu Department of Computer Science and Engineering Shanghai Jiao Tong University Spring 2011.
Saurav Karmakar. Chapter 4: Threads  Overview  Multithreading Models  Thread Libraries  Threading Issues  Operating System Examples  Windows XP.
Operating System Concepts
Silberschatz, Galvin and Gagne ©2009Operating 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.
Contents 1.Overview 2.Multithreading Model 3.Thread Libraries 4.Threading Issues 5.Operating-system Example 2 OS Lab Sun Suk Kim.
Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8 th Edition Chapter 4: Threads.
1 Chapter 5: Threads Overview Multithreading Models & Issues Read Chapter 5 pages
Chapter 4 – Thread Concepts
Chapter 4: Threads Modified by Dr. Neerja Mhaskar for CS 3SH3.
Introduction to threads
Chapter 4: Threads.
Chapter 4 – Thread Concepts
Chapter 4: Threads.
Chapter 5: Threads Overview Multithreading Models Threading Issues
Chapter 4: Multithreaded Programming
Chapter 4: Multithreaded Programming
Chapter 4 Threads.
Chapter 4: Threads.
Chapter 4: Threads.
Chapter 4: Threads.
Chapter 4: Threads.
Chapter 4: Threads.
OPERATING SYSTEMS Threads
Modified by H. Schulzrinne 02/15/10 Chapter 4: Threads.
Chapter 4: Threads.
Chapter 4: Threads.
Chapter 4: Threads.
Chapter 4: Threads.
Chapter 4: Threads.
Chapter 4: Threads.
Presentation transcript:

Chapter 4 – Threads (Pgs 153 – 174)

Threads  A "Basic Unit of CPU Utilization"  A technique that assists in performing parallel computation by setting up sharing for you  A thread consists of: 1. Register set (values), including the PC 2. A stack 3. Shared code, data, files, with the other threads in the same process  A sub-component of a process

Threading  Until now, all our applications have been single-threaded (c.f., multi-threaded)  Threads are sometimes called "lightweight" processes  Threads are not as useful on single CPU (one CPU core) systems  On multi-CPU/core systems, threads allow a single process to use multiple CPUs

Figure 4.1: Threading

Why threads?

Benefits  Responsiveness: E.g., MS Word saving file with one thread and doing input with another  Resource Sharing: Automatic sharing of code and (some) data for an application  Economy: Easier to make and less memory intensive than a process  Scalability(?): Allows a process to use multiple CPUs/cores

Threads vs. Processes  Threads can be a little less expensive in overhead than processes  Threads can use less memory than processes  Threads can require more synchronisation on non-stack variables (e.g., globals, objects)  Differences are very minimal in many modern OS (e.g., some versions of Linux)  Threads may not be fully available in some OS (i.e., limited functionality)

Programming Challenges  Finding independent activities that can be run in parallel  Ensuring that an activity does enough work to justify the overhead of creating a thread  Dividing data sets to support the threads and avoiding data dependencies  Synchronisation of the threads  Testing/Debugging: Thread scheduling (ordering) permutations, reproducing an error

User vs. Kernel Threads  If a thread can be independently scheduled by the OS, it is a kernel thread  This is really what is meant when we say "lightweight process"  If creation and scheduling is done in a library or by a "user" application, the threads are called user threads  Lightweight, easy to make, no O/S support needed  All threads block if one blocks, can't use multiple CPUs, best used for process organisation

Multithreading Models  Many:1 Model  No OS thread support, only user threads  Usually a library, e.g., GNU Portable Threads  1:1 Model  User thread is just an interface to the OS (kernel thread), true light-weight process  Can overload the OS, so limits exist  Many:Many (Hybrid) Model  Arbitrary mapping, best of both worlds  Complicated to implement and use

Thread Libraries  An API for programmers to use threads in their applications  Pthreads – Part of POSIX, may be user or kernel level  Win32 Threads – Windows kernel thread library  Many others, e.g., GNU Portable Threads, Green Threads, l  Some programming languages provide threads as a language feature (e.g., Java, µC++)  Use man pthreads for info about the library on cs.smu.ca

Pthreads  Specification, NOT implementation  Use  Need pthread_attr_t instance for each thread 1. Initialise: pthread_attr_init() 2. Create: pthread_create() 3. Exit: pthread_exit() 4. Wait: pthread_join()

Issues in Threading  fork() : When a copy of a process is made, should a copy of all its threads also be made, or of just the thread calling the fork() ?  cancellation (killing a thread): Resources (e.g., disk buffers) are shared between threads but not between processes  scheduling in many:many models  signals: Which thread gets a signal?  The thread to which the signal applies, the currently executing thread(s)  All threads, some subset of threads  A signal handling thread What to do really depends on the signal generated

Thread Pools  Automatically create a set (pool) of threads when a process is created  Processes can use and reuse the threads in their pool, but cannot create more  Extra startup overhead, but better runtime performance if many threads started/stopped (e.g., web browsers)  Pool size can be dynamic, with changes based on number of processes, CPU usage, free memory, etc.

Thread Data  Threads all share the data of a process (except each have own stack)  Sometimes, a thread needs its own data (i.e., like a process, but with shared code)  Not easy to achieve, and often more work than using processes (particularly when OS shares code pages among processes)

Cloning (Linux)  fork() calls the clone() system call with minimal sharing  pthread_create() calls clone() with maximal sharing  Various "halfway" points exist and can be created  Processes and threads are not very different  Generally what future operating systems will probably be like

To Do:  Work on Assignment 1  Finish reading Chapter 4 (pgs ; this lecture) if you haven’t already  Read Chapter 5 (pgs ; next lecture)