Process Management
CSS 122
Summary of Earlier Concepts
•A computer platform consists of a collection of hardware resources
•Computer applications are developed to perform some task
•It is inefficient for applications to be written directly for a given
hardware platform
•The OS was developed to provide a convenient, feature-rich, secure,
and consistent interface for applications to use
•We can think of the OS as providing a uniform, abstract representation
of resources that can be requested and accessed by applications
OS Management of Application Execution
•Resources are made available to multiple applications
•The processor is switched among multiple
applications so all will appear to be progressing
•The processor and I/O devices can be used efficiently
Process
•A program in execution.
•An instance of a program running on a computer.
•The entity that can be assigned to and executed on a
processor.
•A unit of activity characterized by the execution of a
sequence of instructions, a current state, and an associated
set of system resources
Process Elements
•Two essential elements of a process are:
Program code
❖Which may be shared with other processes that are
executing the same program
A set of data associated with that code
•When the processor begins to execute the program code, we
refer to this executing entity as a process
Process Elements
•While the program is executing, this process can be uniquely
characterized by a number of elements, including:
identifier
program
state priority
counter
memory I/O status accounting
context data
pointers information information
Process Elements Cont.
•Identifier: A unique identifier associated with this process, to
distinguish it from all other processes.
•State: If the process is currently executing, it is in the running state.
•Priority: Priority level relative to other processes.
•Program counter: The address of the next instruction in the program
to be executed.
•Memory pointers: Include pointers to the program code and data
associated with this process, plus any memory blocks shared with
other processes.
Process Elements Cont.
•Context data: These are data that are present in registers in
the processor while the process is executing.
•I/O status information: Includes outstanding I/O requests,
I/O devices assigned to this process, a list of files in use by
the process, and so on.
•Accounting information: May include the amount of
processor time and clock time used, time limits, account
numbers, and so on.
Process Control Block
•Contains the process elements
•Makes it possible to interrupt a running
process and later resume execution as if
the interruption had not occurred
•Created and managed by the operating
system
•Key tool that allows support for
multiple processes (multiprogramming)
Process States
Trace Dispatcher
The behavior of an individual process Small program
can be characterized by listing the that switches the
sequence of instructions that execute processor from
for that process one process to
another
The behavior of the processor can
be characterized by showing how
the traces of the various processes
are interleaved
Process Execution
Traces of Processes
Combined Trace of Processes
Two-State Process Model
•A process may be in one of two states:
❖Running
❖Not-running
Queuing Diagram
Reasons for Process Creation
• New batch job
The OS is provided with a batch job control stream, usually on tape or disk.
When the OS is prepared to take on new work, it will read the next sequence
of job control commands.
• Interactive log-on
A user at a terminal logs on to the system.
• Created by OS to provide a service
The OS can create a process to perform a function on behalf of a user
program, without the user having to wait (e.g., a process to control printing)
Reasons for Process Creation Cont.
•Spawned by existing process
For purposes of modularity or to exploit parallelism, a user program
can dictate the creation of a number of processes.
Process Creation
Process Parent
spawning Child process
process
• When the OS • Is the • Is the new
creates a original, process
process at creating,
the explicit process
request of
another
process
Process Termination
•There must be a means for a process to indicate its completion
•A batch job should include a HALT instruction or an explicit OS
service call for termination
•For an interactive application, the action of the user will indicate
when the process is completed (e.g. log off, quitting an
application)
Reasons for Process Termination
•Normal completion
The process executes an OS service call to indicate that it has
completed running
•Time limit exceeded
The process has run longer than the specified total time limit. There
are a number of possibilities for the type of time that is measured.
These include total elapsed time (“wall clock time”), amount of time
spent executing, and, in the case of an interactive process, the amount
of time since the user last provided any input.
Reasons for Process Termination Cont.
• Memory unavailable
The process requires more memory than the system can provide.
• Bounds violation
The process tries to access a memory location that it is not allowed to access
• Protection error
The process attempts to use a resource such as a file that it is not
allowed to use, or it tries to use it in an improper fashion, such as writing
to a read-only file.
Reasons for Process Termination Cont.
•Arithmetic error
The process tries a prohibited computation (such as division by zero) or
tries to store numbers larger than the hardware can accommodate.
•Time overrun
The process has waited longer than a specified maximum for a certain
event to occur
•I/O failure
An error occurs during input or output, such as inability to find a file,
failure to read or write after a specified maximum number of tries
Reasons for Process Termination Cont.
•Invalid instruction
The process attempts to execute a nonexistent instruction (often a result
of branching into a data area and attempting to execute the data).
•Privileged instruction
The process attempts to use an instruction reserved for the operating
system.
•Data misuse
A piece of data is of the wrong type or is not initialized.
Reasons for Process Termination Cont.
•Operator or OS intervention
For some reason, the operator or the operating system has terminated
the process (e.g., if a deadlock exists).
•Parent termination
When a parent terminates, the operating system may automatically
terminate all of the offspring of that parent
•Parent request
A parent process typically has the authority to terminate any of its
offspring.
A Five-State Model
A Five-State Model Cont.
• Running: The process that is currently being executed
• Ready: A process that is prepared to execute when given the opportunity
• Blocked/Waiting: A process that cannot execute until some event occurs,
such as the completion of an I/O operation
• New: A process that has just been created but has not yet been admitted
to the pool of executable processes by the OS
• Exit: A process that has been released from the pool of executable
processes by the OS, either because it halted or because it aborted for
some reason
Combined Trace of Processes
Process States for Trace
Single Blocked Queue
Multiple Blocked Queue
States with One suspend State
Two Suspend States
Processes and Threads
•Multithreading - The ability of an OS to support multiple,
concurrent paths of execution within a single process
•The unit of resource ownership is referred to as a process or task
•The unit of dispatching is referred to as a thread or lightweight
process
Single Threaded Approaches
•A single execution path per process, in which the concept of a thread is
not recognized, is referred to as a single-threaded approach
•MS-DOS, some versions of UNIX supported only this type of process.
Multithreaded Approaches
•A Java run-time environment is a system of one process with multiple
threads; Windows, some UNIXes, support multiple multithreaded
processes.
One or More Threads in a Process
Each thread has:
•An execution state (Running, Ready, etc.)
•Saved thread context when not running (TCB)
•An execution stack
•Some per-thread static storage for local variables
•Access to the shared memory and resources of its process (all
threads of a process share this)
Threads vs. Processes
Benefits of Threads
•It takes far less time to create a new thread in an existing process,
than to create a brand-new process
•It takes less time to terminate a thread than a process
•It takes less time to switch between two threads within the same
process than to switch between processes.
•Threads enhance efficiency in communication between programs
Thread Use in a Single-User System
•Foreground and background work
•Asynchronous processing
•Speed of execution
•Modular program structure
Threads
• In an OS that supports threads, scheduling and dispatching is done
on a thread basis
• Most of the state information dealing with execution is maintained
in thread-level data structures
❖suspending a process involves suspending all threads of the
process
❖termination of a process terminates all threads within the process
Thread Execution States
•The key states for a thread are:
❖Running
❖Ready
❖Blocked
• Thread operations associated with a change in thread state are:
❖ Spawn (create)
❖ Block
❖ Unblock
❖ Finish
Thread Execution
•A key issue with threads is whether or not they can be scheduled
independently of the process to which they belong.
•Or, is it possible to block one thread in a process without blocking the
entire process?
•If not, then much of the flexibility of threads is lost.
RPC Using Single Thread vs Using One Thread per
Server
Multithreading on a Uniprocessor
Thread Synchronization
•It is necessary to synchronize the activities of the various
threads
❖all threads of a process share the same address space and
other resources
❖any alteration of a resource by one thread affects the other
threads in the same process
CPU Scheduling
•The aim of processor scheduling is to assign processes to be
executed by the processor in a way that meets system
objectives
•System objectives
❖Response time
❖Throughput
❖Processor
Types of processor scheduling
•Long-term scheduling is performed when a new process is
created
•Medium-term scheduling is a decision whether to add a
process to those that are at least partially in main memory and
therefore available for execution
•Short-term scheduling is the actual decision of which ready
process to execute next
Types of processor scheduling Cont.
When does/can scheduling occur?
•When a process terminates
•When a process switches from running to waiting state e.g., when
it does an I/O request
•When a process switches from running to ready state E.g., when a
timer interrupt occurs
•When a process switches from waiting state to ready state E.g.,
completion of I/O
Scheduling Criteria
• Turnaround time is the interval of time between the submission of a process
and its completion.
• Response time is the time from the submission of a request until the
response begins to be received
• Throughput; The scheduling policy should attempt to maximize the number
of processes completed per unit of time.
• CPU utilization Percentage of time that CPU is busy (and not idle), over some
period of time
• Fairness In the absence of guidance from the user or other system-supplied
guidance, processes should be treated the same, and no process should
suffer starvation
Scheduling Performance Criteria
•Waiting time: Sum of the time periods spent not running (e.g., in
ready queue)
CPU Scheduling Algorithms
•First-Come, First-Served (FCFS)
•Round-Robin (RR)
•Shortest process next (SPN)
•The shortest remaining time (SRT)
•Priority (PRI)
Selection Function
FCFS max[w] ❖ w = time spent in system so
RR Constant far, waiting
❖ e = time spent in execution so
SRT min [s − e] far
❖ s =total service time required
SRN min[s] by the process, including e
Decision mode
•The decision mode specifies the instants in time at which the
selection function is exercised
•There are two general categories:
❖Non-preemptive: In this case, once a process is in the Running
state, it continues to execute until (a) it terminates or (b) it blocks
itself to wait for I/O or to request some OS service
❖Preemptive: The currently running process may be interrupted
and moved to the Ready state by the OS. This happen when new
process arrives, interrupt occurs or periodically
Scheduling Policies Example
Deadlock
•Deadlock can be defined as the permanent blocking of a set of
processes that either compete for system resources or communicate
with each other
•All deadlocks involve conflicting needs for resources by two or more
processes.
•A common example is the traffic deadlock
Example of Deadlock
P Q
Get A Get B
Get B Get A
Release A Release B
Release B Release A
Example of Deadlock
P Q
Get A Get B
Get B Get A
Release A Release B
Release B Release A
Example of Deadlock
P Q
Get A Get B
Get B Get A
Release A Release B
Release B Release A
Example of Deadlock
P Q
Get A Get B
Get B Get A
Release A Release B
Release B Release A
Deadlock
Example of Deadlock
P Q
Get A Get B
Get B Get A
Release A Release B
Release B Release A
Deadlock
Example of Deadlock
P Q
Get A Get B
Get B Get A
Release A Release B
Release B Release A
Example of Deadlock
P Q
Get A Get B
Get B Get A
Release A Release B
Release B Release A
Conditions for Deadlock
•Mutual exclusion; Only one process may use a resource at a time.
No process may access a resource unit that has been allocated to
another process.
•Hold and wait; A process may hold allocated resources while
awaiting assignment of other resources.
•No preemption; No resource can be forcibly removed from a
process holding it.
•Circular wait; A closed chain of processes exists, such that each
process holds at least one resource needed by the next
Strategy to deal with Deadlock
•Deadlock prevention: Disallow one of the three necessary
conditions for deadlock occurrence, or prevent circular wait
condition from happening.
•Deadlock avoidance: Do not grant a resource request if this
allocation might lead to deadlock.
•Deadlock detection: Grant resource requests when possible,
but periodically check for the presence of deadlock and take
action to recover.