Oslecture3 4
Oslecture3 4
• Process Concept
• Process State
• Process State-transitions
• Process Control Block
• Context switch
• Operations on Processes
Process Concept
• Process - a program in execution
• process execution proceeds in a sequential fashion
• A process is more than the program code, which is
sometimes known as the text section.
• A process includes the current activity, as
represented by the value of the program
counter and the contents of the processors
registrar,Stack which contains temporary
data,and a data section which contains global
variables.
Contd..
• A process is an active entity, with a
program counter specifying the next
instruction to execute and a set of
associated resources.
Difference Between Program and
Process
Program Process
•A set of instructions the •Program in execution
user/programmer has •An active/Dynamic
written.
•Compete for resources
•Passive/static
•Has code section, data
•Can not compete for section, stack and program
resources counter
•Has code section
Structure of a Process in
memory
stack
heap
data
text
Process State
• A process changes state as it executes.
new admitted
new exit terminated
interrupt
running
ready
Scheduler
I/O or dispatch
event I/O or
completion event wait
waiting
Process States
• New - The process is being created.
• Running - Instructions are being executed.
• Waiting - Waiting for some event to occur.
• Ready - Waiting to be assigned to a
processor.
• Terminated - Process has finished
execution.
Process Transition
• Null->New: A new process is created to
execute a program.
• New->Ready: The Operating system will
move a process from the new state to the ready
state. When it is prepared to take on an
additional process.
• Ready->Running: It is a time to select a new
process to run,the O.S. chooses one of the
processes in the ready state.
Cont..
• Running->Exit: The currently running process
terminated by the Operating System.If the
process indicates that it has completed or if it
abort.
• Running -> Ready: The most common
reason for this transition is that the running
process has reached the maximum allowable
time for uninterrupted execution.
Cont…
• Running -> Waiting: A process is put in
the wait state if it request something for
which it must wait. A request to the
operating system is usually in the form of
a system service call.
Process Control Block(PCB)
• Contains information associated with each
process
– Process State - e.g. new, ready, running etc.
– Program Counter - address of next instruction to be
executed
– CPU registers - general purpose registers, stack pointer
etc.
– CPU scheduling information - process priority, pointer
– Memory Management information - base/limit information
– Accounting information - time limits, process number
– I/O Status information - list of I/O devices allocated
Process Control Block
Pointer Process
State
Process Number
Program Counter
registrars
Memory limits
…
Context Switch
• Switching the CPU to another process requires
saving the state of the old process and loading
the saved state for the new process known as
context switch.
• It is represented in the PCB of a process.
• It includes the value of the CPU registers, the
process state and memory management
information.
Cont..
• When a context switch occurs,the kernel
saves the context of the old process in its
PCB and loads the saved context of the
new process scheduled to run.
• In context switch , times are highly
dependent on hardware support.
Cont..
• For eg. Some processors provide multiple
sets of registers, A context switch simply
includes changing the pointer to the
current register sets, the system resorts
to copying register data to and from
memory as before.
Process Scheduling Queues
• Job Queue - set of all processes in the system
• Ready Queue - set of all processes residing in
main memory, ready and waiting to execute.
• Device Queues - set of processes waiting for an
I/O device.
• Process migration between the various queues.
• Queue Structures - typically linked list, circular
list etc.
Process Scheduling Queues
Contd..
• A common representation of process scheduling
is a queueing diagram.
• Each rectangular box represents a queue.
• Two types of queue:
- The ready queue
- A set of devices queues
• The circle represents the resources that serve
the queues.
• Arrows indicates the flow of processes in the
system.
Contd..
• 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 & is
executing, One of several events could occur:
- The process could issue an I/O request & then be
placed in an I/O queue.
- The process could create a new subprocess & wait for
its termination.
Contd..
- The process could be removed forcibly from the CPU, as
a result of an interrupt, & be put back in the ready queue.
• In the first two cases, the process eventually
switches from waiting state to the ready state &
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 &
has its PCB & resources deallocated.
Operations on Processes
Process Creation:
•Processes are created and deleted dynamically
•Process which creates another process is called a
parent process; the created process is called a
child process.
•Result is a tree of processes
• e.g. UNIX - processes have dependencies and
form a hierarchy.
•Resources required when creating process
• CPU time, files, memory, I/O devices etc.
Cont..
• Resource sharing
– Parent and children share all resources.
– Children share subset of parent’s resources - prevents
many processes from overloading the system.
– Parent and child execute concurrently.
– Parent waits until child has terminated.
• Address Space
– Child process is duplicate of parent process.
– Child process has a program loaded into it.
UNIX Process Hierarchy
UNIX Process Creation
• Fork system call creates new processes
• it consists of:
-thread ID,program counter, register set
and stack space
Contd..
• Many-to-One model
• One-to-One model
• Many-to-Many model
Many-to-One
• Many user-level threads mapped to single
kernel thread
• Thread management is done in user
space
• It is efficient.
• The entire process will block if a thread
makes a blocking system call,because
only one thread can access the kernel at a
time.
Contd..