Unit 2 Process MGMT

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 57

Process Management

Unit 2
Introduction
• A process can be thought of as a program in execution.
• A process is the unit of work in most systems. Systems
consist of a collection of processes: operating-system
processes execute system code, and user processes execute
user code. All these processes may execute concurrently.
• Although traditionally a process contained only a single
thread of control as it ran, most modern operating systems
now support processes that have multiple threads.
VGK
Process Concept
• A process is a program in execution. A process is more than the
program code, which is sometimes known as the text section.
• It also includes the current activity, as represented by the value of the
program counter and the contents of the processor’s registers.
• A process generally also includes the process stack, which contains
temporary data (such as function parameters, return addresses, and
local variables), and a data section, which contains global variables.
• A process may also include a heap,
which is memory that is dynamically
allocated during process run time.
VGK
Process Concept
• Program by itself is not a process.
• A program is a passive entity, such as a file containing a list of
instructions stored on disk (often called an executable file).
• In contrast, a process is an active entity, with a program counter
specifying the next instruction to execute and a set of associated
resources.
• A program becomes a process when an executable file is loaded into
memory.
• Two common techniques for loading executable files: double-clicking
an icon representing the executable file and entering the name of the
executable file on the command line (as in prog.exe or a.out).VGK
s

• Although two processes may be associated with the same program,


they are nevertheless considered two separate execution sequences.
• For instance, several users may be running different copies of the
mail program, or the same user may invoke many copies of the web
browser program.
• Each of these is a separate process; and although the text sections are
equivalent, the data, heap, and stack sections vary.
• It is also common to have a process that spawns many processes as it
runs.

VGK
Process State
• As a process executes, it changes state. The state of a process is
defined in part by the current activity of that process. A process may
be in one of the following states:

VGK
Process Control Block
• Each process is represented in the operating system by a process
control block (PCB)—also called a task control block.
• It contains many pieces of information associated with a specific
process, including these:
1] Process state. The state may be new, ready, running, waiting, halted,
and so on.
2] Program counter. The counter indicates
the address of the next instruction
to be executed for this process.
VGK
3] 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, plus any condition-code
information.

4] CPU-scheduling information. This information includes a process


priority, pointers to scheduling queues, and any other scheduling
parameters.

5] Memory-management information- This information may include


such items as the value of the base and limit registers and the page
tables, or the segment tables, depending on the memory system used
by the operating system VGK
ss

6]Accounting information. This information includes the amount of


CPU and real time used, time limits, account numbers, job or process
numbers, and so on.

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

VGK
Threads
• a process is a program that performs a single thread of execution.
• For example, when a process is running a word-processor program, a
single thread of instructions is being executed.
• This single thread of control allows the process to perform only one
task at a time.
• Most modern operating systems have extended the process concept
to allow a process to have multiple threads of execution and thus to
perform more than one task at a time.

VGK
Process Scheduling
• The objective of multiprogramming is to have some process running
at all times, to maximize 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 (possibly from a set of several available processes) for
program execution on the CPU.
1]For a single-processor system, there will never be more than one
running process.
2] If there are more processes, the rest will have to wait untilVGK
the CPU is
free and can be rescheduled
Scheduling Queues
1] job queue-As processes enter the system, they are put into a job
queue, which consists of all processes in the system.
2] ready queue-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
• The system also includes other queues. The list of processes waiting
for a particular I/O device is called a device queue. Each VGK
device has
its own device queue.
s

VGK
s

VGK
Schedulers
• A process migrates among the various scheduling queues throughout
its lifetime.
• The operating system must select, for scheduling purposes, processes
from these queues in some fashion. The selection process is carried
out by the appropriate scheduler.

• There are three types of schedulers available:


• Long Term Scheduler
• Short Term Scheduler
• Medium Term Scheduler VGK
Schedulers
1] The long-term scheduler, or job scheduler, selects processes from
the pool and loads them into memory for execution.
2] The short-term scheduler, or CPU scheduler, selects from among
the processes that are ready to execute and allocates the CPU to one of
them.
• The primary distinction between these two schedulers lies in
frequency of execution.
• The short-term scheduler must select a new process for the CPU
frequently. A process may execute for only a few milliseconds before
waiting for an I/O request. Often, the short-term scheduler executes
at least once every 100 milliseconds.
• The long-term scheduler controls the degree of multiprogramming
VGK
(the number of processes in memory)
3]medium-term scheduler
• The key idea behind a medium-term scheduler is that sometimes it
can be advantageous to remove a process 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.
• The process is swapped out, and is later swapped in, by the medium-
term scheduler

VGK
Some operating systems, such as time-sharing systems, may introduce an
additional, intermediate level of scheduling

VGK
Context Switch
• Interrupts cause the operating system to change a CPU from
its current task and to run a kernel routine. Such operations
happen frequently on general-purpose systems.
• When an interrupt occurs, the system needs to save the
current context of the process running on the CPU so that it
can restore that context when its processing is done,
essentially suspending the process and then resuming it.
• The context is represented in the PCB of the process
VGK
s

VGK
Operations on Processes
• 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 VGK a process
within the kernel
Process creation- Tree of processes on a typical linux

VGK
s

• 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.
• 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.
• The sshd process is responsible for managing clients that connect to the
system by using ssh (which is short for secure shell).
• 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 theVGKprocess
ps as well as the emacs editor.
s

VGK
Process Termination
• A process terminates when it finishes executing its final statement and asks
the operating system to delete it by using the exit() system call.
• At that point, the process may return a status value (typically an integer) to
its parent process (via the wait() system call).
• All the resources of the process—including physical and virtual memory,
open files, and I/O buffers—are deallocated by the operating system.
• Termination can occur in other circumstances as well. A process can cause
the termination of another process via an appropriate system call (for
example, TerminateProcess() inWindows) Usually, such a system call can be
invoked only by the parent of the process that is to be terminated.
• Otherwise, users could arbitrarily kill each other’s jobs VGK
s

• A parent may terminate the execution of one of its children


for a variety of reasons, such as these:
1]The child has exceeded its usage of some of the resources
that it has been allocated. (To determine whether this has
occurred, the parent must have a mechanism to inspect the
state of its children.)
2] The task assigned to the child is no longer required.
3] The parent is exiting, and the operating system does not
allow a child to continue if its parent terminates.

VGK
Interprocess Communication
• Processes executing concurrently in the operating system may be
either independent processes or cooperating processes.
• A process is independent if it cannot affect or be affected by the
other processes executing in the system.
• Any process that does not share data with any other process is
independent.
• A process is cooperating if it can affect or be affected by the other
processes executing in the system.
• Clearly, any process that shares data with other processes is a
cooperating process
VGK
• There are several reasons for providing an environment that allows process cooperation:

• Information sharing. Since several users may be interested in the same


piece of information (for instance, a shared file), we must provide an
environment to allow concurrent access to such information.

• Computation speedup. If we want a particular task to run faster, we must


break it into subtasks, each of which will be executing in parallel with the
others. Notice that such a speedup can be achieved only if the computer
has multiple processing cores.

• Modularity. We may want to construct the system in a modular fashion,


dividing the system functions into separate processes or threads

VGK
• Convenience. Even an individual user may work on many tasks at the same
time. For instance, a user may be editing, listening to music, and compiling
in parallel.
• Cooperating
s processes require an interprocess
communication (IPC) mechanism that will allow them to
exchange data and information.
• There are two fundamental models of interprocess
communication: shared memory and message passing.
• In the shared-memory model, a region of memory that is
shared by cooperating processes is established.
• Processes can then exchange information by reading and
writing data to the shared region.

• In the message-passing model, communication takes


VGK
place
by means of messages exchanged between the cooperating
processes
s

VGK
Shared memory systems
• Interprocess communication using shared memory requires
communicating processes to establish a region of shared memory.
• Typically, a shared-memory region resides in the address space of the
process creating the shared-memory segment.
• Other processes that wish to communicate using this shared-memory
segment must attach it to their address space.
• normally, the operating system tries to prevent
one process from accessing another process’s memory.
• Shared memory requires that two or more processes
agree to remove this restriction
VGK
Message passing Systems
• 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.
• For example, an Internet chat program could be designed so that chat
participants communicate with one another by exchanging messages

VGK
• A message-passing facility provides at least two operations:
s

1] send(message) 2] receive(message)

• Messages sent by a process can be either fixed or variable in size.


• if only fixed-sized messages can be sent, the system-level
implementation is straightforward, but makes the task of
programming more difficult.
• variable-sized messages require a more complex system level
implementation, but the programming task becomes simpler

VGK
s

• If processes P and Q want to communicate, they must 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
• There are several methods for logically
implementing a link and the send()/receive()
operations:
• Direct or indirect communication
• Synchronous or asynchronous communication
• Automatic or explicit buffering
VGK
Naming

VGK
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.
• 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. VGK
• receive(A, message)—Receive a message from mailbox A.
process scheduling
• CPU scheduling is the basis of multi-programmed operating systems.

• By switching the CPU among processes, the operating system can make the
computer more productive.
• 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.
• 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
VGK
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.

VGK
• CPU–I/O Burst Cycle:-
• 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.


• CPU burst is when the process is being executed in the CPU.
• I/O burst is when the CPU is waiting
for I/O for further execution

• Process execution begins with a CPU burst.


• . Eventually, the final CPU burst
ends with a system
request to terminate execution
VGK
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.
• Note that the ready queue is not necessarily a first-in, first-out (FIFO)
queue.
• Depends on scheduling algorithms, a ready queue can be
implemented as a FIFO queue, a priority queue, a tree, or simply an
unordered linked list. VGK
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

VGK
• The time it takes for the dispatcher to stop one process and start
another running is known as the dispatch latency.
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
VGK for
new process (if one exists in the ready queue) must be selected
execution. There is a choice, however, for situations 2 and 3.
s

• When scheduling takes place only under circumstances 1 and 4, we


say that the scheduling scheme is nonpreemptive or cooperative.
Otherwise, it is preemptive.
• Under nonpreemptive 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.

VGK
Scheduling Criteria
• Different CPU-scheduling algorithms have different properties.

• In choosing which algorithm to use in a particular situation, we must consider the


properties of the various algorithms.
• Many criteria have been suggested for comparing CPU-scheduling algorithms.
• The criteria include ……
• 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. If the CPU is busy executing processes, then work is being


done. One measure of work is the number of processes that are completed VGK
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. From the point of view of a particular process, the
s

important criterion is 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 = periods spent waiting to get into memory +
waiting in the ready queue + executing on the CPU + and doing I/O.
• Waiting time. The CPU-scheduling algorithm does not affect the
amount of time during which a process executes or does I/O. 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
• Response time.:- is the time from the submission of a request until
the first response is produced. This measure, called response VGK 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.
• 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. VGK
• The running process is then removed from the queue.
• the average waiting time under the FCFS policy is often quite long
• Consider the following set of processes that arrive at time 0, with the length of the CPU burst given
in milliseconds:

VGK
• the FCFS scheduling algorithm is nonpreemptive.
s

• Once the CPU has been allocated to a process, that process keeps the
CPU until it releases the CPU, either by terminating or by requesting
I/O.
• The FCFS algorithm is thus particularly troublesome for time-sharing
systems, where it is important that each user get a share of the CPU
at regular intervals.
• It would be disastrous to allow one process to keep the CPU for an
extended period

VGK
Problems on FCFS

VGK
s

VGK
s

VGK
Problem 2
• Assume you have the following jobs to execute with one processor
with the job arranging in the order
Processes Arrival time Burst time

P1 0 8
P2 1 4
P3 2 9
P4 3 5
P5 4 6

• Use FCFS scheduling and calculate average turnaroundtime VGK


and the
average waiting time for the processes
Shortest Job First (SJF) Scheduling
• Till now, we were scheduling the processes according to their arrival time
(in FCFS scheduling). However, SJF scheduling algorithm, schedules the
processes according to their burst time.
• In SJF scheduling, the process with the lowest burst time, among the list
of available processes in the ready queue, is going to be scheduled next.
• However, it is very difficult to predict the burst time needed for a process
hence this algorithm is very difficult to implement in the system.
• Advantages of SJF
• Maximum throughput
• Minimum average waiting and turnaround time
• Disadvantages of SJF
• May suffer with the problem of starvation
VGK
• It is not implementable because the exact Burst time for a process can't be
known in advance.
Example- Shortest Job First (SJF) Scheduling
• In the following example, there are five jobs named as P1, P2, P3, P4
and P5. Their arrival time and burst time are given in the table below.
• Calculate avg. waiting time and avg. turnaround time

VGK
Solution

VGK
Avg waiting time= 27/5
• Since, No Process arrives at time 0 hence; there will be an empty slot in
the Gantt chart from time 0 to 1 (the time at which the first process
arrives).
• According to the algorithm, the OS schedules the process which is having
the lowest burst time among the available processes in the ready queue.
• Till now, we have only one process in the ready queue hence the scheduler
will schedule this to the processor no matter what is its burst time.
• This will be executed till 8 units of time. Till then we have three more
processes arrived in the ready queue hence the scheduler will choose the
process with the lowest burst time.
• Among the processes given in the table, P3 will be executed next since it is
having the lowest burst time among all the available processes.
• So that's how the procedure will go on in shortest job first (SJF) VGK
scheduling
algorithm.
Ex 2-In the following example, there are five jobs named as P1, P2, P3, P4
and P5. Their arrival time and burst time are given in the table below.
Calculate avg. waiting time and avg. turnaround time

VGK

You might also like