4.1 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Exercise: Eratosthenes Sieve Eratosthenes sieve: An algorithm for computing prime numbers:

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.
國立台灣大學 資訊工程學系 Chapter 4: Threads. 資工系網媒所 NEWS 實驗室 Objectives To introduce the notion of a thread — a fundamental unit of CPU utilization that forms the.
Threads.
5.1 Silberschatz, Galvin and Gagne ©2003 Operating System Concepts with Java Chapter 5: Threads Overview Multithreading Models Threading Issues Pthreads.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Lecture 6: Threads Chapter 4.
02/05/2008CSCI 315 Operating Systems Design1 Java Threads Notice: The slides for this lecture have been largely based on those accompanying an earlier.
Chapter 4: Threads. 4.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Chapter 4: Threads Overview Multithreading Models Threading Issues.
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: 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.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Chapter 4: Threads CS 170 TY, Sept 2011.
Chapter 4: Threads. 4.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th edition, Jan 23, 2005 Chapter 4: Threads Overview Multithreading.
4.1 Silberschatz, Galvin and Gagne ©2005 Operating System Principles Chapter 4 Multithreaded Programming Objectives Objectives To introduce a notion of.
02/01/2007CSCI 315 Operating Systems Design1 Java Threads Notice: The slides for this lecture have been largely based on those accompanying the textbook.
Operating System Concepts with Java – 7 th Edition, Nov 15, 2006 Silberschatz, Galvin and Gagne ©2007 Chapter 4: Threads.
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 &
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.
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. 資工系網媒所 NEWS 實驗室 Objectives To introduce the notion of a thread — a fundamental unit of CPU utilization that forms the.
Chapter 4: Threads. 4.2CSCI 380 Operating Systems Chapter 4: Threads Overview Multithreading Models Threading Issues Pthreads Windows XP Threads Linux.
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.
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 ©2009 Operating System Concepts – 8 th Edition Chapter 4: Threads Modified from the slides of the text book. TY, Sept 2010.
Chapter 4: Threads.
1 Module 3 - Threads Reading: Chapter 4 Objective:  Understand the concept of the thread and its relationship to the process.  Study and understand the.
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.
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.
Chapter 4: Threads. 4.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Chapter 4: Threads Overview Multithreading Models Threading Issues.
Saurav Karmakar. Chapter 4: Threads  Overview  Multithreading Models  Thread Libraries  Threading Issues  Operating System Examples  Windows XP.
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.
Nadeem MajeedChoudhary.
Chapter 4: Threads.
Chapter 5: Threads Overview Multithreading Models Threading Issues
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:

4.1 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Exercise: Eratosthenes Sieve Eratosthenes sieve: An algorithm for computing prime numbers: Starts with a sequence 2,3,4,… N, finds all prime numbers <= N 2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26 3,5,7,9,11,13,15,17,19, 21, 23, 25 5,7,11,13,17,19, 23, 25 7,11,13, 17, 19, 23, 25 11,13,17,19,23,25 …

4.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Exercise: Eratosthenes Sieve Idea: Speed-up the computation of the Eratosthenes sieve on a multi- CPU computer by doing the work on each line in a separate process. Process Start (core): for(i=2; i<n; i++) write(i); // write(1, &i, sizeof(int)); Process Filter (core): read(&p); // read(0, &p, sizeof(int)); OutputPrime(p); while (read(&val) >0) if (val %p != 0) write(val); Launched from shell: Start 25 | Filter | Filter | Filter | Filter | …

4.3 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Exercise: Eratosthenes Sieve Problems: How many filter processes to start? Very tedious to launch it this way Solution: Create filter processes automatically Question: Who creates the filter processes? Answer: Start process forks the first filter process i-th filter process forks (i+1)th filter process No need to have separate program files, the filter process can be programmed as a procedure

4.4 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Exercise: Eratosthenes Sieve Process Start (core): pid = fork(); if (pid == 0) filter(); else { for(i=2; i<n; i++) write(i); // write(1, &i, sizeof(int)); } void filter() { read(&p); // read(0, &p, sizeof(int)); OutputPrime(p); pid = fork(); if (pid == 0) filter(); else { while (read(&val) >0) if (val %p != 0) write(val); } }

4.5 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Exercise: Eratosthenes Sieve What is the problem now? Everybody reads from stdin and writes to stdout Solution: Connect the processes using pipes Process Start (core): pipe(pipes); pid = fork(); if (pid == 0) { close(pipes[1]); filter(pipes[0]); } else { close(pipes[0]); for(i=2; i<n; i++) write(pipes[1], i); // write(1, &i, sizeof(int)); close(pipes[1]); }

4.6 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Exercise: Eratosthenes Sieve void filter(int fd) { int pipes[2], pid, p, val; read(fd, &p); // read(0, &p, sizeof(int)); OutputPrime(p); pipe(pipes); pid = fork(); if (pid == 0) { close(pipes[1]); filter(pipes[0]); } else { close(pipes[0]); while (read(fd, &val) >0) if (val %p != 0) write(pipes[1], val); close(pipes[1]); }

4.7 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Exercise: Eratosthenes Sieve Does it work now? no, each filter will launch a new filter need to know when to terminate  when the input of the filter contains single value – its prime number p  when p 2 >=n, all the remaining numbers are primes as well

4.8 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Exercise: Eratosthenes Sieve void filter(int fd) { int pipes[2], pid, p, val; read(fd, &p); // read(0, &p, sizeof(int)); OutputPrime(p) if (p*p>=n) { while(read(fd, &p)) OutputPrime(p); } else { pipe(pipes); pid = fork(); if (pid == 0) { close(pipes[1]); filter(pipes[0]); } else { close(pipes[0]); while (read(fd, &val) >0) if (val %p != 0) write(pipes[1], val); close(pipes[1]); }

4.9 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Exercise: Eratosthenes Sieve Does it work now? Yes! But, can we make Start and Filter standalone processes communicating through stdin/stdout? Yes, but we need to learn how to duplicate/redirect file pointers System call dup2(int newfp, int oldfp) wil replace the oldfp with the newfp Let’s see how it works:

4.10 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Exercise: Eratosthenes Sieve Process Start (core): itoa(n, nString, 10); // convert integer n into string nString pid = fork(); if (pid == 0) { close(pipes[1]); dup2(pipes[0],0); // pipes[0] will be now my stdin Execl(“filter”, “filter”, nString, NULL); } else { close(pipes[0]); dup2(pipes[1], 1); // pipes[1] will be now my stdout for(i=2; i<n; i++) write(1, i); // write(1, &i, sizeof(int)); }

4.11 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Exercise: Eratosthenes Sieve filter.c: int main(int argc, int argv) { int pipes[2], pid, p, val, n; n = atoi(argv[1]); read(0, &p); // read(0, &p, sizeof(int)); OutputPrime(p) if (p*p>=n) { while(read(0, &p)) OutputPrime(p); } else { pipe(pipes); pid = fork(); if (pid == 0) { close(pipes[1]); dup2(pipes[0], 0); execl(“filter”, “filter”, argv[1]); } else { close(pipes[0]); dup2(pipes[1], 1); while (read(fd, &val) >0) if (val %p != 0) write(pipes[1], val); close(pipes[1]); }

4.12 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Exercise: Eratosthenes Sieve Are we done now? Yes,  for a while… What about multithreaded Eratosthenes?  Wait till we learn about threads.

Chapter 4: Threads

4.14 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Threads vs Processes Process A unit/thread of execution, together with code, data and other resources to support the execution. Idea Make distinction between the resources and the execution threads Could the same resources support several threads of execution? Code, data,.., - yes CPU registers, stack - no

4.15 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Single vs Multithreaded Processes

4.16 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Chapter 4: Threads Overview Multithreading Models Threading Issues Pthreads Windows XP Threads Linux Threads Java Threads

4.17 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Why Threads? Responsiveness One thread handles user interaction Another thread does the background work (i.e. load web page) Utilization of multiprocessor architectures One process/thread can utilize only one CPU Well, but all this applies to one thread per process as well, just use processes Efficiency and convenience Resource sharing (shared memory, open files, …) Creating a thread, context switch between threads, can be much cheaper then for full processes Why?

4.18 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Thread Library Provides the programmer an API for creating and managing threads User thread The unit of execution managed by the thread library This is what is presented to the programmer Kernel thread The unit of execution scheduled and managed by the kernel Thread libraries: POSIX Pthreads Java threads Win32 threads Essentially all modern OSs support kernel threads

4.19 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts User Space Thread Library All code and data structures for thread management in user space No system calls involved, no OS support needed Many(all) user threads mapped to single kernel thread

4.20 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Many to One Mapping Properties: Cheap/fast, but runs as one process to the OS scheduler What happens if one thread blocks on an I/O?  All other threads block as well How to make use of multiple CPUs?  Not possible Examples Solaris Green Threads GNU Portable Threads

4.21 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Kernel Space Thread Library Code and data structures in kernel space Need support by the OS Calling a thread library typically results in system call Each user thread is mapped to a kernel thread

4.22 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts One to One Mapping Properties Usually, limited number of threads Thread management relatively costly But provides full benefits of multithreading Examples Windows NT/XP/2000 Linux Solaris 9 and later

4.23 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Many-to-Many Model Allows many user level threads to be mapped to many kernel threads The thread library cooperates with the OS to dynamically map user threads to kernel threads Intermediate costs and most of the benefits of multithreading Examples: Solaris prior to version 9 Windows NT/2000 with the ThreadFiber package

4.24 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Two-level Model Similar to M:M, except that it allows a user thread to be bound to kernel thread Examples IRIX HP-UX Tru64 UNIX Solaris 8 and earlier

4.25 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Single vs Multithreaded Processes

4.26 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Thread Library Provides the programmer an API for creating and managing threads User thread The unit of execution managed by the thread library This is what is presented to the programmer Kernel thread The unit of execution scheduled and managed by the kernel Thread libraries: POSIX Pthreads Java threads Win32 threads Essentially all modern OSs support kernel threads

4.27 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts User to Kernel Thread Mapping

4.28 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Threading Issues Ha! It is nice to have threads, but what does that mean when we think through all consequences? Issues: Semantics of fork() and exec() system calls Thread cancellation Signal handling Thread pools Thread specific data Scheduler activations

4.29 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Threading Issues Ha! It is nice to have threads, but what does that mean when we think through all consequences? Issues: Semantics of fork() and exec() system calls Thread cancellation Signal handling Thread pools Thread specific data Scheduler activations

4.30 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Semantics of fork() and exec() Does fork() duplicate only the calling thread or all threads? Often two versions provided Which one to use? What does exec() do? Well, it replaces the address space, so all threads must go

4.31 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Thread Cancellation Terminating a thread before it has finished Two general approaches: Asynchronous cancellation terminates the target thread immediately  Might leave the shared data in corrupt state  Some resources may not be freed Deferred cancellation  Set a flag which the target thread periodically checks to see if it should be cancelled  Allows graceful termination

4.32 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Signal Handling Signals are used in UNIX systems to notify a process that a particular event has occurred Essentially software interrupt A signal handler is used to process signals  Signal is generated by particular event  Signal is delivered to a process  Signal is handled Options: Deliver the signal to the thread to which the signal applies Deliver the signal to every thread in the process Deliver the signal to certain threads in the process Assign a specific thread to receive all signals for the process

4.33 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Thread Pools A server process might service requests by creating a thread for each request that needs servicing But thread creation costs are wasted time No control over the number of threads, possibly overwhelming the system Solution Create a number of threads in a pool where they await work Advantages:  The overhead for creating threads is paid only at the beginning  Allows the number of threads in the application(s) to be bound to the size of the pool

4.34 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Thread Specific Data Allows each thread to have its own copy of data Useful when you do not have control over the thread creation process (i.e., when using a thread pool)

4.35 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Scheduler Activations Both M:M and Two-level models require communication from the kernel to inform the thread library when a user thread is about to block, and when it again becomes ready for execution When such event occurs, the kernel makes an upcall to the thread library The thread library’s upcall handler handles the event (i.e. save the user thread’s state and mark it as blocked)

4.36 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Chapter 4: Threads Overview Multithreading Models Threading Issues Specific thread libraries Pthreads Win32 Java threads Thread implementations Windows XP Linux Java

4.37 Silberschatz, Galvin and Gagne ©2005 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 the developer of the library Common in UNIX operating systems (Solaris, Linux, Mac OS X) Typical functions: pthread_create (thread, attr, start_routine, arg) pthread_create pthread_exit (status) pthread_exit pthread_join (threadid, status) pthread_join pthread_attr_init (attr) pthread_attr_init

4.38 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Thread Programming Exercise Goal: Write multithreaded matrix multiplication algorithm, in order to make use of several CPUs. Single threaded algorithm for multiplying n x n matrices A and B : for(i=0; i<n; i++) for(j=0; j<n; j++) { C[i,j] = 0; for(k=0; k<n; k++) C[i,j] += A[i,k] * B[k,j]; } Just to make our life easier: Assume you have 6 CPUs and n is multiple of 6. How to start? Any ideas?

4.39 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Multithreaded Matrix Multiplication Idea: create 6 threads have each thread compute 1/6 of the matrix C wait until everybody finished the matrix can be used now Thread 0 Thread 1 Thread 2 Thread 3 Thread 4 Thread 5

4.40 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Let’s go! pthread_t tid[6]; pthread_attr_t attr; inti; pthread_init_attr(&attr); for(i=0; i<6; i++) /* create the working threads */ pthread_create( &tid[i], &attr, worker, &i); for(i=0; i<6; i++) /* now wait until everybody finishes */ pthread_join(tid[i], NULL); /* the matrix C can be used now */ …

4.41 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Let’s go! void *worker(void *param) { int i,j,k; int id = *((int *) param); /* take param to be pointer to integer */ int low = id*n/6; int high = (id+1)*n/6; for(i=low; i<high; i++) for(j=0; j<n; j++) { C[i,j] = 0; for(k=0; k<n; k++) C[i,j] = A[i,k]*B[k,j]; } pthread_exit(0); }

4.42 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Let’s go! Would it work? do we need to pass A,B,C and n in the parameters? no, they are in the shared memory, we are fine did we pass IDs properly? not really, all threads get the same pointer for(i=0; i<6; i++) /* create the working threads */ { id[i] = i; pthread_create( &tid[i], &attr, worker, &id[i]); } Would it work now? should, …

4.43 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Win32 Thread API // thread creation: ThreadHandle = CreateThread( NULL, // default security attributes 0, // default stack size Summation, // function to execute &Param, // parameter to thread function 0, // default creation flags &ThreadId); // returns the thread ID Simple programming example:programming example

4.44 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Java Threads Java threads are created by calling a start() method of a class that extends Thread class, or implements the Runnable interface: public interface Runnable { public abstract void run(); } Java threads are inherent and important part of the Java language, with rich API availablerich API

4.45 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Extending the Thread Class class Worker1 extends Thread { public void run() { System.out.println("I Am a Worker Thread"); } public class First { public static void main(String args[]) { Worker1 runner = new Worker1(); runner.start(); System.out.println("I Am The Main Thread"); }

4.46 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Implementing the Runnable Interface class Worker2 implements Runnable { public void run() { System.out.println("I Am a Worker Thread "); } public class Second { public static void main(String args[]) { Runnable runner = new Worker2(); Thread thrd = new Thread(runner); thrd.start(); System.out.println("I Am The Main Thread"); }

4.47 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Joining Threads class JoinableWorker implements Runnable { public void run() { System.out.println("Worker working"); } public class JoinExample { public static void main(String[] args) { Thread task = new Thread(new JoinableWorker()); task.start(); try { task.join(); } catch (InterruptedException ie) { } System.out.println("Worker done"); }

4.48 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Thread Cancellation Thread thrd = new Thread (new InterruptibleThread()); thrd.start();... // now interrupt it thrd.interrupt();

4.49 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Thread Cancellation public class InterruptibleThread implements Runnable { public void run() { while (true) { /** * do some work for awhile */ if (Thread.currentThread().isInterrupted()) { System.out.println("I'm interrupted!"); break; } // clean up and terminate }

4.50 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Thread Specific Data class Service { private static ThreadLocal errorCode = new ThreadLocal();ThreadLocal public static void transaction() { try { /** * some operation where an error may occur */ catch (Exception e) { errorCode.set(e); } /** * get the error code for this transaction */ public static Object getErrorCode() { return errorCode.get(); }

4.51 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Thread Specific Data class Worker implements Runnable { private static Service provider; public void run() { provider.transaction(); System.out.println(provider.getErrorCode()); } } Somewhere in the code: … Worker worker1 = new Worker(); Worker worker2 = new Worker(); worker1.start(); worker2.start(); … Assume there were different errors in the transactions of both workers, but both transactions finished before any of the prints in the run() methods started. Would the workers print the same errors or not?

4.52 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Chapter 4: Threads Overview Multithreading Models Threading Issues Specific thread libraries Pthreads Win32 Java threads Thread implementations Windows XP Linux Java

4.53 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Windows XP Threads Implements the one-to-one mapping Each thread contains A thread id Register set Separate user and kernel stacks Private data storage area The register set, stacks, and private storage area are known as the context of the threads The primary data structures of a thread include: ETHREAD (executive thread block) KTHREAD (kernel thread block) TEB (thread environment block)

4.54 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Windows XP Threads

4.55 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Linux Threads Linux refers to them as tasks rather than threads Thread creation is done through clone() system call The clone() system call allows to specify which resources are shared between the child and the parent Full sharing  threads Little sharing  like fork()

4.56 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Java Threads Java threads are managed by the JVM

4.57 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Chapter 4: Threads Overview Multithreading Models Threading Issues Specific thread libraries Pthreads Win32 Java threads Thread implementations Windows XP Linux Java More threads examples and exercises

4.58 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Understanding threads Take a look at this program.this What would be its output? Why?

End of Chapter 4