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

PROCESSES

CHAPTER 2
Processes

A process is an instance of a running program

It enables doing several things at the same time


support the ability to have (pseudo) concurrent operation
for example, consider a user PC

In a multiprogramming system, the CPU switches from


process to process quickly, running each for tens or
hundreds of milliseconds
PROCESS PROGRAM

A process is basically an instance A program is basically a


of the computer program that is collection of instructions that
being executed. performs a task when executed
by the computer.
A process is an activity of some
kind A program is something that may
be stored on disk, not doing
A process has a shorter lifetime. anything

A process requires resources such A program has a longer lifetime.


as memory, CPU, Input-Output
devices. A program is stored by hard-disk
and does not require any
resources.
Process Model
All the runnable software on the computer, including the
operating system, is organized into a number of sequential
processes

Conceptually, each process has its own virtual CPU. In


reality, of course, the real CPU switches back and forth
from process to process, but to understand the system, it
is much easier to think about a collection of processes
running in (pseudo) parallel than to try to keep track of
how the CPU switches from program to program

This rapid switching back and forth is called


multiprogramming
Process Model

(a) Multiprogramming four programs. (b) Conceptual model of four


independent, sequential processes. (c) Only one program is active at once.
Process Creation
Four principal events cause processes to be created:
1. System initialization
2. Execution of a process-creation system call
by a running process
3. A user request to create a new process
4. Initiation of a batch job

In all these cases, a new process is created by having an


existing process execute a process creation system call.
Process Creation
1. System initialization
Processes created when an operating system is
booted
foreground processes - interact with (human)
users and perform work for them
background (daemons) processes - not associated
with particular users, but instead have some
specific function
Process Creation
2. System call by a running process
Often a running process will issue system calls to
create one or more new processes to help it do its
job
Creating new processes is particularly useful when
the work to be done can easily be formulated in
terms of several related, but otherwise independent
interacting processes
Process Creation
3. User request to create a new process
In interactive systems, users can start a program by
typing a command or (double) clicking on an icon
which will start a new process and run the selected
program in it
Users may have multiple windows open at once, each
running some process
Using the mouse, the user can select a window and
interact with the process, for example, providing
input when needed
Process Creation
4. Initiation of a batch job
Applies only to batch systems found on large
mainframes
For example an inventory management at the end of
a day at a chain of stores
In this case, users can submit batch jobs to the
system (possibly remotely)
Process Creation System Call
WI NDOWS UNI X
The CreateProcess system call Processes are created with the system
handles both process creation calls known as fork and exec
and loading the correct program
into the new process The fork creates an exact clone of the
calling process. The process which does
This call has the ff. parameters: the creating is the parent of the other
program to be executed process, which is its child
command-line parameters
security attributes After the fork, the parent and the child
bits that control open files processes have the same memory image,
priority information environment strings & open files
specification of the window to
The child process then executes the exec
be created
call to change its memory image and run
pointer to a structure
a new program
Process Termination
Events which cause process termination:

1. Normal exit (voluntary)


2. Error exit (voluntary)
3. Fatal error (involuntary)
4. Killed by another process (involuntary)
Process Termination
Processes terminate because they have done their work.
When a compiler has compiled the program given to it,
the compiler executes a system call to tell the operating
system that it is finished.
The system call to terminate a process is exit in UNIX
and ExitProcess in Windows.

A process might terminate with a system call telling the


operating system to kill some other process.
In UNIX this call is kill.
The corresponding Windows call is TerminateProcess.
Process State
Processes in the operating system can be in any of the
following states:
1. Running (actually using the CPU at that instant)
2. Ready (runnable; temporarily stopped to let another
process run)
3. Blocked (unable to run until some external event
happens)
Process State

A process can be in running, blocked, or ready state.


Transitions between these states are as shown.
Process Table
To implement the process model, the operating system
maintains a table called the process table, with one
entry per process

The process table contains important information


about the process’ state

Information that must be saved when the process is


switched from running to ready or blocked state so that
it can be restarted later
Process Table
The entries in a process table are
commonly referred to as Process
Control Blocks (PCB).
Process Control Block
The PCB stores the following process-specific information:
Process State: can be running, waiting, etc.
Process ID and the parent process ID.
CPU registers and Program Counter. Program Counter holds the
address of the next instruction to be executed for that process.
CPU Scheduling information: Such as priority information and
pointers to scheduling queues.
Memory Management information: For example, page tables,
memory limits, or segment tables.
Accounting information: The user and kernel CPU time consumed
or amount of CPU used for execution, time limits, execution ID, etc.
I/O Status information: Devices allocated, open file tables, etc.
Threads
In many applications, multiple activities are going on at
once
Threads give the ability for the parallel entities to share
an address space and all of its data
not possible with multiple processes (with their
separate address spaces)
Threads are lighter weight than processes, so they are
easier (i.e., faster) to create and destroy
Threads allow computing and IO activities to overlap
Thread Usage
Word Processor

First thread interacts with the user.


Second thread handles reformatting in the background.
Third thread handles the disk backups.
Thread Model
Multithreading is the situation of allowing multiple threads in
the same process

(a) Three processes each with one thread. (b) One process with three threads.
Thread Advantages
Responsiveness:
If the process is divided into multiple threads, one
thread can complete its execution and its output can be
immediately returned.

Faster context switch:


Context switch time between threads is lower compared
to process context switch. Process context switching
needs more CPU overhead.

Effective utilization of multiprocessor system:


If we have multiple threads in a single process, then we
can schedule multiple threads on multiple processor.
Thread Advantages
Resource sharing:
Resources like code, data, and files can be shared among all
threads within a process.

Communication:
Communication between multiple threads is easier, as the
threads shares common address space. While in process we
have to follow some specific communication technique for
communication between two process.

Enhanced throughput of the system:


If a process is divided into multiple threads, and each thread
function is considered as one job, then the number of jobs
completed per unit of time is increased, thus increasing the
throughput of the system.
Process Scheduling
The act of determining which process is in the ready state,
and should be moved to the running state is known as
Process Scheduling.

The two main objectives of process scheduling:


to keep the CPU busy at all times
to deliver "acceptable" response times for all programs

The process scheduler must meet these objectives by


implementing suitable policies for swapping processes in
and out of the CPU.
Process Scheduling
Scheduling fell into one of the two general categories:

Non Pre-emptive Scheduling: When the currently


executing process gives up the CPU voluntarily.

Pre-emptive Scheduling: When the operating system


decides to favour another process, pre-empting the
currently executing process.
Scheduling Queues
All processes, upon entering into the system, are stored
in the Job Queue.

Processes in the Ready state are placed in the Ready


Queue.

Processes waiting for a device to become available are


placed in Device Queues.
Scheduling Queues
A new process is initially put in the Ready queue. It waits in
the ready queue until it is selected for execution

Once the process is assigned to the CPU and is executing,


one of the following events can occur:
The process could issue an I/O request, and then be
placed in the I/O queue.
The process could create a new subprocess and wait for
its termination.
The process could be removed forcibly from the CPU, as
a result of an interrupt, and be put back in the ready
queue.
Scheduling Queues

In the first two cases, the process eventually switches from the waiting state to the ready state,
and is then put back in the ready queue. A process continues this cycle until it terminates, at
which time it is removed from all queues and has its PCB and resources deallocated.
Types of Schedulers
A long-term scheduler is typical of a batch system or a very heavily
loaded system. It runs infrequently, (such as when one process
ends selecting one more to be loaded in from disk in its place), and
can afford to take the time to implement intelligent and advanced
scheduling algorithms.

The short-term scheduler, or CPU Scheduler, runs very frequently


and must very quickly swap one process out of the CPU and swap
in another one.

Some systems also employ a medium-term scheduler. When system


loads get high, this scheduler will swap one or more processes out
of the ready queue system for a few seconds, in order to allow
smaller faster jobs to finish up quickly and clear the system.

You might also like