A a Chap 3 Processes
A a Chap 3 Processes
A a Chap 3 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.