Silberschatz, Galvin and Gagne  2002 5.1 Operating System Concepts Chapter 5: Threads  Overview  Multithreading Models  Threading Issues  Pthreads.

Slides:



Advertisements
Similar presentations
OPERATING SYSTEMS Threads
Advertisements

Chapter 4: Threads. 4.2 Silberschatz, Galvin and Gagne ©2005 AE4B33OSS Chapter 4: Threads Overview Multithreading Models Threading Issues Pthreads Windows.
Chap 4 Multithreaded Programming. Thread A thread is a basic unit of CPU utilization It comprises a thread ID, a program counter, a register set and a.
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: Multithreaded Programming
Modified from Silberschatz, Galvin and Gagne ©2009 Lecture 7 Chapter 4: Threads (cont)
Chapter 4: Multithreaded Programming
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.
4.1 Silberschatz, Galvin and Gagne ©2005 Operating System Principles Chapter 4 Multithreaded Programming Objectives Objectives To introduce a notion of.
Chapter 4: Threads. 4.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th edition, Jan 23, 2005 Chapter 4: Threads Overview Multithreading.
Operating System Concepts with Java – 7 th Edition, Nov 15, 2006 Silberschatz, Galvin and Gagne ©2007 Chapter 4: 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 &
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 4: Multithreaded Programming.
Chapter 4: Threads. 4.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Chapter 4: Threads Overview Multithreading Models Threading Issues.
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 5: Threads Overview Multithreading Models Threading Issues Pthreads Solaris.
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.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 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.
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.
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.
Operating System Concepts Ku-Yaw Chang Assistant Professor, Department of Computer Science and Information Engineering Da-Yeh University.
CHAPTER 5: THREADS ( 线程 ) Overview Overview Multithreading Models (多线程模型) Multithreading Models (多线程模型) Thread Implementation Issues (线程实现事项) Thread Implementation.
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 5: Threads Multithreading Models Threading Issues Pthreads Solaris 2 Threads.
Source: Operating System Concepts by Silberschatz, Galvin and Gagne.
Chapter 4: Threads. 4.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th edition, Jan 23, 2005 Chapter 4: Threads Overview Multithreading.
Overview Multithreading Models Threading Issues Pthreads Solaris 2 Threads Windows 2000 Threads Linux Threads Java Threads.
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 5: Threads Overview Multithreading Models Threading Issues Pthreads Solaris.
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.
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.
Operating System Concepts
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.
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 5: Threads Overview Multithreading Models Threading Issues Pthreads Solaris.
OPERATING SYSTEM CONCEPT AND PRACTISE
Chapter 5: Threads Overview Multithreading Models Threading Issues
Chapter 5: Threads Overview Multithreading Models Threading Issues
Nadeem MajeedChoudhary.
Chapter 5: Threads Overview Multithreading Models Threading Issues
Chapter 4: Threads.
OPERATING SYSTEMS Threads
Chapter 4: Threads.
Chapter 4: Threads.
Chapter 4: Threads.
Chapter 5: Threads Overview Multithreading Models Threading Issues
Chapter 4: Threads.
Chapter 4: Threads.
Chapter 4: Threads.
Presentation transcript:

Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 5: Threads  Overview  Multithreading Models  Threading Issues  Pthreads  Solaris 2 Threads  Windows 2000 Threads  Linux Threads  Java Threads

Silberschatz, Galvin and Gagne  Operating System Concepts Thread definition  A thread, sometimes called LWP (Light Weight Process) is a basic unit of CPU utilization  A traditional (Heavy Weight) process has a single thread  Multithreading applications : web browsers, text editors  Threads Share:  Code  Data  Files  Threads have their own:  Thread ID  Program counter  Register set  Stack

Silberschatz, Galvin and Gagne  Operating System Concepts Single and Multithreaded Processes

Silberschatz, Galvin and Gagne  Operating System Concepts Benefits  Responsiveness: Allow a program to continue running even if part of it is blocked or performing lengthy operations  Resource Sharing: By default, threads share memory resources of the process they belong to.  Economy: Allocating memory and other resources is costly. Threads share resources and are more economical to create, share memory among them and context switch.  Utilization of MP Architectures: Best utilization of Multiprocessors architectures (I.e. Parallel processors run each thread).

Silberschatz, Galvin and Gagne  Operating System Concepts Thread types  Two types of threads:  User threads  Kernel thread  The main difference is in who performs:  Thread creation  Thread scheduling  Thread management OS or User!

Silberschatz, Galvin and Gagne  Operating System Concepts User Threads  Thread management done by user-level threads library  Kernel is unaware of multithreading at the process level since all is handled by the User (typically via user libraries)  Examples of available User libraries for handling threads: - POSIX Pthreads - Mach C-threads - Solaris threads

Silberschatz, Galvin and Gagne  Operating System Concepts Kernel Threads  Supported by the Kernel  Examples (most contemporary OS supports Kernel threads): - Windows 95/98/NT/ Solaris - Tru64 UNIX - BeOS - Linux

Silberschatz, Galvin and Gagne  Operating System Concepts Multithreading Models  Many-to-One  One-to-One  Many-to-Many

Silberschatz, Galvin and Gagne  Operating System Concepts Many-to-One  Many user-level threads mapped to single kernel thread.  Used on systems that do not support kernel threads.  Thread management is done is user space.  Advantage:  Thread management is efficient since all done in User space  Disadvantages:  Entire process will block if a thread makes a blocking system call  No support for parallel processors

Silberschatz, Galvin and Gagne  Operating System Concepts Many-to-One Model

Silberschatz, Galvin and Gagne  Operating System Concepts One-to-One  Each user-level thread maps to kernel thread.  Examples - Windows 95/98/NT/ OS/2  Advantages:  Higher level of concurrency  Supports parallel processors  Disadvantages:  Creating a user thread requires creating the corresponding Kernel thread (much overhead)  Must restrict the number of threads to reduce kernel burdens

Silberschatz, Galvin and Gagne  Operating System Concepts One-to-one Model

Silberschatz, Galvin and Gagne  Operating System Concepts Many-to-Many Model  Hybrid of Many-to-one and One-to-One models  Allows many user level threads to be mapped to many kernel threads.  Allows the operating system to create a sufficient number of kernel threads.  OS that use Many-to-Many Model:  Solaris 2  Windows NT/2000 with the ThreadFiber package  IRIX  HP-UX  Tru64UNIX

Silberschatz, Galvin and Gagne  Operating System Concepts Many-to-Many Model

Silberschatz, Galvin and Gagne  Operating System Concepts Threading Issues  Semantics of fork() and exec() system calls.  Thread cancellation.  Signal handling  Thread pools  Thread specific data

Silberschatz, Galvin and Gagne  Operating System Concepts Thread Issues: Semantics of fork() and exec() system calls.  Question: If one thread in a program calls fork, does the new process duplicate all threads or is the new process single-threaded?  Answer: Two versions of fork is provided by some UNIX versions:  One that duplicates all threads  One that duplicates only the thread that invokes the fork  exec typically works similar to a single process environment. That is: if a thread invokes the exec system call, all threads are also created.

Silberschatz, Galvin and Gagne  Operating System Concepts Thread Issues: Thread cancellation  Thread cancellation is the task of terminating a thread before it has completed.  Example: if multiple threads are concurrently searching through a database and one thread returns result, the remaining threads might be cancelled. Or pressing the stop button on the web browser (often a web page is loaded in a separate thread)  The tread that is to be canalled is referred to as the Target thread.  Two different types of canceling threads:  Asynchronous cancellation: Immediately cancel target thread  Deferred cancellation: target thread periodically checks to see if should be cancelled.

Silberschatz, Galvin and Gagne  Operating System Concepts Thread cancellation: Problems  Difficulty arises when resources have been allocated to a cancelled thread, or  If a thread was cancelled while in the middle of updating data it is sharing with other threads (problems specially sever with Asynchronous cancellation).  Deferred cancellation allows for better clean up upon termination since it has time to update the required fields.  The safe states to terminate threads sometimes referred to as “Cancellation points”

Silberschatz, Galvin and Gagne  Operating System Concepts Thread Issues: Signal handling  Signal is used in UNIX to notify a process that a particular event has occurred. Signal may be received Synchronously or Asynchronously.  Regardless of the type of signal, following will take place:  A signal is generated by the occurrences of an event  A generated signal is delivered to a process  Once delivered, the signal must be handled  Synchronous signals are delivered to the same process that performed the operation causing the signal (Example illegal memory access or div by zero)  Asynchronous signals are typically sent to another process (e.g. when CTRL-C is pressed or Time is expired)

Silberschatz, Galvin and Gagne  Operating System Concepts Thread Issues: Signal handling  Handling signals in single-threaded programs is straightforward, signals are always delivered to a process.  How about handling signals in a multithreaded program, which thread should get it? In general, the following options exit:  Deliver the signal to the tread to which the signal applies (specially true for synchronous signals)  Deliver the signal to every thread in the process  Deliver the signal to certain thread in the process  Assign a specific thread to receive all signals for the process. (Solaris 2 implements this option)  Windows uses Asynchronous Procedure Calls (APCs). The APC facility allows a user thread to specify a function that is to be called when user thread receives an event.

Silberschatz, Galvin and Gagne  Operating System Concepts Thread issues: Thread pools  Unlimited threads could exhaust system resources, such as CPU time or memory. Consider creating a treat for every action while browsing the web!  Thread pools: Create a number of threads at process start up and place them into a pool, where they sit and wait for work.  When server receives a request, it awakens a thread from this pool, if one available, and passes it the request.  Once thread competes its service, it returns to the pool awaiting for more work. If pool contains no available thread, waits for a free one.  Benefits of thread pooling:  Faster to service a request with existing thread than waiting to create one  A thread pool limits the number OS threads that exist at any one point.

Silberschatz, Galvin and Gagne  Operating System Concepts Thread issues: Thread-specific Data  Threads belonging to a process share the data of the process. (a great benefit of multithreading)  However, each thread might need its own copy of certain data in some circumstances (I.e. thread-specific data)  Most thread libraries (e.g. Win32 and Pthreads) provide some form of support for thread-specific data. Java provides support as well.

Silberschatz, Galvin and Gagne  Operating System Concepts Optional reading  The following sections of the text (and corresponding slides) are specific to certain Operating Systems and are NOT Required reading for the course:  Section 5.4  Pthreads  Section 5.5  Solaris 2 Threads  Section 5.6  Windows 200 Threads  Section 5.7  Linux Threads  Section 5.8  Java Threads

Silberschatz, Galvin and Gagne  Operating System Concepts Pthreads  a POSIX standard (IEEE c) 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.

Silberschatz, Galvin and Gagne  Operating System Concepts Pthread Example #include Main () { pthread_id tid; pthread_att attr; /* perform whaterver code you are designed to do */ pthread_attr_init(&attr); /* get thread attributes, e.g. stack size, scheduling info */ pthread_create(&tid, &attr, RunFunc, parm1, parm2); /* create a new thread /* perform other tasks */ pthread_join(tid, NULL); /* now wait for the thread to exist */ } RunFunc(Parm1, Parm 2) { /* do whatever you intended to do */ pthread_exit(0); }

Silberschatz, Galvin and Gagne  Operating System Concepts Solaris 2 Threads

Silberschatz, Galvin and Gagne  Operating System Concepts Solaris Process

Silberschatz, Galvin and Gagne  Operating System Concepts Windows 2000 Threads  Implements the one-to-one mapping.  Each thread contains - a thread id - register set - separate user and kernel stacks - private data storage area

Silberschatz, Galvin and Gagne  Operating System Concepts 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)

Silberschatz, Galvin and Gagne  Operating System Concepts Java Threads  Java threads may be created by:  Extending Thread class  Implementing the Runnable interface  Java threads are managed by the JVM.

Silberschatz, Galvin and Gagne  Operating System Concepts Java Thread States