Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
4 views

Unit 2 Process Management- - Copy

The document provides an overview of process management in operating systems, detailing the concept of processes, their states, and the role of the Process Control Block (PCB). It explains process scheduling, context switching, and the types of schedulers, including long-term, short-term, and medium-term schedulers. Additionally, it discusses inter-process communication methods such as shared memory and message passing, highlighting the producer-consumer problem as an example.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Unit 2 Process Management- - Copy

The document provides an overview of process management in operating systems, detailing the concept of processes, their states, and the role of the Process Control Block (PCB). It explains process scheduling, context switching, and the types of schedulers, including long-term, short-term, and medium-term schedulers. Additionally, it discusses inter-process communication methods such as shared memory and message passing, highlighting the producer-consumer problem as an example.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 134

Process Management

Process Concept, Process State, PCB,


Process Scheduling, Scheduling Algorithm
Process

• A process is a program in execution.


• The execution of a process must progress in a sequential fashion.
• A process is defined as an entity which represents the basic unit of work to be implemented
in the system.
• Component of a process are following
• The program code, also called text section
• Current activity including program counter, processor registers
• Stack containing temporary data
 Function parameters, return addresses, local variables
• Data section containing global variables
• Heap containing memory dynamically allocated during run time
Program

• Program is passive entity stored on disk (executable file), process is active


• Program becomes process when executable file loaded into memory.
• Execution of program started via GUI mouse clicks, command line entry of its name, etc.
• One program can be several processes.
• Consider multiple users executing the same program.
• A program by itself is not a process. It is a static entity made up of program statement
while process is a dynamic entity.
• Program contains the instructions to be executed by processor.
• A program takes a space at single place in main memory and continues to stay there.
• A program does not perform any action by itself.
Process in Memory
Process State
• As a process executes, it changes state.
• The state of a process is defined as the current activity of the process.
• Process can have one of the following five states at a time.
• New: The process is being created
• Ready: The process is waiting to be assigned to a processor(CPU) r allocated
to them by the operating system so that they can run.
• Running: Process instructions are being executed (i.e. The process that is
currently being executed)
• Waiting: The process is waiting for some event to occur (such as the completion
of an I/O operation).
• Terminated: The process has finished execution
Diagram of Process State
Process Control Block (PCB)

• Each process is represented in the operating


system by a Process Control Block (PCB) also
called a task control block.
• PCB is the data structure used by the operating
system.
• Operating system groups all information that
needs about particular process.
• PCB contains many pieces of information
associated with a specific process which is
described below.
Pointer-
• Pointer points to another process control block. Pointer is used for maintaining the scheduling
list.
Process State-
• Process state may be new, ready, running, waiting and so on.
Program Counter-
• Program Counter indicates the address of the next instruction to be executed for this process.

CPU Register –
• CPU registers include general purpose register, stack pointers, index registers and accumulators
etc. number of register and type of register totally depends upon the computer architecture.
Memory management information
• This information may include the value of base and limit registers, the page
tables, or the segment tables depending on the memory system used by the
operating system.
• This information is useful for de-allocating the memory when the process
terminates.
Accounting information
• This information includes the amount of CPU and real time used, time limits, job
or process numbers, account numbers etc.
• Process control block includes CPU scheduling, I/O resource management, file
management information etc.
• The PCB serves as the repository for any information which can vary from
process to process.
• Loader/linker sets flags and registers when a process is created.
• If that process gets suspended, the contents of the registers are saved on a stack
and the pointer to the particular stack frame is stored in the PCB.
• By this technique, the hardware state can be restored so that the process can be
scheduled to run again.
CPU Switch From Process to Process

• In the Operating System, there are cases when you have


to bring back the process that is in the running state to
some other state like ready state or wait/block state.
• If the running process wants to perform some I/O
operation, then you have to remove the process from the
running state and then put the process in the I/O queue.
• Sometimes, the process might be using a round-robin
scheduling algorithm where after every fixed time
quantum, the process has to come back to the ready state
from the running state.
• So, these process switchings are done with the help of
Context Switching.
Context Switching

• A context switching is a process that involves switching of the CPU from


one process or task to another.
• In this phenomenon, the execution of the process that is present in the running
state is suspended by the kernel and another process that is present in the ready
state is executed by the CPU.
• It is one of the essential features of the multitasking operating system. The
processes are switched so fastly that it gives an illusion to the user that all the
processes are being executed at the same time.
• But the context switching process involved a number of steps that need to be
followed.
• You can't directly switch a process from the running state to the ready state.
• You have to save the context of that process.
• If you are not saving the context of any process P then after some time, when the
process P comes in the CPU for execution again, then the process will start
executing from starting.
• But in reality, it should continue from that point where it left the CPU in its
previous execution.
• So, the context of the process should be saved before putting any other process in
the running state.
• A context is the contents of a CPU's registers and program counter at any point in
time.
Context switching can happen due to the following reasons:
• When a process of high priority comes in the ready state.
• In this case, the execution of the running process should be stopped and the higher
priority process should be given the CPU for execution.
• When an interruption occurs then the process in the running state should be
stopped and the CPU should handle the interrupt before doing something else.
• When a transition between the user mode and kernel mode is required then you
have to perform the context switching.
Steps involved in Context Switching

The process of context switching involves a number of steps.


The following diagram depicts the process of context switching
between the two processes P1 and P2.
• In the above figure, you can see that initially, the process P1 is in the running state
and the process P2 is in the ready state.
• Now, when some interruption occurs then you have to switch the process P1 from
running to the ready state after saving the context and the process P2 from ready
to running state.
• The following steps will be performed:
1. Firstly, the context of the process P1 i.e. the process present in the running state will be
saved in the Process Control Block of process P1 i.e. PCB1.
2. Now, you have to move the PCB1 to the relevant queue i.e. ready queue, I/O queue,
waiting queue, etc.
3. From the ready state, select the new process that is to be executed i.e. the process P2.
4. Now, update the Process Control Block of process P2 i.e. PCB2 by setting the process
state to running. If the process P2 was earlier executed by the CPU, then you can get the
position of last executed instruction so that you can resume the execution of P2.
5. Similarly, if you want to execute the process P1 again, then you have to follow the same
steps as mentioned above(from step 1 to 4).
Process Scheduling

 The process scheduling is the activity of the process manager that handles the
removal of the running process from the CPU and the selection of another
process on the basis of a particular strategy.
 Process scheduling is an essential part of a Multiprogramming operating system.
 Such operating systems allow more than one process to be loaded into the
executable memory at a time and loaded process shares the CPU using time
multiplexing.
 Maximize CPU use, quickly switch processes onto CPU for time sharing.
 Process scheduler selects among available processes for next execution on CPU.
• Maintains scheduling queues of processes
• Job queue – set of all processes in the system
• Ready queue – set of all processes residing in main memory, ready and
waiting to execute
• Device queues – set of processes waiting for an I/O device
• Processes migrate among the various queues
Scheduling Queues

• Scheduling queues refers to queues of processes


or devices.
• When the process enters into the system, then
this process is put into a job queue.
• This queue consists of all processes in the
system.
• The operating system also maintains other
queues such as device queue.
• Device queue is a queue for which multiple
processes are waiting for a particular I/O
device. Each device has its own device queue.

Ready Queue And Various I/O Device Queues


Representation of Process Scheduling

• This figure shows the queuing


diagram of process scheduling.
• Queue is represented by
rectangular box.
• The circles represent the
resources that serve the
queues
• The arrows indicate the
process flow in the system.

Queuing diagram represents queues, resources, flows


Queues are of two types
1. Ready queue
2. Device queue
• A newly arrived process is put in the ready queue.
• Processes waits in ready queue for allocating the CPU.
• Once the CPU is assigned to a process, then that process will execute.
• While executing the process, any one of the following events can occur.
• The process could issue an I/O request and then it would be placed in an I/O queue.
• The process could create new sub process and will wait for its termination.
• The process could be removed forcibly from the CPU, as a result of interrupt and put
back in the ready queue.
Two State Process Model

Running
• When new process is created by Operating System that process enters into the system as in
the running state.

Non-Running
• Processes that are not running are kept in queue, waiting for their turn to execute.
• Each entry in the queue is a pointer to a particular process.
• Queue is implemented by using linked list. Use of dispatcher is as follows.
• When a process is interrupted, that process is transferred in the waiting queue.
• If the process has completed or aborted, the process is discarded. In either case, the
dispatcher then selects a process from the queue to execute
Schedulers

• Schedulers are special system software which handles process scheduling in


various ways.
• Their main task is to select the jobs to be submitted into the system and to decide
which process to run.
• Schedulers are of three types
1. Long Term Scheduler
2. Short Term Scheduler
3. Medium Term Scheduler
Long Term Scheduler

• Long term scheduler is also known as job scheduler. .


• It chooses the processes from the pool (secondary memory) and keeps them in the
ready queue maintained in the primary memory.
• Long term scheduler determines which programs are admitted to the system for
processing.
• Job scheduler selects processes from the queue and loads them into memory for
execution.
• Process loads into the memory for CPU scheduling.
• Long Term scheduler mainly controls the degree of Multiprogramming.
• The purpose of long term scheduler is to choose a perfect mix of IO bound and
CPU bound processes among the jobs present in the pool.
• If the job scheduler chooses more IO bound processes then all of the jobs may
reside in the blocked state all the time and the CPU will remain idle most of the
time. This will reduce the degree of Multiprogramming.
• Therefore, the Job of long term scheduler is very critical and may affect the
system for a very long time.

• Time-sharing operating systems


have no long term scheduler.
• When process changes the state
from new to ready, then there is
use of long term scheduler.
Short Term Scheduler

• Short term scheduler is also known as CPU scheduler.


• It selects one of the Jobs from the ready queue and dispatch to the CPU for the execution.
• A scheduling algorithm is used to select which job is going to be dispatched for the
execution.
• The Job of the short term scheduler can be very critical in the sense that if it selects job
whose CPU burst time is very high then all the jobs after that, will have to wait in the
ready queue for a very long time.
• This problem is called starvation which may arise if the short term scheduler makes some
mistakes while selecting the job.
• Main objective is increasing system performance in accordance with the chosen set of
criteria.
• It is the change of ready state to running state of the process.
• CPU scheduler selects process among the processes that are ready to execute and allocates
CPU to one of them.
Short Term Scheduler
Medium Term Scheduler

• Medium term scheduler takes care of the swapped out processes.


• If the running state processes needs some IO time for the completion then there is
a need to change its state from running to waiting.
• Medium term scheduler is used for this purpose.
• It removes the process from the running state to make room for the other
processes.
• Such processes are the swapped out processes and this procedure is called
swapping.
• The medium term scheduler is responsible for suspending and resuming the
processes.
• It reduces the degree of multiprogramming. The swapping is necessary to have a
perfect mix of processes in the ready queue.
Inter Process Communication

• Inter Process communication is the mechanism provided by the operating


system that allows processes to communicate with each other.
• This communication could involve a process letting another process know that
some event has occurred or the transferring of data from one process to another.
• A diagram that illustrates inter-process communication is as follows −
• A process can be of two types: Independent process and Co-operating process.
• An independent process is not affected by the execution of other processes while a
co-operating process can be affected by other executing processes.
• Though one can think that those processes, which are running independently, will
execute very efficiently, in reality, there are many situations when co-operative
nature can be utilized for increasing computational speed, convenience and
modularity.
• Inter process communication (IPC) is a mechanism which allows processes to
communicate with each other and synchronize their actions.
• The communication between these processes can be seen as a method of co-
operation between them. Processes can communicate with each other through
both: Shared memory and Message passing
• The Figure 1 below shows a basic structure
of communication between processes via the
shared memory method and via the message
passing method.
• An operating system can implement both
method of communication.
• First, we will discuss the shared memory
methods of communication and then message
passing.
• Communication between processes using
shared memory requires processes to share
some variable and it completely depends on
how programmer will implement it.
• One way of communication using shared memory can be imagined like this:
• Suppose process1 and process2 are executing simultaneously and they share some
resources or use some information from another process.
• Process1 generate information about certain computations or resources being used and
keeps it as a record in shared memory.
• When process2 needs to use the shared information, it will check in the record stored in
shared memory and take note of the information generated by process1 and act accordingly.
• Processes can use shared memory for extracting information as a record from another
process as well as for delivering any specific information to other processes.
Let’s discuss an example of communication between processes using shared memory
method.
Shared Memory Method

Producer-Consumer Problem
• There are two processes: Producer and Consumer.
• Producer produces some item and Consumer consumes that item.
• The two processes share a common space or memory location known as a buffer where the
item produced by Producer is stored and from which the Consumer consumes the item, if
needed.
• There are two versions of this problem: the first one is known as unbounded buffer problem in
which Producer can keep on producing items and there is no limit on the size of the buffer,
• the second one is known as the bounded buffer problem in which Producer can produce up to a
certain number of items before it starts waiting for Consumer to consume it.
• We will discuss the bounded buffer problem.
Bounded buffer problem
• First, the Producer and the Consumer will share some common memory, then
producer will start producing items.
• If the total produced item is equal to the size of buffer, producer will wait to get it
consumed by the Consumer.
• Similarly, the consumer will first check for the availability of the item.
• If no item is available, Consumer will wait for Producer to produce it.
• If there are items available, Consumer will consume it.
Producer Process Code

• item nextProduced;

• while(1){

• // check if there is no space
• // for production.
• // if so keep waiting.
• while((free_index+1) mod buff_max == full_index);

• shared_buff[free_index] = nextProduced;
• free_index = (free_index + 1) mod buff_max;
• }
Consumer Process Code

• item nextConsumed;

• while(1){

• // check if there is an available
• // item for consumption.
• // if not keep on waiting for
• // get them produced.
• while((free_index == full_index);

• nextConsumed = shared_buff[full_index];
• full_index = (full_index + 1) mod buff_max;
• }
• In the above code, the Producer will start producing again when the
(free_index+1) mod buff max will be free because if it it not free, this implies that
there are still items that can be consumed by the Consumer so there is no need to
produce more.
• Similarly, if free index and full index point to the same index, this implies that
there are no items to consume.
Messaging Passing Method

• Now, We will start our discussion of the communication between processes via
message passing.
• In this method, processes communicate with each other without using any kind of
shared memory.
• If two processes p1 and p2 want to communicate with each other, they proceed as
follows:
• Establish a communication link (if a link already exists, no need to establish it again.)
• Start exchanging messages using basic primitives.
We need at least two primitives:
• send(message, destination) or send(message)
• receive(message, host) or receive(message)
• The message size can be of fixed size or of
variable size.
• If it is of fixed size, it is easy for an OS
designer but complicated for a programmer
and if it is of variable size then it is easy for
a programmer but complicated for the OS
designer.
• A standard message can have two parts: header and body.
• The header part is used for storing message type, destination id, source id,
message length, and control information.
• The control information contains information like what to do if runs out of buffer
space, sequence number, priority. Generally, message is sent using FIFO style.
Message Passing through Communication Link.

Direct and Indirect Communication link


• Now, We will start our discussion about the methods of implementing
communication link. While implementing the link, there are some questions
which need to be kept in mind like :
1. How are links established?
2. Can a link be associated with more than two processes?
3. How many links can there be between every pair of communicating
processes?
4. What is the capacity of a link? Is the size of a message that the link can
accommodate fixed or variable?
5. Is a link unidirectional or bi-directional?
• A link has some capacity that determines the number of messages that can reside
in it temporarily for which every link has a queue associated with it which can be
of zero capacity, bounded capacity, or unbounded capacity.
• In zero capacity, the sender waits until the receiver informs the sender that it has
received the message.
• In non-zero capacity cases, a process does not know whether a message has been
received or not after the send operation.
• For this, the sender must communicate with the receiver explicitly.
• Implementation of the link depends on the situation, it can be either a direct
communication link or an in-directed communication link.
• Direct Communication links are implemented when the processes uses a specific
process identifier for the communication, but it is hard to identify the sender
ahead of time.
For example: the print server.
• In-direct Communication is done via a shared mailbox (port), which consists of
a queue of messages. The sender keeps the message in mailbox and the receiver
picks them up.
Message Passing through Exchanging the Messages.

• Synchronous and Asynchronous Message Passing:


• A process that is blocked is one that is waiting for some event, such as a resource
becoming available or the completion of an I/O operation.
• IPC is possible between the processes on same computer as well as on the
processes running on different computer i.e. in networked/distributed system.
• In both cases, the process may or may not be blocked while sending a message or
attempting to receive a message so message passing may be blocking or non-
blocking.
• Blocking is considered synchronous and blocking send means the sender will be
blocked until the message is received by receiver.
• Similarly, blocking receive has the receiver block until a message is available.
• Non-blocking is considered asynchronous and Non-blocking send has the sender
sends the message and continue.
• Similarly, Non-blocking receive has the receiver receive a valid message or null.
• After a careful analysis, we can come to a conclusion that for a sender it is more
natural to be non-blocking after message passing as there may be a need to send
the message to different processes.
• However, the sender expects acknowledgement from the receiver in case the send
fails.
• Similarly, it is more natural for a receiver to be blocking after issuing the receive
as the information from the received message may be used for further execution.
• At the same time, if the message send keep on failing, the receiver will have to
wait indefinitely.
• That is why we also consider the other possibility of message passing. There are
basically three preferred combinations:
• Blocking send and blocking receive
• Non-blocking send and Non-blocking receive
• Non-blocking send and Blocking receive (Mostly used)
• In Direct message passing, The process which want to communicate must explicitly
name the recipient or sender of communication.
• e.g. send(p1, message) means send the message to p1.
• Similarly, receive(p2, message) means receive the message from p2.
• In this method of communication, the communication link gets established automatically,
which can be either unidirectional or bidirectional, but one link can be used between one
pair of the sender and receiver and one pair of sender and receiver should not possess
more than one pair of links.
• Symmetry and asymmetry between sending and receiving can also be implemented i.e.
either both process will name each other for sending and receiving the messages or only
the sender will name receiver for sending the message and there is no need for receiver
for naming the sender for receiving the message.
• In Indirect message passing, processes use mailboxes (also referred to as ports) for
sending and receiving messages.
• Each mailbox has a unique id and processes can communicate only if they share a
mailbox.
• Link established only if processes share a common mailbox and a single link can be
associated with many processes.
• Each pair of processes can share several communication links and these links may be
unidirectional or bi-directional.
• Suppose two process want to communicate though Indirect message passing, the
required operations are: create a mail box, use this mail box for sending and receiving
messages, then destroy the mail box. The standard primitives used are: send(A,
message) which means send the message to mailbox A.
Examples of IPC Systems - POSIX

• POSIX Shared Memory


• Process first creates shared memory segment
shm_fd = shm_open(name, O CREAT | O RDWR, 0666);
• Also used to open an existing segment to share it
• Set the size of the object
ftruncate(shm fd, 4096);
 Now the process could write to the shared memory
sprintf(shared memory, "Writing to shared
memory");
• POSIX : uses shared memory method.
• Mach : uses message passing
• Windows XP : uses message passing using local procedural calls
• Communication in client/server Architecture:
• There are various mechanism:
• Pipe
• Socket
• Remote Procedural calls (RPCs)
CPU Scheduling in OS

• CPU scheduling is the basis of multiprogrammed operating systems.


• By switching the CPU among processes, the operating system can make the computer more productive.
• In this chapter, we introduce basic CPU-scheduling concepts and present several CPU-scheduling
algorithms.
• We also consider the problem of selecting an algorithm for a particular system.
• In a single-processor system, only one process can run at a time.
• Others must wait until the CPU is free and can be rescheduled.
• The objective of multiprogramming is to have some process running at all times, to maximize CPU
utilization.
• The idea is relatively simple. A process is executed until it must wait, typically for the completion of
some I/O request.
• In a simple computer system, the CPU then just sits idle.
• All this waiting time is wasted; no useful work is accomplished.
• With multiprogramming, we try to use this time productively.
• Several processes are kept in memory at one time.
• When one process has to wait, the operating system takes the CPU away from that
process and gives the CPU to another process.
• This pattern continues. Every time one process has to wait, another process can take over
use of the CPU.
• Scheduling of this kind is a fundamental operating-system function.
• Almost all computer resources are scheduled before use.
• The CPU is, of course, one of the primary computer resources.
• Thus, its scheduling is central to operating-system design.
• The success of CPU scheduling depends on an observed
property of processes: process execution consists of a cycle
of CPU execution and I/O wait.
• Processes alternate between these two states. Process
execution begins with a CPU burst.
• That is followed by an I/O burst, which is followed by
another CPU burst, then another I/O burst, and so on.
• Eventually, the final CPU burst ends with a system request to
terminate execution.
CPU Scheduler

• Whenever the CPU becomes idle, the operating system must select one of the
processes in the ready queue to be executed.
• The selection process is carried out by the short-term scheduler, or CPU
scheduler.
• The scheduler selects a process from the processes in memory that are ready to
execute and allocates the CPU to that process.
Preemptive Scheduling

CPU-scheduling decisions may take place under the following four circumstances:
1. When a process switches from the running state to the waiting state (for
example, as the result of an I/O request or an invocation of wait() for the
termination of a child process)
2. When a process switches from the running state to the ready state (for
example, when an interrupt occurs)
3. When a process switches from the waiting state to the ready state (for
example, at completion of I/O)
4. When a process terminates
• For situations 1 and 4, there is no choice in terms of scheduling.
• A new process (if one exists in the ready queue) must be selected for execution.
• There is a choice, however, for situations 2 and 3.
• When scheduling takes place only under circumstances 1 and 4, we say that the scheduling
scheme is non-preemptive or cooperative.
• Otherwise, it is preemptive.
• Under non-preemptive scheduling, once the CPU has been allocated to a process, the process
keeps the CPU until it releases the CPU either by terminating or by switching to the waiting
state.
• This scheduling method was used by Microsoft Windows 3.x. Windows 95 introduced
preemptive scheduling, and all subsequent versions of Windows operating systems have
used preemptive scheduling.
• The Mac OS X operating system for the Macintosh also uses preemptive
scheduling; previous versions of the Macintosh operating system relied on
cooperative scheduling.
• Unfortunately, preemptive scheduling can result in race conditions when data are
shared among several processes.
• Consider the case of two processes that share data.
• While one process is updating the data, it is preempted so that the second process
can run.
• The second process then tries to read the data, which are in an inconsistent state.
• Preemption also affects the design of the operating-system kernel.
Dispatcher

• Another component involved in the CPU-scheduling function is the dispatcher.


• The dispatcher is the module that gives control of the CPU to the process selected by the
short-term scheduler.
• This function involves the following:
• Switching context
• Switching to user mode
• Jumping to the proper location in the user program to restart that program.
• The dispatcher should be as fast as possible, since it is invoked during every process
switch.
• The time it takes for the dispatcher to stop one process and start another running is
known as the dispatch latency
Scheduling Criteria

CPU utilization.
• We want to keep the CPU as busy as possible. Conceptually, CPU utilization can range from
0 to 100 percent. In a real system, it should range from 40 percent (for a lightly loaded
system) to 90 percent (for a heavily loaded system).
Throughput.
• One measure of work is the number of processes that are completed per time unit, called
throughput. For long processes, this rate may be one process per hour; for short
transactions, it may be ten processes per second.
Turnaround time
• The interval from the time of submission of a process to the time of completion is the
turnaround time. Turnaround time is the sum of the periods spent waiting to get into
memory, waiting in the ready queue, executing on the CPU, and doing I/O.
Waiting time
• Waiting time is the sum of the periods spent waiting in the ready queue.
Response time
• Another measure is the time from the submission of a request until the first
response is produced. This measure, called response time, is the time it takes to
start responding, not the time it takes to output the response.
Scheduling Algorithms

• CPU scheduling deals with the problem of deciding which of the processes in the
ready queue is to be allocated the CPU.
• There are many different CPU-scheduling algorithms.
1. First-Come, First-Served Scheduling
2. Shortest-Job-First Scheduling
3. Priority Scheduling
4. Round-Robin Scheduling
5. Multilevel Queue Scheduling
First-Come, First-Served Scheduling

• The simplest CPU-scheduling algorithm is the first-come, first-served (FCFS)


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 to 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.
• On the negative side, the average waiting time under the FCFS policy is
• often quite long.
Example

• Consider the following set of processes that arrive at time 0, with the length of the
CPU burst given in milliseconds:
Process Burst Time
P1 24
P2 3
P3 3
• Find out Turn around time and waiting time of each process.
Solution

• If the processes arrive in the order P1, P2, P3, and are served in FCFS order, we get the
result shown in the following Gantt chart, which is a bar chart that illustrates a
particular schedule, including the start and finish times of each of the participating
processes:

Gantt chart
• The waiting time is 0 milliseconds for process P1
• 24 milliseconds for process P2
• 27 milliseconds for process P3
• Average waiting time: (0 + 24 + 27)/3 = 17
P1 P2 P3
0 24 27 30

Waiting Time • Turn-around time


P1= CPU allocate time-Arrival • P1= Process Completion Time-
Time Arrival Time
P1=0-0=0 • P1=24-0=24
P2=24-0=24 • P2=27-0=27
P3=27-0=27 • P3=30-0=30
Average WAT= (0+24+27)/3 • Average TAT= (24+27+30)/3
WAT=51/3=17 • TAT=81/3=27
Case II- FCFS

Suppose that the processes arrive in the order:


P2 , P3 , P1
• The Gantt chart for the schedule is:

P2 P3 P1
0 3 6 30

• Waiting time for P1 = 6; P2 = 0; P3 = 3 || Turn around time for P1=30, P2=3, P3=6
• Average waiting time: (6 + 0 + 3)/3 = 3 || Average TAT= (30+3+3)/3 = 36/3=12
• Much better than previous case
• There is a convoy effect as all the other processes wait for the one big
process to get off the CPU.
• This effect results in lower CPU and device utilization than might be
possible if the shorter processes were allowed to go first.
Shortest-Job-First Scheduling

• A different approach to CPU scheduling is the shortest-job-first (SJF) scheduling


algorithm.
• 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 that has the smallest next
CPU burst.
• If the 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 its total length.
Example

• As an example of SJF scheduling, consider the following set of processes, with


the length of the CPU burst given in milliseconds:
Process Burst Time
P1 6
P2 8
P3 7
P4 3
Find Average waiting time and turn around time.
Solution

• Using SJF scheduling, we would schedule these processes according to the


following Gantt chart:
P4 P1 P3 P2
0 3 9 16 24

Waiting Time Turn Around Time


P1 WT=3, P2 WT=16 P1 TAT=9, P2 TAT=24
P3 WT=9, P4 WT=0 P3 TAT=16, P4 TAT=3
SO Average TAT = (9+24+16+3)/4= 52/4=13 Millisecond
Average WT = (3 + 16 + 9 + 0) / 4
28/4=7 millisecond
• The SJF algorithm can be either preemptive or non-preemptive.
• The choice arises when a new process arrives at the ready queue while a previous
process is still executing.
• The next CPU burst of the newly arrived process may be shorter than what is left
of the currently executing process.
• A preemptive SJF algorithm will preempt the currently executing process,
whereas a non-preemptive SJF algorithm will allow the currently running process
to finish its CPU burst.
• Preemptive SJF scheduling is sometimes called shortest-remaining-time-first
scheduling.
Preemptive SJF Example

• As an example, consider the following four processes, with the length of the CPU
burst given in milliseconds:
ProcessA arri Arrival TimeT Burst Time
P1 0 8
P2 1 4
P3 2 9
P4 3 5

Find average waiting time.


Formula for Waiting Time = CPU Allocation time –Arrival time
Solution

P1 P2 P4 P1 P3
0 1 5 10 17 26

• Process P1 is started at time 0, since it is the only process in the queue. Process P2 arrives at time
1.
• The remaining time for process P1 (7 milliseconds) is larger than the time required by process P2
(4 milliseconds), so process P1 is preempted, and process P2 is scheduled.
• So Waiting Time = CPU Allocation time –Arrival time
• P1 WT=10-1=9, P2 WT=1-1=0
• P3 WT=17-2=15, P4 WT=5-3=2
• Average Waiting Time = (9+0+15+2)/4=
• 26/4= 6.5 Millisecond
P1 P2 P4 P1 P3
0 1 5 10 17 26

Turn Around Time=Process Completion Time-Arrival Time


P1 TAT=17-0=17
P2 TAT=5-1=4
P3 TAT=26-2=24
P4 TAT=10-3=7
Average Turn Around Time= (17+4+24+7)/4
52/4=13 Millisecond
Priority Scheduling

• The SJF algorithm is a special case of the general priority-scheduling algorithm.


• A priority is associated with each process, and the CPU is allocated to the process
with the highest priority.
• Equal-priority processes are scheduled in FCFS order.
• Priority scheduling can be either preemptive or non-preemptive.
• When a process arrives at the ready queue, its priority is compared with the priority of
the currently running process.
• A 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 non-preemptive priority scheduling algorithm will simply put the new process at
the head of the ready queue.
• A major problem with priority scheduling algorithms 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 prevent a low-priority 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.
• A solution to the problem of indefinite blockage of low-priority processes is
• aging.
• Aging involves gradually increasing the priority of processes that wait in the
system for a long time.
• For example, if priorities range from 127 (low) to 0 (high), we could increase
the priority of a waiting process by 1 every 15 minutes.
• Eventually, even a process with an initial priority of 127 would have the
highest priority in the system and would be executed.
• In fact, it would take no more than 32 hours for a priority-127 process to age to
a priority-0 process.
Example of Priority Scheduling

• 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 Burst Time Priority
P1 10 3
P2 1 1
P3 2 4
P4 1 5
P5 5 2
• Using Priority Scheduling find the Average waiting time and turn around time
(Note lower number is higher priority)
Solution

• Using priority scheduling, we would schedule these processes according to the


following Gantt chart:

• P1 WT=6 P1 TAT=16
• P2 WT=0 P2 TAT=1
• P3 WT=16 P3 TAT=18
• P4 WT=18 P5 WT=1 P4 TAT=19 P5 TAT=6
• Average Waiting Time Average Turn Around Time
• (6+0+16+18+1)/5=8.2 (16+1+18+19+6)/5= 12
Round-Robin Scheduling
• The round-robin (RR) scheduling algorithm is designed especially for timesharing
systems.
• It is similar to FCFS scheduling, but preemption is added to enable the system to
switch between processes.
• A small unit of time, called a time quantum or time slice, is defined.
• A time quantum is generally from10 to 100 milliseconds in length.
• The ready queue is treated as a circular queue.
• The CPU scheduler goes around the ready queue, allocating the CPU to each process
for a time interval of up to 1 time quantum.
• To implement RR scheduling, we again treat 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 from the ready queue, sets 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.
• 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 he ready queue.
• The average waiting time under the RR policy is often long.
Example

• Consider the following set of processes that arrive at time 0, with the length of the
CPU burst given in milliseconds and time quantum of each process is 4
millisecond :
Process Burst Time
P1 24
P2 3
P3 3
Find waiting time.
Solution

• If we use a time quantum of 4 milliseconds, then process P1 gets the first 4 milliseconds.
• Since it requires 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.
• Process P2 does not need 4 milliseconds, so it quits before its time quantum expires.
• The CPU is then 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 as follows:

P1 P2 P3 P1 P1 P1 P1 P1
0 4 7 10 14 18 22 26 30
P1 P2 P3 P1 P1 P1 P1 P1
0 4 7 10 14 18 22 26 30

• P1 Waiting Time=10-4=6 P1 TAT= 30-0=30


• P2 Waiting Time=4 P2 TAT= 7-0=7
• P3 Waiting Time=7 P3 TAT=10
• Average WT=(6+4+7)/3 Avg. TAT=(30+7+10)/3=47/3=
• 17/3=5.66 millisecond 15.66 Millisecond
• Some more formula
• Waiting time =Turn around time – Burst Time
• Turn Around Time = Exit Time – Arrival Time
Some More Example

• Consider the set of 5 processes whose arrival time and burst time are given below-
Solution

Average Turn Around time =


(13 + 11 + 3 + 6 + 10) / 5 =
43 / 5 = 8.6 unit
Average waiting time =
(8 + 8 + 2 + 4 + 7) / 5 =
29 / 5 = 5.8 unit
P1 P2 P3 P4 P5 P1 P2 P5
Example-3

• Consider the set of 6 processes whose arrival time and burst time are
given below-
P1 P2 P3 P4 P5 P6 P1 P2 P5 P6 P2 P5

• WT=
• P1 WT= TAT-Burst Time 13-4=09
• P2 WT= 19-5=14
• P3
Example

• Consider the set of 5 processes whose arrival time and burst time are
given below-
Solution
Example 2
Example-SJF
Example
Multilevel Queue

• Ready queue is partitioned into separate queues, eg:


• foreground (interactive)
• background (batch)
• Process permanently in a given queue
• Each queue has its own scheduling algorithm:
• foreground – RR
• background – FCFS
• Scheduling must be done between the queues:
• Fixed priority scheduling; (i.e., serve all from foreground then from background). Possibility of
starvation.
• Time slice – each queue gets a certain amount of CPU time which it can schedule amongst its processes;
i.e., 80% to foreground in RR
• 20% to background in FCFS
Multilevel Feedback Queue

• MuA process can move between the various queues; aging can be
implemented this way
• Multilevel-feedback-queue scheduler defined by the following parameters:
• number of queues
• scheduling algorithms for each queue
• method used to determine when to upgrade a process
• method used to determine when to demote a process
• method used to determine which queue a process will enter when that process
needs service
• ltilevel Feedback Queue
Example of Multilevel Feedback Queue

Three queues:
Q0 – RR with time quantum 8 milliseconds
Q1 – RR time quantum 16 milliseconds
Q2 – FCFS

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

• A cooperating process is one that can affect or be affected by other processes


executing in the system.
• Cooperating processes can either directly share a logical address space (that is, both
code and data) or be allowed to share data only through files or messages.
• A situation like this, where several processes access and manipulate the same data
concurrently and the outcome of the execution depends on the particular order in which
the access takes place, is called a race condition.
• To guard against the race condition above, we need to ensure that only one process at a
time can be manipulating the variable counter.
• In such applications, several threads—which are quite possibly sharing data—are
running in parallel on different processing cores. Clearly,
• we want any changes that result from such activities not to interfere with one
another.
• Because of the importance of this issue, we devote a major portion of this chapter
to process synchronization and coordination among cooperating processes.
The Critical-Section Problem

• We begin our consideration of process synchronization by discussing the so called


critical-section problem.
• Consider a system consisting of n processes {P0, P1, ..., Pn−1}.
• Each process has a segment of code, called a critical section, in which the process
may be changing common variables, updating a table, writing a file, and so on.
• The important feature of the system is that, when one process is executing in its
critical section, no other process is allowed to execute in its critical section. That
is, no two processes are executing in their critical sections at the same time.
• The critical-section problem is to design a protocol that the processes can use to
cooperate.
• Each process must request permission to enter its
critical section. The section of code implementing this
request is the entry section.
• The critical section may be followed by an exit
section.
• The remaining code is the remainder section.
• The general structure of a typical process Pi is shown
in Figure
• The entry section and exit section are enclosed in boxes
to highlight these important segments of code.
• The entry section and exit section are enclosed in boxes
to highlight these important segments of code.
A solution to the critical-section problem must satisfy the following three
requirements:
1. Mutual exclusion. If process Pi is executing in its critical section, then no
other processes can be executing in their critical sections.
2. Progress. If no process is executing in its critical section and some processes
wish to enter their critical sections, then only those processes that are not
executing in their remainder sections can participate in deciding which will
enter its critical section next, and this selection cannot be postponed
indefinitely.
3. Bounded waiting. There exists a bound, or limit, on the number of times that
other processes are allowed to enter their critical sections after a process has
made a request to enter its critical section and before that request is granted.
• Two general approaches are used to handle critical sections in operating systems:
preemptive kernels and nonpreemptive kernels.
• A preemptive kernel allows a process to be preempted while it is running in kernel
mode.
• A nonpreemptive kernel does not allow a process running in kernel mode to be
preempted; a kernel-mode process will run until it exits kernel mode, blocks, or
voluntarily yields control of the CPU.
• Later in this chapter, we explore how various operating systems manage
preemption within the kernel.
Peterson’s Solution

• Next, we illustrate a classic software-based solution to the critical-section problem


known as Peterson’s solution.
• Because of the way modern computer architectures perform basic machine-
language instructions, such as load and store, there are no guarantees that
Peterson’s solution will work correctly on such architectures.
• However, we present the solution because it provides a good algorithmic
description of solving the critical-section problem and illustrates some of the
complexities involved in designing software that addresses the requirements of
mutual exclusion, progress, and bounded waiting.
• Peterson’s solution is restricted to two
processes that alternate execution between
their critical sections and remainder sections.
• The processes are numbered P0 and P1.
• For convenience, when presenting Pi , we
use Pj to denote the other process; that is, j
equals 1 − i.
• Peterson’s solution requires the two
processes to share two data items:
int turn;
boolean flag[2];
• The variable turn indicates whose turn it is to enter its critical section. That is, if
turn == i, then process Pi is allowed to execute in its critical section.
• The flag array is used to indicate if a process is ready to enter its critical section.
• For example, if flag[i] is true, this value indicates that Pi is ready to enter its
critical section.
• With an explanation of these data structures complete, we are now ready to
describe the algorithm shown in Figure.
• To enter the critical section, process Pi first sets flag[i] to be true and then sets
turn to the value j, thereby asserting that if the other process wishes to enter the
critical section, it can do so.
• We now prove that this solution is correct. We need to show that:
1. Mutual exclusion is preserved.
2. The progress requirement is satisfied.
3. The bounded-waiting requirement is met.
Semaphores

• A semaphore S is an integer variable


that, apart from initialization, is
accessed only through two standard
atomic operations: wait() and signal().
• The wait() operation was originally
termed P (from the Dutch proberen, “to
test”); signal() was originally called V
(from verhogen, “to increment”).
• The definition of wait() is as follows:
• All modifications to the integer value of the semaphore in the wait() and signal()
operations must be executed indivisibly.
• That is, when one process modifies the semaphore value, no other process can
simultaneously modify that same semaphore value.
• In addition, in the case of wait(S), the testing of the integer value of S (S ≤ 0), as
well as its possible modification (S--), must be executed without interruption.
Semaphore Usage

• Operating systems often distinguish between counting and binary semaphores.


• The value of a counting semaphore can range over an unrestricted domain.
• The value of a binary semaphore can range only between 0 and 1.
• Thus, binary semaphores behave similarly to mutex locks.
• In fact, on systems that do not provide mutex locks, binary semaphores can be used
instead for providing mutual exclusion.
• Counting semaphores can be used to control access to a given resource consisting
of a finite number of instances.
• The semaphore is initialized to the number of resources available.
• We can also use semaphores to solve various synchronization problems.
• For example, consider two concurrently running processes: P1 with a statement S1 and P2 with a
statement S2. Suppose we require that S2 be executed only after S1 has completed.
• We can implement this scheme readily by letting P1 and P2 share a common semaphore synch,
initialized to 0.
In process P1, we insert the statements
• S1;
• signal(synch);
In process P2, we insert the statements
• wait(synch);
• S2;
• Because synch is initialized to 0, P2 will execute S2 only after P1 has invoked signal(synch),
which is after statement S1 has been executed.
Classic Problems of Synchronization

• The Bounded-Buffer Problem


• The Readers–Writers Problem
• The Dining-Philosophers Problem
The Bounded-Buffer Problem

• The bounded-buffer problem is commonly used to illustrate the power of


synchronization primitives. Here, we present a general structure of this scheme
without committing ourselves to any particular implementation.
• In our problem, the producer and consumer processes share the following data
structures:
• int n;
• semaphore mutex = 1;
• semaphore empty = n;
• semaphore full = 0
• We assume that the pool consists of n buffers, each capable of holding one item.
• The mutex semaphore provides mutual exclusion for accesses to the buffer pool and
is initialized to the value 1.
• The empty and full semaphores count the number of empty and full buffers.
• The semaphore empty is initialized to the value n; the semaphore full is initialized to
the value 0.
• The code for the producer process is shown in Figure 5.9, and the code for the
consumer process is shown in Figure 5.10.Note the symmetry between the producer
and the consumer. We can interpret this code as the producer producing full buffers
for the consumer or as the consumer producing empty buffers for the producer.
The Readers–Writers Problem

• Suppose that a database is to be shared among several concurrent processes.


• Some of these processes may want only to read the database, whereas others may
want to update (that is, to read and write) the database.
• We distinguish between these two types of processes by referring to the former as
readers and to the latter as writers.
• Obviously, if two readers access the shared data simultaneously, no adverse effects
will result. However, if a writer and some other process (either a reader or a writer)
access the database simultaneously, chaos may ensue.
• To ensure that these difficulties do not arise, we require that the writers have
exclusive access to the shared database while writing to the database.
• This synchronization problem is referred to as the readers–writers problem.

You might also like