Chapter 2. Processes and Process Management
Chapter 2. Processes and Process Management
Operating System
CoSc 3023
Muluken E.
(MSc)
Email: [email protected]
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.
An instance of a computer program that is being executed in
one of the operating system (virtual process) is known as
process.
When a program begins its execution is known as process.
Process – a program in execution
Slide 3
Process Concept
A Process consists of
Execution context
Code
Data
Stack
The process is divided in to execution contexts.
Execution context having - program counter (Pc)
- stack pointer (Sp)
- data register
Slide 4
Process concept…
S.N. Component & Description
Stack
1
The process Stack contains the temporary data such as method/function
parameters, return address and local variables.
Heap
2
This is dynamically allocated memory to a process during its run time.
Text
3
This includes the current activity represented by the value of Program Counter
and the contents of the processor's registers.
Data
4
This section contains the global and static variables.
Process concept…
Single PC Multiple PCs Multiprogramming of four
(CPU’s point of view) (process point of view) programs
Conceptual model
A 4 independent processes
B A C D Processes run sequentially
B Only one program active at any
C
instant!
B That instant can be very short…
D
D
C
B
A
Time
Process concept…
The rate at which process perform it’s computation is not uniform
Thus processes must not be programmed with built-in assumption
about timing.
The difference b/n Program and Process is subtle but crucial.
The key idea is a process is an activity of some kind.
it has
A program
Input/out
state
Operation on Process
OS has a responsible for providing a mechanism to
create and destroy processes dynamically.
Process Creation
Process Termination
.
Process Creation…
Parent process creates children processes.
The child processes can also create other processes forming a tree of process.
Generally, process identified and managed via a process identifier (pid).
Address space-
Child duplicate of parent
Child has a new process to be loaded into it
Resource sharing
Parent and children share all resources.
Children share subset of parent’s resources.
Parent and child share no resources.
Execution
Parent and children execute concurrently.
Parent waits until some or all of its children terminate.
Process creation…
fork(): The fork () system call is used to create a separate, duplicate
process.
This call creates an exact clone of the calling process.
Parent and child process may share
Same memory image
Same environmental strings
Same open file
exec(): When an exec () system call is invoked, the program specified in the
parameter to exec () will replace the entire process-including all threads.
Process Creation…
But in Windows
Windows has no concept of process hierarchy
Process creation
Loading the correct program
)
Return: TRUE only if the process and thread are successfully created.
Process Termination
Process executes last statement and asks the OS to decide
it (exit ()).
The output data is from child to parent (by wait())
Process resources are deallocated by OS.
Parent may terminate execution of children processes
(abort()).
Child has exceeded allocation of resources.
Task assigned to child is no longer required.
Process Termination…
Conditions that terminate processes can be
Voluntary
Involuntary
Voluntary
Normal exit
Error exit
Involuntary
Fatal error (only sort of involuntary)
Killed by another process
Process Termination
Normal exit (Voluntary)
When a compiler finishes its work it executes a system call
Exit Unix
ExitProcess Windows
Screen oriented programs support Voluntary exit
dispatch
Exit
enter
Not running
running
Pause
Process States
Slide 19
Process state transition
Transition 1- Occurs when the process discovers that it can’t continue
This state transition is:
Block(process Name): RunningBlock
Transition 2– occurs when the scheduler decides time allotted for the process is
expired
This state transition is:
Time-Run-Out(process Name): RunningReady
Transition 3 -occurs when all other process have had their share and it is time for
the first process to run again
This state transition is :
Dispatch(process Name): ReadyRunning
Process state transition…
Transition 4 -occurs when the external event for which a process was waiting
happens
This state transition is :
Wakeup(Process Name): Blocked Ready
o Dispatching loop
Implementation…process context
Contains all states necessary
to run a program
The information the process
needs to do the job: code,
data, stack, heap.
This is known as User level
context.
Implementation…PCB
To implement a process
model OS maintains a
table (array of
structures)called the
process table also
called PCB (process
control block)
One entry for each
Process
Inter-process Communication
Processes executing concurrently in the operating system may
be either independent process or cooperating processes.
Independent process cannot affect or be affected by the execution of
another process.
Cooperating process can affect or be affected by the execution of
another process
Advantages of process cooperation
Information sharing
Computation speed-tasks get divided into multiple tasks, all task
run parallel and achieve the required result.
Modularity by dividing system functions into separate processes
Convenience – if a user is using the same data in different tasks
conflict can be arise so this system try to avoid conflicts.
Cooperating Processes
Cooperating processes require an inter-process communication (IPC)
mechanism to exchange data and information.
Exchange of data between 2 or more separate and independent
process is called inter process communication.
Processes frequently need to communicate with other
processes/threads to share.
• Messages
• Semaphores
• Shared memory
m
m m m
P1 P1
uni c as t m ul ti c as t
tim e
r eq u es t 1
r es p o n s e 1
r eq u es t 2
in ter p r o c es s c o m m u n ic atio n
ex ec u tio n f lo w
r es p o n s e2
p r o c es s b lo c k ed
E ve nt di ag r am fo r a pr o t o c o l
Why we need to study-IPC
First – How one process can pass information to another.
activities.
Protocols and related issues.
Two of the above issues are equally well works for threads and the first one
race conditions.
Find some way to prohibit more than one process from reading
Security
space
The benefit of multithreaded programming can be broken
down into four major categories:
1. Responsiveness
It is an interactive application may allow a program to continue
running even if part of it is blocked or is performing a lengthy
operation.
2. Resource sharing
It allows an application to have several different threads of
activity within the same address space.
Disadvantage of threads over Multi-processor….
3. Economy
Allocating memory and resources for process creation is costly.
Hybrid implementation
User level threads
Implement in user-level
libraries, rather than via
systems calls
Thread switching does not
need to call operating system
and to cause interrupt
to the kernel.
In fact, the kernel knows
nothing about user-level
threads and manages them
as if they were single-threaded
processes.
User level threads-Advantage
Require no modification to operating systems.
Some OS doesn’t support thread package implementation
Simple Representation
Each thread is represented simply by a PC, registers, stack and a
small control block, all stored in the user process address space
called thread table.
Simple management
creating a thread, switching between threads and synchronization
between threads.
Help process to have their own customized scheduling.
Fast and Efficient
Thread switching is not much more expensive than a procedure
call.
User level threads- Disadvantage
Lack of coordination b/n threads and OS kernel.
User-level threads requires non-blocking systems call
Ex. Read on call the keyboard before any char is buffered…. Creates
changes on the semantics of read
The UNIX version alternative ex. Safe select system call by jacket/ wrapper
code
Error during a page fualt
Kernel-Level Threads
In this method, the kernel knows about
and manages the threads.
No runtime system is needed in this case.
Instead of thread table in each process,
the kernel has a thread table that keeps
track of all threads in the system.
In addition, the kernel also maintains
the traditional process table to keep
track of processes. Operating Systems
kernel provides system call to create and
manage threads.
Kernel-Level Threads- Advantages
Because kernel has full knowledge of all threads, Scheduler may decide to
give more time to a process having large number of threads than process
having small number of threads.
Kernel-level threads are especially good for applications that frequently
block.
Kernel level threads do not require any new, non- blocking system calls
Thread recycling is possible
Thread
Kernel