The Process:
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 thatis dynamically allocated during process run time. The structure of a process in memory is
shown in Figure
We emphasize that a 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), whereas a process is an
active entity, with a program counter specifying the next instruction to execute and a set of associated
resources.
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. Each process may be in one of the following states:
New. The process is being created.
Running. Instructions are being executed.
Waiting. The process is waiting for some event to occur (such as an I/0 completion or reception of a
signal).
Ready. The process is waiting to be assigned to a processor.
Terminated. The process has finished execution.
Fig: Process state
Process Control Block
Each process is represented in the operating system by a process_ control block _ (PCB)-also called a
task control block. A PCB is shown in Figure . It contains many pieces of information associated with a
specific process, including these:
Process control block
Process state. The state may be new, ready running, waiting, halted, 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
may include accumulators, index registers, stack pointers, and general-purpose registers, plus any
condition-code information.
CPU-scheduling information. This information includes a process priority, pointers to scheduling queues,
and any other scheduling parameters.
Memory-management information: This information may include such information as the value of the
base and limit registers, the page tables, or the segment tables, depend on the memory system used by
the operating system
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.
In brief the PCB simply serves as the repository for any information that may vary from process to
process.
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. For a single-processor system, there will never be more than one running process. If there are
more processes, the rest will have 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 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.
The system also includes other queues. When a process is allocated the CPU, it executes for a while and
eventually quits, is interrupted, or waits for the occurrence of a particular event, such as the completion
of an I/0 requests. Suppose the process makes an I/O request to a shared device, such as a disk. Since
there are many processes in the system, the disk may be busy with the I/0 request of some other
process. The process therefore may have to wait for the disk. The list of processes waiting for a
particular I/0 device is called a device queue.
A common representation of process scheduling is a queueing diagram, such as that in Figure . Each
rectangular box represents a queue. Two types of queues are present: the ready queue and a set of
device queues. The circles represent the resources that serve the queues, and the arrows indicate the
flow of processes in the system.
A new process is initially put in the ready queue. It waits there until it is selected for execution, or is
dispatched. Once the process is allocated the CPU and is executing, one of several events could occur:
1.The process could issue an I/0 request and then be placed in an I/0 queue.
2.The process could create a new subprocess and wait for the subprocess's termination
3. The process could be removed forcibly from the CPU, as a result of an interrupt, and be put back in
the ready queue.
Schedulers:
A process migrates among the various scheduling queues throughout its lifetime. The operating system
must select, for scheduling purposes, and the selection process is carried out by the appropriate
scheduler.
The long-term scheduler, or job scheduler, selects processes from this pool and loads them into memory
for execution. 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/0 request.
The long-term scheduler executes much less frequently; minutes may separate the creation of one new
process and the next. The long-term scheduler controls the degree of multiprogramming (the number of
processes in memory). If the degree of multiprogramming is stable, then the average rate of process
creation must be equal to the average departure rate of processes leaving the system.
It is important that the long-term scheduler make a careful selection. In general, most processes can be
described as I/ 0 bound or CPU bound. An I/O-bound process is one that spends more of its time doing
I/O than it spends doing computations. A CPU-bound process, in contrast, generates I/0 requests
infrequently, using more of its time doing computations. It is important that the long-term scheduler
select a good process mix of I/O-bound and CPU-bound processes.
The key idea behind a medium-term scheduler is that sometimes it can be advantageous to remove
processes from memory (and from active contention for the CPU) and thus reduce the degree of
multiprogramrning. 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.
Context Switch
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; it includes the value of the
CPU registers, the process state and memory-management information. Generically, we perform a state
save of the current state of the CPU, and then a state restore to resume operations.
Switching the CPU to another process requires performing a state save of the current process and a
state restore of a different process. This task is known as a context switch. When a context switch
occurs, the kernel saves the context of the old process in its PCB and loads the saved context of the new
process scheduled to run. Context-switch time is pure overhead, because the system does no useful
work while switching. Its speed varies from machine to machine, depending on the memory speed.