Survey							
                            
		                
		                * Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Chapter 4: Threads Operating System Concepts with Java – 8th Edition 14.1 Silberschatz, Galvin and Gagne ©2009 Announcements  Project -2 deadline extension? – 09/26  10% bonus who submit before 09/24 11:59 PM  Project-3 will be uploaded by 09/27  Multithreaded server using sockets  Midterm exam – 10/03 or 10/08  In class exam, closed book, 1 cheat sheet allowed  Signup for homework presentations Operating System Concepts with Java – 8th Edition 14.2 Silberschatz, Galvin and Gagne ©2009 User-level – Kernel-level Thread Mapping  Many-to-One  One-to-One  Many-to-Many Operating System Concepts with Java – 8th Edition 14.3 Silberschatz, Galvin and Gagne ©2009 Many-to-One  Many user-level threads mapped to single kernel thread  Thread management done by user-level library  Entire process blocks if a thread makes blocking system call  Not suitable for multi-processor environment  Examples:  Solaris Green Threads  GNU Portable Threads Operating System Concepts with Java – 8th Edition 14.4 Silberschatz, Galvin and Gagne ©2009 Many-to-One Model Operating System Concepts with Java – 8th Edition 14.5 Silberschatz, Galvin and Gagne ©2009 One-to-One  One user-level thread mapped to single kernel thread  Provides for more concurrency  Suitable for multi-processor systems  Drawback: Thread creation requires kernel intervention  Systems restrict number of threads  Examples:  Linux Threads  WIndowsThreads Operating System Concepts with Java – 8th Edition 14.6 Silberschatz, Galvin and Gagne ©2009 One-to-one Model Operating System Concepts with Java – 8th Edition 14.7 Silberschatz, Galvin and Gagne ©2009 Many – to - Many  Multiplexes many user-level threads to equal or smaller number of kernel threads  Number of kernel threads may be specific to application or system  Does not suffer from drawbacks of one-to- one or many-to-one models  No restriction on number of threads user can create  Example: Windows NT/2000 with ThreadsFiber package Operating System Concepts with Java – 8th Edition 14.8 Silberschatz, Galvin and Gagne ©2009 Many-to-Many Model Operating System Concepts with Java – 8th Edition 14.9 Silberschatz, Galvin and Gagne ©2009 Two-Level Model  Variant of Many-to-Many model  Multiplexes user-level threads to equal or smaller number of kernel threads  Also allows a user-level thread to bind to a kernel-level thread  Examples:  IRIX  HP-UX  Tru64 UNIX  Solaris 8 and earlier Operating System Concepts with Java – 8th Edition 14.10 Silberschatz, Galvin and Gagne ©2009 Two-level Model Operating System Concepts with Java – 8th Edition 14.11 Silberschatz, Galvin and Gagne ©2009 Thread Libraries  Thread library provides programmer with API for creating and managing threads  Library entirely in user space  Code and data structures entirely in user space  Kernel-level library supported by the OS  Code and data structures in kernel space  Invoking functions will cause system call  Pthreads – Can be either user or system-level  Win32 Threads – Kernel level  Java Threads – Depends upon the host OS Operating System Concepts with Java – 8th Edition 14.12 Silberschatz, Galvin and Gagne ©2009 Pthreads  May be provided either as user-level or kernel- level  A POSIX standard (IEEE 1003.1c) API for thread creation and synchronization – not an actual implementation  API specifies behavior of the thread library, implementation is up to development of the library  Common in UNIX operating systems (Solaris, Linux, Mac OS X) Operating System Concepts with Java – 8th Edition 14.13 Silberschatz, Galvin and Gagne ©2009 Pthreads (Contd.)  pthread_t – thread identifier  pthread_attr_t – thread attributes  pthread_create – function to create threads  Need  The to specify a start routine routine can take a single parameter (pointer)  Global variables shared among all threads  pthread_wait() – wait for thread to complete  pthread_exit() – Thread terminates itself Operating System Concepts with Java – 8th Edition 14.14 Silberschatz, Galvin and Gagne ©2009 Java Threads  Java threads are managed by the JVM  Java two mechanisms for creating threads  Create a new class derived from Thread class overriding the run() method  Implementing the Runnable interface  Must define the run() method  Code provided in the run() method executes as new thread Operating System Concepts with Java – 8th Edition 14.15 Silberschatz, Galvin and Gagne ©2009 Java Threads (Contd.)  Code that the new thread executes provided in run() method of class implementing runnable  Creating a thread – start() method  Note that creating a thread object does not create the thread  Allocates memory for new thread  Calls run() method of the class implementing runnable  Data sharing via passing object references  join () for waiting on threads (equivalent to pthreads_join()) Operating System Concepts with Java – 8th Edition 14.16 Silberschatz, Galvin and Gagne ©2009 Java Threads - Example Program Operating System Concepts with Java – 8th Edition 14.17 Silberschatz, Galvin and Gagne ©2009 Java Threads - Example Program Operating System Concepts with Java – 8th Edition 14.18 Silberschatz, Galvin and Gagne ©2009 Java Thread States Operating System Concepts with Java – 8th Edition 14.19 Silberschatz, Galvin and Gagne ©2009 Threading Issues  Semantics of fork() and exec() system calls  Thread cancellation of target thread  Asynchronous or deferred  Signal handling  Thread pools  Thread-specific data  Scheduler activations Operating System Concepts with Java – 8th Edition 14.20 Silberschatz, Galvin and Gagne ©2009 Semantics of fork() and exec()  What happens if a thread calls fork()  Duplicate  Some only the calling thread or all threads? Unix systems provide two types of fork()  exec() works in a similar fashion as discussed before  If exec() called right after forking, no need to duplicate all threads  If exec() not called after forking all threads need to be duplicated Operating System Concepts with Java – 8th Edition 14.21 Silberschatz, Galvin and Gagne ©2009 Thread Cancellation  Terminating a thread before it has finished  Why termination is needed?  Asynchronous cancellation  Another thread immediately terminates target thread  Drawback: Cancelled thread might be holding some system resources or updating shared data  Deferred cancellation  Target thread periodically checks if it should be cancelled  Cancellation points in Pthreads  Provides opportunity for cancelled thread to cleanup Operating System Concepts with Java – 8th Edition 14.22 Silberschatz, Galvin and Gagne ©2009 Java Thread Cancellation  Asynchronous termination via stop() method – deprecated  interrupt() method for deferred cancellation  Sets the interruption status  Threads can periodically check their interruption status using interrupted or isInterrupted() methods  Both are defined in the Thread class  currentThread() to obtain the thread object corresponding to the thread that is currently running Operating System Concepts with Java – 8th Edition 14.23 Silberschatz, Galvin and Gagne ©2009 Signal Handling  Signals are used in to notify a process that a particular event has occurred.  A signal handler is used to process signals. 1. Signal is generated by particular event 2. Signal is delivered to a process 3. Signal is handled  Two types of signals  Synchronous signals  Asynchronous signals Operating System Concepts with Java – 8th Edition 14.24 Silberschatz, Galvin and Gagne ©2009 Signal Handling (Contd.)  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  Unix systems allow threads to specify the signals it will block and signals it will process  Deliver signal to any thread that does not block it Operating System Concepts with Java – 8th Edition 14.25 Silberschatz, Galvin and Gagne ©2009 Thread Pools  On-demand thread creation has inherent problems  Create a number of threads in a pool where they await work.  Advantages:  Usually slightly faster to service a request with an existing thread than create a new thread.  Allows the number of threads in the application(s) to be bound to the size of the pool. Operating System Concepts with Java – 8th Edition 14.26 Silberschatz, Galvin and Gagne ©2009 End of Chapter 4 Operating System Concepts with Java – 8th Edition 14.27 Silberschatz, Galvin and Gagne ©2009 Chapter 5: CPU Scheduling Operating System Concepts with Java – 8th Edition 14.28 Silberschatz, Galvin and Gagne ©2009 Chapter 5: CPU Scheduling  Basic Concepts  Scheduling Criteria  Scheduling Algorithms  Thread Scheduling  Multiple-Processor Scheduling  Operating Systems Examples  Algorithm Evaluation Operating System Concepts with Java – 8th Edition 14.29 Silberschatz, Galvin and Gagne ©2009 Objectives  To introduce CPU scheduling, which is the basis for multiprogrammed operating systems  To describe various CPU-scheduling algorithms  To discuss evaluation criteria for selecting a CPU-scheduling algorithm for a particular system Operating System Concepts with Java – 8th Edition 14.30 Silberschatz, Galvin and Gagne ©2009 Basic Concepts  Maximum CPU utilization obtained with multiprogramming  Not a good idea to “idle” the CPU  Jobs typically do I/O to get the data they need to process  Process will not actively utilize CPU when doing I/O  CPU–I/O Burst Cycle – Cycle of CPU execution and I/O wait Operating System Concepts with Java – 8th Edition 14.31 Silberschatz, Galvin and Gagne ©2009 Alternating Sequence of CPU And I/O Bursts Operating System Concepts with Java – 8th Edition 14.32 Silberschatz, Galvin and Gagne ©2009 CPU Burst Distribution  Processes alternate between CPU burst and I/O burst  Process begins with a CPU burst and ends with a CPU burst  Duration of CPU and I/O bursts vary widely  CPU-bound jobs have relatively long CPU- bursts  I/O bound jobs have shorter CPU bursts Operating System Concepts with Java – 8th Edition 14.33 Silberschatz, Galvin and Gagne ©2009 Histogram of CPU-burst Times Operating System Concepts with Java – 8th Edition 14.34 Silberschatz, Galvin and Gagne ©2009 CPU Scheduler  Selects from among the processes in ready queue and allocates the CPU to one of them  CPU scheduling decisions may take place when a process: 1. Switches from running to waiting state (e.g., I/O) 2. Switches from running to ready state (e.g., interrupt) 3. Switches from waiting to ready 4. Terminates  Scheduling under 1 and 4 is nonpreemptive  All other scheduling is preemptive Operating System Concepts with Java – 8th Edition 14.35 Silberschatz, Galvin and Gagne ©2009 Dispatcher  Dispatcher module gives control of the CPU to the process selected by the short-term scheduler; this involves:  switching context  switching to user mode  jumping to the proper location in the user program to restart that program  Dispatch latency – time it takes for the dispatcher to stop one process and start another running Operating System Concepts with Java – 8th Edition 14.36 Silberschatz, Galvin and Gagne ©2009 Scheduling Criteria  CPU utilization – %age of time CPU is busy  Throughput – # of processes that complete their execution per time unit  Turnaround time – amount of time to execute a particular process  Waiting time – amount of time a process has been waiting in the ready queue  Response time – amount of time it takes from when a request was submitted until the first response is produced, not output (for time-sharing environment) Operating System Concepts with Java – 8th Edition 14.37 Silberschatz, Galvin and Gagne ©2009 Scheduling Algorithm Optimization Criteria  Max CPU utilization  Max throughput  Min turnaround time  Min waiting time  Min response time Operating System Concepts with Java – 8th Edition 14.38 Silberschatz, Galvin and Gagne ©2009