0% found this document useful (0 votes)
13 views

Lecture 4 - Process

Uploaded by

bilalgillani1000
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

Lecture 4 - Process

Uploaded by

bilalgillani1000
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 34

PROCESS

Bilal Ahmed
1
Program?
VS
Process?
Process
• In general, a process is a program in execution. Process Control Block

• A Program is not a Process by default.


• A program is a passive entity, i.e. a file containing a list of instructions
stored on disk (secondary memory) (often called an executable file).
• Potential of Execution
• A program becomes a Process when an executable file is loaded into
main memory and when it’s PCB is created.
• A process on the other hand is an Active Entity, which require
resources like main memory, CPU time, registers, system buses etc.
Process
• Even if two processes may be associated with same program, Process Control Block

they will be considered as two separate execution sequences


and are totally different process.

• For instance, if a user has invoked many copies of web browser


program, each copy will be treated as separate process.

Bilal Ahmed
Process Control Block
• Identifier: A unique identifier associated with this process, to distinguish it from all other
Process Control Block
processes.
• State: If the process is currently executing, it is in the running state.
• Priority: Priority level relative to other processes.
• Program counter: The address of the next instruction in the program to be executed.
• Memory pointers: Include 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 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
Bilal Ahmed
Program vs Process
Point Program Process
A set of instructions written to perform a
Definition An instance of a program being executed.
specific task.

State Static; exists as code on disk or in Dynamic; exists in memory and has a state
storage. (e.g., running, waiting).

Resources Does not require system resources when Requires CPU time, memory, and other
not running. resources during execution.
Independence Exists independently and is not Can operate concurrently with other
executing. processes.

Can interact with other processes and


Interaction Does not interact with other programs or
the operating system through system
the system.
calls and inter-process communication.

Bilal Ahmed
Bilal Ahmed
Two-State Process Model
• At any time, a process is either being executed by a processor, or it isn’t.
• In this model, a process may be in one of the two states: Running or Not Running
• When the OS creates a new process, it creates a process control block for the
process and enters that process into the system in the Not Running state.
• The process exists, is known to the OS, and is waiting for an opportunity to execute.
• From time to time, the currently running process will be interrupted, and the
dispatcher portion of the OS will select some other process to run.
• The former process moves from the Running state to the Not Running state, and
one of the other processes moves to the Running state.
Two-State Process Model
Continued…
• Each process must be represented in some way so the OS can keep track of it.
• That is, there must be some information relating to each process, including current state
and location in memory; this is the process control block.
• Processes that are not running must be kept in some sort of queue, waiting their turn to
execute
• There is a single queue in which each entry is a pointer to the process control block of a
particular process.
• We can describe the behavior of the dispatcher in terms of this queueing diagram.
• A process that is interrupted is transferred to the queue of waiting processes.
• Alternatively, if the process has completed or aborted, it is discarded (exits the system). In
either case, the dispatcher takes another process from the queue to execute
• What if processes are all ready to execute
• The queue is a first-in-first-out list and the processor operates in round-robin fashion on
the available processes (each process in the queue is given a certain amount of time, in
turn, to execute and then returned to the queue, unless blocked)
• What about the processes in the Not Running state are ready to execute, while others are
blocked, waiting for an I/O operation to complete
• Thus, using a single queue, the dispatcher could not just select the process at the oldest
end of the queue. Rather, the dispatcher would have to scan the list looking for the
process that is not blocked and that has been in the queue the longest
• The 2 state model lacks the capacity to handle
Five-State Process Model

• Running: The process that is currently being executed.


• Ready: A process that is prepared to execute when given the opportunity.
• Blocked/Waiting: A process that cannot execute until some event occurs, such as the completion of
an I/O operation.
• New: A process that has just been created but has not yet been admitted to the pool of executable
processes by the OS. Typically, a new process has not yet been loaded into main memory, although its
process control block has been created.
• Exit: A process that has been released from the pool of executable processes by the OS, either
because it halted or because it aborted for some reason.
Five-State Process Model

• The arrangement suggests the way in which a queueing discipline might be implemented with two
queues: a Ready queue and a Blocked queue.
• As each process is admitted to the system, it is placed in the Ready queue.
• When it is time for the OS to choose another process to run, it selects one from the Ready queue. \
• In the absence of any priority scheme, this can be a simple first-in-first-out queue.
• When a running process is removed from execution, it is either terminated or placed in the Ready or
Blocked queue, depending on the circumstances.
• Finally, when an event occurs, any process in the Blocked queue that has been waiting on that event
only is moved to the Ready queue
Five-State Process Model
• When an event occurs, the OS must scan the entire
blocked queue, searching for those processes waiting on
that event.
• In a large OS, there could be hundreds or even thousands
of processes in that queue.
• Therefore, it would be more efficient to have a number of
queues, one for each event.
• Then, when the event occurs, the entire list of processes
in the appropriate queue can be moved to the Ready
state
• If the dispatching of processes is dictated by a priority
• scheme, then it would be convenient to have a number of
Ready queues, one for each priority level.
• The OS could then readily determine which is the highest-
priority ready process that has been waiting the longest
Schedulers
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.
Schedulers
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.
Types of Schedulers
Long Term Scheduler (LTS)/Spooler: Long-term schedulers determine which processes
enter the ready queue from the job pool. Operating less frequently than short-term
schedulers, they focus on long-term system goals such as maximizing throughput.
Medium-term scheduler: The medium-term scheduler swaps processes in and out of
memory to optimize CPU usage and manage memory allocation. By doing so, it adjusts the
degree of multiprogramming and frees up memory as needed. Swapping allows the system
to pause and later resume a process, improving overall system efficiency.
Short Term Scheduler (STS): 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.
Point Long-Term Scheduler Short-Term Scheduler Middle Scheduler
Controls the admission of new Selects which ready Adjusts the degree of
processes into the system. process will execute multiprogramming, moving
Function next. processes between ready and
waiting queues.
Executes infrequently as it Executes frequently Executes at an intermediate
Frequency deals with the admission of to rapidly switch frequency, balancing long-term
new processes. between processes. and short-term needs.
Determines which programs Controls the mix of CPU-bound
Manages CPU scheduling
Responsibility are admitted to the system and I/O-bound processes to
and the switching of
from the job pool. optimize throughput.
processes.
Influences overall system Directly impacts CPU Balances system load to
Impact on
performance and degree of utilization and prevent resource bottlenecks
System
multiprogramming. response time. or idle resources.
Performance
Makes decisions considering
Makes decisions based on Makes decisions based both short-term and long-term
Decision Making long-term goals like system on short-term goals like goals, optimizing resource
throughput. minimizing response allocation.
time.
Bilal Ahmed
Dispatcher
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.

The dispatcher should be as fast as possible, since it is invoked during every process switch.
The time it takes for the dispatcher to stop one process and start another running is known as
the dispatch latency.

Remember the Diagram?

Bilal Ahmed
Operating System Control Structures
• If OS is to manage processes and
resources, it must have information
about the current status of each
process and resource.
• The OS constructs and maintains
tables of information about each
entity that it is managing.
• Four different types of tables
maintained by the OS: memory, I/O,
file, and process

Bilal Ahmed
Operating System Control Structures
• Memory tables are used to keep track of both main (real) and secondary (virtual) memory.
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
• I/O tables are used by the OS to manage the I/O devices and channels of the
computer system. At any given time, an I/O device may be available or assigned to a
particular process.
• File Tables provide information about the existence of files, their location on
secondary memory, their current status, and other attributes
• Finally, the OS must maintain process tables to manage processes.

Bilal Ahmed
Process Control Structures
• Consider what the OS must know if it is to manage and control a process. First, it must
know where the process is located; second, it must know the attributes of the process
that are necessary for its management (e.g., process ID and process state)
• Process Location : The collection of program, data, stack, and attributes of a process
are referred as Process Image

Bilal Ahmed
Process Control Structures
• Process Atrributes : We can group the process control block information into three
general categories:
• Process identification
• Processor state information
• Process control information

Bilal Ahmed
Process Control Structures

Bilal Ahmed
Process Control Structures

Bilal Ahmed
Process in Memory

Bilal Ahmed
Process in Memory
Process Control Block (PCB)
The top section of the image (shaded in grey) represents the Process Control
Block, which is a data structure in the operating system kernel that contains
essential information about each process. The PCB includes:
•Process Identification: Stores information such as the process ID (PID), which
uniquely identifies each process in the system.
•Processor State Information: Holds the current state of the process. It includes
the value of the program counter, CPU registers, and flags. This information is
critical when a process is context-switched out so that it can resume from where
it left off when scheduled back.
•Process Control Information: Stores information about the process scheduling,
priority, memory management, and any I/O devices the process may be using.

Bilal Ahmed
Process in Memory
User Stack
Each process has its own user stack, which is where the process stores temporary
data, such as function call parameters, return addresses, and local variables. The
stack grows and shrinks as functions are called and return within the process.
Private User Address Space
This segment represents the private memory space allocated for the process. It
includes the code (program) and data sections that the process needs for execution.
Each process has its own private memory, which cannot be accessed by other
processes unless explicitly shared.
Shared Address Space
The shared address space is a portion of memory that can be accessed by multiple
processes simultaneously. This is often used for inter-process communication (IPC)
or to share data between processes without having to duplicate it in each process’s
private memory.
Bilal Ahmed
The Creation and Termination of Processes
Process Creation
When a new process is to be added to those currently being managed, the OS builds the data structures used to
manage the process, and allocates address space in main memory to the process

Bilal Ahmed
The Creation and Termination of Processes
• It can be useful to allow one process to cause the creation of another. For example, an application process
may generate another process to receive data that the application is generating, and to organize those data
into a form suitable for later analysis.
• The new process runs in parallel to the original process and is activated from time to time when new data are
available.
• This arrangement can be very useful in structuring the application.
• As another example, a server process (e.g., print server, file server) may generate a new process for each
request that it handles.
• When the OS creates a process at the explicit request of another process, the action is referred to as process
spawning
• When one process spawns another, the former is referred to as the parent process, and the spawned process
is referred to as the child process. Typically, the “related” processes need to communicate and cooperate
with each other.

Bilal Ahmed
The Creation and Termination of Processes
Process Termination
• Any computer system must provide a means for a process to indicate its completion.
• A batch job should include a Halt instruction or an explicit OS service call for termination.
• In the former case, the Halt instruction will generate an interrupt to alert the OS that a process has
completed.
• For an interactive application, the action of the user will indicate when the process is completed.
• For example, in a time-sharing system, the process for a particular user is to be terminated when the user
logs off or turns off his or her terminal. On a personal computer or workstation, a user may quit an
application (e.g., word processing or spreadsheet).
• All of these actions ultimately result in a service request to the OS to terminate the requesting process.
• Additionally, a number of error and fault conditions can lead to the termination of a process.
• Finally, in some operating systems, a process may be terminated by the process that created it, or when
the parent process is itself terminated Bilal Ahmed
The Creation and Termination of Processes

Bilal Ahmed
The Creation and Termination of Processes

Bilal Ahmed
States of process execution????
cycle of CPU execution
or wait and i/o execution
or wait.
CPU Bound and I/O Bound Processes
A process execution consists of a cycle of CPU execution or wait and i/o execution or wait. Normally a
process alternates between two states.
Process execution begin with the CPU burst that may be followed by a i/o burst, then another CPU and
i/o burst and so on. Eventually in the last will end up on CPU burst. So, process keep switching between
the CPU and i/o during execution.
I/O Bound Processes: An I/O-bound process is one that spends more of its time doing I/O than it
spends doing computations.
CPU Bound Processes: A CPU-bound process, generates I/O 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.
If all processes are I/O bound, the ready queue will almost always be empty, and the short-term
scheduler will have little to do.
Similarly, if all processes are CPU bound, the I/O waiting queue will almost always be empty, devices will
go unused, and again the system will be unbalanced.
Context Switch
• 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.

You might also like