Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

A a Chap 3 Processes

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 43

Processes

OS
Process Concept
• An operating system executes a variety of programs:
– Batch system – jobs
– Time-shared systems – user programs or tasks
• Textbook uses the terms job and process almost
interchangeably
• Process – a program in execution; process execution must
progress in sequential fashion
• A process includes:
– program counter and process registers
– stack and heap
– text section
– data section
Cont...
• A process can initiate a subprocess, which is a called a
child process (and the initiating process is sometimes
referred to as its parent ).
• A child process is a replica of the parent process and
shares some of its resources, but cannot exist if the
parent is terminated.
• Processes can exchange information or synchronize their
operation through several methods of interprocess
communication
Cont...
Process memory is divided into four sections as
shown in the Figure below:
• The text section comprises the compiled
program code, read in from non-volatile storage
when the program is launched.
•The data section stores global and static
variables, allocated and initialized prior to
executing main program.
Process in Memory
Cont…
Variables are used to store information to be referenced and
manipulated in a computer program.
For example, assume you want to store two values 10 and 20
in your program and at a later stage, you want to use these two
values.
Variables are declared differently as per the programming
language
Global variables are declared outside any function, and they
can be accessed (used) on any function in the program.
Cont..
Local variables are declared inside a function, and
can be used only inside that function.
A static variable is a variable that has been
allocated "statically", meaning that its lifetime (or
"extent") is the entire run of the program.
Dynamic Memory Allocation.. Memory allocated
"on the fly" during run time
Cont…
• The heap is used for dynamic memory allocation,
and is managed via calls to new, delete, malloc (just
a function), free, etc.
• The stack is used for local variables. Space on the stack
is reserved for local variables when they are declared ( at
function entrance or elsewhere, depending on the
language ), and the space is freed up when the variables
go out of scope.
Cont..
• Note that the stack and the heap start at opposite
ends of the process's free space and grow towards
each other. If they should ever meet, then either a
stack overflow error will occur, or else a call to new
or malloc will fail due to insufficient memory
available.
• When processes are swapped out of memory and
later restored, additional information must also be
stored and restored. Key among them are the
program counter and the value of all program
registers.
Process State
• As a process executes, it changes state
– 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/O completion or reception of a
signal)
– ready: The process is waiting to be assigned to a
processor
– terminated: The process has finished execution
Process states
Process Operations
Process Creation
•In general-purpose systems, some way is needed
to create processes as needed during operation.
There are four principal events that lead to
processes creation.
•System initialization.
•Execution of process Creation System calls by a
running process.
•A user request to create a new process.
Cont..
• Initialization of a batch job.
• Foreground processes interact with users.
Background processes, are those that stay in
background, sleeping but suddenly springing to
life to handle activity such as email, webpage,
printing, and so on.
• Background processes are called daemons. This
call creates an exact clone of the calling process.
Cont..
• A process may create a new process by some
create process such as 'fork', the creating process is
called parent process and the created one is called
the child processes.
• Only one parent is needed to create a child process.
• This creation of process (processes) yields a
hierarchical structure of processes, each child has
only one parent but each parent may have many
children.
Cont…
• After the fork, the two processes, the parent and
the child, have the same memory image, the
same environment strings and the same open
files.
• After a process is created, both the parent and
child have their own distinct address space. If
either process changes a word in its address
space, the change is not visible to the other
process.
Cont..
The following are some reasons for creation of
a process
•User logs on.
•User starts a program.
•Operating systems creates process to provide
service, e.g., to manage printer.
•Some program starts another process, e.g.,
Netscape calls xv to display a picture.
Cont..
Process Termination
•A process terminates when it finishes executing its last
statement. Its resources are returned to the system, it is
purged from any system lists or tables, and its process
control block (PCB) is erased i.e., the PCB's memory space
is returned to a free memory pool.
The new process terminates the existing process, usually
due to following reasons:
•Normal Exit Most processes terminates because they
have done their job.
Cont..
• Error Exit When a process discovers a fatal error. For
example, a user tries to compile a program that does
not exist.
• Fatal Error An error caused by a process due to a bug
in program for example, executing an illegal instruction,
referring non-existing memory or dividing by zero.
• Killed by another Process A process executes a
system call telling the Operating Systems to terminate
some other process.
Process State Transitions
•Transition 1 occurs when process discovers that it
cannot continue. If running process initiates an I/O
operation before its allotted time expires, the running
process voluntarily relinquishes the CPU.
This state transition is: Block (process-name):
Running → Block.
•Transition 2 occurs when the scheduler decides that
the running process has run long enough and it is time
to let another process have CPU time.

This state transition is: Time-Run-Out (process-


name): Running → Ready.
Cont…
• Transition 3 occurs when all other processes have
had their share and it is time for the first process to
run again this state transition is: Dispatch
(process-name): Ready → Running.
• Transition 4 occurs when the external event for
which a process was waiting (such as arrival of input)
happens.
This state transition is: Wakeup (process-name):
Blocked → Ready.
Cont..
• Transition 5 occurs when the process is created.

This state transition is: Admitted (process-


name): New → Ready.
• Transition 6 occurs when the process has finished
execution.

This state transition is: Exit (process-name):


Running → Terminated.
Process Control Block (PCB)
Each process is represented in the O.S. by a PCB.
Information associated with each process
• Process state
• Program counter
• CPU registers
• CPU scheduling information
• Memory-management information
• Accounting information
• I/O status information
Cont…
• A PCB contains the following Information:
• Process state : new, ready, …
• Program counter : indicates the address of the next
instruction to be executed for this program. It is a
register in a computer processor that has the
address of the instruction being executed
• CPU registers : set of data holding places that are
part of the processor, includes stack pointers, …
• CPU scheduling information : includes process
priority, pointers to scheduling queues.
Cont..
• Memory-management information: includes the
value of base and limit registers (protection),
specify the logical address space …
• Accounting information: includes amount of CPU
and real time used, account numbers, process
numbers, …
• I/O status information: includes list of I/O devices
allocated to this process, a list of open files, …
Cont…
• Assume that we are dealing with a system
composed solely of processes; A process
descriptor or process control block (PCB) is
constructed at process creation and represents
the process during its existence.
• Process descriptors are used and modified by
basic process and resource operations, interrupt
routines, and schedulers; they may also be
referenced during performance monitoring and
analysis.
Cont..
• Each process is uniquely identified by its
descriptor. Pointers to PCBs are maintained by the
system.
• In addition, a process p often has a system-wide
unique identification p->ID supplied by the user or
the system. Its purpose is to allow convenient and
explicit interprocess references.
Cont..
• To eliminate conflicts arising from the use of the
same ID for more than one process, we assume
that the system assigns a new unique ID to every
process at the time of its creation.
• New process ID’s are obtained using the function:
pid = Get New PID() Since all pointers to process
descriptors are maintained by the system, we also
assume the existence of a function, p = Get
PCB(pid) that takes a unique pid as argument and
returns the pointer p to the process’s descriptor.
Cont..
• The execution of a process p can be described as
a sequence of state vectors s0, s1, ..., si, ..., where
each state vector si contains the address of the
current program instruction to be executed and the
values of all variables of the program.
• It also comprises the state of the processor
executing p, the allocated address space, and any
other resources currently associated with p.
Cont..
• In other words, a state vector of a process p is
that amount of information required by a
processor in order to run p, or to restart p after it
has been interrupted.
• The state vector can be changed either by the
execution of p or by the execution of other objects
that share some state vector components with p.
Cont..
• CPU State contains copies of all hardware
registers and flags of the CPU. This includes the
program counter, instruction and data registers,
and protection and error flags.
In general, CPU State contains
• the information stored in a typical machine state-
word; this data is saved automatically by the
hardware when an interrupt occurs.
Cont..
• Thus as long as a process is running, its CPU
State field is meaningless—it contains obsolete
values saved during the last interrupt.
• When the process is not running, its CPU State
contains the necessary data to restart the process
at the point of its last interruption. The Processor
field identifies the CPU that is executing p. This is
meaningful only in a multiprocessor system; in a
uniprocessor it is omitted.
Cont..
• The Memory field gives the main memory map for
the process. It describes its real address space
and, in case of a virtual memory, the mapping
between the virtual name space and physical
memory locations.
• Thus, Memory could contain upper and lower
bounds registers for a contiguous partition in main
memory, a list of memory blocks allocated to the
process, or a pointer to a page or segment table.
Cont..
• The next component Open Files specifies all files
that are currently open. Typically, it points to a list of
open file descriptors that record such information as
file type, disk location, current reading or writing
position, and buffer state.
• The last field, Other Resources, represents jointly all
other interesting resources owned by the process.
These might be such resources as peripherals,
secondary storage space, or files.
Cont..
• The Priority field indicates the importance
of the process relative to other processes.
This information is used by the scheduler
when deciding which process should be
running next.
• In the simplest case, Priority could be a
single integer value, assigned to the
process at the time of creation.
Cont…
• More typically, the priority of a process
consists of two components: a statically
assigned base priority, and a dynamically
changing current priority.
• The latter can be derived using a complex
function of the base priority, the resource
demands of the process, and the current
state of the environment, i.e., other
processes and resources in the system.
CONTEXT SWITCH
• When CPU switches to another process (because
of an interrupt), the system must save the state of
the old process and load the saved state for the
new process
• Context-switch time is overhead; the system does
no useful work while switching
• An Interrupt is a signal to the processor emitted by
hw/sw indicating an event that needs immediate
attention
Cont..
• Each switch of the CPU from one process to
another is called a context switch.
• A context switch is the process of storing and
restoring the state of a process so that execution
can be resumed from the same point at a later
time.
• This enables multiple processes to share a single
CPU and is an essential feature of a multitasking
operating system. What constitutes the context is
determined by the processor and the OS.
Cont…..
• Switching from one process to another
requires a certain amount of time for doing
the administration - saving and loading
registers and memory maps, updating
various tables and lists .
Cont..
• A context switch can mean a register context
switch, a task context switch, a stack frame switch,
a thread context switch, or a process context
switch.
• The state of the process includes all the registers
that the process may be using, especially the
program counter, plus any other operating system
specific data that may be necessary. This data is
usually stored in a data structure called a process
control block (PCB).
Cont..
• In order to switch processes, the PCB for the first
process must be created and saved.
• The PCBs are sometimes stored upon a per-
process stack in kernel memory (as opposed to
the user-mode call stack), or there may be some
specific operating system defined data structure
for this information.
Cont…
• Since the operating system has effectively
suspended the execution of the first process,
it can now load the PCB and context of the
second process.
Steps in Summary
• Save the context of the process that just
finished executing so that, when the scheduler
gets back to the execution of the first process,
it can restore this state and continue.
Cont…
• Place the process that just finished
executing at the end of the queue for its
priority.
• Find the highest priority queue that contains
ready processes.
• Remove the processes at the head of the
queue, load its context, and execute it.
CPU Switch From Process to Process

You might also like