CS 346 – Chapter 4 Threads –How they differ from processes –Definition, purpose Threads of the same process share: code, data, open files –Types –Support.

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.
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.
國立台灣大學 資訊工程學系 Chapter 4: Threads. 資工系網媒所 NEWS 實驗室 Objectives To introduce the notion of a thread — a fundamental unit of CPU utilization that forms the.
Threads.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Lecture 6: Threads Chapter 4.
Modified from Silberschatz, Galvin and Gagne ©2009 Lecture 7 Chapter 4: Threads (cont)
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.
02/01/2007CSCI 315 Operating Systems Design1 Java Threads Notice: The slides for this lecture have been largely based on those accompanying the textbook.
Chapter 4: Threads. 4.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th edition, Jan 23, 2005 Chapter 4: Threads Overview Multithreading.
1 School of Computing Science Simon Fraser University CMPT 300: Operating Systems I Ch 4: Threads Dr. Mohamed Hefeeda.
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.
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.
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.
Thread. A basic unit of CPU utilization. It comprises a thread ID, a program counter, a register set, and a stack. It is a single sequential flow of control.
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.
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.
Cosc 4740 Chapter 4 & 5 Threads & Scheduling. Motivation Threads run within application (process) Multiple tasks with the application can be implemented.
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.
CHAPTER 5: THREADS ( 线程 ) Overview Overview Multithreading Models (多线程模型) Multithreading Models (多线程模型) Thread Implementation Issues (线程实现事项) Thread Implementation.
Chapter 4: Threads.
Source: Operating System Concepts by Silberschatz, Galvin and Gagne.
Shan Gao Fall 2007 Department of Computer Science Georgia State University.
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: Threads.
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.
Chapter 4: Threads. 4.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th edition, Jan 23, 2005 Outline n Overview n 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
CISC2200 Threads Fall 09. Process  We learn the concept of process  A program in execution  A process owns some resources  A process executes a program.
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 5: Threads  Overview  Multithreading Models  Threading Issues  Pthreads.
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.
CMSC 421 Spring 2004 Section 0202 Part II: Process Management Chapter 5 Threads.
Chapter 4 – Thread Concepts
Chapter 4: Threads.
Chapter 4 – Thread Concepts
Chapter 4: Threads.
Chapter 4: Threads.
Chapter 4: Threads.
Chapter 4: Threads.
Chapter 4: Threads.
Chapter 4: Threads.
Chapter 4: Threads.
Chapter 4: Threads.
Chapter 4: Threads.
Chapter 4: Threads.
Chapter 4: Threads.
Presentation transcript:

CS 346 – Chapter 4 Threads –How they differ from processes –Definition, purpose Threads of the same process share: code, data, open files –Types –Support by kernel and programming language –Issues such as signals –User thread implementation: C and Java Commitment –For next day, please read chapter 4

Thread intro Also called “lightweight process” One process may have multiple threads of execution Allows a process to do 2+ things concurrently –Games –Simulations Even better: if you have 2+ CPU’s, you can execute in parallel Multicore architecture  demand for multithreaded applications for speedup More efficient than using several concurrent processes

Threads A process contains: –Code, data, open files, registers, memory usage (stack + heap), program counter Threads of the same process share –Code, data, open files What is unique to each thread? Can you think of example of a computational algorithm where threads would be a great idea? –Splitting up the code –Splitting up the data Any disadvantages?

2 types of threads User threads –Can be managed / controlled by user –Need existing programming language API support: POSIX threads in C Java threads Kernel threads –Management done by the kernel  Possible scenarios –OS doesn’t support threading –OS support threads, but only at kernel level – you have no direct control, except possibly by system call –User can create thread objects and manipulate them. These objects map to “real” kernel threads.

Multithreading models Many-to-one: User can create several thread objects, but in reality the kernel only gives you one. Multithreading is an illusion One-to-one: Each user thread maps to 1 real kernel thread. Great but costly to OS. There may be a hard limit to # of live threads. Many-to-many: A happy compromise. We have multithreading, but the number of true threads may be less than # of thread objects we created. –A variant of this model “two-level” allows user to designate a thread as being bound to one kernel thread.

Thread issues What should OS do if a thread calls fork( )? –Can duplicate just the calling thread –Can duplicate all threads in the process exec ( ) is designed to replace entire current process Cancellation –kill thread before it’s finished –“Asynchronous cancellation” = kill now. But it may be in the middle of an update, or it may have acquired resources. You may have noticed that Windows sometimes won’t let you delete a file because it thinks it’s still open. –“Deferred cancellation”. Thread periodically checks to see if it’s time to quit. Graceful exit.

Signals Reminiscent of exception in Java Occurs when OS needs to send message to a process –Some defined event generates a signal –OS delivers signal –Recipient must handle the signal. Kernel defines a default handler – e.g. kill the process. Or, user can write specific handler. Types of signals –Synchronous: something in this program caused the event –Asynchronous: event was external to my program

Signals (2) But what if process has multiple threads? Who gets the signal? For a given signal, choose among 4 possibilities: –Deliver signal to the 1 appropriate thread –Deliver signal to all threads –Have the signal indicate which threads to contact –Designate a thread to receive all signals Rules of thumb… –Synchronous event  just deliver to 1 thread –User hit ctrl-C  kill all threads

Thread pool Like a motor pool When process starts, can create a set of threads that sit around and wait for work Motivation –overhead in creating/destroying –We can set a bound for total number of threads, and avoid overloading system later How many threads? –User can specify –Kernel can base on available resources (memory and # CPU’s) –Can dynamically change if necessary

POSIX threads aka “Pthreads” C language Commonly seen in UNIX-style environments: –Mac OS, Linux, Solaris POSIX is a set of standards for OS system calls –Thread support is just one aspect POSIX provides an API for thread creation and synchronization API specifies behavior of thread functionality, but not the low-level implementation

Pthread functions pthread_attr_init –Initialize thread attributes, such as –Schedule priority –Stack size –State pthread_create –Start new thread inside the process. –We specify what function to call when thread starts, along with the necessary parameter –The thread is due to terminate when its function returns pthread_join –Allows us to wait for a child thread to finish

Example code #include int sum; main() { pthread_t tid; pthread_attr attr; pthread_attr_init(&attr); pthread_create(&tid, &attr, fun, argv[1]); pthread join(tid, NULL); printf(“%d\n”, sum); } int fun(char *param)... void *fun(void *param) { // compute a sum: // store in global // variable... }

Java threads Managed by the Java virtual machine Two ways to create threads 1.Create a class that extends the Thread class –Put code inside public void run( ) 2.Implement the Runnable interface –public void run( ) Parent thread (e.g. in main() …) –Create thread object – just binds name of thread –Call start( ) – creates actual running thread, goes to run( ) See book example

Skeletons class Worker extends Thread { public void run() { // do stuff } public class Driver { // in main method: Worker w = new Worker(); w.start();... Continue/join } class Worker2 implements Runnable { public void run() { // do stuff } Public class Driver2 { // in main method: Runnable w2=new Worker2(); Thread t = new Thread(w2); t.start(); //...Continue/join }

Java thread states This will probably sound familiar! New –From here, go to “runnable” at call to start( ) Runnable –Go to “blocked” if need I/O or going to sleep –Go to “dead” when we exit run( ) –Go to “waiting” if we call join( ) for child thread Blocked –Go to “runnable” when I/O is serviced Waiting Dead