Al3452 Os Unit-2
Al3452 Os Unit-2
A process willneed certain resources—such as CPU time, memory, files, and I/O
devices
—to accomplish its task.
These resources are allocated to the processeither when it is created or while it is
executing.
A process is the unit of work in most systems.
Systems consist ofa collection of processes: operating-systemprocesses execute
system
code, and user processes execute user code.
The operating system is responsible for several important aspects ofprocess and
thread management: the creation and deletion of both userand system processes; the
scheduling of processes; and the provision ofmechanisms for synchronization,
communication, and deadlock handlingfor processes.
Processes
Process Concept
Process State
Process Scheduling
The objective of multiprogramming is to have some process running at alltimes, to
maximize CPU utilization.
The objective of time sharing is to switch theCPU among processes so frequently that
users can interact with each programwhile it is running.
To meet these objectives, the process scheduler selectsan available process
forprogram execution on the CPU.
For a single-processor system, there will neverbe more than one running process.
If there are more processes, the rest willhave to wait until the CPU is free and can be
rescheduled.
Scheduling Queues
As processes enter the system, they are put into a job queue, which consistsof all
processes in the system.
The processes that are residing in main memoryand 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 containspointers to the first and final PCBs in the list. Each
PCB includes a pointer fieldthat points to the next PCB in the ready queue.
The system also includes other queues.
When a process is allocated theCPU, it executes for a while and eventually quits, is
interrupted, or waits forthe occurrence of a particular event, such as the completion of
an I/O request.
The list of processes waiting for a particular I/O device is called adevice queue. Each
device has its own device queue as shown below.
Each rectangular box represents a queue. Two typesof queues are present: the ready
queue and a set of device queues. The circlesrepresent the resources that serve the
queues, and the arrows indicate the flowof processes in the system.
A new process is initially put in the ready queue. It waits there until it isselected for
execution, or dispatched. Once the process is allocated the CPUand is executing, one
of several events could occur:
o The process could issue an I/O request and then be placed in an I/O queue.
o The process could create a new child process and wait for the
child’stermination.
o The process could be removed forcibly from the CPU, as a result of an
interrupt, and be put back in the ready queue.
In the first two cases, the process eventually switches fromthe waiting stateto the
ready state and is then put back in the ready queue. A process continuesthis cycle until
it terminates, at which time it is removed from all queues andhas its PCB and
resources deallocated.
Schedulers
A process migrates among the various scheduling queues throughout itslifetime.
The operating system must select, for scheduling purposes, processesfrom these
queues in some fashion.
The selection process is carried out by theappropriate scheduler.
Often, in a batch system, more processes are submitted than can be
executedimmediately. These processes are spooled to a mass-storage device (typically
adisk), where they are kept for later execution.
The long-term scheduler, or jobscheduler, selects processes from this pool and
loads them into memory forexecution.
The short-term scheduler, or CPU scheduler, selects from amongthe processes that
are ready to execute and allocates the CPU to one of them.
The short-term scheduler must select a new process for the CPUfrequently. A process
The long-term scheduler executes much less frequently; minutes may separatethe
creation of one new process and the next.
The long-term schedulercontrols the degree of multiprogramming (the number of
processes in memory).
It is important that the long-term scheduler make a careful selection. Ingeneral, most
processes can be described as either I/O bound or CPU bound.
An I/O-bound process is one that spends more of its time doing I/O thanit spends
doing computations.
A CPU-bound process, in contrast, generatesI/O requests infrequently, using more of
its time doing computations.
It isimportant that the long-term scheduler select a good process mix of I/O-boundand
CPU-bound processes.
Some operating systems, such as time-sharing systems, may introduce anadditional,
intermediate level of scheduling. This medium-term scheduler isdiagrammed in
Figure below.
Operations on Processes
The processes in most systems can execute concurrently, and they may be
created and deleted dynamically.
Thus, these systems must provide a mechanism for process creation and
termination.
Process Creation
During the course of execution, a process may create several new processes.
The creating process is called a parent process, and the new processes are
called the children of that process.
Each of these new processes may in turn create other processes, forming a tree
of processes.
Most operating systems (including UNIX, Linux, and Windows) identify
processes according to a unique process identifier (or pid), which is typically
an integer number.
The pid provides a unique value for each process in the system, and it can be
used as an index to access various attributes of a process within the kernel.
Figure illustrates a typical process tree for the Linux operating system,
showing the name of each process and its pid.
init
pid = 1
emacs tcsch
ps
pid = 9204 pid = 4005
pid = 9298
The init process (which always has a pid of 1) serves as the root parent process
for all user processes.
Once the system has booted, the init process can also create various user
processes, such as a web or print server, an ssh server, and the like.
In Figure, we see two children of init—kthreadd and sshd. The kthreadd
process is responsible for creating additional processes that perform tasks on
behalf of the kernel (in this situation, khelper and pdflush).
The sshd process is responsible for managing clients that connect to the
system by using ssh.
The login process is responsible for managing clients that directly log onto the
system.
In this example, a client has logged on and is using the bash shell, which has
been assigned pid 8416.
Using the bash command-line interface, this user has created the process ps as
well as the emacs editor.
On UNIX and Linux systems, we can obtain a listing of processes by using the
ps command.
For example, the command
ps -el
will list complete information for all processes currently active in the system.
When a process creates a new process, two possibilities for execution exist:
1. The parent continues to execute concurrently with its children.
2. The parent waits until some or all of its children have terminated.
There are also two address-space possibilities for the new process:
1. The child process is a duplicate of the parent process (it has the same
program and data as the parent).
2. The child process has a new program loaded into it.
To illustrate these differences, let’s first consider the UNIX operating system.
In UNIX, each process is identified by its process identifier, which is a unique
integer.
A new process is created by the fork() system call.
The new process consists of a copy of the address space of the original
process.
This mechanism allows the parent process to communicate easily with its
child process.
Figure illustrates the UNIX system calls
item next_consumed;
while (true) {
while (in == out)
; /* do nothing */
next_consumed = buffer[out];
out = (out + 1) % BUFFER_SIZE;
/* consume the item in next consumed */
}
This scheme allows at most BUFFER SIZE − 1 items in the buffer at the same
time.
Message-Passing Systems
The scheme requires that these processes share a region of memory and that
the code for accessing and manipulating the shared memory be written
explicitly by the application programmer.
Another way to achieve the same effect is for the operating system to provide
the means for cooperating processes to communicate with each other via a
message-passing facility.
Message passing provides a mechanism to allow processes to communicate
and to synchronize their actions without sharing the same address space.
It is particularly useful in a distributed environment, where the communicating
processes may reside on different computers connected by a network.
A message-passing facility provides at least two operations:
send(message) receive(message)
Messages sent by a process can be either fixed or variable in size
If processes P andQwant to communicate, theymust send messages to and
receive messages from each other: a communication link must exist between
them.
This link can be implemented in a variety of ways.
Here are several methods for logically implementing a link and the
send()/receive() operations:
1. Direct or indirect communication
2. Synchronous or asynchronous communication
3. Automatic or explicit buffering
We look at issues related to each of these features next.
Direct Communication
Naming
Processes that want to communicate must have a way to refer to each other.
They can use either direct or indirect communication.
Under direct communication, each process that wants to communicate must explicitly
name the recipient or sender of the communication.
In this scheme, the send() and receive() primitives are defined as:
• send(P, message)—Send a message to process P.
• receive(Q, message)—Receive a message from process Q.
A communication link in this scheme has the following properties:
• 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.
• A link is associated with exactly two processes.
• Between each pair of processes, there exists exactly one link.
This scheme exhibits symmetry in addressing; that is, both the sender process and the
receiver process must name the other to communicate.
In this scheme, the send() and receive() primitives are defined as follows:
• send(P, message)—Send a message to process P.
• receive(id, message)—Receive a message from any process.
The variable id is set to the name of the process with which communication has taken
place.
Disadvantages
Limited modularity of the resulting process definitions
Changing the identifier of a process may necessitate examining all other
process definitions.
Indirect communication
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 processes and from which messages can be removed.
Each mailbox has a unique identification.
A process can communicate with another process via a number of different
mailboxes, but two processes can communicate only if they have a shared mailbox.
The send() and receive() primitives are defined as follows:
• send(A, message)—Send a message to mailbox A.
• receive(A, message)—Receive a message from mailbox A.
In this scheme, a communication link has the following properties:
• A link is established between a pair of processes only if both members of
the pair have a shared mailbox.
• A link may be associated with more than two processes.
• Between each pair of communicating processes, a number of different links may
exist, with each link corresponding to one mailbox.
Mailbox sharing
P1, P2, and P3 share mailbox A
P1, sends; P2 and P3 receive
Who gets the message?
Solutions
Allow a link to be associated with at most two processes
Allow only one process at a time to execute a receive operation
Allow the system to select arbitrarily the receiver. Sender is notified
who the receiver was.
Buffering
Whether communication is direct or indirect, messages exchanged by communicating
processes reside in a temporary queue.
Basically, such queues can be implemented in three ways:
• Zero capacity. The queue has a maximum length of zero; thus, the link cannot have
any messages waiting in it. In this case, the sender must block until the recipient
receives the message.
• Bounded capacity. The queue has finite length n; thus, at most n messages can
reside in it. If the queue is not full when a new message is sent, the message is placed
in the queue and the sender can continue execution without waiting. The link’s
capacity is finite, however. If the link is full, the sender must block until space is
available in the queue.
• Unbounded capacity. The queue’s length is potentially infinite; thus, any
number of messages can wait in it. The sender never blocks.
CPU Scheduler
Short-term scheduler selects from among the processes in ready queue, and allocates
the CPU to one of them
Queue may be ordered in various ways
CPU scheduling decisions may take place when a process:
1. Switches from running to waiting state
2. Switches from running to ready state
3. Switches from waiting to ready
4. Terminates
Scheduling under 1 and 4 is nonpreemptive
All other scheduling is preemptive
Consider access to shared data
Consider preemption while in kernel mode
Consider interrupts occurring during crucial OS activities
Dispatcher
Dispatcher module gives control of the CPU to the process selected by the short-term
scheduler; this involves:
o switching context
o switching to user mode
o jumping to the proper location in the user program to restart
that program
Dispatch latency – time it takes for the dispatcher to stop one process and start another
running
Scheduling Criteria
CPU utilization – keep the CPU as busy as possible
Throughput – # of processes that complete their execution per time unit
Turnaround time – amount of time to execute a particular process
Waiting time – amount of time a process has been waiting in the ready queue
Response time – amount of time it takes from when a request was submitted until the
first response is produced, not output (for time-sharing environment)
P4 P1 P3 P2
0 3 9 16 24
P1 P2 P4 P1 P3
0 1 5 10 17 26
Preemptive
Nonpreemptive
SJF is priority scheduling where priority is the inverse of predicted next CPU burst
time
Problem Starvation – low priority processes may never execute
Solution Aging – as time progresses increase the priority of the process
Example of Priority Scheduling
Process Burst Time Priority
P1 10 3
P2 1 1
P3 2 4
P4 1 5
P5 5 2
Priority scheduling Gantt Chart
P1 P2 P3 P1 P1 P1 P1 P1
0 4 7 10 14 18 22 26 30