140 likes | 246 Views
CGS 3763 Operating Systems Concepts Spring 2013. Dan C. Marinescu Office: HEC 304 Office hours: M- Wd 11:30 - 12:30 A M. Last time: Threads Today: Pthreads Java threads CPU scheduling Next time CPU Scheduling Reading assignments
E N D
CGS 3763 Operating Systems Concepts Spring 2013 Dan C. Marinescu Office: HEC 304 Office hours: M-Wd 11:30 - 12:30 AM
Last time: Threads Today: Pthreads Java threads CPU scheduling Next time CPU Scheduling Reading assignments Chapters 4 and 5 of the textbook Lecture 19 – Wednesday, February 20, 2013 Lecture 19
Pthreads POSIX (Portable Operating System Interface) - a family of standards specified by the IEEE for maintaining compatibility between operating systems. Pthreads – POSIX standard defining an API for thread creation and synchronization. A tutorial on Pthreads at https://computing.llnl.gov/tutorials/pthreads/#Pthread Lecture 19
Pthreads • Threads use and exist within these process resources, yet are able to be scheduled by the operating system and run as independent entities largely because they duplicate only the bare essential resources that enable them to exist as executable code. • This independent flow of control is accomplished because a thread maintains its own: • Stack pointer • Registers • Scheduling properties (such as policy or priority) • Set of pending and blocked signals • Thread specific data. Lecture 19
Timing results for: (1) the fork()subroutine; (2) the pthread_create()subroutine. Timings in seconds reflect 50,000 process/thread creations. Don't expect the system and user times to add up to real time, because these are SMP systems with multiple CPUs working on the problem at the same time. Lecture 19
Pthreads API Thread management: Routines that work directly on threads - creating, detaching, joining, etc. They also include functions to set/query thread attributes (joinable, scheduling etc.) Mutexes: Routines that deal with synchronization, called a "mutex", which is an abbreviation for "mutual exclusion". Mutex functions provide for creating, destroying, locking and unlocking mutexes. These are supplemented by mutex attribute functions that set or modify attributes associated with mutexes. Condition variables: Routines that address communications between threads that share a mutex. Based upon programmer specified conditions. This group includes functions to create, destroy, wait and signal based upon specified variable values. Functions to set/query condition variable attributes are also included. Synchronization: Routines that manage read/write locks and barriers. Lecture 19
Java threads • Threads a fundamental programming model in Java. • Managed by JVM (Java Virtual Machine). • The method we use in the next example is to implement Runnable interface public interface Runnable { public abstract void run(); } • When a class implements a Runnable interface it must define a run() method. The code running when the run() method is invoked runs as a separate thread. • Two methods: • Start() start a thread • Join() block the calling thread until a thread terminates Lecture 19
Example: Java program for summation of positive integers from 1to upper. • The Summation class implements the Runnable interface. • Creation of a thread object create an instance of a Thread class pass the constructor a Runnableobject • The actual thread creation use the start() method to • Allocate the memory and initialize the new thread • Call the run() method to make the thread eligible to run under JVM (Java Virtual Machine) • When the summation program runs two threads are created: • The parent thread it starts the execution in the main() method. • The child thread created by the start()method • it begins execution in the run() method of the Summation class. • it terminates after outputting the result and exits from its run() method. Lecture 19
CPU scheduling • CPU scheduling – the “Running” process either relinquishes the control of the CPU by itself or it is forced by the scheduler to do so. Then the scheduler selects a process from those in the ``Ready’’ queue and gives it the control of the CPU and • Non-preemptive scheduling the ``Running’’ process decides by itself to 1. switch from ``Running’’ to ``Waiting’’ or the ``Ready’’ state. 2. Terminates execution • Preemptive scheduling the ``Running’’ process is forced to relinquish the control of the CPU by the scheduler. • CPU burst the period of time a process is in control of the CPU. • I/O burst the period of time a process is in a “Waiting” state. Lecture 19
Metrics and objectives of scheduling policies • Two types of systems • Batch processing of jobs • Interactive, e.g., transaction processing systems • The metrics • Utilization ratio of useful time versus total time • Throughput number of transactions or jobs per unit of time • Turn around time the time it takes to complete a job • Response time the time to get a response at a request • Waiting time the time a job or a transaction has to wait before being processed • The objectives • Maximize: CPU utilization and Throughput • Minimize: Turn around time, Response time, Waiting time • The objectives can be contradictory!! Lecture 19
Histogram of the CPU burst time Lecture 19
Typical alternations of CPU and I/O bursts Lecture 19