Sie sind auf Seite 1von 63

Process Management

What is a Process?
A process is a program in execution. A process in execution needs resources like processing resource, memory and IO resource. Imagine a program written in C my_prog.c. After compilation we get an executable. If we now give a command like ./a.out it becomes a process.

What is a Process 2
A computer can have several process running or active at any given time. In case of multiple users on a system, all users share a common processing resource

Multi-programming and Time Sharing


Let us consider a system with only one processor and one user running one program: prog_1. IO and processing will happen alternately. When IO is required, say keyboard input, the processor idles. This is because we are nearly a million times slower than the processor !!!

One Program - One User - Uni-processor Operation

*Note: Most of the time the processor is idling !!

Processor Utilization - 1
Now think about the processor utilization. What percentage of time are we engaging the processor? Recall that Von Neumann computing requires a program to reside in main memory to run. Clearly, having just one program would result in gross under utilization of the processor. To enhance utilization, we should try to have more than one ready-to-run program resident in main memory.

Processor Utilization - 2
A processor is the central element in a computer s operation. A computer s throughput depends upon the extent of utilization of its processor. Previous figure shows processor idling for very long periods of time when only one program is executed. Now let us consider two ready-to-run memory resident programs and their execution sequence.

Multi-programming Support in OS - 1

Consider two programs : prog_1 and prog_2 resident in main memory.

Multi-programming Support in OS - 2
In the figure, When prog_1 is not engaging the processor may be utilized to run another ready-to-run program. Clearly, these two programs can be processed without significantly sacrificing the time required to process either of them.

Multi-programming Support in OS - 3
Disadvantage - Overhead faced while switching the context of use of the processor. Advantage 1. Computer resource utilization is improved. 2 Memory utilization is improved with multiple processes residing in main memory. 3 A system would give maximum throughput when all its components are busy all the time.

Response Time - 1
Consider the following scenario :

The number of ready-to-run programs must be maximized to maximize throughput of processor.  These programs could belong to different users.

Response Time - 2
A system with its resources being used by multiple users is called time sharing system. For example, a system with multiple terminals. Also a web server serving multiple clients.
However, such a usage has overheads. Lets see some of the overheads.

Response Time - 3
In case of a switch in the context of the use of the processor, we must know where in the program sequence the program was suspended.  In addition, intermediate results stored in registers have to be safely stored in a location before suspension.

Response Time - 4
When a large number of resident user programs compete for the processor resource, the frequency of storage, reloads and wait periods also increase.  If overheads are high, users will have to wait longer for their programs to execute =>Response Time of the system becomes longer.
Response Time is the time interval which spans the time from when the last character has been input to the time when the first character of the output appears.

Response Time : Some Facts


In a time sharing system, it is important to achieve an acceptable response time. In a plant with an on-line system, system devices are continuously monitored : to determine the criticality of a plant condition. Come to think of it even a library system is an on-line system.  If an online system produces a response time within acceptable limits, we say it is a real-time system.

Process States
In all the previous examples, we said  A process is in RUN state if is engaging the processor,  A process is in WAIT state if it is waiting for IO to be completed In our simplistic model we may think of 5 states: 1. 2. 3. 4. 5. New-process Ready-to-run Running Waiting-for-IO and Exit

Modeling Process States

Process states : Management issues - 1


When a process is created, OS assigns it an ID - pid, and creates a data structure to record its progress. The state of the process is now readyto-run. OS has a dispatcher that selects a ready-to-run process and assigns to the processor.  OS allocates a time slot to run this process. OS monitors the progress of every process during its life time.

Process states : Management issues - 2


A process may not progress till a certain event occurs - synchronizing signal. A process waiting for IO is said to be blocked for IO.  OS manages all these process migrations between process states.

A Queuing Model
Data structures are used for process management. OS maintains a queue for all ready-to-run processes. OS may have separate queue for each of the likely events (including completion of IO).

Queues Based Model - 1

Queues Based Model - 2


This model helps in the study and analysis of chosen OS policies. As an example, Consider the First Come First Served policy for ready-to-run queue. To compare this policy with one that prioritizes processes we can study: The average and maximum delays experienced by the lowest priority process.

Queues Based Model - 3


Comparison of the response times and throughputs in the two cases. Processor utilization in the two cases and so on. Such studies offer new insights - for instance, what level of prioritization leads to starvation.

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. Switches from running to ready state. Switches from waiting to ready. Terminates.

Scheduling under 1 and 4 is nonpreemptive. All other scheduling is preemptive.


Operating System Concepts

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

Scheduling Criteria
CPU utilization keep the CPU as busy as possible 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 timesharing environment)
Operating System Concepts

Optimization Criteria
Max CPU utilization Max throughput Min turnaround time Min waiting time Min response time

Operating System Concepts

First-Come, First-Served (FCFS) Scheduling Process Burst Time P1 24 P2 3 P3 3 Suppose that the processes arrive in the order: P1 , P2 , P3 The Gantt Chart for the schedule is: P1 P2 P3
0 24 27 30

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


Operating System Concepts

FCFS Scheduling (Cont.)


Suppose that the processes arrive in the order P2 , P3 , P1 . The Gantt chart for the schedule is:
P2 P3 P1 0 3 6 30

Waiting time for P1 = 6; P2 = 0; P3 = 3 Average waiting time: (6 + 0 + 3)/3 = 3 Much better than previous case. Convoy effect short process behind long process

Operating System Concepts

Shortest-Job-First (SJR) Scheduling


Associate with each process the length of its next CPU burst. Use these lengths to schedule the process with the shortest time. Two schemes:
nonpreemptive once CPU given to the process it cannot be preempted until completes its CPU burst. preemptive if a new process arrives with CPU burst length less than remaining time of current executing process, preempt. This scheme is know as the Shortest-Remaining-Time-First (SRTF).

Operating System Concepts

SJF is optimal gives minimum average waiting time for a given set of processes.

Example of Non-Preemptive SJF


Process Arrival Time Burst Time P1 0.0 7 P2 2.0 4 P3 4.0 1 P4 5.0 4 SJF (non-preemptive)
P1 0
Operating System Concepts

P3 7 8

P2 12

P4 16

Average waiting time = (0 + 6 + 3 + 7)/4 - 4

Example of Preemptive SJF


Process Arrival Time Burst Time P1 0.0 7 P2 2.0 4 P3 4.0 1 P4 5.0 4 SJF (preemptive)
P1 0 2 P2 4 P3 5 P2 7 P4 11 P1 16

Average waiting time = (9 + 1 + 0 +2)/4 - 3


Operating System Concepts

Priority Scheduling
A priority number (integer) is associated with each process The CPU is allocated to the process with the highest priority (smallest integer | highest priority). Preemptive nonpreemptive 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.
Operating System Concepts

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. Performance q large FIFO q small q must be large with respect to context switch, otherwise overhead is too high.
Operating System Concepts

Example of RR with Time Quantum = 20


Process P1 P2 P3 P4 The Gantt chart is:
P1 0 P2 20 37 P3 P4 57 P1 77 P3

Burst Time 53 17 68 24

P4

P1

P3

P3

97 117

121 134 154 162

Typically, higher average turnaround than SJF, but better response.


Operating System Concepts

Time Quantum and Context Switch Time

Operating System Concepts

Turnaround Time Varies With The Time Quantum

Operating System Concepts

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. 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
Operating System Concepts

Multilevel Queue Scheduling

Operating System Concepts

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: number 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 method used to determine which queue a process will enter when that process needs service
Operating System Concepts

Example of Multilevel Feedback Queue


Three queues:
Q0 time quantum 8 milliseconds Q1 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.

Operating System Concepts

Multilevel Feedback Queues

Operating System Concepts

Multiple-Processor Scheduling
CPU scheduling more complex when multiple CPUs are available. Homogeneous processors within a multiprocessor. Load sharing Asymmetric multiprocessing only one processor accesses the system data structures, alleviating the need for data sharing.
Operating System Concepts

Real-Time Scheduling
Hard real-time systems required to complete a critical task within a guaranteed amount of time. Soft real-time computing requires that critical processes receive priority over less fortunate ones.

Operating System Concepts

Algorithm Evaluation
Deterministic modeling takes a particular predetermined workload and defines the performance of each algorithm for that workload. Queueing models

Operating System Concepts

Threads (1)
Figure 2-6 (a) Three processes each with one thread.

Threads (2)
Figure 2-6 (b) One process with three threads.

Threads (3)

Figure 2-7. The first column lists some items shared by all threads in a process. The second one lists some items private to each thread.

Single and Multithreaded Processes

Benefits
Responsiveness Resource Sharing Economy Utilization of MP Architectures

Types of Threads
User level Threads Kernel level Threads

User level threads Faster to create & manage Implemented by a thread lib at the user level Can run on any OS Multithread application cannot take advantage of multiprocessing Support provided at user level called user level thread

Kernel level threads Slower to create & manage OS support directly to kernel threads Specific to the OS Kernel routines themselves can be multithreaded Support may be provided by kernel is called kernel level threads

Multithreading Models
Many-to-One One-to-One Many-to-Many

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

Many-to-One Model

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

One-to-one Model

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

Many-to-Many Model

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

Two-level Model

Thread Scheduling (1)

(a)

Figure 2-28. (a) Possible scheduling of user-level threads with a 50-msec process quantum and threads that run 5 msec per CPU burst.

Thread Scheduling (2)


Figure 2-28. (b) Possible scheduling of kernel-level threads with the same characteristics as (a).

(b)