0% found this document useful (0 votes)
91 views79 pages

Process Concepts

A process represents a program in execution. It includes the program code, current activity like the program counter, process ID, and CPU register contents. A process also has a process stack, data section, and heap. The operating system manages processes through a process control block that stores information like the process ID, state, and resources. Processes can transition between states like new, ready, running, waiting, blocked, and terminated. Suspending processes to disk allows more processes to run by freeing up memory.

Uploaded by

21PC12 - GOKUL D
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
91 views79 pages

Process Concepts

A process represents a program in execution. It includes the program code, current activity like the program counter, process ID, and CPU register contents. A process also has a process stack, data section, and heap. The operating system manages processes through a process control block that stores information like the process ID, state, and resources. Processes can transition between states like new, ready, running, waiting, blocked, and terminated. Suspending processes to disk allows more processes to run by freeing up memory.

Uploaded by

21PC12 - GOKUL D
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 79

Process

MSc CS 2020 Batch - BY JK


Introduction
• Early computers allowed only one program to be executed at a time.
• This program had complete control of the system and had access to system
resources.
• In contrast, contemporary computer systems allow multiple programs to be
loaded into memory and executed simultaneously.
• This resulted in the notion of a process, which is a program in execution.

MSc CS 2020 Batch - BY JK


Process Concept
• A time shared system has user programs or tasks.
• Even on a single-user system, a user may be able to run several programs at one
time: a word processor, a web browser and an email package.
• The above all are processes.

MSc CS 2020 Batch - BY JK


The Process
• A process is a program in execution.
• But it is more than program code (text section).
• It also includes current activity as represented by the value of the program
counter, process-id and the contents of the CPU registers.
• A process generally includes,
• Process stack(contains temporary data - function parameters, return
addresses and local variables)
• Data section (contains global variables)
• Heap (for dynamically allocated memory)

MSc CS 2020 Batch - BY JK


Memory Layout of C Programs
• A typical memory representation of C program consists of following sections
• Text segment
• Initialized data segment
• Uninitialized data segment(bss)
• Stack
• Heap

MSc CS 2020 Batch - BY JK


Memory Layout of C Programs
• The GNU size command can be used to determine the size (in bytes) of some of
these sections.
• Assuming the name of the executable file of the above C program is memory, the
following is the output generated by entering the command size memory:
text data bss dec hex filename
1158 284 8 1450 5aa memory
• The data field refers to initialized data, and bss refers to uninitialized data. (bss is
a historical term referring to block started by symbol.)
• The dec and hex values are the sum of the three sections represented in decimal
and hexadecimal, respectively.

MSc CS 2020 Batch - BY JK


The Process
• 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 a disk (often an exe file).
• 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 exe file is loaded into memory.
• Eg: double clicking pgm.exe (or) typing ./a.out in command prompt

MSc CS 2020 Batch - BY JK


The Process
• Although two processes may be associated with same program, they are
nevertheless two separate execution sequences.
• Eg:
• Several users running same copy 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 segments vary.

MSc CS 2020 Batch - BY JK


Process Control Block (PCB)
• Each process is represented in the OS by a PCB.
• It contains many pieces of information associated with a specific process.
• PCB contains sufficient information so that it is possible to interrupt a running
process and later resume execution.
• It enables OS to support multiple processes.

MSc CS 2020 Batch - BY JK


Process Control Block (PCB)
Information associated with each process.
• Process identifier
• Process state
• Program counter
• CPU registers
• CPU scheduling information
• Memory-management information
• Accounting information
• I/O status information

MSc CS 2020 Batch - BY JK


Process Control Block (PCB)

MSc CS 2020 Batch - BY JK


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 any one of the following states.
• new: The process is being created.
• running: Instructions are being executed.
• waiting/blocked: The process is waiting for some event to occur.
• ready: The process is waiting to be assigned to a processor.
• terminated: The process has finished execution.

MSc CS 2020 Batch - BY JK


Five State Process Model
• Only one process can be running an any processor at any instant.
• Many processes can be ready and waiting

MSc CS 2020 Batch - BY JK


State transitions
• Null->new
• A new process is created to execute a program
• New->ready
• The OS will move process from new to ready when it is prepared to take on
additional process. There is a limit on number of processes in main memory
• Ready->running
• When it is time to select a process to run, the OS chooses one of the
processes in ready state. Done by scheduler or dispatcher.
• Running->exit
• Currently running process terminated by OS, if the process indicates that it
has completed or if it aborts.
MSc CS 2020 Batch - BY JK
State transitions
• Running -> ready
• The most common reason for this transition is that the running processes has
reached the max allowable time or some other high priority process has come in for
execution
• Running->Waiting/Blocked
• A process is put to blocked state if it requests for OS services like an I/O operation, or
resource.
• Waiting/Blocked->ready
• When the event for which the process has been waiting occurs this transition
happens
• Ready->exit or Waiting/Blocked->exit
• A parent process may terminate a child process at any time. Also if parent terminates
all child processes associated with parent may also terminate.

MSc CS 2020 Batch - BY JK


Suspended Processes
• Processor is faster than I/O devices, so many processes could be waiting for I/O
• Swap these processes to disk to free up more memory and use processor on
more processes
• Blocked/waiting state becomes suspend state when swapped to disk

MSc CS 2020 Batch - BY JK


MSc CS 2020 Batch - BY JK
Process States during Suspension
• One new state
• Suspend
or
• Two new states
• Blocked/Suspend
• Ready/Suspend

MSc CS 2020 Batch - BY JK


One Suspend State

MSc CS 2020 Batch - BY JK


Two Suspend States - Seven State Process
Model

MSc CS 2020 Batch - BY JK


Additional process states
• Blocked/Suspend : the process in secondary memory and awaiting an
event
• Ready/ Suspend: the process in secondary storage but is available for
execution as soon as it is loaded in memory

MSc CS 2020 Batch - BY JK


New State transitions
• Blocked ->blocked/suspend
• If there are no ready processes, then at least one blocked process is swapped out to
secondary storage to make space for another process which is not blocked.
• Blocked/suspend ->ready/suspend
• This transition happens when the event for which the process has been waiting for occurs
• Ready/suspend->ready
• When no ready processes in memory, the OS will need to bring one in to continue execution
or process in ready/suspend state has high priority than ready state processes.
• Ready->ready suspend
• Usually OS will suspend a blocked process to suspend state. But it may be necessary to
suspend a ready process if that is the only way to free large block of main memory or blocked
process may be high priority process than ready process

MSc CS 2020 Batch - BY JK


Other transitions to consider
• New->ready/suspend and new ->ready
• When a new process is created it can be added either to ready state or ready suspend state
based on the availability of main memory.
• Blocked/Suspend->Blocked
• It would be reasonable to bring a blocked process into main memory if it is a high priority
process than any other process in ready suspend state and the OS has reason to believe that
the blocking event for the process will occur soon.
• Running ->ready/suspend
• OS can pre-empt a running process and move it to ready suspend state directly because a
blocked suspended high priority process has just become unblocked.
• Any state ->exit
• A process get terminated while running, due to its completion or fatal error conditions.
Sometimes if a parent process terminates, the child processes associated with the parent
process can also terminate, whatever may be the state the child process are in.

MSc CS 2020 Batch - BY JK


New State of a Process
• A new state corresponds to a process that has just been defined.
• An identifier is associated with the process.
• Any tables needed to manage the process are allocated and built.
• At this stage the process is in new state.
• While a process is in the new state, information concerning the process that is
needed by the OS is maintained in control tables in main memory.
• When the process is in new state, the code of the program is not in main memory
and no space has been allocated for the data associated with the program.
• The program remains in secondary storage.

MSc CS 2020 Batch - BY JK


Reasons for Process Creation
• When a new process is to be added to those currently being managed, the OS
builds the data structures that are used to manage the process and allocates
address space in main memory to the process.

MSc CS 2020 Batch - BY JK


Process Creation

Process
Parent process Child process
spawning
• when the OS • is the original, • is the new
creates a creating, process
process at the process
explicit request
of another
process

MSc CS 2020 Batch - BY JK


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.

MSc CS 2020 Batch - BY JK


Process Creation

The above figure illustrates a typical process tree for the Linux
operating system, showing the name of each process and its pid.
MSc CS 2020 Batch - BY JK
Process Creation
• The systemd process (which always has a pid of 1) serves as the root parent
process for all user processes, and is the first user process created when the
system boots.
• Once the system has booted, the systemd process creates processes which provide additional
services such as a web or print server, an ssh server, and the like.
• Two children of systemd—logind and sshd.
• The logind 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
vim editor.
• The sshd process is responsible for managing clients that connect to the system by using ssh
(which is short for secure shell).

MSc CS 2020 Batch - BY JK


Process Creation
• Traditional UNIX systems identify the process init as the root of all child
processes.
• init (also known as System V init) is assigned a pid of 1, and is the first process
created when the system is booted.
• On a process tree similar to what is shown in previous figure, init is at the root.
• Linux systems initially adopted the System V init approach, but recent
distributions have replaced it with systemd.
• systemd serves as the system’s initial process, much the same as System V init;
however it is much more flexible, and can provide more services, than init.

MSc CS 2020 Batch - BY JK


Process Creation
• 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:
• The parent continues to execute concurrently with its children.
• The parent waits until some or all of its children have terminated.
• There are also two address-space possibilities for the new process:
• The child process is a duplicate of the parent process (it has the same program and data as the
parent - text segment is common for both process, data segment of parent duplicated(copied) for
child process).
• The child process has a new program loaded into it.

MSc CS 2020 Batch - BY JK


C Program - Process Creation & Termination
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/wait.h>
int main()
{
int forkresult;

printf("Parent now going to fork a child... \n");

forkresult=fork();

if(forkresult==-1)
{
printf("Fork failed\n");
exit(0);
}

MSc CS 2020 Batch - BY JK


C Program - Process Creation & Termination
else if(forkresult!=0)
{
printf("Parent waits for child to execute ls then parent executes pwd.\n");
wait(NULL);
execlp("pwd","pwd",NULL);
printf("This line gets printed when the above exec statement pwd fails\n");
}
else
{
printf("Child : I'm now going to execute ls!\n\n\n");
execlp ("ls", "ls", NULL);
printf ("Child prints %d: AAAAH ! ! My EXEC failed ! ! ! !\n", getpid());
exit(1);
}
}

MSc CS 2020 Batch - BY JK


Terminate State of a Process
• A process exits a system in following stages.
• When it reaches a natural completion point
• When it aborts due to an unrecoverable error
• When another process with the appropriate authority causes the process to abort.
• Termination moves the process to the exit state, and the process is no longer eligible for
execution.
• The tables and other information associated with the process are temporarily preserved
by the OS, for the auxiliary programs, to extract any needed information.
• Once these processes has extracted the needed information, the OS no longer needs to
maintain any data relating to the process, as it is deleted from the system.

MSc CS 2020 Batch - BY JK


Reasons for
Process
Termination

MSc CS 2020 Batch - BY JK


Process Termination
• When a process terminates, its resources are deallocated by the operating
system.
• However, its entry in the process table must remain there until the parent calls
wait(), because the process table contains the process’s exit status.
• A process that has terminated, but whose parent has not yet called wait(), is
known as a zombie process.
• All processes transition to this state when they terminate, but generally they exist
as zombies only briefly.
• Once the parent calls wait(), the process identifier of the zombie process and its
entry in the process table are released.

MSc CS 2020 Batch - BY JK


Process Termination
• Now consider what would happen if a parent did not invoke wait() and instead
terminated, thereby leaving its child processes as orphan processes.
• Traditional UNIX systems addressed this scenario by assigning the init process as
the new parent to orphan processes.
• The init process periodically invokes wait(), thereby allowing the exit status of any
orphaned process to be collected and releasing the orphan’s process identifier
and process-table entry.
• Although most Linux systems have replaced init with systemd, the latter process
can still serve the same role, although Linux also allows processes other than
systemd to inherit orphan processes and manage their termination.

MSc CS 2020 Batch - BY JK


Process Description

MSc CS 2020 Batch - BY JK


Process Description
• The OS controls events within the
computer system. It schedules and
dispatches processes for execution by
the processor, allocates resources to
processes, and responds to requests
by user processes for basic services.
• Fundamentally, we can think of the OS
as that entity that manages the use of
system resources by processes.

MSc CS 2020 Batch - BY JK


Process Description
• In above figure
• There are a number of processes (P1, . . ., Pn,) that have been created and
exist in virtual memory.
• Each process, during the course of its execution, needs access to certain
system resources, including the processor, I/O devices, and main memory.
• In the figure, process P1 is running; at least part of the process is in main
memory, and it has control of two I/O devices.
• Process P2 is also in main memory but is blocked waiting for an I/O device
allocated to P1.
• Process Pn has been swapped out and is therefore suspended.

MSc CS 2020 Batch - BY JK


Operating System Control Structures
• For the OS is to manage processes and resources, it must have
information about the current status of each process and resource.
• Tables are constructed for each entity the operating system manages

MSc CS 2020 Batch - BY JK


OS Control Tables

MSc CS 2020 Batch - BY JK


OS Control Tables
• A general idea of the scope of the tables is in Figure, which shows four different
types of tables maintained by the OS:
• memory,
• I/O,
• file,
• process.

• Although the details will differ from one OS to another, fundamentally, all
operating systems maintain information in these four categories.

MSc CS 2020 Batch - BY JK


Memory Tables
• Memory tables are used to keep track of both main and secondary memory.
• Must include this information:
• Allocation of main memory to processes
• Allocation of secondary memory to processes
• Protection attributes for access to shared memory regions
• Information needed to manage virtual memory
• Memory tables are used to keep track of allocation of both main (real) and secondary
(virtual) memory to processes.
• Some of main memory is reserved for use by the OS; the remainder is available for use
by processes.
• Processes are maintained on secondary memory using some sort of virtual memory or
simple swapping mechanism.
MSc CS 2020 Batch - BY JK
I/O Tables
• Used by the OS to manage the I/O devices of the computer.
• The OS needs to know
• Whether the I/O device is available or assigned
• The status of I/O operation
• The location in main memory being used as the source or destination of the I/O
transfer

MSc CS 2020 Batch - BY JK


File Tables
• The OS may also maintain file tables.
• These tables provide information about:
• Existence of files
• Location on secondary memory
• Current Status
• other attributes.
• Sometimes this information is maintained by a file management system, in which
case the OS has little or no knowledge of files.
• In other operating systems, much of the detail of file management is managed by
the OS itself.

MSc CS 2020 Batch - BY JK


Process Tables
• To manage processes the OS needs to know details of the processes
• Current state
• Process ID
• Location in memory
• etc
• Process control block
• Process image is the collection of program, Data, stack, and attributes(PCB)
• Memory, I/O, and files are managed on behalf of processes, so there must be some
reference to these resources, directly or indirectly, in the process tables.
• The files referred to in the file tables are accessible via an I/O device and will, at some
times, be in main or virtual memory.
• The tables themselves must be accessible by the OS and therefore are subject to
memory management. MSc CS 2020 Batch - BY JK
Process Control Structures
• For the OS to manage and control a process, it must know:
• Where the process is located
• The attributes of the process like process id, process state…

MSc CS 2020 Batch - BY JK


Process Location
• Each process has a number of attributes used by OS for process control.
• These collection of attributes referred to a Process Control Block
• The collection of program, data, stack and attributes(PCB) can be referred to as
Process Image.

MSc CS 2020 Batch - BY JK


MSc CS 2020 Batch - BY JK
Process Location
• For the OS to manage process, at least a small portion of its image must be
loaded into main memory or at least virtual memory and OS must know the
location of each process.
• Therefore process tables maintained by the OS must show the location of the
entire process image.
• Each entry contains at least a pointer to a process image.

MSc CS 2020 Batch - BY JK


Process Attributes
• A sophisticated multiprogramming system requires a great deal of information
about each process.
• Different systems will organize this information in different ways.
• For now, let us simply explore the type of information that might be of use to an
OS without considering in any detail how that information is organized.
• We can group the process control block information into three general categories:
• Process identification
• Processor state information
• Process control information

MSc CS 2020 Batch - BY JK


Typical Elements of
Process Control Block

MSc CS 2020 Batch - BY JK


Process Identification
• Each process is assigned a unique numeric identifier.
• Many of the other tables controlled by the OS may use process identifiers to
cross-reference process tables
• In virtually all operating systems, each process is assigned a unique numeric identifier, which
may simply be an index into the primary process table; otherwise there must be a mapping
that allows the OS to locate the appropriate tables based on the process identifier.

MSc CS 2020 Batch - BY JK


Process Identification
• For example,
• The memory tables may be organized so as to provide a map of main memory with
an indication of which process is assigned to each region.
• Similar references will appear in I/O and file tables.
• When processes communicate with one another, the process identifier informs the
OS of the destination of a particular communication.
• When processes are allowed to create other processes, identifiers indicate the
parent and descendants of each process.
• In addition to these process identifiers, a process may be assigned a user identifier
that indicates the user responsible for the job.

MSc CS 2020 Batch - BY JK


Processor State Information
• This consists of the contents of processor registers.
• User-visible registers
• Control and status registers
• Stack pointers
• Program status word (PSW)
• contains status information
• Example: the EFLAGS register on Pentium processors

MSc CS 2020 Batch - BY JK


Processor State Information
• Processor state information consists of the contents of processor registers.
• While a process is running, the information is in the registers.
• When a process is interrupted, all of this register information must be saved so
that it can be restored when the process resumes execution. Nature and number
of registers vary by processor design
• Typically, the register set will include user-visible registers, control and status
registers, and stack pointers.

MSc CS 2020 Batch - BY JK


Process Control Information
• This is the additional information needed by the OS to control and
coordinate the various active processes.

MSc CS 2020 Batch - BY JK


Structure of Process Images in Virtual Memory

MSc CS 2020 Batch - BY JK


Structure of Process Images in Virtual Memory
• The above figure suggests the structure of process images in virtual memory.
• Each process image consists of a process control block, a user stack, the private
address space of the process, and any other address space that the process
shares with other processes.
• In the figure, each process image appears as a continuous range of addresses.
• In actual implementation, this may not be the case, it may depend on the
memory management scheme and the way in which control structures are
organized by the OS.

MSc CS 2020 Batch - BY JK


Role of the Process Control Block
• The process control block is the most important data structure in an OS.
• Each process control block contains all of the information about a process that is
needed by the OS.
• The blocks are read and/or modified by virtually every module in the OS, including
those involved with scheduling, resource allocation, interrupt processing, and
performance monitoring and analysis.
• Hence Process Control Block requires protection due to the below two problems:
• A faulty routine could cause damage to the PCB destroying the OS’s ability to manage
the process
• Any design change to the PCB could affect many modules of the OS

MSc CS 2020 Batch - BY JK


Process Control

MSc CS 2020 Batch - BY JK


Process Control – Modes of Execution
• The less-privileged mode is often referred to as the user mode, because user programs
typically would execute in this mode.
• More-privileged mode is System Mode also known as
• Control Mode
• Kernel Mode
• Protected Mode
• Certain instructions can only be executed in the more-privileged mode.
• Including reading or altering a control register, such as the program status word;
• Primitive I/O instructions;
• Instructions that relate to memory management.
• In addition, certain regions of memory can only be accessed in the more-privileged
mode.
MSc CS 2020 Batch - BY JK
Process Control – Modes of Execution
• Functions typically found in the kernel of an OS

MSc CS 2020 Batch - BY JK


Process Control – Modes of Execution
• How does the processor know in which mode it is to be executing? And how does
it change?
• Typically a flag (single bit) in the program status word (PSW). This bit is
changed in response to certain events.
• Typically, when a user makes a call to an operating system service or when an
interrupt triggers execution of an operating system routine, the mode is set to
the kernel mode and, upon return from the service to the user process, the
mode is set to user mode.

MSc CS 2020 Batch - BY JK


Process Control – Process Creation
• Once the OS decides to create a new process it can proceed as
follows:
• Assigns a unique process identifier
• At this time, a new entry is added to the primary process table, which contains one entry
per process
• Allocates space for the process
• This includes all elements of process image. The size of memory to be allocated can be
assigned as default based on type of process, as per user request or the parent process
can pass the needed values to the OS.
• Initializes process control block
• Process Identification portion
• Processor State Information portion
• Process Control Information portion

MSc CS 2020 Batch - BY JK


Process Control – Process Creation
• Sets up appropriate linkages
• If OS maintains each scheduling queue as linked list, then the new process must be put in
the Ready or Ready/Suspend list
• Creates or expand other data structures
• The OS may maintain an accounting file on each process to be used subsequently for
billing or performance assessment purposes.

MSc CS 2020 Batch - BY JK


Process/Context Switching and
Mode Switching

MSc CS 2020 Batch - BY JK


Process Control – Process Switching
• At some time, a running process is interrupted and the OS assigns
another process to the Running state and turns control over to that
process.
• Several design issues are raised regarding process switching
• What events trigger a process switch?
• We must distinguish between mode switching and process switching.
• What must the OS do to the various data structures under its control to
achieve a process switch?

MSc CS 2020 Batch - BY JK


When to switch processes?
• A process switch may occur any time that the OS has gained control
from the currently running process. Possible events giving OS control
are:

MSc CS 2020 Batch - BY JK


Process Control – Process Switching
• Two kinds of system interrupts,
• one is simply called an interrupt,
• and the other called a trap.
• “Interrupts” are due to some sort of event that is external to and independent of
the currently running process, such as the completion of an I/O operation.
• With an ordinary interrupt, control is first transferred to an interrupt handler,
which does some basic housekeeping and then branches to an OS routine that is
concerned with the particular type of interrupt that has occurred.
• “Traps” relate to an error or exception condition generated within the currently
running process, such as an illegal file access attempt.

MSc CS 2020 Batch - BY JK


Process Control – Process Switching
• With traps, the OS determines if the error or exception condition is fatal.
• If so, then the currently running process is moved to the Exit state and a process switch
occurs.
• If not, then the action of the OS will depend on the nature of the error and the design of the
OS.
• It may attempt some recovery procedure or simply notify the user.
• It may do a process switch or resume the currently running process.

• Finally, the OS may be activated by a supervisor call from the program being
executed.
• For example, a user process is running and an instruction is executed that requests an I/O
operation, such as a file open.
• This call results in a transfer to a routine that is part of the operating system code.
The use of a system call may place the user process in the Blocked state.
MSc CS 2020 Batch - BY JK
Different Kinds of Interrupts
• Clock Interrupt
• The OS determines whether the currently running process has been executing
for maximum allowable unit of time referred to as time slice. If so, this
process must be switched to a ready state and another process dispatched.
• I/O Interrupt
• If the I/O operation completes for which one or more processes are waiting,
then OS moves all those processes from blocked state to ready state. Then OS
may continue with currently running process or schedule another high
priority process that has been unblocked.

MSc CS 2020 Batch - BY JK


Different Kinds of Interrupts
• Memory Fault
• The processor encounters a virtual memory address reference for a word that
is not in main memory. The OS must bring in the block of memory containing
the reference from secondary memory to main memory.
• After the I/O request is issued to bring in the block of memory, the process
with the memory fault is placed in a blocked state; the OS then performs a
process switch to resume execution of another process.
• After the desired block is brought into memory, that process is placed in the
Ready state.

MSc CS 2020 Batch - BY JK


Process Control – Mode Switching
• In the interrupt stage, the processor checks for interrupt. If an interrupt is
pending, the processor does the following:
• It sets the program counter to the starting address of an interrupt handler
program.
• It switches from user to kernel mode so that the interrupt processing code
may include privileged instructions.
• The processor now proceeds to fetch stage and fetches the first instruction of the
interrupt handler program, which will service the interrupt.
• Now the context of the process that has been interrupted is saved into that PCB
of the interrupted program.

MSc CS 2020 Batch - BY JK


Process Control – Mode Switching
• What is the context of the process that gets saved when that interrupt occurs?
• It must include any information that may be altered by the execution of the
interrupt handler and that will be needed to resume the program that was
interrupted.
• Thus the portion of the PCB that was referred to as processor state information
must be saved.
• This includes the program counter, other processor registers and stack
information.

MSc CS 2020 Batch - BY JK


Process Control – Mode Switching
• In most operating systems, the occurrence of an interrupt does not necessarily
mean a process switch.
• It is possible that, after the interrupt handler has executed, the currently running
process must resume execution.
• In that case, all that is necessary is to save the processor state information when
the interrupt occurs and restore the information when control is returned to the
program that was running.
• If the interrupt is to be followed by a switch to another process, then some work
will need to be done.

MSc CS 2020 Batch - BY JK


Process Control – Change of Process State
• A mode switch may occur without changing the state of the process that is
currently in the running state.
• In that case, the context saving and subsequent restoral involves little overhead.
• If the currently running process is to be moved to another state (Ready, Blocked,
etc.), then the OS must make substantial changes in its environment.

MSc CS 2020 Batch - BY JK


Process Control – Change of Process State
• The steps in a process switch are:
• Save context of processor including program counter and other registers
• Update the process control block of the process that is currently in the
Running state
• Move process control block to appropriate queue – ready; blocked;
ready/suspend
• Select another process for execution
• Update the process control block of the process selected
• Update memory-management data structures
• Restore context of the selected process
• Thus the process switch, which involves a state change, requires more effort, than
a mode switch
MSc CS 2020 Batch - BY JK

You might also like