ISE OS Mod2
ISE OS Mod2
SEMESTER: 4
MODULE: 2
NUMBER OF HOURS: 08
CONTENTS:
Process :
• Process concept
• Process scheduling
• Operations on processes
• Inter process communication
Process Management:
• Basic concepts
• Scheduling Criteria;
• Scheduling Algorithms;
MODULE 2
PROCESS MANAGEMENT
Process Concept
The Process
Process memory is divided into four sections as shown in the figure below:
• The stack is used to store temporary data such as local variables, function parameters, function
return values, return address etc.
• The heap which is memory that is dynamically allocated during process run time
• The data section stores global variables.
• The text section comprises the compiled program code.
• Note that, there is a free space between the stack and the heap. When the stack is full, it grows
downwards and when the heap is full, it grows upwards.
1
Figure: Process in memory.
Process State
Q) Illustrate with a neat sketch , the process states and process control block .
Process State
A Process has 5 states. Each process may be in one of the following states –
For each process there is a Process Control Block (PCB), which stores the process-specific information
as shown below –
• Process State – The state of the process may be new, ready, running, waiting, and so on.
• Program counter – The counter indicates the address of the next instruction to be executed for
this process.
• CPU registers - The registers vary in number and type, depending on the computer architecture.
They include accumulators, index registers, stack pointers, and general-purpose registers. Along
2
with the program counter, this state information must be saved when an interrupt occurs, to
allow the process to be continued correctly afterward.
• CPU scheduling information- This information includes a process priority, pointers to
scheduling queues, and any other scheduling parameters.
• Memory-management information – This includes information such as the value of the base
and limit registers, the page tables, or the 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 – This information includes the list of I/O devices allocated to the
process, a list of open files, and so on.
The PCB simply serves as the repository for any information that may vary from process to process.
Process Scheduling
3
Scheduling Queues
• As processes enter the system, they are put into a job queue, which consists of all processes in
the system.
• The processes that are residing in main memory and are ready and waiting to execute are kept
on a list called the ready queue. This queue is generally stored as a linked list.
• A ready-queue header contains pointers to the first and final PCBs in the list. Each PCB includes
a pointer field that points to the next PCB in the ready queue.
4
Figure: Queueing-diagram representation of process scheduling.
Schedulers
Schedulers are software which selects an available program to be assigned to CPU.
• A long-term scheduler or Job scheduler – selects jobs from the job pool (of secondary
memory, disk) and loads them into the memory.
If more processes are submitted, than that can be executed immediately, such processes will be
in secondary memory. It runs infrequently, and can take time to select the next process.
• The short-term scheduler, or CPU Scheduler – selects job from memory and assigns the CPU
to it. It must select the new process for CPU frequently.
• The medium-term scheduler - selects the process in ready queue and reintroduced into the
memory.
An efficient scheduling system will select a good mix of CPU-bound processes and I/O bound
processes.
• If the scheduler selects more I/O bound process, then I/O queue will be full and ready queue
will be empty.
• If the scheduler selects more CPU bound process, then ready queue will be full and I/O queue
will be empty.
Time sharing systems employ a medium-term scheduler. It swaps out the process from ready
queue and swap in the process to ready queue. When system loads get high, this scheduler will
swap one or more processes out of the ready queue for a few seconds, in order to allow smaller
faster jobs to finish up quickly and clear the system.
5
Context switching
• The task of switching a CPU from one process to another process is called context switching.
Context-switch times are highly dependent on hardware support (Number of CPU registers).
• Whenever an interrupt occurs (hardware or software interrupt), the state of the currently running
process is saved into the PCB and the state of another process is restored from the PCB to the
CPU.
• Context switch time is an overhead, as the system does not do useful work while switching.
Operations on Processes
6
A process will need certain resources (CPU time, memory, files, I/O devices) to accomplish its task.
When a process creates a subprocess, the subprocess may be able to obtain its resources in two ways:
• directly from the operating system
• Subprocess may take the resources of the parent process.
The resource can be taken from parent in two ways –
▪ The parent may have to partition its resources among its children ▪
Share the resources among several children.
There are two options for the parent process after creating the child:
• Wait for the child process to terminate and then continue execution. The parent makes a wait()
system call.
• Run concurrently with the child, continuing to execute without waiting.
Two possibilities for the address space of the child relative to the parent:
• The child may be an exact duplicate of the parent, sharing the same program and data segments
in memory. Each will have their own PCB, including program counter, registers, and PID. This
is the behaviour of the fork system call in UNIX.
• The child process may have a new program loaded into its address space, with all new code and
data segments. This is the behaviour of the spawn system calls in Windows.
In UNIX OS, a child process can be created by fork() system call. The fork system call, if
successful, returns the PID of the child process to its parents and returns a zero to the child
7
process. If failure, it returns -1 to the parent. Process IDs of current process or its direct parent
can be accessed using the getpid( ) and getppid( ) system calls respectively.
The parent waits for the child process to complete with the wait() system call. When the child
process completes, the parent process resumes and completes its execution.
In windows the child process is created using the function createprocess( ). The createprocess( ) returns
1, if the child is created and returns 0, if the child is not created.
Process Termination
• A process terminates when it finishes executing its last statement and asks the operating
system to delete it, by using the exit () system call. All of the resources assigned to the
process like memory, open files, and I/O buffers, are deallocated by the operating system.
• A process can cause the termination of another process by using appropriate system call.
The parent process can terminate its child processes by knowing of the PID of the child.
• A parent may terminate the execution of children for a variety of reasons, such as:
• The child has exceeded its usage of the resources, it has been allocated.
• The task assigned to the child is no longer required.
• The parent is exiting, and the operating system terminates all the children. This is called
cascading termination.
Interprocess Communication
• Information Sharing - There may be several processes which need to access the same file. So
the information must be accessible at the same time to all users.
• Computation speedup - Often a solution to a problem can be solved faster if the problem can
be broken down into sub-tasks, which are solved simultaneously (particularly when multiple
8
processors are involved.)
• Modularity - A system can be divided into cooperating modules and executed by sending
information among one another.
• Convenience - Even a single user can work on multiple tasks by information sharing.
Cooperating processes require some type of inter-process communication. This is allowed by two
models:
1. Shared Memory systems 2.
Message passing systems.
2. Useful for sending large block of data Useful for sending small data.
System call is used only to create System call is used during every
3.
shared memory read and write operation.
Message is sent faster, as there are no
4. Message is communicated slowly.
system calls
• Shared Memory is faster once it is set up, because no system calls are required and access
occurs at normal memory speeds. Shared memory is generally preferable when large amounts
of information must be shared quickly on the same computer.
• Message Passing requires system calls for every message transfer, and is therefore slower, but
it is simpler to set up and works well across multiple computers. Message passing is generally
preferable when the amount and/or frequency of data transfers is small.
Shared-Memory Systems
• A region of shared-memory is created within the address space of a process, which needs to
communicate. Other process that needs to communicate uses this shared memory.
• The form of data and position of creating shared memory area is decided by the process.
Generally, a few messages must be passed back and forth between the cooperating processes
first in order to set up and coordinate the shared memory access.
9
• The process should take care that the two processes will not write the data to the shared memory
at the same time.
• This is a classic example, in which one process is producing data and another process is
consuming the data.
• The data is passed via an intermediary buffer (shared memory). The producer puts the data to
the buffer and the consumer takes out the data from the buffer. A producer can produce one item
while the consumer is consuming another item. The producer and consumer must be
synchronized, so that the consumer does not try to consume an item that has not yet been
produced. In this situation, the consumer must wait until an item is produced.
• There are two types of buffers into which information can be put – • Unbounded buffer
• Bounded buffer
• With Unbounded buffer, there is no limit on the size of the buffer, and so on the data produced
by producer. But the consumer may have to wait for new items.
• With bounded-buffer – As the buffer size is fixed. The producer has to wait if the buffer is full
and the consumer has to wait if the buffer is empty.
This example uses shared memory as a circular queue. The in and out are two pointers to the array.
Note in the code below that only the producer changes "in", and only the consumer changes "out".
10
Note that the buffer is empty when [ in == out]
Message-Passing Systems
A mechanism to allow process communication without sharing address space. It is used in distributed
systems.
• Message passing systems uses system calls for "send message" and "receive message".
• A communication link must be established between the cooperating processes before messages
can be sent.
• There are three methods of creating the link between the sender and the receiver- o
Direct or indirect communication (naming) o Synchronous or asynchronous
communication (Synchronization) o Automatic or explicit buffering.
1. Naming
Processes that want to communicate must have a way to refer to each other. They can use either direct
or indirect communication.
a) Direct communication the sender and receiver must explicitly know each other’s name. The
syntax for send() and receive() functions are as follows-
11
Disadvantages of direct communication – any changes in the identifier of a process, may have to
change the identifier in the whole system (sender and receiver), where the messages are sent and
received.
A mailbox or port is used to send and receive messages. Mailbox is an object into which messages can
be sent and received. It has a unique ID. Using this identifier messages are sent and received.
Two processes can communicate only if they have a shared mailbox. The send and receive functions
are –
• send (A, message) – send a message to mailbox A
• receive (A, message) – receive a message from mailbox A
2. Synchronization
The send and receive messages can be implemented as either blocking or non-blocking.
Blocking (synchronous) send - sending process is blocked (waits) until the message is
received by receiving process or the mailbox.
Non-blocking (asynchronous) send - sends the message and continues (does not wait)
3. Buffering
When messages are passed, a temporary queue is created. Such queue can be of three capacities:
Zero capacity – The buffer size is zero (buffer does not exist). Messages are not stored in
the queue. The senders must block until receivers accept the messages.
Bounded capacity- The queue is of fixed size(n). Senders must block if the queue is full.
After sending ‘n’ bytes the sender is blocked.
Unbounded capacity - The queue is of infinite capacity. The sender never blocks.
12
Process Management
Basic Concepts
• In a single-processor system,
▪ Only one process may run at a time.
▪ Other processes must wait until the CPU is rescheduled.
• Objective ofmultiprogramming:
▪ To have some process running at all times, in order to maximize CPU utilization.
13
CPU Scheduler
• Thisscheduler
▪ selects a waiting-process from the ready-queue and ▪ allocates
CPU to the waiting-process.
• The ready-queue could be a FIFO, priority queue, tree andlist.
• The records in the queues are generally process control blocks (PCBs) of theprocesses.
CPU Scheduling
• Four situations under which CPU scheduling decisions takeplace:
1. When a process switches from the running state to the waiting state. For ex; I/O request.
2. When a process switches from the running state to the ready state. For ex: when an
interrupt occurs.
3. When a process switches from the waiting state to the ready state. For ex:
completion of I/O.
4. When a process terminates.
• Scheduling under 1 and 4 is non- preemptive. Scheduling under 2 and 3 is preemptive.
Preemptive Scheduling
• This is driven by the idea of prioritizedcomputation.
• Processes that are runnable may be temporarilysuspended Disadvantages:
1. Incurs a cost associated with access toshared-data.
2. Affects the design of the OSkernel.
Dispatcher
• It gives control of the CPU to the process selected by the short-termscheduler.
• The functioninvolves:
1. Switchingcontext
2. Switching to user mode&
3. Jumping to the proper location in the user program to restart that program
It should be as fast as possible, since it is invoked during every process switch.
• Dispatch latency means the time taken by the dispatcherto ▪ stop one process and
▪ start another running.
SCHEDULING CRITERIA:
In choosing which algorithm to use in a particular situation, depends upon the properties of the various
algorithms.Many criteria have been suggested for comparing CPUscheduling algorithms. The
criteria include the following:
14
1. 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 used system).
2. Throughput: If the CPU is busy executing processes, then work is being done. 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.
3. Turnaround time. This is the important criterion which tells how long it takes to execute
that process. 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/0.
4. Waiting time: The CPU-scheduling algorithm does not affect the amount of time during
which a process executes or does I/0, it affects only the amount of time that a process spends
waiting in the ready queue.Waiting time is the sum of the periods spent waiting in the ready
queue.
5. Response time:In an interactive system, turnaround time may not be the best criterion.
Often, a process can produce some output fairly early and can continue computing new
results while previous results are being output to the user. Thus, 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. The turnaround time is generally limited by the speed of the output device.
SCHEDULING ALGORITHMS
CPU scheduling deals with the problem of deciding which of the processes in the ready-
queue is to be allocated theCPU. Following are some schedulingalgorithms:
1. FCFS scheduling (First Come FirstServed)
2. Round Robin scheduling
3. SJF scheduling (Shortest JobFirst)
4. SRT scheduling
5. Priority scheduling
6. Multilevel Queue schedulingand
7. Multilevel Feedback Queuescheduling
FCFS Scheduling
• The process that requests the CPU first is allocated the CPUfirst.
• The implementation is easily done using a FIFOqueue.
• Procedure:
1. When a process enters the ready-queue, its PCB is linked onto the tail of thequeue.
2. When the CPU is free, the CPU is allocated to the process at the queue’shead.
3. The running process is then removed from the queue.
• Advantage:
1. Code is simple to write & understand.
• Disadvantages:
15
1. Convoy effect: All other processes wait for one big process to get off theCPU.
2. Non-preemptive (a process keeps the CPU until it releasesit).
3. Not good for time-sharingsystems.
4. The average waiting time is generally notminimal.
• Example: Suppose that the processes arrive in the order P1, P2,P3.
SJF Scheduling
• The CPU is assigned to the process that has the smallest next CPUburst.
• If two processes have the same length CPU burst, FCFS scheduling is used to break thetie.
• For long-term scheduling in a batch system, we can use the process time limit specified by the
user, as the‘length’
• SJF can't be implemented at the level of short-term scheduling, because there is no way to know
the length of the next CPUburst Advantage:
1. The SJF is optimal, i.e. it gives the minimum average waiting time for a given set of
processes.
• Disadvantage:
1. Determining the length of the next CPU burst.
16
2. PreemptiveSJF
If the new process has a shorter next CPU burst than what is left of the executing
process, that process is preempted. It is also known as SRTF scheduling (Shortest-
Remaining-Time-First).
• Example (for non-preemptive SJF): Consider the following set of processes, with the length of
the CPU-burst time given inmilliseconds.
preemptive SJF/SRTF: Consider the following set of processes, with the length
Priority Scheduling
• A priority is associated with eachprocess.
• The CPU is allocated to the process with the highestpriority.
• Equal-priority processes are scheduled in FCFSorder.
• Priorities can be defined either internally orexternally.
1. Internally-defined priorities.
▪ Use some measurable quantity to compute the priority of a process.
▪ For example: time limits, memory requirements, no. f open files.
17
2. Externally-defined priorities.
▪ Set by criteria that are external to the OS For example:
▪ importance of the process, political factors
• Priority scheduling can be either preemptive or non-preemptive.
1.Preemptive
The CPU is preempted if the priority of the newly arrived process is higher than the priority
of the currently running process.
2. Non Preemptive
Advantage:
▪ Higher priority processes can be executed first.
• Disadvantage:
▪ Indefinite blocking, where low-priority processes are left waiting indefinitely
for CPU. Solution: Aging is a technique of increasing priority of processes
that wait in system for a long time.
Example: Consider the following set of processes, assumed to have arrived at time 0, in the
order PI, P2, ..., P5, with the length of the CPU-burst time given inmilliseconds.
18
The ready-queue is kept as a FIFO queue of processes
• CPUscheduler
1. Picks the first process from theready-queue.
2. Sets a timer to interrupt after 1 time quantumand
3. Dispatches theprocess.
• One of two things will thenhappen.
1. The process may have a CPU burst of less than 1 time quantum. In this case, the process
itself will release the CPU voluntarily.
2. 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 OS. The process will be put at the
tail of the ready-queue.
• Advantage:
▪ Higher average turnaround than SJF.
• Disadvantage:
▪ Better response time than SJF.
• Example: Consider the following set of processes that arrive at time 0, with the length of the
CPU-burst time given inmilliseconds.
19
Fig: How a smaller time quantum increases context switches
20