Processes
Process Concept
An operating system executes a variety of programs that run as a
process.
Process – a program (Job) in execution; process execution must
progress in sequential fashion. No parallel execution of instructions of
a single process
This entity contains at least two types of elements:
• Program code and a set of data
Process (also called task) is an executed program code on the
processor, that is, a part of an active entity
A Thread is a portion of a process that can be run independently
Process Concept (Cont.)
Program is passive entity stored on disk (executable file);
process is active
• Program becomes process when an executable file is
loaded into memory
Execution of program started via GUI mouse clicks, command
line entry of its name, etc.
One program can be several processes
• Consider multiple users executing the same program
Program vs Process vs Thread
A program functionProgram.py
A process Execution of this program (run the program)
A thread a portion (sub-task) of this process
Thread 1: Taking
fName value from user
Thread 2: Taking
sName value from user
Thread 3: Calling a
function (greeting)
Thread 4: Printing message
on computer screen
Difference between program & process
PROGRAM PROCESS
It consists of instructions in any It consists of instructions in
programming language. machine code.
It resides in the secondary storage
It resides in the main memory
device
The program only needs memory The resource requirement is quite
for storage high in the case of process
Its life span is unlimited. Its life span is limited
It is a passive entity. It is an active entity.
Difference between process & thread
PROCESS THREAD
Process means any program is in Thread means a segment (part) of
execution. a process.
The process is isolated. Threads share memory.
If one process is blocked then it will If a user-level thread is blocked,
not affect the execution of other then all other user-level threads
processes are blocked.
The process does not share data Threads share data with each
with each other. other.
It takes more time for execution. It takes less time for execution.
Process in Memory
Multiple parts of Process
• The program code, also called text
section
• Current activity including program
counter, processor registers
• Data section containing global
variables
• Stack containing temporary data
• Function parameters, return
addresses, local variables
• Heap containing memory
dynamically allocated during run
time
Memory Layout of a C Program
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
• Ready: The process is waiting to be assigned to a processor
• Terminated: The process has finished execution
Diagram of Process State
Process Control Block (PCB)
Information associated with each process(also called
Process(task) control block)
Process state – running, waiting, etc.
Program counter – location of instruction to next
execute
CPU registers – contents of all process-centric
registers
CPU scheduling information- priorities, scheduling
queue pointers
Memory-management information – memory
allocated to the process
Accounting information – CPU used, clock time
elapsed since start, time limits
I/O status information – I/O devices allocated to
process, list of open files
Process Representation in Linux
Represented by the C structure task_struct
pid t_pid; /* process identifier */
long state; /* state of the process */
unsigned int time_slice /* scheduling information */
struct task_struct *parent;/* this process’s parent */
struct list_head children; /* this process’s children */
struct files_struct *files;/* list of open files */
struct mm_struct *mm; /* address space of this
process */
Operations on Processes
System must provide mechanisms for:
• Process creation
• Process termination
Process Creation
Parent process create children processes, which, in turn
create other processes, forming a tree of processes
Generally, process identified and managed via a process
identifier (pid)
Resource sharing options
• Parent and children share all resources
• Children share subset of parent’s resources
• Parent and child share no resources
Execution options
• Parent and children execute concurrently
• Parent waits until children terminate
Process Creation (Cont.)
Address space
• Child duplicate of parent
• Child has a program loaded into it
UNIX examples
• fork() system call creates new process
• exec() system call used after a fork() to replace the process’
memory space with a new program
• Parent process calls wait()waiting for the child to terminate
A Tree of Processes in Linux
Process Termination
Process executes last statement and then asks the operating
system to delete it using the exit() system call.
• Returns status data from child to parent (via wait())
• Process’ resources are deallocated by operating system
Parent may terminate the execution of children processes using
the abort() system call. Some reasons for doing so:
• Child has exceeded allocated resources
• Task assigned to child is no longer required
• The parent is exiting, and the operating systems does not
allow a child to continue if its parent terminates
Process Termination
Some operating systems do not allow child to exists if its parent
has terminated. If a process terminates, then all its children
must also be terminated.
• cascading termination. All children, grandchildren, etc.,
are terminated.
• The termination is initiated by the operating system.
The parent process may wait for termination of a child process
by using the wait()system call. The call returns status
information and the pid of the terminated process
pid = wait(&status);
If no parent waiting (did not invoke wait()) process is a
zombie
If parent terminated without invoking wait(), process is an
orphan
Process Scheduling
Process Scheduling
Program scheduler
• Initialize each program
• Only concerned with selecting programs from a queue of incoming
programs
• Places them in a process queue (READY queue)
Process scheduler
• Determines which processes get the CPU, when, and for how long.
• Also decides when processing should be interrupted decides when
each step will be executed recognizes when a process has
concluded and should be terminated
Process Scheduling
Process scheduler selects among available processes
for next execution on CPU core
Goal -- Maximize CPU use, quickly switch processes onto
CPU core
Maintains scheduling queues of processes
• Ready queue – set of all processes residing in main
memory, ready and waiting to execute
• Wait queues – set of processes waiting for an event
(i.e., I/O)
• Processes migrate among the various queues
Representation of Process Scheduling
CPU Switch From Process to Process
A context switch occurs when the CPU switches from
one process to another.
Context Switch
When CPU switches to another process, the system must
save the state of the old process and load the saved state
for the new process via a context switch
Context of a process represented in the PCB
Context-switch time is pure overhead; the system does no
useful work while switching
• The more complex the OS and the PCB the longer
the context switch
Time dependent on hardware support
• Some hardware provides multiple sets of registers per
CPU multiple contexts loaded at once
C Program Forking Separate Process
Creating a Separate Process via Windows API