Вы находитесь на странице: 1из 17

Process

Process state and Transition The state of a process is defined in an part by the current activity of that process. Definition: - The Process state is an indicator of the nature of the current activity in a process. Table describes four fundamental process states. A Process is in the blocked state if it has made a resources request to the kernel and the request is yet to be granted or if it wishes to wait until some event occurs. The process enters the ready state when the request is granted or the event occurs, and it enters the running state when it is dispatched. The process enters the terminated state when its execution completes or when it is terminated by the OS for some reason. A conventional computer system contains one CPU, so at most one process can be in the running state. Any number of processes can however exist in the waiting, ready and terminated state. State New Running Waiting Description The process is being created A CPU is currently executing instruction in the process code The process has to wait until a resources request made by it granted, or it wishes to wait until a specific event occurs. A CPU should not be allocated to it until its wait is complete. The process wishes to use the CPU to continue its operation; however, it has not been scheduled. The operation of the process, i.e. the execution of the program represented by it, has completed normally, or the OS has aborted it.

Ready Terminated

Process State Transition (Five State Diagram) A state transition for a process Pi is a change in its state. A State transition is caused by the occurrence of some event in the system. When a process Pi in the running state makes an I/O request, it has to enter the waiting state until its I/O operation completes. At the end of the I/O operation Pis state changes from waiting to ready. Similar state changes occur when a process makes some request that cannot be satisfied by the OS straightaway. The process state changes to waiting when the request is made, i.e., when request event occurs, and changes to ready when the request is satisfied. The ready process becomes running when the CPU is allocated to it. A running process becomes ready when it is preempted either because a higher priority process becomes ready or because its time slice elapses. Figure shows a fundamental state transition for a process.

Created By:- Deependra Rastogi

Page 1

Process

Figure indicates the types of events that lead to each state transition for a process; the possible transitions are as follows: State Transition NullNew NewReady Description A new process is created to execute a program. The OS will move a process from the New state to the Ready state when it is prepared to take on an additional process. Most systems set some limit based on the number of existing processes or the amount of virtual memory committed to existing processes. This limit assures that there are not so many active processes as to degrade performance. When it is time to select a process to run, the OS chooses one of the processes in the Ready state. This is the job of the scheduler or dispatcher.

ReadyRunning

RunningTerminated The currently running process is terminated by the OS if the process indicates that it has completed RunningReady The most common reason for this transition is that the running process has reached the maximum allowable time for uninterrupted execution; virtually all multiprogramming operating systems impose this type of time discipline. There are several other alternative causes for this transition, which are not implemented in all operating systems. Of particular importance is the case in which the OS assigns different levels of priority to different processes. Suppose, for example, that process A is running at a given priority level, and process B, at a higher priority level, is blocked. If the OS learns that the event upon which process B has been waiting has occurred, moving B to a ready state, then it can interrupt process A and
Page 2

Created By:- Deependra Rastogi

Process
dispatch process B. We say that the OS has preempted process A. Finally, a process may voluntarily release control of the processor. An example is a background process that performs some accounting or maintenance function periodically RunningWaiting A process is put in the Blocked state if it requests something for which it must wait. A request to the OS is usually in the form of a system service call; that is, a call from the running program to a procedure that is part of the operating system code. For example, a process may request a service from the OS that the OS is not prepared to perform immediately. It can request a resource, such as a file or a shared section of virtual memory that is not immediately available. Or the process may initiate an action, such as an I/O operation, that must be completed before the process can continue. When processes communicate with each other, a process may be blocked when it is waiting for another process to provide data or waiting for a message from another process. Five Major cause of waiting are: Process requests an I/O operation Process requests memory or some other resource Process wishes to wait for a specified interval of time Process waits for message from another process Process wishes to wait for some action b another process BlockedReady A process in the Blocked state is moved to the Ready state when the event for which it has been waiting occurs. For clarity, this transition is not shown on the state diagram. In some systems, a parent may terminate a child process at any time. Also, if a parent terminates, all child processes associated with that parent may be terminated.

ReadyTerminated

BlockedTerminated The comments under the preceding item apply. Process Control Block The process control block ( PCB ) contains all information pertaining to a process that is used in controlling its operation such as its id, priority, state, PSW and contents of CPU registers as also information used in accessing resources and implementing communication with other processes. Figure describes fields of the PCB data structure.

Created By:- Deependra Rastogi

Page 3

Process

The priority and state information is used by the scheduler. It passes the id of the selected process to the dispatcher. For a process that is not in the running state, the PSW and CPU registers fields together contain a snapshot of the CPU when it was last released by the process, i.e., they hold contents of the various control and data register of the CPU when the process got blocked or was preempted. Execution of the process can be resumed by simply loading this information from its PCB into the CPU. This action would be performed the next time this process is to be dispatched. At any given point in time, while the program is executing, this process can be uniquely characterized by a number of elements, including the following: Process State The state may be new, ready, running, and waiting, halted and so on. This counter indicates the address of the next instruction to be executed for this process The register very in number and type, depending on the computer architecture. They include accumulators, index registers, stack pointer, and general purpose register, plus an condition-code information. Along with the program counter, this state information must be saved when an interrupts occurs to allow the process to be continued correctly. This information includes a process priority, pointer to scheduling queues, and any other scheduling parameters.
Page 4

Program Counter

CPU Registers

CPU-Scheduling Information

Created By:- Deependra Rastogi

Process
Memory Management This information may include such information as the value of the Information base and limit registers, the page tables, or the segment tables, depending on the memory system used by the operating system. Accounting Information This information includes the amount of the CPU and real time used, time limits, account numbers, job or process numbers and so on. This information includes the list of I/O devices allocated to the process, a list of open files and so on.

I/O State Information

CPU Switch From Process to Process

Created By:- Deependra Rastogi

Page 5

Process
Scheduler A Process migrates among the various scheduling queues throughout its lifetime. The operating system must select, fro scheduling purposes, processes from these queue in some fashion. The selection process is carried out by the appropriate Scheduler. Types of Scheduler The aim of processor scheduling is to assign processes to be executed by the processor or processors over time, in a way that meets system objectives, such as response time, throughput, and processor efficiency. In many systems, this scheduling activity is broken down into three separate functions: long-, medium-, and short term scheduling. Long-term scheduling is performed when a new process is created. This is a decision whether to add a new process to the set of processes that are currently active. Medium-term scheduling is a part of the swapping function. This is a decision whether to add a process to those that are at least partially in main memory and therefore available for execution. Short-term scheduling is the actual decision of which ready process to execute next Long-term scheduling Medium-term scheduling Short-term scheduling I/O scheduling The decision to add to the pool of processes to be executed The decision to add to the number of processes that are partially or fully in main memory The decision as to which available process will be executed by the processor The decision as to which processs pending I/O request shall be handled by an available I/O device

Created By:- Deependra Rastogi

Page 6

Process

Level of Scheduling Scheduling affects the performance of the system because it determines which processes will wait and which will progress. This point of view is presented in Figure, which shows the queues involved in the state transitions of a process. Fundamentally, scheduling is a matter of managing queues to minimize queuing delay and to optimize performance in a queuing environment. Long-Term Scheduling The long-term scheduler determines which programs are admitted to the system for processing. Thus, it controls the degree of multiprogramming. Once admitted, a job or user program becomes a process and is added to the queue for the short-term scheduler. If the degree of multiprogramming is stable than the average rate of process creation must be equal to the average departure rate of processes leaving the system. Thus the long term scheduler may need to be invoked only when a process leaves the system. Because of the longer
Created By:- Deependra Rastogi Page 7

Process
interval between executions, the long term scheduler can effort to take more time to decide which process should be selected for execution.

Queuing Diagram of Scheduling The long-term scheduler creates processes from the queue when it can. There are two decisions involved here. First, the scheduler must decide when the operating system can take on one or more additional processes. Second, the scheduler must decide which job or jobs to accept and turn into processes. The decision as to when to create a new process is generally driven by the desired degree of multiprogramming. The more processes that are created, the smaller is the percentage of time that each process can be executed (i.e., more processes are competing for the same amount of processor time). Thus, the long-term scheduler may limit the degree of multiprogramming to provide satisfactory service to the current set of processes. Each time a job terminates, the scheduler may decide to add one or more new jobs. Additionally, if the fraction of time that the processor is idle exceeds a certain threshold, the long-term scheduler may be invoked. The decision as to which job to admit next can be on a simple first-come-first served basis, or it can be a tool to manage system performance. The criteria used may include priority, expected execution time, and I/O requirements. For example, if the information is available, the scheduler may attempt to keep a mix of processor bound and I/O-bound processes. Also, the decision may be made depending on which I/O resources are to be requested, in an attempt to balance I/O usage.
Created By:- Deependra Rastogi Page 8

Process
Medium-Term Scheduling The medium term scheduler is the key idea behind a medium term scheduler is that sometimes it can be advantageous to remove processes from memory and thus reduce the degree of multiprogramming. Later the process can be reintroduced into memory and its execution can be continued where it left off. This scheme is called swapping. The process is swapped out, and is later swapped in, by the medium term scheduler. Typically, the swapping-in decision is based on the need to manage the degree of multiprogramming. On a system that does not use virtual memory, memory management is also an issue. Thus, the swapping-in decision will consider the memory requirements of the swapped-out processes. Short-Term Scheduling In terms of frequency of execution, the long-term scheduler executes relatively infrequently and makes the coarse-grained decision of whether or not to take on a new process and which one to take. The medium-term scheduler is executed somewhat more frequently to make a swapping decision. The short-term scheduler, also known as the dispatcher, executes most frequently and makes the fine-grained decision of which process to execute next. The short-term scheduler is invoked whenever an event occurs that may lead to the blocking of the current process or that may provide an opportunity to preempt a currently running process in favor of another. Examples of such events include Clock interrupts I/O interrupts Operating system calls Signals (e.g., semaphores) The short term scheduler or CPU scheduler select from among the processes that are ready to execute and allocates the CPU to one of them. The short term Scheduler must select a new process for the CPU frequently. A Process may execute only a few milliseconds before waiting for an I/O request. Often the short term scheduler executes at least once every 100 milliseconds. Because of the short time between executions the short term scheduler fast. If it take 10 milliseconds to decide to execute a process for 100 milliseconds, then 10/(100+10)= 9 percent of the CPU is being used simply for scheduling the work. Context Switching When an interrupt occurs, the system needs to save the current context of the process currently running o0n the CPU so that it can restore that context when its processing is done,
Created By:- Deependra Rastogi Page 9

Process
essentially, suspending the process and then resuming it. The context is represented in the PCB of the process; it includes the value of the CPU registers, the process state and memory management information. Generally we perform a state save of the current state of the CPU, be it in Kernel or user mode, and then a state restore to resume operations. Switching the CPU to another process requires performing a state save of the current process and a state restore of a different process. This task is known as a Context Switching. When a context switch occurs the kernel save the context of the old process in its PCB and loads the saved context of the new process scheduled to run. Context switch time is pure overhead because of the system does no useful work while switching.

Created By:- Deependra Rastogi

Page 10

Process Scheduling Algorithms


First-Come, First-Served Scheduling (FCFS) CPU-Scheduling algorithm is the first-come, first-served scheduling algorithm, with this scheme, the process that requests the CPU first is allocated the CPU first. The implementation of the FCFS policy is easily managed with a FIFO queue. When a process enters the ready queue, its PCB is linked onto the tail of the queue. When the CPU is free, it is allocated from the process at the head of the queue. The running process is then removed from the queue. The code for FCFS scheduling is simple to write and understand. The average waiting time under the FCFS policy, however, is often quite long. Consider the following set of processes that arrive at time 0, with the length of the CPU burst given in milliseconds:

In addition, consider the performance of FCFS Scheduling in a dynamic Situation. Assume we have one CPU-bound process and many I/O-bound processes. As the processes flow
Created By:- Deependra Rastogi Page 11

Process
around the system, the following scenario may result. The CPU-bound process will get and hold the CPU. During this time, all the other processes will finish their I/O and will move into the ready queue, waiting for the CPU. While the process wait in the ready queue, the I/O devices are idle. Eventually, the CPU-bound process finishes its CPU burst and move to an I/O device. All the I/O-bound processes, which have short CPU bursts, execute quickly and move, back to the I/O queues. At this point, the CPU sites idle. The CPU bound process will then move back to the ready queue and be allocated the CPU. Again all the I/O processes end up waiting in the read queue until the CPU-bound process is done. There is a CONVEY EFFECT as well the other processes wait for the one big process to get off the CPU. The FCFS scheduling algorithm is nonpreemptive. Once the CPU has been allocated to a process, that process keeps the CPU until it releases the CPU, either by terminating or by requesting I/O. Shortest Job Next Scheduling A different approach to CPU scheduling is the shortest-job-next scheduling algorithm. This algorithm associates with each process the length of the processs next CPU burst. When the CPU is available, it is assigned to the process that has the smallest next CPU bursts of two processes are the same, FCFS scheduling is used to break the tie. Note that a more appropriate term for this scheduling method would be the shortest-next-CPU-burst algorithm, because scheduling depends on the length of the next CPU burst of a process, rather than the total length. As an example of SJN scheduling, consider the following set of processes, with the length of the CPU burst given in milliseconds: Process P1 P2 P3 P4 Burst Time 6 8 7 3

Using this scheduling, we would schedule these processes according to the following Gantt chart:

The waiting time is 3 milliseconds fro process P1, 16 milliseconds for process P2, 9 milliseconds for process P3, and 0 milliseconds for process P4. Thus the average waiting time is (3+16+9+0)/4 = 7 milliseconds. By Comparison we were using the FCFS scheduling scheme, the average waiting time would be 10.25 milliseconds.
Created By:- Deependra Rastogi Page 12

Process
The SJN Scheduling algorithm is provably optimal, in that it gives the minimum average waiting time for a given set of processes. Moving a short process before a long one decreases the waiting time of the short process. Consequently the average waiting time decreases. The real difficulty with SJN algorithm is knowing the length of the next CPU request. For long term scheduling in a batch system, we can use as the length the process time limit that a user specifies when he submit the job. Thus users are motivated to estimate the process time limit accurately since a lower value may mean faster response. SJF scheduling is used frequently in long-term scheduling. Although the SJN algorithm is optimal it cannot be implemented at the level of short-term CPU scheduling. There is no way to know the length of the next CPU burst. One approach is trying to approximate SJN Scheduling. We may not know the length of the next CPU burst; we may be able to predict its value. The next CPU burst is generally predicted as an exponential average of the measured lengths of previous CPU bursts. Let tn be the length of the nth CPU burst, and let be our predicted value for the next CPU burst. Then for define

This formula define an exponential average. The value of tn contains our most recent information; store the past history. The parameter controls the relative weight of recent and past history in our prediction. If then and recent history has no effect. If then and only the most recent CPU burst matters. More commonly, so recent history and past history are equally weight. To understand the behavior of the exponential average, we can expend the formula for By substituting for , to find
n+1

tn+(1 - )

tn -1 + +(1 -

)j

tn -1 + +(1 -

)n=1 tn

The SJN algorithm can be either preemptive or nonpreemptive. The choice arises when a new process arrives at the read queue while previous process is still executing. A preemptive SJN algorithm will preempt the currently executing a process, whereas a nonpreemptive SJN algorithm will allow the currently running process to finish its CPU burst. As an example, consider the following four processes, with the length of the CPU burst given in millisecond:
Created By:- Deependra Rastogi Page 13

Process
Process P1 P2 P3 P4 Arrival Time 0 1 2 3 Burst Time 8 4 9 5

If the processes arrive at the ready queue at the times shown and need the indicated burst times, then the resulting preemptive SJN schedule is as depicted in the following Gantt chart:

Waiting time of Processes P1 9 0 = 9 P2 1 1 = 0 P3 17 2 = 15 P4 5 3 = 2 Average Waiting time ( 9 + 0 + 15 + 2)/4 = 6.5 millisecond The resulting nonpreemptive SJN schedule is as depicted in the following Gantt chart:

Waiting time of Processes P1 0 0 = 0 P2 8 1 = 7 P3 17 2 = 15 P4 12 3 = 9 Average Waiting Time ( 0 + 7 + 15 + 9 )/4 = 7.75 millisecond Round Robin Scheduling The round robin scheduling algorithm is designed especially for timesharing systems. It is similar to FCFS scheduling, but preemption is added to switch between processes. A small unit of time, called a time quantum or time slice, is defined. A time quantum is generally from 10 to 100 milliseconds. The ready queue is treated as a circular queue. To implement RR Scheduling, we keep the ready queue as a FIFO queue of processes. New processes are added to the tail of the ready queue. The CPU scheduler picks the first process
Created By:- Deependra Rastogi Page 14

Process
from the ready queue, set a timer to interrupt after 1 time quantum, and dispatches the process. One of two things will then happen. The process may have a CPU burst of less than 1 time quantum. In this case the process itself will release the CPU voluntarily. The scheduler will then proceed to the next process in the ready queue. Otherwise, if the CPU burst of the currently running process is longer than 1 time quantum, the timer will go off and will cause an interrupt to the operating system. A context switch will be executed, and the process will be put at the tail of the ready queue. The CPU scheduler will then select the next process in the ready queue. The average waiting time under the RR policy is often long. Consider the following set of processes that arrive at time 0, with length of the CPU burst given in milliseconds: Process P1 P2 P3 Burst Time 24 3 3

If we use a time quantum of 4 milliseconds, then process P1 gets the first 4 milliseconds. Since it require another 20 milliseconds, it is preempted after the first time quantum, and the CPU is given to the next process in the queue, process P2. Since process P2 does not need to 4 milliseconds it quite before its time quantum expires. The CPU is given to the next process, process P3. Once each process has received 1 time quantum, the CPU is returned to process P1 for an additional time quantum. The resulting RR schedule is

The waiting times of process are 0 P1 6 milliseconds P2 4 milliseconds P3 7 milliseconds Average Waiting Time is ( 6 + 4 + 7)/3 = 5.66 milliseconds 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. Each process must wait no longer than (n-1) * q time units until its next time quantum. For example with five processes and a time quantum of 20 milliseconds each process will get up to 20 milliseconds every 100 milliseconds.

Created By:- Deependra Rastogi

Page 15

Process
The performance of the RR algorithm depends heavily on the size of the size of the time quantum. At one extreme, if the time quantum is extremely large, the RR policy is the same as the FCFS policy. Priority Scheduling The SJN algorithm is a special case of the general priority scheduling algorithm. A priority associated with each process and the CPU allocated to the process with the highest priority. Equal-priority processes are scheduled in FCFS order. An SJN algorithm is simply a priority algorithm where the priority is the inverse of the next CPU burst. The larger the CPU burst the lower the and vice versa. Note that we discuss scheduling in term of high priority and low priority. Priorities are generally indicated by some fixed range of numbers such as 0 to 7 or 0 to 4095. However there is no general agreement on whether 0 is the highest or lowest priority. Some system use low number to represent low priority other use low number for high priority. As an example consider the following set of processes assumed to have arrived at time 0, in the order P1, P2, .., P5 with the length of the CPU burst given in milliseconds: Process P1 P2 P3 P4 P5 Burst Time 10 1 2 1 5 Priority 3 1 4 5 2

Using priority scheduling these processes according to following Gantt Chart:

Waiting Time for the Processes P1 6 milliseconds P2 0 milliseconds P3 16 milliseconds P4 18 milliseconds P5 1 milliseconds The average waiting time is (6 + 0 +16 + 18 +1)/5 = 8.2 milliseconds Priority scheduling can be preemptive or nonpreemptive. When a process arrives at the ready queue, its priority is compared with the priority of the currently running process. A
Created By:- Deependra Rastogi Page 16

Process
preemptive priority scheduling algorithm will preempt the CPU if the priority of the newly arrived process is higher than the priority of the currently running process. A nonpreemptive priority acheduling algorithm will simply put the new process at the head of the ready queue.
Process P1 P2 P3 P4 Arrival time 0 3 5 8 Burst time 4 6 3 2 Priority 2 1 3 1

Non-preemptive Priority Scheduling In this type of scheduling the CPU is allocated to the process with the highest priority after completing the present running process.

Preemptive Priority Scheduling In this type of scheduling the CPU is allocated to the process with the highest priority immediately upon the arrival of the highest priority process. If the equal priority process is in running state, after the completion of the present running process CPU is allocated to this even though one more equal priority process is to arrive.

A major problem with priority scheduling algorithm is indefinite blocking or starvation. A process that is ready to run but waiting for the CPU can be considered blocked. A priority scheduling algorithm can leave some low priority processes waiting indefinitely. In a heavily loaded computer system, a steady stream of higher-priority processes can have prevented a lowpriority process from ever getting the CPU. Generally one of two things will happen. Either the process will eventually be run or the computer system will eventually crash and lose all unfinished low-priority processes. Example:- Rumor has it that when they shut down the IBM 7094 at MIT in 1973, they found a low priority process that had been submitted in 1967 and had not yet been run. A Solution to the problem of indefinite blocking of low-priority processes is aging. Aging is technique of gradually increasing the priority of processes that wait in the system for a long time.

Created By:- Deependra Rastogi

Page 17

Вам также может понравиться