50
8/11/2019 Theory of Operating Systems http://slidepdf.com/reader/full/theory-of-operating-systems 1/50 OPERATING SYSTEMS THEORY LECTURE NOTES Chapter 2 PROCESS MANAGEMENT

Theory of Operating Systems

Embed Size (px)

Citation preview

Page 1: Theory of Operating Systems

8/11/2019 Theory of Operating Systems

http://slidepdf.com/reader/full/theory-of-operating-systems 1/50

OPERATING SYSTEMSTHEORY

LECTURE NOTES

Chapter 2PROCESS MANAGEMENT

Page 2: Theory of Operating Systems

8/11/2019 Theory of Operating Systems

http://slidepdf.com/reader/full/theory-of-operating-systems 2/50

Page 3: Theory of Operating Systems

8/11/2019 Theory of Operating Systems

http://slidepdf.com/reader/full/theory-of-operating-systems 3/50

Processes versus Programs

Normally (in so-called sequential  programs)there is exactly one process per program, butin concurrent  programs, there may be severalprocesses executing the same program

 A process is more than a program code. Aprocess is an 'active' entity as opposed to aprogram which is considered to be a 'passive'entity.

 A program is an algorithm expressed in somesuitable notation, (e.g., programminglanguage). Being passive, a program is only apart of a process.

Page 4: Theory of Operating Systems

8/11/2019 Theory of Operating Systems

http://slidepdf.com/reader/full/theory-of-operating-systems 4/50

Processes versus Programs…. 

 A process, on the other hand, includes:

Current value of program counter

Contents of the processors registers

Value of the variables The process stack which typically contains temporary

data such as subroutine parameter, return address,

and temporary variables.

 A data section that contains global variables, etc.

When you execute a program you have just

compiled, the OS generates a process to run

the program.

Page 5: Theory of Operating Systems

8/11/2019 Theory of Operating Systems

http://slidepdf.com/reader/full/theory-of-operating-systems 5/50

Threads

 Associated with a process are its code, its data, the

operating resources allocated to it, and one or more

threads  of execution.

 A thread is a flow of execution through the process’s

code, with its own program counter, system registers

and stack.

Typically, an OS will have a separate thread for each

distinct activity.

In particular, the OS will have a separate thread for

each process, and that thread will perform operating

system activities on behalf of the process.

Page 6: Theory of Operating Systems

8/11/2019 Theory of Operating Systems

http://slidepdf.com/reader/full/theory-of-operating-systems 6/50

Process-thread classification

Operating systems can be classified as one of

three types of systems: 

single process single threaded, eg, DOS

multi-process single-threaded, eg, Unix multi-process multi-threaded, eg. Windows 2000

The simplest arrangement is single-process

single threaded where only one process can

execute at a time. Once a process isallocated to the CPU, that process runs to

completion before any other process can

execute. Eg, DOS

Page 7: Theory of Operating Systems

8/11/2019 Theory of Operating Systems

http://slidepdf.com/reader/full/theory-of-operating-systems 7/50

CPU utilization

Multiprocessing systems are far more practical.

They allow users to perform multiple concurrent

tasks and they make more efficient use of

computer resources. CPUs execute much faster than I/O devices.

Millions of instructions can be executed in the

time it may take to satisfy a single I/O request.

In a single-process environment, the CPU sitsidle, waiting for the I/O request to be satisfied.

For a typical program, the CPU might be idle for

50% of the time.

Page 8: Theory of Operating Systems

8/11/2019 Theory of Operating Systems

http://slidepdf.com/reader/full/theory-of-operating-systems 8/50

CPU utilization…. 

Multiprocessing systems allow the CPU tobe shared by multiple processes.

The system interleaves the execution of the

processes, allowing each one theopportunity to periodically use the CPU.

If one process is idle, waiting for an I/Orequest to be satisfied, another process not

waiting for I/O may be allocated to the CPU. With enough memory and careful tuning,

CPU utilization approaching 100% can beachieved.

Page 9: Theory of Operating Systems

8/11/2019 Theory of Operating Systems

http://slidepdf.com/reader/full/theory-of-operating-systems 9/50

Process Scheduling 

On multiprocessing systems, scheduling

is used to determine which process is

given control of the CPU.

Scheduling may be divided into three

phases: 

long-term (job scheduling),

medium-term (swapping), and

short-term (dispatching).

Page 10: Theory of Operating Systems

8/11/2019 Theory of Operating Systems

http://slidepdf.com/reader/full/theory-of-operating-systems 10/50

Long-term (Job scheduling)

Job scheduling, determines which jobs or processes

may compete for systems resources.

Typically, once the job scheduler makes a job active,

it stays active until it terminates.

The main objective of the job scheduler is to provide

the medium-term scheduler with an appropriate

number of jobs.

Too few jobs, and the CPU may sit idle because

all jobs are blocked.

Too many jobs, and the memory management

system becomes overloaded, degrading system

efficiency.

Page 11: Theory of Operating Systems

8/11/2019 Theory of Operating Systems

http://slidepdf.com/reader/full/theory-of-operating-systems 11/50

Medium-term (Swapping)

The swapper , swaps processes in and

out of memory.

 Any memory management system that

supports multiprocessing can use

swapping to allow more processes to

share a system that can physically fit in

memory.

Page 12: Theory of Operating Systems

8/11/2019 Theory of Operating Systems

http://slidepdf.com/reader/full/theory-of-operating-systems 12/50

Short-term (Dispatching)

The dispatcher , allocates the CPU to a

process that is loaded into main memory

and ready to run.

Typically, the dispatcher allocates the CPUfor a fixed maximum amount of time.

 A process that must release the CPU after

exhausting its time slot returns to the poolof processes from which the dispatcher

selects the process to execute.

Page 13: Theory of Operating Systems

8/11/2019 Theory of Operating Systems

http://slidepdf.com/reader/full/theory-of-operating-systems 13/50

Process States 

 At its simplest, scheduling can consist of just short-term scheduling. On such a system a process existsin one of four states:

 Blocked : A process that is waiting for some event to

occur before it can continue executing. Mostfrequently, this event is completion of an I/O operation.Blocked processes do not require the services of theCPU since the execution cannot proceed until theblocking event completes.

 Ready : A process that is not allocated to a CPU but isready to run. A ready process could execute ifallocated to a CPU.

 Running : A process that is executing on a CPU. If thesystem has n CPUs, at most n processes may be in

the running state.

Page 14: Theory of Operating Systems

8/11/2019 Theory of Operating Systems

http://slidepdf.com/reader/full/theory-of-operating-systems 14/50

Process States….. 

and Term inated : A process that has halted its execution

but a record of the process is still maintained by theoperating system (on Unix, these are referred to aszombie processes).

The operating system may wish to retain informationon a terminated process for a variety of reasons: the process has an I/O operation pending

completion,

it has child processes that are still executing and the

system need to retain information about the processthat created them,

or it has a parent or related process that in the futuremay request information about the terminatedprocess.

Page 15: Theory of Operating Systems

8/11/2019 Theory of Operating Systems

http://slidepdf.com/reader/full/theory-of-operating-systems 15/50

Process States….. 

Medium-term scheduling adds two process states:

 Swapped-Blo cked : A process that is waiting for some

event to occur and has been swapped out to

secondary storage.

 Swapped-Ready : A process that is ready to run but isswapped out to secondary storage. A swapped-ready

process may not be allocated to the CPU since it is not

in main memory.

The important characteristic of a process in aswapped state is its absence from the pool of

processes considered for allocation to a CPU by the

short-term scheduler.

Page 16: Theory of Operating Systems

8/11/2019 Theory of Operating Systems

http://slidepdf.com/reader/full/theory-of-operating-systems 16/50

Process States….. 

Long-term scheduling also adds the

following state:

 Held : A process that has been created but

will not be considered for loading intomemory or for execution.

Typically, only new processes can become

held processes.

Once a process leaves the held state, it

does not return to it.

Page 17: Theory of Operating Systems

8/11/2019 Theory of Operating Systems

http://slidepdf.com/reader/full/theory-of-operating-systems 17/50

State Transitions

Depending on the number of schedulers

implemented, and therefore the number of

states present, a number of different state

models may be created to describe asystem’s behaviour.

Figure 2-1 depicts the complete seven-

state model and the transitions that occurbetween the states.

Page 18: Theory of Operating Systems

8/11/2019 Theory of Operating Systems

http://slidepdf.com/reader/full/theory-of-operating-systems 18/50

State Transitions…. 

1) Process Creation Held: Upon creation, aprocess is put into the held state.

2) Held Swapped-Ready or

Held Ready:

 A process is activated, making another processavailable for management by the medium-termscheduler.

Since there may be insufficient memory for another

swapped –in process, held processes may becomeswapped-ready processes.

 An operating system has the option of swapping inany new process and swapping out an older processif there is insufficient memory.

Page 19: Theory of Operating Systems

8/11/2019 Theory of Operating Systems

http://slidepdf.com/reader/full/theory-of-operating-systems 19/50

State Transitions…. 

3) Swapped-Ready Ready: A process isswapped in when sufficient memory for it becomesavailable. This can happen for a number of reasons:

The medium-term scheduler made room available

by swapping out other processes, processes released memory previously allocated

to them,

processes terminated releasing all memoryallocated to them, or

the amount of memory allocated to variousprocesses is reduced based on changing systemconditions.

Page 20: Theory of Operating Systems

8/11/2019 Theory of Operating Systems

http://slidepdf.com/reader/full/theory-of-operating-systems 20/50

State Transitions…. 

4) Swapped-Blocked Blocked: A system

can also decide to swap in blocked processes

(although not often)

 A blocked process cannot execute, so placing it in

memory appears to waste that resource.

In some contexts, the process may be a high-priority

process

or the system may be lightly loaded with significant,

unused memory available.

In either case, moving a swapped-blocked process to

the blocked state allows the process to execute

sooner, once the blocking condition is satisfied.

Page 21: Theory of Operating Systems

8/11/2019 Theory of Operating Systems

http://slidepdf.com/reader/full/theory-of-operating-systems 21/50

State Transitions…. 

5) Ready Swapped  –Ready orBlocked Swapped-Blocked:

The medium-term scheduler swaps out

processes to release memory resources forother processes.

This may be done because other processeshave requested additional memory or

Because the amount of memory that must beallocated to various processes has increaseddue to changing system conditions.

Page 22: Theory of Operating Systems

8/11/2019 Theory of Operating Systems

http://slidepdf.com/reader/full/theory-of-operating-systems 22/50

State Transitions…. 

6) Swapped-Blocked Swapped-Readyor   Blocked Ready:

 A process in one of the blocked states is

waiting for some event to occur. When thatevent occurs, the process is moved to thecorresponding ready state.

7) Ready Running: When no process

is allocated to the CPU, the short-termscheduler selects the next running processfrom the pool of ready processes.

Page 23: Theory of Operating Systems

8/11/2019 Theory of Operating Systems

http://slidepdf.com/reader/full/theory-of-operating-systems 23/50

State Transitions…. 

8) Running Ready: Processes are allocatedthe CPU for maximum amount of time or t ime sl ice. 

If a process does not become blocked or voluntarilyrelease the CPU prior to the end of its time slice, a

timer run-out (TRO) interrupt will transfer control tothe OS and the short-term scheduler.

The short-term scheduler then moves the process tothe ready state and chooses which process to runnext.

It is also possible that the transition of a high-priorityprocess from a blocked state will trigger the shortterm scheduler to preempt a low-priority runningprocess.

Page 24: Theory of Operating Systems

8/11/2019 Theory of Operating Systems

http://slidepdf.com/reader/full/theory-of-operating-systems 24/50

State Transitions…. 

9) Running Blocked: Tasks such asperforming I/O operations, communicatingwith other processes, or waiting on systemconditions are controlled by factors external

to the process. In most of these cases, continued execution

of the process must be suspended pendingnotification that the task has been completed.

To prevent the CPU from being idle while theprocess waits on an event, the process ismoved to the blocked state so the CPU maybe allocated to a runable process.

Page 25: Theory of Operating Systems

8/11/2019 Theory of Operating Systems

http://slidepdf.com/reader/full/theory-of-operating-systems 25/50

State Transitions…. 

10) Running Terminated: Executing

processes terminate by either performing

some illegal action or by normally completing

the execution of their code. 11) Any State Terminated:  A

process can be terminated by some action

external to the process. No matter what state

a process is in, the OS or another processmay be able to terminate the process, moving

it to the terminated state.

Page 26: Theory of Operating Systems

8/11/2019 Theory of Operating Systems

http://slidepdf.com/reader/full/theory-of-operating-systems 26/50

Scheduling Criteria 

The goal of a scheduling algorithm is to identify theprocess whose selection will result in the “best”possible system performance. Listed here are somecommonly used scheduling criteria:

CPU Utilization: The percentage of time the CPU isexecuting a process. The system load affects thelevel of utilization; high utilization is more easilyachieved on more heavily loaded systems.

The importance of this criterion typically varies

depending on the degree the system is shared. On a single-user system, CPU utilization is relatively

unimportant.

On a large, expensive time-shared system, CPU use

may be the primary consideration.

Page 27: Theory of Operating Systems

8/11/2019 Theory of Operating Systems

http://slidepdf.com/reader/full/theory-of-operating-systems 27/50

Scheduling Criteria…. 

Balanced Utilization: The percentage of time allresources are utilized. Instead of just evaluating CPUutilization, utilization of memory, I/O devices, and othersystem resources are also considered. 

Throughput: The number of processes the system canexecute in a period of time. Evaluation of throughputmust consider the average length of a process. Onsystems with long processes, throughput will be lessthan on systems with short processes. 

Turnaround Time: The average period of time it takesa process to execute. This includes all the time itspends in the system (computed by subtracting the timethe process was created from the time it wasterminated). Turnaround time is inversely proportional to

throughput.

Page 28: Theory of Operating Systems

8/11/2019 Theory of Operating Systems

http://slidepdf.com/reader/full/theory-of-operating-systems 28/50

Scheduling Criteria…. 

Wait Time: The average period of time a process spendswaiting. One deficiency of turnaround time as a measureof performance is that the time a process spendsproductively computing increases the turnaround time,lowering the performance evaluation. Wait time presents a

more accurate measure of performance because it doesnot include the time a process is executing on the CPU orperforming I/O; it includes only the time a process spendswaiting. 

Response Time: On interactive systems, the average

time it takes the system to start responding to user inputs.On a system where there is a dialog between process anduser, turnaround time may be mostly dependent on thespeed of the user’s responses and relatively unimportant.Of more concern is the speed with which the system

responds to each users input.

Page 29: Theory of Operating Systems

8/11/2019 Theory of Operating Systems

http://slidepdf.com/reader/full/theory-of-operating-systems 29/50

Scheduling Criteria…. 

Predictability: Users prefer consistency. Eg, aninteractive system that routinely responds within asecond, but on occasion takes 10 secs to respond,may be viewed more negatively than a system that

consistently responds in 2 secs. Although averageresponse time in the latter system is greater, usersmay prefer the system with greater predictability. 

Fairness: The degree to which all processes aregiven equal opportunity to execute. In particular, aprocess should not suffer from starvat ion, ie, if itbecomes stuck in a scheduling queue indefinitely. 

Priorities: Give preferential treatment to processeswith higher priorities.

Page 30: Theory of Operating Systems

8/11/2019 Theory of Operating Systems

http://slidepdf.com/reader/full/theory-of-operating-systems 30/50

Scheduling Algorithms 

In the early days of computing, scheduling

was mostly nonpreempt ive;  a process

retained control of the CPU until the process

blocked or terminated (suitable for batch jobs where response time was of no

concern).

On today’s interactive systems, preempt ive  

scheduling is used. The scheduler may

preempt a process before it blocks or

terminates, in order to allocate the CPU to

another process.

Page 31: Theory of Operating Systems

8/11/2019 Theory of Operating Systems

http://slidepdf.com/reader/full/theory-of-operating-systems 31/50

Scheduling Algorithms….. 

Scheduling algorithms identify the processwhose selection will result in the “best”possible system performance.

Six scheduling algorithms have seenwidespread use.

First-Come First-Served Scheduling.

Shortest Job First Scheduling.

Shortest Remaining Time Scheduling. Round Robin Scheduling.

Priority Scheduling.

Multilevel Feedback Queue Scheduling

Page 32: Theory of Operating Systems

8/11/2019 Theory of Operating Systems

http://slidepdf.com/reader/full/theory-of-operating-systems 32/50

First-Come First-Served 

The simplest scheduling algorithm

Jobs are scheduled in the order they arereceived.

FCFS is non-preemptive.

Implementing a queue of the processes to bescheduled or by storing the time the processwas received and selecting the process with

the earliest time easily accomplishimplementation.

FCFS favours CPU-bound processes.

Page 33: Theory of Operating Systems

8/11/2019 Theory of Operating Systems

http://slidepdf.com/reader/full/theory-of-operating-systems 33/50

Shortest Job First 

 Another non-preemptive algorithm.

It selects the job processes with the shortest

expected processing time.

In case of a tie, FCFS scheduling can beused.

SJF favors short jobs over long ones.

In extreme cases, the constant arrival of small

 jobs can lead to starvation of long jobs.

Page 34: Theory of Operating Systems

8/11/2019 Theory of Operating Systems

http://slidepdf.com/reader/full/theory-of-operating-systems 34/50

Shortest Remaining Time 

 A preemptive version of Shortest Job First.

 Any time a new process enters the pool of

processes to be scheduled, the scheduler

compares the expected value for itsremaining processing time with that of the

process currently scheduled.

If the new process’s time is less, the currently

scheduled process is preempted.

Like SJF, SRT favours short jobs and long

 jobs can be victims of starvation.

Page 35: Theory of Operating Systems

8/11/2019 Theory of Operating Systems

http://slidepdf.com/reader/full/theory-of-operating-systems 35/50

Page 36: Theory of Operating Systems

8/11/2019 Theory of Operating Systems

http://slidepdf.com/reader/full/theory-of-operating-systems 36/50

Page 37: Theory of Operating Systems

8/11/2019 Theory of Operating Systems

http://slidepdf.com/reader/full/theory-of-operating-systems 37/50

Multilevel Feedback Queues 

The major disadvantage with the five

preceding algorithms is that they basically

treat all jobs the same.

 An algorithm using Mult i level FeedbackQueues  (MFQ) addresses this deficiency by

customizing the scheduling of a process

based on the process’s performance

characteristics.

This algorithm is however not discussed

here.

Page 38: Theory of Operating Systems

8/11/2019 Theory of Operating Systems

http://slidepdf.com/reader/full/theory-of-operating-systems 38/50

Process Attributes

The OS stores information about a process in a

Process Contro l Block (PCB). The PCB contains

the information / attributes the OS needs to know

about a process:

Process state and scheduling information,

Memory management information,

Hardware run-state information,

Process signaling information,

 Access control information,

I/O and allocated resource information, and

Other accounting information.

Page 39: Theory of Operating Systems

8/11/2019 Theory of Operating Systems

http://slidepdf.com/reader/full/theory-of-operating-systems 39/50

Run State And Scheduling

In addition to knowing the state of a process,the OS must identify all the processes in aparticular state.

This involves maintaining separate list orqueues for each state.

For processes that are in the blocked state,information on the blocking event must bemaintained.

Information used by scheduling algorithms,such as priority level or time of last allocationto the CPU, can be found in the PCB.

Page 40: Theory of Operating Systems

8/11/2019 Theory of Operating Systems

http://slidepdf.com/reader/full/theory-of-operating-systems 40/50

Memory Management

Each time a process is given control of the

CPU, the memory management hardware must

be configured so that only the memory

locations allocated to that process may be

accessed.

For each process, the OS must maintain a copy

of the memory configuration for that process.

When a process obtains the CPU, a register isset to point to the process’s configuration

settings, or the configuration values are copied

into special memory management registers.

Page 41: Theory of Operating Systems

8/11/2019 Theory of Operating Systems

http://slidepdf.com/reader/full/theory-of-operating-systems 41/50

Hardware State

On a multi-programmed machine, when a process is

given control of the CPU, various registers must be

restored to the values they had when the process

was suspended.

The OS must store these values for each processthat is not in the run state.

This may include values for the program counter,

accumulator registers, general registers, stack

pointers, index registers, arithmetic logic unitcondition codes, and execution priority level.

Resetting all the values when a new process takes

control of the CPU is called a context-swi tch.

Page 42: Theory of Operating Systems

8/11/2019 Theory of Operating Systems

http://slidepdf.com/reader/full/theory-of-operating-systems 42/50

Signaling 

 An OS may support some form of process signaling.

 A signal is a virtual interrupt created by theoperating system.

 A signal may originate with a process or with the OS

and is sent to one or more processes.  A process receiving a signal may respond by

ignoring it, by terminating, or by executing aspecified function.

The OS must maintain information on how aprocess will respond should it receive signals.

It may also need to maintain a list of signals thathave been sent to a process but have not yet beenserviced.

Page 43: Theory of Operating Systems

8/11/2019 Theory of Operating Systems

http://slidepdf.com/reader/full/theory-of-operating-systems 43/50

Access Control 

Two types of access control informationmay be stored:

information that determines the access

rights of the process and information on the default access rights

assigned to any object created by thisprocess.

Depending on the access controlmechanism used, this information may bepasswords, capabilities, or access controllists.

Page 44: Theory of Operating Systems

8/11/2019 Theory of Operating Systems

http://slidepdf.com/reader/full/theory-of-operating-systems 44/50

Input And Output 

The OS must maintain information on

files and devices allocated to a process

or opened for I/O by a process.

On most systems, a file or device mustbe opened before it can be accessed. An

open file table stores information on each

file or device opened by a process. Other information might include the

current working directory of a process or

a per-process root directory.

Page 45: Theory of Operating Systems

8/11/2019 Theory of Operating Systems

http://slidepdf.com/reader/full/theory-of-operating-systems 45/50

Other information

Process id:  A process’s unique identification no. 

Parent process id: The identification number of theparent process. When a process is created, thecreating process is the parent; the created process is

the child. Elapsed CPU time: Total CPU time used by a

process to date.

Maximum CPU time: Maximum amount of CPU timea process may use.

Priority level: A factor in any of the schedulingdecisions may be a processed priority level.

Memory allocation:  Total amount of memoryallocated to a process, etc, etc.

Page 46: Theory of Operating Systems

8/11/2019 Theory of Operating Systems

http://slidepdf.com/reader/full/theory-of-operating-systems 46/50

Process Supervisor Calls: Creation

The OS must provide a mechanism for

creating processes.

When a process is created, the OS must

create a PCB for that process. Initial settings for information in the PCB may

be determined from parameters specified in

the supervisor call that created the process.

Overall resources available to a process may

be limited to a subset of those available to the

parent process or user.

Page 47: Theory of Operating Systems

8/11/2019 Theory of Operating Systems

http://slidepdf.com/reader/full/theory-of-operating-systems 47/50

Process Supervisor Calls: Termination

Mechanisms for terminating a process include: an exit  supervisor call that terminates the calling

process,

an abort  call that allows one process to terminate

another, and operating system in tervent ion  should the process

attempt some restricted activity.

The processes that are permitted to abortanother process are typically limited to the

parent process, processes in the same group,processes associated with the user, and/orprocesses associated with privileged systemusers.

Page 48: Theory of Operating Systems

8/11/2019 Theory of Operating Systems

http://slidepdf.com/reader/full/theory-of-operating-systems 48/50

Process Supervisor Calls: Termination…. 

On some systems, when a parent process

terminates, all its children are

automatically terminated. The children

would then have their children terminated,a process known as cascading

terminat ion.

Page 49: Theory of Operating Systems

8/11/2019 Theory of Operating Systems

http://slidepdf.com/reader/full/theory-of-operating-systems 49/50

Process Supervisor Calls: Wait

It is also possible for an OS to suspend

execution of a parent while the child

executes.

On such systems, a wait  call allows theparent to suspend itself until one or more

of its children terminates.

The wait call may also provide amechanism for a termination status to be

passed back from the child to the parent.

Page 50: Theory of Operating Systems

8/11/2019 Theory of Operating Systems

http://slidepdf.com/reader/full/theory-of-operating-systems 50/50

Process Supervisor Calls: Others

In addition to calls to create and

terminate processes, supervisor calls

may exist for:

sending and controlling the receipt ofsoftware signals,

synchronizing concurrent processes,

sending communications betweenprocesses, etc.