Unit 2
Unit 2
Processes-Process Concept:
• Process state. The state may be new, ready, running, waiting, halted, and so on.
• Program counter: The counter indicates the address of the next instruction to be executed for this process.
• CPU registers: The registers vary in number and type, depending on the computer architecture. They
include accumulators, index registers, stack pointers, and general-purpose registers, plus any condition-code
information. Along with the program counter, this state information must be saved when an interrupt occurs,
to allow the process to be continued correctly afterward (Figure 3.4).
• CPU-scheduling information:This information includes a process priority, pointers to scheduling queues,
and any other scheduling parameters.
• Memory-management information: This information may include such items as the value of the base and
limit registers and the page tables, or the segment tables, depending on the memory system used by the
operating system
• Accounting information. This information includes the amount of CPU and real time used, time limits,
account numbers, job or process numbers, and so on.
• I/O status information. This information includes the list of I/O devices allocated to the process, a list of
open files, and so on.
Process Scheduling:
The objective of multiprogramming is to have some process running at all times, to maximize CPU
utilization. the process scheduler selects an available process (possibly from a set of several available
processes) for program execution on the CPU. For a single-processor system, there will never be more than
one running process. If there are more processes, the rest will have to wait until the CPU is free and can be
rescheduled.
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 Processes migrate among
the various queues.
A common representation of process scheduling is a queueing diagram. Two types of queues are
present: the ready queue and a set of device queues. The circles represent the resources that serve the queues,
and the arrows indicate the flow of processes in the system. A new process is initially put in the ready queue.
It waits there until it is selected for execution, or dispatched. Once the process is allocated the CPU and is
executing, one of several events could occur:
• The process could issue an I/O request and then be placed in an I/O queue.
• The process could create a new child process and wait for the child’s termination.
• The process could be removed forcibly from the CPU, as a result of an interrupt, and be put back in the
ready queue.
CS6401- Operating System
STUDENTSFOCUS.COM
Sri vidya College of Engineering & Technology, Virudhunagar Course material
Schedulers
Long-term scheduler (or job scheduler) – selects which processes should be brought into the
ready queue
Short-term scheduler (or CPU scheduler) – selects which process should be executed next and
allocates CPU
Some operating systems, such as time-sharing systems, may introduce an additional, intermediate level of
scheduling. The key idea behind a medium-term scheduler is that sometimes it can be advantageous to remove a
process from memory (and from active contention for the CPU) and thus reduce the degree of multiprogramming.
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
Context-switch time is overhead; the system does no useful work while switching
Time dependent on hardware support
Operations on Processes
Process Creation
Parent process create children processes, which, in turn create other processes, forming a
tree of processes
Resource sharing
o Parent and children share all resources
o Children share subset of parent’s resources
o Parent and child share no resources
Execution
o Parent and children execute concurrently
o Parent waits until children terminate
Address space
int main()
{
pid_t pid;
/* fork another process */
pid = fork();
if (pid < 0) { /* error occurred */
fprintf(stderr, "Fork Failed");
exit(-1);
}
else if (pid == 0) { /* child process */
execlp("/bin/ls", "ls", NULL);
}
else { /* parent process */
/* parent will wait for the child to complete */
wait (NULL);
printf ("Child Complete");
exit(0);
}
}
A tree of processes on a typical Solaris
Process Termination
Process executes last statement and asks the operating system to delete it (exit)
o Output data from child to parent (via wait)
o Process’ resources are deallocated by operating system
Parent may terminate execution of children processes (abort)
o Child has exceeded allocated resources
o Task assigned to child is no longer required
o If parent is exiting
Some operating system do not allow child to continue if its parent terminates
All children terminated - cascading termination
Cooperating Processes
Direct Communication
Indirect Communication
Messages are directed and received from mailboxes (also referred to as ports)
o Each mailbox has a unique id
o Processes can communicate only if they share a mailbox
Properties of communication link
o Link established only if processes share a common mailbox
o A link may be associated with many processes
o Each pair of processes may share several communication links
o Link may be unidirectional or bi-directional
Threads- Overview
A thread is a basic unit of CPU utilization; it comprises a thread ID, a program counter, a
register set, and a stack. It shares with other threads belonging to the same process its code section, data
section, and other operating-system resources, such as open files and signals. A traditional (or heavyweight)
process has a single thread of control. If a process has multiple threads of control, it can perform more than
one task at a time.
Benefits
The benefits of multithreaded programming can be broken down into four major categories:
2. Resource sharing. Processes can only share resources through techniques such as shared
memory and message passing.
3. Economy. Allocating memory and resources for process creation is costly. Because threads share
the resources of the process to which they belong, it is more economical to create and context-
switch threads.
4. Scalability. The benefits of multithreading can be even greater in a multiprocessor architecture,
where threads may be running in parallel on different processing cores.
Multicore Programming
Earlier in the history of computer design, in response to the need for more computing
performance, single-CPU systems evolved into multi-CPU systems. A more recent, similar trend in
system design is to place multiple computing cores on a single chip. Each core appears as a separate
processor to the operating Whether the cores appear across CPU chips or within CPU chips, we call
these systems multicore or multiprocessor systems.
Multithreaded programming provides a mechanism for more efficient use of these multiple
computing cores and improved concurrency. Consider an application with four threads. On a system
with a single computing core, concurrency merely means that the execution of the threads will be
interleaved over time because the processing core is capable of executing only one thread at a time.
On a system with multiple cores, however,
Concurrency means that the threads can run in parallel, because the system can assign a
separate thread to each core .Notice the distinction between parallelism and concurrency in this
discussion. A system is parallel if it can perform more than one task simultaneously. In contrast, a
concurrent system supports more than one task by allowing all the tasks to make progress.
Multithreading Models
Many-to-One
One-to-One
Many-to-Many
1. Many-to-One
Many user-level threads mapped to single kernel thread
Examples:
o Solaris Green Threads
o GNU Portable Threads
2. One-to-One
Each user-level thread maps to kernel thread
Examples
o Windows NT/XP/2000
o Linux
o Solaris 9 and later
3. Many-to-Many Model
Allows many user level threads to be mapped to many kernel threads
Allows the operating system to create a sufficient number of kernel threads
Solaris prior to version 9
Windows NT/2000 with the ThreadFiber package
Windows 7
Windows implements the Windows API, which is the primary API for the family of Microsoft
operating systems (Windows 98, NT, 2000, and XP, as well as Windows 7). Indeed, much of what is
mentioned in this section applies to this entire family of operating systems. A Windows application runs as a
separate process, and each process may contain one or more threads.
• A private storage area used by various run-time libraries and dynamic link libraries (DLLs).
The register set, stacks, and private storage area are known as the context of the thread. The primary
data structures of a thread include:
• ETHREAD—executive thread block
• KTHREAD—kernel thread block
• TEB—thread environment block
Process Synchronization
There are n processes that are competing to use some shared data
Each process has a code segment, called critical section, in which the shared data is accessed.
Problem – ensure that when one process is executing in its critical section, no other process is allowed
to execute in its critical section.
Requirements to be satisfied for a Solution to the Critical-Section Problem:
1. Mutual Exclusion - If process Pi is executing in its critical section, then no other processes can be
executing in their critical sections.
2. Progress - If no process is executing in its critical section and there exist some processes that wish to enter
their critical section, then the selection of the processes that will enter the critical section next cannot be
postponed indefinitely.
3. Bounded Waiting - A bound must exist on the number of times that other processes are allowed to enter
their critical sections after a process has made a request to enter its critical section and before that request is
granted.
do {
entry section
critical section
exit section
remainder section
} while (true);
Two general approaches are used to handle critical sections in operating systems: preemptive
kernels and nonpreemptive kernels.
A preemptive kernel allows a process to be preempted while it is running in kernel mode.
A non-preemptive kernel does not allow a process running in kernel mode to be preempted; a kernel-
mode process will run until it exits kernel mode, blocks, or voluntarily yields control of the CPU.
Mutex Locks
A high-level abstraction that provides a convenient and effective mechanism for process
synchronization
Only one process may be active within the monitor at a time monitor monitor-name
initialization code
To allow a process to wait within the monitor, a condition variable must be declared as o condition
x, y;
Two operations on a condition variable:
x.wait () –a process that invokes the operation is suspended.
x.signal () –resumes one of the suspended processes(if any)
Monitor DP
{
enum { THINKING; HUNGRY, EATING) state [5] ;
condition self [5];
void pickup (int i) {
state[i] = HUNGRY;
test(i);
if (state[i] != EATING) self [i].wait;
}
void putdown (int i) {
state[i] = THINKING;
// test left and right neighbors
test((i + 4) % 5);
test((i + 1) % 5);
}
void test (int i) {
if ( (state[(i + 4) % 5] != EATING) &&
(state[i] == HUNGRY) &&
(state[(i + 1) % 5] != EATING) ) {
state[i] = EATING ;
self[i].signal () ;
}
}
initialization_code() {
for (int i = 0; i < 5; i++)
state[i] = THINKING;
}
}
Semophores
It is a synchronization tool that is used to generalize the solution to the critical section problem in
complex situations.
A Semaphore s is an integer variable that can only be accessed via two indivisible (atomic) operations
namely
wait (s)
{
1. wait or P operation ( to test )
2. signal or V operation ( to increment )
while(s0);
s--;
}
signal (s)
{
s++;
}
Mutual Exclusion Implementation using semaphore
do
{
wait(mutex);
critical section
remainder section
} while (1);
signal(mutex);
Semaphore Implementation
The semaphore discussed so far requires a busy waiting. That is if a process is in critical-section, the
other process that tries to enter its critical-section must loop continuously in the entry code.
To overcome the busy waiting problem, the definition of the semaphore operations wait and signal
should be modified.
When a process executes the wait operation and finds that the semaphore value is not
positive, the process can block itself. The block operation places the process into a waiting
queue associated with the semaphore.
A process that is blocked waiting on a semaphore should be restarted when some other
process executes a signal operation. The blocked process should be restarted by a wakeup
operation which put that process into ready queue.
To implemented the semaphore, we define a semaphore as a record as:
typedef struct {
int value;
} semaphore;
CS6401- Operating System
STUDENTSFOCUS.COM
Sri vidya College of Engineering & Technology, Virudhunagar Course material
Example: Consider a system of two processes , P0 & P1 each accessing two semaphores ,S & Q, set
to the value 1.
P0 P1
Wait (S) Wait (Q)
Wait (Q) Wait (S)
. .
. .
. .
Signal(S) Signal(Q)
Signal(Q) Signal(S)
Suppose that P0 executes wait(S), then P1 executes wait(Q). When P0 executes wait(Q), it
must wait until P1 executes signal(Q).Similarly when P1 executes wait(S), it must wait until
P0 executes signal(S). Since these signal operations cannot be executed, P0 & P1 are
deadlocked.
Another problem related to deadlock is indefinite blocking or starvation, a situation where a
process wait indefinitely within the semaphore. Indefinite blocking may occur if we add or
remove processes from the list associated with a semaphore in LIFO order.
Types of Semaphores
Counting semaphore – any positive integer value
Binary semaphore – integer value can range only between 0 and 1
CPU Scheduling
CPU Scheduler
Whenever the CPU becomes idle, the operating system must select one of the processes in the ready queue to
be executed.
The selection process is carried out by the short-term scheduler (or CPU scheduler).
The ready queue is not necessarily a first-in, first-out (FIFO) queue. It may be a FIFO queue, a priority queue,
a tree, or simply an unordered linked list.
Preemptive Scheduling
CPU scheduling decisions may take place under the following four circumstances:
1. When a process switches from the running state to the waiting state
2. When a process switches from the running state to the ready state
3. When a process switches from the waiting state to the ready state
4. When a process terminates
Under 1 & 4 scheduling scheme is non preemptive.
Otherwise the scheduling scheme is preemptive.
Non-preemptive Scheduling
In non preemptive scheduling, once the CPU has been allocated a process, the process keeps the CPU
until it releases the CPU either by termination or by switching to the waiting state.
This scheduling method is used by the Microsoft windows environment.
Dispatcher
The dispatcher is the module that gives control of the CPU to the process selected by the short-term
scheduler.
This function involves:
1. Switching context
2. Switching to user mode
3. Jumping to the proper location in the user program to restart that program
Scheduling Criteria
1. CPU utilization: The CPU should be kept as busy as possible. CPU utilization may range from 0 to 100
percent. In a real system, it should range from 40 percent (for a lightly loaded system) to 90 percent (for a
heavily used system).
2. Throughput: Itis the number of processes completed per time unit. For long processes, this rate may be 1
process per hour; for short transactions, throughput might be 10 processes per second.
3. Turnaround time: The interval from the time of submission of a process to the time of completion is the
turnaround time. Turnaround time is the sum of the periods spent waiting to get into memory, waiting in the
ready queue, executing on the CPU, and doing I/O.
4. Waiting time: Waiting time is the sum of the periods spent waiting in the ready queue.
5. Response time: It is the amount of time it takes to start responding, but not the time that it takes to output
that response.
P1 24
P2 3
P3 3
• Associate with each process the length of its next CPU burst. Use these lengths to schedule the
process with the shortest time
• SJF is optimal – gives minimum average waiting time for a given set of processes
P1 0.0 6
P2 2.0 8
P3 4.0 7
P4 5.0 3
• Can be done by using the length of previous CPU bursts, using exponential averaging
• =0
– n+1 = n
• =1
– n+1 = tn
+(1 - )j tn -j + …
+(1 - )n +1 0
• Since both and (1 - ) are less than or equal to 1, each successive term has less weight than its
predecessor
Priority Scheduling
• The CPU is allocated to the process with the highest priority (smallest integer highest priority)
– Preemptive
– nonpreemptive
• SJF is a priority scheduling where priority is the predicted next CPU burst time
• Each process gets a small unit of CPU time (time quantum), usually 10-100 milliseconds. After this
time has elapsed, the process is preempted and added to the end of the ready queue.
• If there are n processes in the ready queue and the time quantum is q, then each process gets 1/n of
the CPU time in chunks of at most q time units at once. No process waits more than (n-1)q time
units.
• Performance
– q large FIFO
CS6401- Operating System
STUDENTSFOCUS.COM
Sri vidya College of Engineering & Technology, Virudhunagar Course material
– q small q must be large with respect to context switch, otherwise overhead is too high
P1 24
P2 3
P3 3
Multilevel Queue
– foreground – RR
– background – FCFS
– Fixed priority scheduling; (i.e., serve all from foreground then from background). Possibility
of starvation.
– Time slice – each queue gets a certain amount of CPU time which it can schedule amongst its
processes; i.e., 80% to foreground in RR
• A process can move between the various queues; aging can be implemented this way
– number of queues
– method used to determine which queue a process will enter when that process needs service
Deadlocks
• A set of blocked processes each holding a resource and waiting to acquire a resource held by another
process in the set.
• Example
– P1 and P2 each hold one disk drive and each needs another one.
• Example
P0 P1
System Model
• Circular wait: there exists a set {P0, P1, …, P0} of waiting processes such that P0 is waiting for a
resource that is held by P1, P1 is waiting for a resource that is held by
P2, …, Pn–1 is waiting for a resource that is held by
Pn, and P0 is waiting for a resource that is held by P0.
Resource-Allocation Graph
– P = {P1, P2, …, Pn}, the set consisting of all the processes in the system.
– R = {R1, R2, …, Rm}, the set consisting of all resource types in the system.
• Process
• Pi requests instance of Rj
• Pi is holding an instance of Rj
Basic Facts
Deadlock Prevention
• Mutual Exclusion – not required for sharable resources; must hold for non-sharable resources.
• Hold and Wait – must guarantee that whenever a process requests a resource, it does not hold any
other resources.
– Require process to request and be allocated all its resources before it begins execution, or
allow process to request resources only when the process has none.
– Low resource utilization; starvation possible.
• No Preemption –
– If a process that is holding some resources requests another resource that cannot be
immediately allocated to it, then all resources currently being held are released.
– Preempted resources are added to the list of resources for which the process is waiting.
– Process will be restarted only when it can regain its old resources, as well as the new ones
that it is requesting.
• Circular Wait – impose a total ordering of all resource types, and require that each process requests
resources in an increasing order of enumeration.
Deadlock Avoidance
Requires that the system has some additional a priori information available.
• Simplest and most useful model requires that each process declare the maximum number of resources
of each type that it may need.
• The deadlock-avoidance algorithm dynamically examines the resource-allocation state to ensure that
there can never be a circular-wait condition.
• Resource-allocation state is defined by the number of available and allocated resources, and the
maximum demands of the processes.
Safe State
• When a process requests an available resource, system must decide if immediate allocation leaves the
system in a safe state.
• System is in safe state if there exists a sequence <P1, P2, …, Pn> of ALL the processes is the systems
such that for each Pi, the resources that Pi can still request can be satisfied by currently available
resources + resources held by all the Pj, with j < i.
• That is:
– If Pi resource needs are not immediately available, then Pi can wait until all Pj have finished.
– When Pj is finished, Pi can obtain needed resources, execute, return allocated resources, and
terminate.
– When Pi terminates, Pi +1 can obtain its needed resources, and so on.
Avoidance algorithms
• Single instance of a resource type. Use a resource-allocation graph
• Multiple instances of a resource type. Use the banker’s algorithm
Resource-Allocation Graph Scheme
• Claim edge Pi Rj indicated that process Pj may request resource Rj; represented by a dashed line.
• Claim edge converts to request edge when a process requests a resource.
• Request edge converted to an assignment edge when the resource is allocated to the process.
• When a resource is released by a process, assignment edge reconverts to a claim edge.
• Resources must be claimed a priori in the system.
Banker’s Algorithm
• Multiple instances.
• Each process must a priori claim maximum use.
• When a process requests a resource it may have to wait.
• When a process gets all its resources it must return them in a finite amount of time.
• Let n = number of processes, and m = number of resources types.
• Available: Vector of length m. If available [j] = k, there are k instances of resource type Rj
available.
• Max: n x m matrix. If Max [i,j] = k, then process Pi may request at most k instances of
resource type Rj.
• Allocation: n x m matrix. If Allocation[i,j] = k then Pi is currently allocated k instances of
Rj.
• Need: n x m matrix. If Need[i,j] = k, then Pi may need k more instances of Rj to complete its
task.
Need [i,j] = Max[i,j] – Allocation [i,j].
3 resource types:
P1 200 322
P2 302 902
P3 211 222
P4 002 433
Need
ABC
P0 743
P1 122
P2 600
P3 011
P4 431
• The system is in a safe state since the sequence < P1, P3, P4, P2, P0> satisfies safety criteria.
Deadlock Detection
• Detection algorithm
• Recovery scheme
• Periodically invoke an algorithm that searches for a cycle in the graph. If there is a cycle, there exists
a deadlock.
• An algorithm to detect a cycle in a graph requires an order of n2 operations, where n is the number of
vertices in the graph.
• Available: A vector of length m indicates the number of available resources of each type.
• Allocation: An n x m matrix defines the number of resources of each type currently allocated to each
process.
• Request: An n x m matrix indicates the current request of each process. If Request [ij] = k, then
process Pi is requesting k more instances of resource type. Rj.
Detection Algorithm
4. If Finish[i] == false, for some i, 1 i n, then the system is in deadlock state. Moreover, if
Finish[i] == false, then Pi is deadlocked.
P1 200 202
P2 303 000
P3 211 100
P4 002 002
• Sequence <P0, P2, P3, P1, P4> will result in Finish[i] = true for all i.
Request
ABC
P0 000
P1 201
P2 001
P3 100
P4 002
• State of system?
– Can reclaim resources held by process P0, but insufficient resources to fulfill other processes;
requests.
– How long process has computed, and how much longer to completion.
• Rollback – return to some safe state, restart process for that state.
• Starvation – same process may always be picked as victim, include number of rollback in cost factor.