0% found this document useful (0 votes)
21 views23 pages

M2

OS module 2 VTU BCS302

Uploaded by

abhinavhs3124
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views23 pages

M2

OS module 2 VTU BCS302

Uploaded by

abhinavhs3124
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 23

Operating Systems

PROCESS MANAGEMENT
A program is a set of instructions which is stored on a disk. A process is a program loaded into
memory for execution. Thus a program can be termed as passive entity and a process as an active
entity.
In case of C program, a program is written and stored in .c extension. When this .c extension file is
compiled, it is converted into machine language .o executable file. These are all programs, when this
.o file is loaded into memory for execution it is called as process.
Fig 2.1 shows a process in memory. When a process is created, these are the fields present in a
process. A text section contains the program code to be executed, also contains program counter
which will give next instruction to be executed along with a set of registers. The data section
contains the global variables declared for the program. The stack section consists of function
parameters, return addresses, local variables etc. The heap section is a memory which is allocated
dynamically during the process runtime.

Fig 2.1: Process in memory


A program can have a single process or multiple processes. If a program has multiple processes
running, then multiple execution sequences are created. Several users may be running same mail
program or single may run several copies of web browsers. Each of these is a separate process, here
the text section will be same but the data, stack and heap section vary.
Process State:
As the process runs, it changes its state. The state of a process gives information about the current
activity of the process. Each process may be in one of the following state as shown in Fig 2.2.

 New: Process is being created.


 Ready: The process is waiting to be assigned to a processor.
 Running: The instructions are being executed.
Operating Systems

Fig 2.2 Process states

 Waiting: The process is waiting for some event to occur (such as I/O completion or
reception of a signal)
 Terminated: The process has finished its execution.
Several processes may be in ready or waiting state but only 1 process will be in running state per
processor.
Process Control Block:
The process control block is a data structure in operating system kernel containing information
needed to manage a particular process. It is also called as task control block.

Fig 2.3: Process Control Block


A PCB is shown in Fig 2.3, it contains many information associated with a specific process like:

 Process State: The state may be new, ready, running, waiting and so on.
 Program Counter: Indicates the address of next instruction to be executed for this process.
 Registers: The registers vary in number and type depending on the computer architecture.
They include accumulators, index registers, stack pointers and general purpose registers.
When an interrupt occurs, the program counter and state information of these registers
must be saved to allow a process to be continued correctly afterwards as shown in Fig 2.4
 CPU scheduling information: Includes process priority, pointers to scheduling queues and
any other scheduling parameters.
Operating Systems

Fig 2.4: CPU switch from process to process

 Memory Management information: The information may include the value of base and limit
registers, the page tables or segment tables.
 Accounting information: This information includes the amount of CPU and real time used,
time limits, account numbers, job or process numbers and so on.
 I/O status Information: Information includes list of I/O devices allocated to the process, a
list of open files and so on.
PCB is a repository for information that may vary from process to process.
A process is a program that performs single thread of execution or multiple threads of execution.
On a system that allows thread, a PCB is expanded to include information for each thread.
Process Scheduling:
The objective of multiprogramming is to have some process running at all to time to maximize the
CPU utilization. The objective of time sharing is to switch the CPU among processes so frequently
that users can interact with each program while it is running. To meet these objectives, the process
scheduler selects an available process for program execution on the CPU.
Scheduling Queues:
As process enters the system, they are put into the job queue which consists of all processes in the
system. The processes that are in main memory and are ready and waiting to execute are kept on a
list called the ready queue.
As shown in Fig 2.5, this queue is generally stored as a linked list. A ready queue header contains
pointers to the first and final PCB’s in the list. Each PCB includes a pointer field that points to the
next PCB in the ready queue. The system also includes other queues. When a process is allocated
the CPU, it executes for a while and eventually quits, is interrupted or waits for the occurrence of a
particular event such as the completion of an I/O request. Suppose the process makes an I/O
request to a shared device, like disk.
Operating Systems

Fig 2.5: Ready Queue and various I/O device queues


The disk may be busy in job given by other process as it is shared. Since it is busy, the process may
have to wait to get access to disk. The list of processes waiting for a particular I/O device is called
device queue. Each device has its own queue.

Fig 2.6: Queueing Diagram for process scheduling


A common representation of process scheduling is done by using queueing diagram. Here the
rectangular box contains queues and circle represents the resources which serves the queues.
A new process is kept in ready queue and it waits there until it is selected for execution. Once CPU is
allocated, one of the following events occurs

 Process could issue I/O request and then be placed in an I/O queue.
 Process could create sub-process and wait for it to finish its execution.
 Process could be removed forcibly from the CPU, due to interrupt and be put back in ready
queue.
Operating Systems

In the first 2 cases, the process switches from waiting state to ready state and is then put back in
ready queue. A process continues this cycle until it terminates, once it terminates it is removed
from all queues and PCB and its resources are deallocated.
Schedulers:
A process migrates from one queue to another during its execution. For scheduling purpose the
operating system must select the processes from these queues. This selection process is carried out
by appropriate scheduler.
Often more processes are submitted than can be executed immediately. These processes are
spooled to a mass storage device where they are kept for later execution. 2 types of scheduler are
used.

 The long term scheduler selects process from this pool and loads them into memory for
execution.
 The short term scheduler selects from among the processes that are ready to execute and
allocates CPU to one of them.
 The frequency of selection between these 2 schedulers vary. The short term scheduler must
select new process frequently i.e it selects once I every 100 milliseconds. The long term
scheduler selects new processes less frequently minutes may separate the creation of new
process and the next.
 The processes can be divided into I/O bound process which spends more time in doing I/O
than it spends doing computations and CPU bound process generates I/O requests less
frequently but does CPU more computations. Long term scheduler must select the processes
carefully to have good mix.
Some operating systems introduces intermediate level of scheduling such as in time sharing
systems.

Fig 2.7: Medium Term scheduling


The Fig 2.7 shows a medium term scheduler used in time sharing operating systems. The idea to
use medium term scheduler is to remove some 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. Medium term scheduler swaps in and
swaps out the process from memory to improve the process mix.
Switching the CPU into another process requires performing a state save of current process and a
state restore of different process. This task is known as context switch. When a context switch
occurs, the kernel saves the state information of old process in its PCB and loads the saved context
Operating Systems

of new process scheduled to run. Context switch time is pure overhead as it does no useful work
while switching.
Operations on Processes:
A process may create several processes via create process system call during the course of
execution. The creating process is called parent process and the created process is called child
process. Each of these child processes inturn creates several processes which forms a tree like
structure. Each of these processes are identified by using process identifier (pid).
Whenever a process is created it needs several resources (CPU time, memory, files, I/O devices) for
its execution. When a process creates subprocess, the subprocess may be able to obtain the
resources from the operating system directly or it may be constrained to subset of resources of
parent process.
When a process creates a new process, 2 possibilities exists

 The parent continues to execute concurrently with its children.


 The parent waits until some or all of its children have terminated.
The address space of new process can be

 The child process is a duplicate of parent process (same program and data)
 The child process has a new program loaded into it.
In UNIX, each process has a unique id i.e pid, a new process is created by using fork ( ) system call.
The new process consists of same address space as of parent process. This makes it easier for
parent process to communicate with child. When a fork ( ) call is made, it returns 2 values i.e zero to
child process and process id (PID) of child process to parent process.
Exec( ) system call is used after fork ( ) call, exec ( ) call is used to load new program into the
process. The exec ( ) system call loads a binary file into memory and starts its execution. The parent
can then create more children or if it has nothing to do while a child runs, it can issue a wait ( )
system call to move itself out of ready queue until termination of child.

Fig 2.8: Process creation using fork ( ) call


Fig 2.8 shows the process creation using fork ( ) call where after fork ( ) call, the parent executes
wait ( ) call to wait for the child process to finish the job. Meanwhile the child process executes exec
( ) call and once its work is finished it calls exit ( ) call to terminate itself.
A child process asks operating system to delete it by calling exit ( ) system call. When it is called, the
child process returns a status value to parent process. All the resources of the process like physical
and virtual memory, open files, I/O buffers are deallocated by the operating system.
Operating Systems

A parent may terminate the execution of one of its child process for various reasons like:

 The child has exceeded the usage of resources that it has been allocated.
 The task assigned to child is no longer required.
 The parent is exiting and the operating system does not allow child to continue without the
parent.
In some of the systems when a parent terminates, along with it the child process will also be
terminated. This phenomenon is referred to as cascading termination, normally initiated by the
operating system.
Interprocess Communication:
Process executing concurrently in the operating system may be independent processes or
cooperating processes.
A process is independent if it does not affect or get affected by any other process executing in the
system. Any process that does not share the data with any process is independent.
A process is cooperating if it gets affected or if it affects any other process executing in the system.
Any process that shares the data with other processes is a cooperating process.
There are several reasons for providing an environment that allows process cooperation:

 Information Sharing: Several users may be interested in same information, we must provide
an environment to allow concurrent access.
 Computation Speedup: If we want a particular task to run faster, break it into subtasks each
of which will be executing in parallel with others.
 Modularity: To construct a system in a modular fashion dividing the system functions into
separate processes or threads.
 Convenience: Individual user may work on many tasks at same time.
Cooperating processes require an interprocess communication mechanism that will allow them to
exchange data and information. There are 2 fundamental models of interprocess communication:
1. Shared Memory
2. Message passing
Operating Systems

Fig 2.9: a) Message passing model b) Shared memory model


In shared memory model, a region of memory that is shared by the cooperating processes is
established. Processes can exchange the information by reading and writing data into the
shared region.
Advantages:

 Maximum speed and convenience of communication


 Faster than message passing as message passing uses many system calls
 Uses system call to establish shared memory region.
In the message passing model, communication takes place by means of messages exchanged
between the processes.
Advantages:

 Useful for exchanging small amount of data


 Easier to implement than shared memory for inter-computer communication.
Shared memory systems:
In shared memory systems, a region of memory is established. This region resides in the address
space of process creating the shared memory segment. The other process which wish to
communicate must attach this region to its address space. Since operating systems does not allow
one process to access memory of other process, the processes must agree to remove this restriction
in shared memory system.
The form of data and location is determined by processes and operating system is not having
control over it. These processes are also responsible to not write at same location simultaneously.
Producer - Consumer Problem:
A producer process produces information that is consumed by consumer process. Producer
consumer problem uses shared memory. To allow both of them to run concurrently, a buffer of
items is made available to producer to fill and the consumer to empty it. This buffer resides in
region of shared memory by producer and consumer processes. A producer can produce one item
Operating Systems

while the consumer is consuming another item. They must be synchronized, so the consumer does
not try to consume the item which is not yet produced.
So, 2 types of buffer can be used.

 The unbounded buffer does not have limit on the size of buffer. Here consumer may have to
wait for new items but the producer can always produce new items.
 The bounded buffer assumes a fixed buffer size. Here the consumer must wait if the buffer is
empty and producer must wait if buffer is full.
Message Passing Systems:
The cooperating processes communicate with each other by passing message with the help of
operating system. The message passing system is mainly used in distributed environment where
communicating processes reside in different computers connected by network.
Message passing system uses the 2 operations: send (message) and receive (message). To use these
operations a communication link must exist between the processes. The link can be implemented in
variety of ways. Several methods for logically implementing a link and send/ receive operations are

 Direct or Indirect communication


 Synchronous or asynchronous communication
 Automatic or Explicit buffering.
Naming:
The process that wants to communicate must have a way to refer each other. Under direct
communication, each process that wants to communicate, must explicitly name the recipient or
sender of communication. This is symmetric addressing.

 Send (P, message)  Send a message to process P


 Receive (Q, message)  Receive a message from process Q
In this scheme, a link is established automatically between every pair of processes that want to
communicate. The processes need to know only each other’s identity to communicate.
In asymmetric addressing, only the sender names the recipient, the recipient is not required to
name the sender.

 Send (P, message)  Send a message to process P


 Receive (id, message)  Receive a message from any process; the variable id is set to name
of the process with which communication has taken place.
With indirect communication, the messages are sent to and received from mailboxes or ports. A
mailbox can be viewed abstractly as an object into which messages can be placed by the processes
and from which messages can be removed. Each mailbox has unique identification. Here, a process
can communicate with some other process via a number of different mailboxes. Two processes can
communicate only if the processes have shared mailbox.

 Send (A, message)  Send a message to mailbox A


 Receive (A, message)  Receive a message from mailbox A
Operating Systems

A mailbox may be owned by a process or operating system.


If a mailbox is owned by process (in address space of process) then we distinguish between the
owner (which can only receive) and user (which can only send). When a process terminates,
mailbox associated with it also terminates.
If a mailbox is owned by operating system, then it is independent and is not attached to any
particular process. The operating system must provide some mechanism which will process to do:

 Create a mailbox
 Send and receive messages through mailbox
 Delete a mailbox
The process that creates mailbox is default owner. However the ownership and privilege may be
passed to other processes through appropriate system calls.
Synchronization:
Message passing may be either synchronous or asynchronous

 Blocking Send: The sending process is blocked until the message is received by receiving
process or mailbox
 Non-Blocking Send: Sender sends the message and resumes other operation
 Blocking Receive: Receiver blocks until a message is received.
 Non-Blocking Receive: Receiver retrieves either a valid message or null
Buffering:
Messages exchanged by communicating processes reside in temporary queue. Such queues can be
implemented in 3 ways

 Zero capacity: Queue has maximum length of zero. The link cannot have any message
waiting in it. The sender must block until recipient receives the message. It is referred as
message system with no buffering.
 Bounded capacity: The queue has finite length n; atmost n messages resides in it. If link is
not full, message is stored in queue and sender can continue other operations without
waiting. If link is full, the sender must block until space is available in queue.
 Unbounded capacity: Queue’s length is infinite; any number of messages can wait in it. The
sender never blocks.
The other 2 cases are referred as systems with automatic buffering.

Multithreaded Programming
In a client server model, when a server receives a request, it creates a process to provide service.
This was the technique that was used before. Process creation is time consuming and also resource
insensitive. To overcome this overhead, threads were created for the same process. It is generally
more efficient to use one process that contains multiple threads than creating more processes.
Operating Systems

Till now we have seen the programs which were run on only single threads execution. In modern
desktop PC’s many software packages are multithreaded.
Threads consists of thread ID, register sets, stack but it shares data section and code section with
other threads belonging to same process as shown in Fig 2.10.

Fig 2.10: Single Threaded and Multithreaded processes


Threads also play a vital role in Remote Procedure Call (RPC) systems. RPC allows interprocess
communication similar to ordinary function or procedure call. Typically RPC servers are
multithreaded. When a server receives a message, it services the message by using separate thread.
This helps the server to service several concurrent requests as shown in Fig 2.11.

Fig 2.11: Server Operations using threads


Many operating system kernels are now multithreaded; several threads operate in the kernel and
each thread performs a specific task such as managing devices or interrupt handling.
The benefits of multithreaded programming are:
Responsiveness: Multithreading an interactive application can allow a program to continue
running even if a part of it is blocked or is performing a lengthy operation, thereby increasing
responsiveness to the user.
Resource Sharing:By default, threads share memory and the resource of the process to which they
belong due to which an application can have several different threads of activity within the same
address space.
Operating Systems

Economy: Allocating the memory and resources for process creation is costly. Because threads
share resources of the process to which they belong, it is more economical to create and context
switch threads.
Scalability: Multiprocessor architecture can be used efficiently by using multithreaded programs
where threads may be running in parallel on different processors.
Nowadays many cores are mounted on the chip. Each core will have a separate processor
embedded in it. In a single core system, the execution of an application having 4 threads, the
threads are interleaved over time as shown in Fig 2.13

Fig 2.13: Single core system execution


In case a multicore system, the threads can be executed in parallel as system can assign separate
core for each thread as shown in Fig 2.14

Fig 2.14: Parallel execution on multicore systems


When a multicore system is used, it provides various challenges for system programmers and
application programmers to modify the existing programs to run on multicore and as well as on

 Dividing Activities: Applications must be examined to find the areas that can be divided
into separate, concurrent tasks and thus can run in parallel on individual cores.
 Balance: While identifying the task which run parallel, care must be taken each task
performs equal work on cores. Giving a core for task which doesn’t provide more value to
process may not be worth.
 Data Splitting: Just like applications, the data accessed and manipulated by the tasks must
be divided to run on separate cores.
 Data Dependency: The data accessed by the tasks must be examined for dependencies
between 2 or more tasks. In instances where one task depends data used by another task,
programmers must ensure that the execution of tasks is synchronized to accommodate the
data dependency.
 Testing and Debugging: Testing and debugging is more difficult for concurrent programs
than single threaded applications.
Multithreading Models:
Operating Systems

Threads can be divided into user threads and kernel threads. User threads are supported above the
kernel and are managed without kernel support whereas the kernel threads are supported and
managed directly by the operating system.
But there exist a relationship between user threads and kernel threads on following models
Many to One model:
In this model, many user threads are mapped to a single kernel thread as shown in Fig 2.15. Thread
management is done by thread library in user space, so it is efficient but the entire process will
block if a thread makes a blocking system call. Here a user can create as many user threads he
wishes, but only 1 kernel thread will be created at same time. Also since only one thread can access
the kernel at a time, multiple threads are unable to run in parallel on multiprocessors. EG: In
Solaris, green threads uses this model, GNU portable Threads.

Fig 2.15: Many to One model


One to One model:
In this model, each user thread is mapped to each kernel thread as shown in Fig 2.16. It provides
more concurrency than many to one model as it allows threads to run even when a blocking system
call is used by other thread. The only drawback here is creating a user thread must create a
corresponding kernel thread as well. Since this is an overhead, most systems have a limit on
number of thread creation. Eg: Linux, Windows 95, 98, NT, 2000, XP operating systems.

Fig 2.16: One to One model


Many to Many model:
In this model, many user level threads are multiplexed to a smaller or equal number of kernel
threads as shown in Fig 2.17. The number of kernel threads may be specific to a particular
Operating Systems

application or a particular machine. Here developer can create as many user threads as he wishes
and the corresponding kernel threads can run in parallel on a multiprocessor. This will remove the
overheads found in many to one and one to one models. EG: Solaris prior to 9, Windows NT/2000
with ThreadFiber package

Fig 2.17: Many to Many model


One variation in this many to many model is, not only does it multiplex many user threads with
smaller or equal number of kernel threads but it also allows a user level thread to be bound to
kernel thread as shown in Fig 2.18. EG: HP-UX, Tru64 UNIX

Fig 2.18: Two Level Model


Threading Issues:
The fork ( ) and exec ( ) system call that was described earlier was for single threaded processes.
The semantics for fork ( ) and exec ( ) calls changes in case of multithreaded programs.
Some UNIX systems have 2 versions of fork ( ) system call, one that duplicates all the threads and
the other which duplicates only the thread that invoked the fork ( ) system call.
Exec ( ) system call does the same work as described above. Among these 2 versions, the usage of
fork ( ) call depends on the application. If exec ( ) is called directly after fork ( ) then duplicating all
the threads are unnecessary, as the program specified in exec ( ) parameter replaces the process.
Here, duplicating only the calling thread is appropriate. If exec ( ) is not called after fork ( ) then
duplicating all the threads is necessary.
Operating Systems

Thread Cancellation is a task of terminating a thread before it has completed. A thread that is to
be cancelled is often referred to as target thread. Cancellation of target thread can occur in 2
scenarios:

 Asynchronous cancellation: One thread immediately terminates the target thread.


 Deferred cancellation: The target thread periodically checks whether it should
terminate, allowing it an opportunity to terminate itself in an orderly fashion.
The difficulty with asynchronous cancellation occurs in situation where resources have been
allocated to target thread or while updating a data it is sharing with other threads. Cancelling an
asynchronous thread may not free necessary system wide resource.
With deferred cancellation, 1 thread indicates that a target thread is to be cancelled. But the target
thread cancels only after setting a flag to determine whether to be cancelled or not by the target
thread. The thread can perform this check at a point where it can be cancelled safely. This point in
Pthreads is called as cancellation point.
A signal is used to notify that an event has occurred. A signal may be received either synchronously
or asynchronously.
Synchronous signals include illegal memory access and division by 0. Synchronous signals are
delivered to same process that performed some operation to generate that signal.
When a signal is generated by an external event to running process, that process receives the signal
asynchronously. Ex: Terminating a process with CTRL-C and having timer expire.
A signal may be handled by one of 2 possible handlers

 A default signal handler


 A user defined signal handler
Every signal has a default signal handler that is run in the kernel when handling a signal. The
default action can be overridden by a user defined signal handler that is called to handle a signal.
Handling a signal in a single threaded process is easier as the signal is delivered to process itself.
But in case of multithreaded processes a signal can be handled in following ways:

 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
Most multithreaded versions of UNIX allow a thread to specify which signals it accepts and which it
will block. So, the asynchronous signals may be delivered to those processes which are not blocking
it. Since the signal is to be handled only once, it will be delivered to the 1st process which is not
blocking it.
Thread Pools
When a server receives a request from a client, it creates a thread to provide service for that client.
To do this, a new thread is created, even though the thread creation is more superior compared to
Operating Systems

process creation, it does consume some time to create. And once a thread finishes its job, it will be
deleted.
Also if for every concurrent request a thread is created then the system would be full of threads
exhausting system resources such as CPU time or memory. For these purposes a thread pool is
created.
During process start up, a number of threads are created and stored into a pool, where they sit and
wait for work. When a server receives a request, it awakens a thread from this pool if available and
passes it the request for service. Once a thread completes the service it returns back to the pool and
awaits more work. If the pool contains no available thread, the server waits until one becomes free.
Advantages:

 Usually slightly faster to service a request with an existing thread than create a new thread
 Allows the number of threads in the application(s) to be bound to the size of the pool
Scheduler Activations
The systems implementing many to many models and two level model place an intermediate data
structure between user and kernel threads. That structure is known as lightweight process as
shown in Fig 2.19. To the user thread library, the LWP acts as virtual processor on which the
application can schedule a user thread to run. Each LWP is attached to kernel thread and it is the
kernel thread that operating system schedules to run on physical processors. If kernel thread
blocks, LWP blocks and up the chain user thread attached to LWP is also blocked.

Fig 2.19: Lightweight Process (LWP)


The communication between user thread library and kernel is known as scheduler activation.
Working:

 Kernel provides application with set of LWP’s and the application can schedule the user
threads on the available LWP.
 Kernel must inform application about certain events called as upcall.
 Upcalls are handled by thread library with an upcall handler which is run on LWP.
One event that triggers an upcall is when an application thread is about to block. Here

 Kernel makes an upcall to application informing about the specific thread which is going to
block
 Kernel allocates new virtual processor (LWP)
Operating Systems

 Upcall handler runs on new LWP which saves the state of blocking thread and relinquishes
the old LWP on which the block occurred.
 A new thread is scheduled by upcall handler on new LWP
 When an expected event occurs in old LWP, this information is sent by kernel to application
about the old LWP being eligible to run again.
 After marking the unblocked thread as eligible to run, the application schedules an eligible
thread to run on an available virtual processor (LWP).
Process Scheduling:
In single processor system, only process runs at a single point of time. Other processes must wait
until the CPU is free and can be rescheduled.
In case of multiprogramming, CPU will be busy running one process or other having high utilization.
A process is executed until it waits for some I/O operations to complete. In normal computers, CPU
sits idle during this time. In multiprogramming, several processes are kept in memory, ready for
execution, the CPU is allocated for such processes when previous process waiting for I/O to
complete.
Whenever the CPU becomes idle, the Operating system must select a process from the ready queue
for execution, this selection is carried out by a short term scheduler. The process thus selected is
allocated with the CPU for its execution.
Ready queue may not necessarily be First Come First serve, priority based or FIFO.
The scheduling algorithms can be divided into preemptive scheduling and non-preemptive
scheduling.Scheduling of process is done in the following situations
1. When a process moves from running state to waiting state (wait for I/O)
2. When a process moves running state to ready state ( in case of interrupts)
3. When a process moves from waiting state to ready state (once I/O is finished)
4. When a process terminates.
The 1st and 4th situation uses a non-preemptive scheduling, others use preemptive scheduling.
Under non-preemptive scheduling the CPU once allocated to process will be returned back only if
process is finished or if process moves to waiting state due to I/O operation. This is used in
Windows 3.x
Preemptive scheduling was 1st used in Windows 95 and later on continued in next Windows
operating system. Preemptive scheduling incurs cost associated with access to shared data.
Preemption also affects the design of operating system kernel.
A dispatcher is module which gives control of the CPU to the process selected by the short term
scheduler. The function involves

 Switching Context
 Switching to user mode
 Jumping to proper location in user program to restart that program.
The time taken for a dispatcher to stop one process and start another running is known as dispatch
latency.
Operating Systems

Many criteria have been suggested for comparing the CPU scheduling algorithms. Those criteria are

 CPU utilization: The CPU should be busy all the time by executing one or the other process.
Conceptually the utilization ranges from 0 to 100 %. In real time it ranges from 40% to
90%.
 Throughput: Measure of work is the number of processes that are completes in a unit time
will give the throughput. For long process, 1 process/hour. For short transactions 10
processes/ second
 Turnaround Time: It is the time taken by a process for execution. Turnaround time is the
sum of periods spent waiting to get into memory, waiting in ready queue, executing on CPU
and doing I/O.
 Waiting Time: It is the time taken by process while waiting in a ready queue.
 Response Time: It is time taken by a process in interactive system to respond to a request.
Response time is the time it takes to start responding not the time it takes to output the
response.
It is desirable to have high CPU utilization and throughput and minimize the turnaround time,
response time and waiting time.
Scheduling Algorithm:
The problem of deciding which process is to be allotted with the CPU is overcome by using
scheduling algorithm. The following are the various CPU scheduling algorithms
First Come First Served Scheduling
It is the simplest CPU scheduling algorithm. In this scheme, the CPU is first allocated to the process
which requested the CPU first. The FCFS policy uses FIFO queue. When a process requests for CPU,
its PCB is linked onto the tail of the FIFO queue. When the CPU is free, The CPU is allotted to the
process which is present in the head of the queue. The average waiting time for FCFS algorithm is
too long.
Example: Consider the following processes which arrive at time 0, with the length of CPU burst
given in milliseconds

Process Burst Time


P1 24
P2 3
P3 3
If the process arrive in the order P1, P2, P3 and are served in FCFS order, the result is shown using
Gantt chart.

P1 P2 P3
0 24 27 30
The waiting time of process P1 is 0 milliseconds, process P2 is 2 milliseconds and process P3 is 27
milliseconds. Thus average waiting time is (0+24+27)/3=17 milliseconds.
Likewise, if processes arrive in P2, P3, P1 order, then the results can be shown using Gantt chart as

P2 P3 P1
Operating Systems

0 3 6 30
The average waiting time here is (0+3+6)/3 = 3 milliseconds. Thus average waiting time varies
substantially and is not minimal.
The FCFS also leads to convoy effect in case we have a 1 CPU bound process and many I/O bound
processes. The FCFS scheduling algorithm is non-preemptive. Once the CPU has been allocated to a
process, that process will keep CPU until either during termination of itself or by requesting I/O.
FCFS cannot be used efficiently in time sharing systems as each user needs some time for its
execution.
Shortest Job First Scheduling
This algorithm associates with each process the length of the process’s next CPU burst. When the
CPU is available, it is assigned to the process which is having small CPU burst. If the next 2
processes have the same CPU burst, then FCFS algorithm is used to break the tie.
Example: Consider the following set of processes, with the length of CPU burst given in milliseconds

Process Burst Time


P1 6
P2 8
P3 7
P4 3
Using SJF scheduling, the results of these processes is given in Gantt chart are

P4 P1 P3 P2
0 3 9 16 24
The waiting time for Process P4 is 0 milliseconds, P1 is 3 milliseconds, P3 is 9 milliseconds and P2
is 16 milliseconds.
The average waiting time is (0+3+9+16)/4=7 milliseconds.
The SJF scheduling algorithm is provably optimal. Moving a short process before a long one
decreases the waiting time of short process and increases the waiting time of long process. The
above example showed the non-preemptive SJF scheduling algorithm.
The SJF scheduling algorithm can be preemptive as well. A preemptive SJF algorithm will
preemptive the currently executing process if the newly arrived process has less CPU burst time.
Example: Consider the following 4 processes, with a length of CPU burst given in milliseconds

Process Arrival Time Burst Time


P1 0 8
P2 1 4
P3 2 9
P4 3 5
The results of these process execution is shown using Gantt chart as

P1 P2 P4 P1 P3
0 1 5 10 17 26
Operating Systems

Process P started at time 0, At time 1, P2 arrives, now the remaining time for P1 and P2 are
calculated, P1 has 7 milliseconds remaining and P2 has 4 milliseconds, so the short job P2 is started.
At time 3, P3 arrives, now the remaining time is calculated where, P1=7 ms, P2=3 ms, P3= 9ms. So
P2 is continued. At time 4, P4 also arrives. The remaining time at this point is for P1=7ms, P2=1ms,
P3=9ms and P4=5ms. P2 is continued and it finishes its work at time t=5. Later the shortest of them
are checked and accordingly the processes are executed.
Priority Scheduling
A priority is associated with each process and the CPU is allocated to the process with highest
priority. Equal priority process are scheduled according to the FCFS order. A priority p is allotted to
each process, according to the value of p the processes are executed.
Priorities are indicated by some fixed range of numbers like 0 to 7 or 0 to 4095. According to the
systems used, 0 is either allocated as highest priority or lowest priority.
Example: Consider the following processes assumed to have arrived at time 0, in order P1, P2,
P3,….P5 with the length of CPU burst given in milliseconds

Process Burst Time Priority


P1 10 3
P2 1 1
P3 2 4
P4 1 5
P5 5 2
Using priority scheduling, we would schedule these processes according to the following Gantt
chart.

P2 P5 P1 P3 P4
0 1 6 16 18 19
The average waiting time is 8.2 milliseconds.
Priority scheduling can be either preemptive or non-preemptive. When a new process arrives to the
ready queue, its priority is checked with the currently executing process. A preemptive priority
scheduling algorithm will preempt the CPU if the priority of the newly arrived process is higher
than the currently running process. A non-preemptive process will simply put the new process at
the head of the queue.
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 termed as blocked. This algorithm can leave
some low priority process to be blocked indefinitely.
A solution to above said problem is aging. Aging is a technique of gradually increasing the priority
of processes that wait in the system for a long time. For example, if priorities range from 127 to 0
where 0 being highest priority, the priority of waiting process is increased by 1 in every 15 minutes.
Eventually a process with priority as 127 will become process with highest priority 0 in 32 hours.
Round Robin Scheduling

 One of the oldest, simplest, fairest and most widely used algorithm is round robin (RR).
Operating Systems

 In the round robin scheduling, processes are dispatched in a FIFO manner but are given a
limited amount of CPU time called a time-slice or a quantum.
 If a process does not complete before its CPU-time expires, the CPU is preempted and given
to the next process waiting in a queue. The preempted process is then placed at the back of
the ready list.
 Round Robin Scheduling is preemptive (at the end of time-slice) therefore it is effective in
timesharing environments in which the system needs to guarantee reasonable response
times for interactive users.
 The only interesting issue with round robin scheme is the length of the quantum. Setting the
quantum too short causes too many context switches and lower the CPU efficiency. On the
other hand, setting the quantum too long may cause poor response time and approximates
FCFS.
In any event, the average waiting time under round robin scheduling is often quite long.
1. 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.
2. 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.
3. Performance ->q large. FIFO ->q small. q must be large with respect to context switch,
otherwise overhead is too high. Example: Process Burst Time
Example: Consider the following processes which arrive at time 0, with the length of CPU burst
given in milliseconds

Process Burst Time


P1 24
P2 3
P3 3
If the process arrive in the order P1, P2, P3 and are served in Round Robin scheduling, with the a
quantum defined as 4milliseconds each, the result is shown using Gantt chart.

P1 P2 P3 P1 P1 P1 P1 P1
0 4 7 10 14 18 22 26 30
Multilevel Queue Scheduling
A multilevel queue scheduling algorithm partitions the ready queue in several separate queues, for
instance,
In a multilevel queue scheduling, processes are permanently assigned to one queues. The processes
are permanently assigned to one another, based on some property of the process, such as Memory
size, Process priority, Process type. Algorithm choose the process from the occupied queue that has
the highest priority, and run that process either Preemptive or Non-preemptively. Each queue has
its own scheduling algorithm or policy.
Operating Systems

Fig 2.20: Multilevel Queue Scheduling


PossibilityI
If each queue has absolute priority over lower-priority queues then no process in the queue could
run unless the queue for the highest-priority processes were all empty. For example, in the Fig 2.20,
no process in the batch queue could run unless the queues for system processes, interactive
processes and interactive editing processes will all empty.
Possibility II
If there is a time slice between the queues then each queue gets a certain amount of CPU times,
which it can then schedule among the processes in its queue. For instance;

 80% of the CPU time to foreground queue using RR.


 20% of the CPU time to background queue using FCFS.
Since processes do not move between queues so, this policy has the advantage of low scheduling
overhead, but it is inflexible.
Multilevel Feedback Queue Scheduling
Multilevel feedback queue-scheduling algorithm allows a process to move between queues. It uses
many ready queues and associate a different priority with each queue. The Algorithm chooses to
process with highest priority from the occupied queue and run that process either preemptively or
non-preemptively. If the process uses too much CPU time it will moved to a lower-priority queue.
Similarly, a process that wait too long in the lower-priority queue may be moved to a higher-
priority queue may be moved to a highest-priority queue. Note that this form of aging prevents
starvation.

 A process entering the ready queue is placed in queue 0.


 If it does not finish within 8 milliseconds time, it is moved to the tail of queue 1.
 If it does not complete, it is preempted and placed into queue 2.
 Processes in queue 2 run on a FCFS basis, only when 2 run on a FCFS basis queue, only
when queue 0 and queue 1 are empty.
Example:-Three queues:

• Q0 – RR with time quantum 8 milliseconds


Operating Systems

• Q1 – RR time quantum 16 milliseconds


• Q2 – FCFS
Scheduling as shown in Fig 2.21

 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.

Fig 2.21: Multilevel Feedback Queue Scheduling

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy