PROCESS
PRay@CSE
What to call all the CPU activities ?
A batch system executes jobs, whereas a time-shared
system has user programs, or tasks.
Even on a single-user system such as Microsoft
Windows, a user may be able to run several programs
at one time: a word processor, a web browser, and an
e-mail package.
In many respects, all these activities are similar, so we
call all of them “Processes”.
PRay@CSE
The terms job and process are used almost
interchangeably in this text.
Although we prefer the term process, much of
operating-system theory and terminology was
developed during a time, when the major
activity of operating systems was job
processing.
PRay@CSE
PROCESS CONCEPT-AN INTRODUCTION
A process is an activity of some kind. It has a program
code, input, output and a state.
We define the process as an instance of a program in
execution.
It is the unit of work in a modern time-sharing
operating system.
Note that a process is more than the program code.
PRay@CSE
COMPARISON OF A PROCESS AND A PROGRAM
PRay@CSE
Process
Therefore, we can think of a process as consisting of
three components:
An executable program.
The associated data needed by the program
(variables, work space, buffers, etc.).
The execution context of the program (Process
State).
PRay@CSE
This last element is essential. The execution
context, or process state, is the internal data, by
which the operating system is able to supervise
and control the process.
This internal information is separated from the
process, because the operating system has
information not permitted to the process.
PRay@CSE
The context includes all of the information that
the operating system needs to manage the
process and that the processor needs to execute
the process properly.
The context includes the contents of the various
processor registers, such as the program
counter and data registers.
PRay@CSE
PRay@CSE
Previous figure 2.8 indicates a way in which processes may be managed.
Two processes, A and B, exist in portions of main memory. That is, a block
of memory is allocated to each process that contains the program, data,
and context information.
Each process is recorded in a process list built and maintained by the OS.
The process list contains one entry for each process, which includes a
pointer to the location of the block of memory that contains the process.
The entry may also include part or all of the execution context of the
process.
The remainder of the execution context is stored elsewhere, perhaps with
the process itself (as indicated in Figure 2.8) or frequently in a separate
region of memory.
PRay@CSE
The process index register contains the index into the process list of the
process currently controlling the processor.
The program counter points to the next instruction in that process to be
executed.
The base and limit registers define the region in memory occupied by the
process: The base register is the starting address of the region of memory
and the limit is the size of the region (in bytes or words).
The program counter and all data references are interpreted relative to the
base register and must not exceed the value in the limit register. This
prevents interprocess interference.
In Figure 2.8, the process index register indicates that process B is executing.
Process A was previously executing but has been temporarily interrupted.
PRay@CSE
Process
Several definitions of the term “process”;
A program in execution.
An instance of a program running on a computer.
The entity that can be assigned to and executed on a
processor.
A unit of activity characterized by the execution of a
sequence of instructions, a current state, and an
associated set of system resources.
PRay@CSE
Process Control Block
There is a Process Control Block
for each process, enclosing all
the information about the
process.
It is also known as the task
control block. It is a data
structure, which contains the
following:
PRay@CSE
PCB
We can also think of a process as an entity that
consists of a number of elements.
Two essential elements of a process are program
code (which may be shared with other processes
that are executing the same program) and a set of
data associated with that code.
PRay@CSE
Let us suppose that the processor begins to execute this
program code, and we refer to this executing entity as a
process.
At any given point in time, while the program is executing, this
process can be uniquely characterized by a number of
elements, including the following:
Identifier: A unique identifier associated with this process, to
distinguish it from all other processes.
State: If the process is currently executing, it is in the running
state.
Priority: Priority level relative to other processes.
PRay@CSE
Program counter: The address of the next instruction in the program to be
executed.
Memory pointers: Includes pointers to the program code and data associated
with this process, plus any memory blocks shared with other processes.
Context data: These are data that are present in registers in the processor
while the process is executing.
I/O status information: Includes outstanding I/O requests, I/O devices (e.g.,
tape drives) assigned to this process, a list of files in use by the process, and
so on.
Accounting information: May include the amount of processor time and clock
time used, time limits, account numbers, and so on.
PRay@CSE
The information in the preceding list is stored in a data
structure, typically called a process control block (Figure 3.1),
that is created and managed by the OS.
The significant point about the process control block is that it
contains sufficient information so that it is possible to interrupt
a running process and later resume execution as if the
interruption had not occurred.
The process control block is the key tool that enables the OS to
support multiple processes and to provide for multiprocessing.
PRay@CSE
PRay@CSE
PRay@CSE
When a process is interrupted, the current values of the program counter
and the processor registers (context data) are saved in the appropriate
fields of the corresponding process control block, and the state of the
process is changed to some other value, such as blocked or ready (described
subsequently).
The OS is now free to put some other process in the running state.
The program counter and context data for this process are loaded into the
processor registers and this process now begins to execute.
Thus, we can say that a process consists of program code and associated
data plus a process control block.
For a single-processor computer, at any given time, at most one process is
executing and that process is in the running state.
PRay@CSE
PROCESS STATES
PRay@CSE
Five state process model PRay@CSE
Using Two Queues
PRay@CSE
PROCESS STATES
The lifecycle of a process can be divided into several
states, each with its own characteristic to describe the
process.
It means that as a process starts executing, it goes
through different states. Each process may be in one of
the following states:
1. New: The process has been created but not yet
admitted to the pool of executable processes.
PRay@CSE
2. Ready: The process is ready for execution
and is waiting to be allocated to a processor.
All ready processes are kept in a queue and
they keep waiting for CPU time to be allocated
by the O.S. in order to run.
A program called as scheduler or dispatcher,
which is part of O.S, picks up one ready
process for execution by passing a control to it.
PRay@CSE
3. Running: A process is now executing; actually using
the CPU.
The instructions within a process are executing. When
a process gets a control from CPU plus other
resources, it starts executing.
The running process may require some I/O during
execution.
Depending on the particular scheduling policy of OS, it
may pass its control back to that process or OS may
schedule another process if one is ready to run.
PRay@CSE
4. Waiting/blocked: A process is waiting for some event
to occur before it can continue execution.
A waiting process lacks some resources other than the
CPU.
Such processes are normally not considered for
execution until the related suspending conditions is
fulfilled.
The running process becomes suspended by invoking
I/O routines whose result it needs in order to proceeds.
PRay@CSE
[Link]: When the process finally stops. A process
terminates when it finishes executing its last
statement.
Please note that at this point of time, the process may
return some data to its parent process.
Also note that the process can terminate during some
other circumstances also.
A process can cause the termination of another
process via an appropriate system call.
PRay@CSE
Let us consider a very simple example. Figure 3.2 shows a memory layout of
three processes.
To simplify the discussion, we assume no use of virtual memory; thus all
three processes are represented by programs that are fully loaded in main
memory.
In addition, there is a small dispatcher program that switches the processor
from one process to another.
Figure 3.3 shows the traces of each of the processes during the early part
of their execution.
The first 12 instructions executed in processes A and C are shown. Process
B executes four instructions, and we assume that the fourth instruction
invokes an I/O operation for which the process must wait.
PRay@CSE
Example Execution
Memory
layout of
three
processes.
PRay@CSE
Figure 3.4 shows the interleaved traces resulting from the first 52
instruction cycles (for convenience, the instruction cycles are
numbered).
In this figure, the shaded areas represent code executed by the
dispatcher.
The same sequence of instructions is executed by the dispatcher
in each instance because the same functionality of the
dispatcher is being executed.
We assume that the OS only allows a process to continue
execution for a maximum of six instruction cycles, after which it
is interrupted; this prevents any single process from
monopolizing processor time.
PRay@CSE
As Figure 3.4 shows, the first six instructions of process A are
executed, followed by a time-out and the execution of some
code in the dispatcher, which executes six instructions before
turning control to process B.
After four instructions are executed, process B requests an I/O
action for which it must wait.
Therefore, the processor stops executing process B and moves
on, via the dispatcher, to process C.
After a time-out, the processor moves back to process A.
When this process times out, process B is still waiting for the I/O
operation to complete, so the dispatcher moves on to process C
again. PRay@CSE
Trace of Processes
PRay@CSE
PRay@CSE
Process States
PRay@CSE
Using Two Queues
PRay@CSE
Multiple Blocked Queues
PRay@CSE
Suspended Processes
Processor is faster than I/O so all processes could be
waiting for I/O.
Swap these processes to disk to free up more
memory.
Blocked state becomes suspend state when swapped
to disk.
Two new states
Blocked/Suspend
Ready /Suspend
PRay@CSE
One Suspend State
PRay@CSE
Reasons for Process Suspension
PRay@CSE
Two Suspend States
PRay@CSE
PROCESS SCHEDULING-READY QUEUE
V/S DEVICE QUEUE
PRay@CSE
Ready Queue
It is a doubly linked list of processes which are
ready to run.
They are ordered by priority, the highest priority
process is at the front of the queue.
Because queue works on First-in First-out (FIFO)
principle, so whenever the CPU takes up a new
process, it actually selects the process with the
highest priority.
PRay@CSE
Please note that when we say that the processes are
listed together, we mean that actually their PCBs are
linked together.
The header of the queue contains two pointers. The
first pointer points to the first PCB and the second
points to the last PCB in the list.
Also note that each PCB has a pointer to point to the
next process in the ready queue.
PRay@CSE
Ready
Queue
Device
Queue
PRay@CSE
Device Queue
There are some processes which are blocked
due to unavailability of an I/O device.
These processes form a device queue. There
is a separate queue for each I/O device.
PRay@CSE
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/O request of some other process.
The process therefore may have to wait for the disk.
The list of processes waiting for a particular I/O
device is called a device queue. Each device has its
own device queue.
PRay@CSE
System Call
• In computing, a system call is the programmatic way in which a computer
program requests a service from the kernel of the operating system it is
executed on.
• A system call is a way for programs to interact with the operating system.
• A computer program makes a system call when it makes a request to the
operating system’s kernel.
• System call provides the services of the operating system to the user
programs via Application Program Interface(API).
• It provides an interface between a process and operating system to allow
user-level processes to request services of the operating system.
PRay@CSE
• System calls are the only entry points into the kernel system.
All programs needing resources must use system calls.
• In general, system calls are available as assembly language
instructions.
• System calls are usually made when a process in user mode
requires access to a resource. Then it requests the kernel to
provide the resource via a system call.
PRay@CSE
• As can be seen from this diagram, the processes execute normally in the user
mode until a system call interrupts this.
• Then the system call is executed on a priority basis in the kernel mode.
• After the execution of the system call, the control returns to the user mode
and execution of user processes can be resumed.
PRay@CSE
Types of System Calls
• Process Control: These system calls deal with processes such as process
creation, process termination etc.
• File Management: These system calls are responsible for file manipulation
such as creating a file, reading a file, writing into a file etc.
• Device Management: These system calls are responsible for device
manipulation such as reading from device buffers, writing into device buffers
etc.
• Information Maintenance: These system calls handle information and its
transfer between the operating system and the user program.
• Communication: These system calls are useful for interprocess
communication. They also deal with creating and deleting a communication
connection.
PRay@CSE
PRay@CSE
fork()
• Fork system call is used for creating a new process, which is called child
process, which runs concurrently with the process that makes the fork() call
(parent process).
• After a new child process is created, both processes will execute the next
instruction following the fork() system call.
• A child process uses the same pc(program counter), same CPU registers, same
open files which use in the parent process.
• It takes no parameters and returns an integer value.
PRay@CSE
• Negative Value: creation of a child process
was unsuccessful.
Zero: Returned to the newly created child
process.
Positive value: Returned to parent or caller.
The value contains process ID of newly
created child process.
PRay@CSE
• If the call to fork() is executed successfully, Unix will make two
identical copies of address spaces, one for the parent and the
other for the child.
• Both processes will start their execution at the next statement
following the fork() call. In this case, both processes will start
their execution at the assignment statement as shown below:
PRay@CSE
• Both processes start their execution right after the system call fork().
• Since both processes have identical but separate address spaces, those variables
initialized before the fork() call have the same values in both address spaces.
• Since every process has its own address space, any modifications will be
independent of the others. In other words, if the parent changes the value of its
variable, the modification will only affect the variable in the parent process's
address space.
• Other address spaces created by fork() calls will not be affected even though
they have identical variable names.
PRay@CSE
#include <stdio.h>
#include <stdio.h> #include <sys/types.h>
#include <sys/types.h> int main()
#include <unistd.h> {
int main() printf(“India”);
{ fork();
printf(“India”); fork();
fork(); printf("hello\n");
printf("Hello world!\n"); return 0;
return 0; }
}
PRay@CSE