Sunteți pe pagina 1din 21

UNIT II UNIT II Management, Thread Scheduling Chapter 3: Process Concept Process Concepts Chapter 4: Multithread Programming Threads Chapter

5: Process Scheduling Scheduling Criteria Algorithms their evaluation Thread Scheduling Case Studies UNIX / Linux / Windows Process Concepts Process A program in execution. Process execution must progress in Sequential fashion. includes: Program Counter Stack Data Section Process Process States Process Control Block (PCB) Switch from Process to Process Process Scheduling Queues Schedulers Cooperating Processes Inter Process Communication (IPC)

UNIT II

Process States As a process executes, it changes state new: The process is being created running: Instructions are being executed waiting: The process is waiting for some event to occur ready: The process is waiting to be assigned to a processor terminated: The process has finished execution

Process Control Block (PCB) Information associated with each process Process State Program Counter CPU Registers CPU Scheduling information Memory Management information Accounting information I/O Status information

UNIT II

CPU Switch From Process to Process

Process Scheduling Queues Job queue Set of all processes in the system

Ready queue Set of all processes residing in main memory, ready and waiting to execute

Device queues 3

UNIT II Set of processes waiting for an I/O device

Processes migrate among the various queues

Ready Queue and Various I/O Device Queues

Representation of Process Scheduling

UNIT II

Schedulers

Long-term scheduler (or job scheduler)


Selects which processes should be brought into the ready queue

Short-term scheduler (or CPU scheduler)


Selects which process should be executed next and allocates CPU

Addition of Medium Term Scheduling

Cooperating Processes Independent process cannot affect or be affected by the execution of another process

Cooperating process 5

UNIT II can affect or be affected by the execution of another process

Advantages of process cooperation Information sharing Computation Speed-up Modularity Convenience

Interprocess Communication (IPC) Mechanism for processes to communicate and to synchronize their actions

Message system
Processes communicate with each other without resorting to shared variables

Provides two operations:


send(message) message size fixed or variable receive(message)

If P and Q wish to communicate, they need to:


establish a communication link between them exchange messages via send/receive

Implementation of communication link


Physical (e.g., shared memory, hardware bus) Logical (e.g., logical properties) Threads

Threads Benefits User Threads Kernel Threads Multithreading Models Threading Issues Application Threads 6

UNIT II Threads

Light Weight Process (LWP). Basic unit of CPU utilisation. Comprises of


o o o o Thread ID Program Counter Register Set Stack

Types
o o User Threads Kernel Threads

Single and Multithreaded Processes

Benefits 7

UNIT II Responsiveness Resource Sharing Economy Utilization of Multi Processor Architectures

User Threads Thread management done by user-level threads library Three primary thread libraries: POSIX Pthreads Win32 threads Java threads

Kernel Threads Supported by the Kernel Examples Windows XP/2000 Solaris Linux Tru64 UNIX Mac OS X

Multithreading Models Many-to-One Model One-to-One Model Many-to-Many Model 8

UNIT II

Many-to-One Model Many user-level threads mapped to single kernel thread Examples: Solaris Green Threads GNU Portable Threads

One-to-One Model Each user-level thread maps to kernel thread Examples Windows NT/XP/2000 Linux Solaris 9 and later

UNIT II

Many-to-Many Model Allows many user level threads to be mapped to many kernel threads Allows the operating system to create a sufficient number of kernel threads Solaris prior to version 9 Windows NT/2000 with the ThreadFiber package

Two-Level Model Similar to Many-to-Many, except that it allows a user thread to be bound to kernel thread. Examples IRIX HP-UX 10

UNIT II Tru64 UNIX Solaris 8 and earlier

Threading Issues Semantics of fork() and exec() system calls Thread Cancellation Signal Handling Thread Pools Thread Specific Data Scheduler Activations

Thread Cancellation Terminating a thread before it has finished. General approaches: 11

UNIT II Asynchronous cancellation Terminates the target thread immediately

Deferred cancellation Allows the target thread to periodically check, if it should be cancelled Signal Handling

Signals are used in UNIX systems 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

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
Thread Pools

Create a no. 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 no. of threads in the application(s) to be bound to the size of the pool 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) Scheduler Activations

Both Many-to-Many and Two-level models require communication to maintain the appropriate number of kernel threads allocated to the application Scheduler activations provide upcalls A communication mechanism from the kernel to the thread library

This communication allows an application to maintain the correct number kernel threads Application Threads 12

UNIT II Pthreads Windows XP Threads Linux Threads Java Threads Pthreads A POSIX standard (IEEE 1003.1c) API for thread creation and synchronization 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 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) 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

13

UNIT II Java Threads Java threads are managed by the JVM Java threads may be created by: Extending Thread class Implementing the Runnable interface Java Thread States

CPU Scheduler Selects from among the processes in memory that are ready to execute, and allocates the CPU to one of them CPU scheduling decisions may take place when a process: 1. 2. 3. 4. Switches from running to waiting state (Non-Preemptive) Switches from running to ready state (Preemptive) Switches from waiting to ready (Preemptive) Terminates (Non-Preemptive) Scheduling Criteria CPU utilization Keep the CPU as busy as possible

Throughput
No. 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 14

UNIT II

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) Optimization Criteria

Max CPU utilization Max throughput Min turnaround time Min waiting time Min response time Scheduling Algorithms

FCFS Scheduling SJF Scheduling Non-Preemptive Preemptive

Priority Scheduling Round Robin Multilevel Queue Scheduling Multilevel Feedback Scheduling FCFS Scheduling Process Burst Time P1 P2 P3 24 3 3

Suppose that the processes arrive in the order: P1 , P2 , P3 Gantt Chart:

15

UNIT II Waiting time for P1 = 0; P2 = 24; P3 = 27 Average waiting time: (0 + 24 + 27) / 3 = 17 Process Burst Time P1 P2 P3 24 3 3

Suppose that the processes arrive in the order: P2 , P3 , P1 Gantt Chart:

Waiting time for P1 = 6; P2 = 0; P3 = 3 Average waiting time: (6 + 0 + 3) / 3 = 3 Shortest-Job-First (SJF) Scheduling Associate with each process the length of its next CPU burst. Use these lengths to schedule the process with the shortest time Schemes: Non-Preemptive Once CPU given to the process it cannot be preempted until completes its CPU burst

Preemptive / Shortest-Remaining-Time-First (SRTF) If a new process arrives with CPU burst length less than remaining time of current executing process, preempt.

Optimal
Gives minimum average waiting time for a given set of processes

Example of Non-Preemptive SJF Process P1 P2 P3 P4 Arrival Time 0.0 2.0 4.0 5.0 Burst Time 7 4 1 4 16

UNIT II Gantt Chart:

Average waiting time = (0 + 6 + 3 + 7) / 4 = 4 Example of Preemptive SJF Process Arrival Time P1 P2 P3 P4 Gantt Chart: 0.0 2.0 4.0 5.0 7 4 1 4 Burst Time

Average waiting time = (9 + 1 + 0 +2) / 4 = 3 Priority Scheduling A priority No. (Integer) is associated with each process The CPU is allocated to the process with the highest priority Smallest integer highest priority Preemptive Non-preemptive SJF is a priority scheduling where priority is the predicted next CPU burst time

Problem Starvation Low priority processes may never execute

Solution Aging
As time progresses increase the priority of the process 17

UNIT II

Round Robin (RR) Each process gets a small unit of CPU time (time quantum), usually 10-100 milliseconds. After this time has elapsed, the process is preempted and added to the end of the ready queue. If there are n processes in the ready queue and the time quantum is q, then each process gets 1/n of the CPU time in chunks of at most q time units at once. No process waits more than (n-1)q time units. Example of RR with Time Quantum = 20 Process Burst Time P1 P2 P3 P4 Gantt chart: 53 17 68 24

Higher average turnaround than SJF, but better response Multilevel Queue Ready queue is partitioned into separate queues: Foreground (Interactive) Background (Batch)

Each queue has its own scheduling algorithm


Foreground RR Background FCFS

Scheduling must be done between the queues


Fixed priority scheduling; (i.e., serve all from foreground then from background). Possibility of starvation. 18

UNIT II

Time slice Each queue gets a certain amount of CPU time which it can schedule amongst its processes; i.e., 80% to foreground in RR 20% to background in FCFS

Multilevel Queue Scheduling

Multilevel Feedback Queue A process can move between the various queues; aging can be implemented this way

Multilevel-feedback-queue scheduler defined by the following parameters:


No. of queues Scheduling algorithms for each queue Method used to determine when to upgrade a process Method used to determine when to demote a process 19

UNIT II Method used to determine which queue a process will enter when that process needs service

Example of Multilevel Feedback Queue Queues: Q0 RR with time quantum 8 milliseconds Q1 RR time quantum 16 milliseconds Q2 FCFS

Scheduling
A new job enters queue Q0 which is served FCFS. When it gains CPU, job receives 8 milliseconds. If it does not finish in 8 milliseconds, job is moved to queue Q1. At Q1 job is again served FCFS and receives 16 additional milliseconds. If it still does not complete, it is preempted and moved to queue Q2.

Windows XP Priorities

20

UNIT II

Linux Scheduling Time-sharing Algorithm Prioritized credit-based Process with most credits is scheduled next

Credit subtracted when timer interrupt occurs When credit = 0, another process chosen When all processes have credit = 0, recrediting occurs Based on factors including priority and history

Real-time Algorithm
Soft real-time Posix.1b compliant Classes FCFS RR

Highest priority process always runs first

21

S-ar putea să vă placă și