Os Unit-Ii
Os Unit-Ii
Process
• A process is an instance of a program in execution.
• Batch systems work in terms of "jobs".
• Many modern process concepts are still
expressed in terms of jobs, ( e.g. job
scheduling ), and the two terms are often
used interchangeably.
The Process
▸ Process memory is divided into four sections
▹ The text section comprises the compiled program code, read
in from non-volatile storage when the program is launched.
▹ The data section stores global and static variables, allocated
and initialized prior to executing main.
▹ The heap is used for dynamic memory allocation, and is
managed via calls to new, delete, malloc, free, etc.
▹ The stack is used for local variables. Space on the stack is
reserved for local variables when they are declared and the
space is freed up when the variables go out of scope.
Note :that the stack and the heap start at opposite ends of the
process'sfree space and grow towards each other. If they should ever meet, then either a stack overflow error
will occur, or else a call to new or malloc will fail due to insufficient memory available.
Process Scheduling:
The act of determining which process is in the ready state, and should be moved to
the running state is known as Process Scheduling.
Process Scheduling Objectives
• The two main objectives of the process scheduling system are
• To keep the CPU busy at all times
• To deliver "acceptable" response times for all programs, particularly for interactive ones.
• The process scheduler must meet these objectives by implementing suitable policies for swapping
processes in and out of the CPU.
Scheduling Queues
• All processes, upon entering into the system, are
stored in the Job Queue.
• Processes in the Ready state are placed in
the Ready Queue.
• Processes waiting for a device to become available
are placed in Device Queues. There are unique
device queues available for each I/O device.
• A new process is initially put in the Ready queue.
It waits in the ready queue until it is selected for
execution(or dispatched). Once the process is
assigned to the CPU and is executing, one of the
following several events can occur:
• The process could issue an I/O request, and
then be placed in the I/O queue.
• The process could create a new subprocess and wait for its termination.
• The process could be removed forcibly from the CPU, as a result of an interrupt, and be put
back in the ready queue.
Dispatcher is responsible for loading the process selected by Short-term scheduler on the CPU (Ready to
Running State) Context switching is done by dispatcher only.
• A dispatcher does the following:
• Switching context.
• Switching to user mode.
• Jumping to the proper location in the newly loaded program.
Medium-term scheduler :
• It is responsible for suspending and resuming
the process.
• It mainly does swapping (moving processes
from main memory to disk and vice versa).
• Swapping may be necessary to improve the
process mix or because a change in memory
requirements has overcommitted available
memory, requiring memory to be freed up.
• It is helpful in maintaining a perfect balance
between the I/O bound and the CPU bound. It
reduces the degree of multiprogramming.
• When system loads get high, this scheduler will Addition of a medium-term scheduling to the queueing
diagram
swap one or more processes out of the ready queue
system
for a few seconds, in order to allow smaller faster jobs to finish up quickly and clear the system.
Context Switch
▸ Whenever an interrupt arrives, the CPU must do
a state-save of the currently running process,
then switch into kernel mode to handle the
interrupt, and then do a state-restore of the
interrupted process.
▸ Similarly, a context switch occurs when the time
slice for one process has expired and a new
process is to be loaded from the ready queue.
This will be instigated by a timer interrupt,
which will then cause the current process's state
to be saved and the new process's state to be
UNIT-2 OPERATING SYSTEMS CSE- AIML , ACOE Page 4
restored.
▸ Saving and restoring states involves saving and restoring all of the registers and program counter(s),
as well as the process control blocks described above. Diagram showing CPU switch from process
▸ Context switching happens VERY VERY frequently, to process
and the overhead of doing the switching is st
lost CPU time, so context switches ( state saves & restores ) need to be as fast as possible. Some
hardware has special provisions for speeding this up, such as a single machine instruction for saving or
restoring all registers at once.
Operations on Processes
▸ Process Creation
▸ Process Termination
Process Creation
▸ A process may create several new processes, via a create-process system
call, during the course of execution.
▸ The creating process is called a parent process and the new processes
are called the children of that process
▸ Each of these new processes may in turn creates other processes forming
a tree of processes
How Resource sharing will be done between parent and children
▸ Parent process can share all resource with children
▸ Parent can share some resource with children
▸ Parent never share any resource with children
When a process creates a new process, two possibilities exist in term of execution:
▸ 1.The parent continues to execute concurrently with its children.
▸ 2.The parent waits until some or all of its children terminated
There are also two possibilities in terms of the address space of the new process
▸ 1.The child process is a duplicate of the parent process(it has the same program and data as the
parent)
▸ 2.The child process had a new program
loaded into it.
▸ Unix examples:
▸ fork() system call creates a new process
▸ exec system call replaces newly created
process with new process
Process Termination
1. A process terminates when its finishes executing its finalProcess creation
statement usingthe
and asks theoperating
fork() system call.
system
to delete it by the exit() system call.
2. At that point, the process may return a status value(typically an integer) to its parent process(via
the wait() system call).
3. All the resources of the process-including physical and virtual memory open files, and I/O buffers are
deallocated by the operating system.
Termination can occur in other circumstances as well:
1.A process can cause the termination of another process via an appropriate system call
Interprocess Communication
1. Processes executing concurrently in the operating system may be either independent processes or
cooperating processes.
2.A process is independent if it cannot affect or be affected by the other processes executing in the system.
Any process that does not share data with any other process is independent.
3.A process is cooperating if it can affect or be affected by the other processes executing in the system.
1. There are several reasons for providing an environment that allows processcooperation:
1. Information sharing
2. Computation speedup.
3. Modularity
4. Convenience
2. Information sharing. Since several users may be interested in the same piece of information (for
instance, a shared file), we must provide an environment to allow concurrent access to such
information.
3. Computation speedup. If we want a particular task to run faster, we must break it into subtasks,
each of which will be executing in parallel with the others. Notice that such a speedup can be achieved only
if the computer has multiple processing cores.
4. Modularity. We may want to construct the system in a modular fashion dividing the system
functions into separate processes or threads,
5. Convenience. Even an individual user may work on many tasks at the same time. For instance, a
user may be editing, listening to music, and compiling in parallel.
Message-Passing Systems
• Message passing provides a mechanism to allow processes to communicate and to synchronize
their actions without sharing the same address space.
• It is particularly useful in a distributed environment, where the
communicating processes may reside on different computers connected by
a network.
• A message-passing facility provides at least two operations:
send(message) receive(message)
• Messages sent by a process can be either fixed or variable in size.
Fixed-sized messages can be sent, the system-level
implementation is straightforward. (But makes the task of
programming more difficult)
Variable-sized messages require a more complex system level implementation, ( but
the programming task becomes simpler)
• If processes P and Q want to communicate, they must send messages to andreceive messages from each
other:
• A communication link must exist between them. This link can be implemented in a variety of ways.
Here are several methodsfor logically implementing a link and the send()/receive() operations
Direct Communication
Direct communication, each process that wants
to communicate must explicitly name the recipient
or sender of the communication. P Q
1. A communication link in this scheme has the
following properties:
2. A link is established automatically between every pair of processes that want to communicate.
The processes need to know only each other’s identity to communicate.
3. A link is associated with exactly two processes.
4. Between each pair of processes, there exists exactly one link.
Indirect Communication
The messages are sent to and received from mailboxes, or ports
1. send(A, message)—Send a message to mailbox A.
2. receive(A, message)—Receive a message from mailbox A.
In this scheme, a communication link has the following properties:
2. A link is established between a pair of processes only if both members of the pair have a
shared mailbox.
3. A link may be associated with more than two processes.
4. Between each pair of communicating processes, a number of different links may exist,
with each link corresponding to one mailbox..
Synchronization
1. Message passing may be either blocking or nonblocking— also known as synchronous and
asynchronous
1. Blocking send. The sending process is blocked until the message is received by the
receiving process or by the mailbox.
2. Blocking receive. The receiver blocks until a message is available.
3. Nonblocking send. The sending process sends the message and resumes operation.
4. Nonblocking receive. The receiver retrieves either a valid message or a null.
Buffering
1. Whether communication is direct or indirect, messages exchanged by communicating processes
reside in a temporary queue. Basically, such queues can be implemented in three ways:
1. Zero capacity. :The queue has a maximum length of zero; thus, the link cannot have
any messages waiting in it. In this case, the sender must block until the recipient receives
the message.
Multithreaded Programming
1. Definition of Thread
1. A thread is the unit of execution
within a process. (Or)
2. A thread is a basic unit of CPU
utilization, consisting of a program
counter, a stack, and a set of
registers, ( and a thread ID. )
3. Threads are also known as
Lightweight processes.
4. A traditional (or heavyweight) process
has a single thread of control.
5. If a process has multiple threads of control, it can perform more than one task at a time.
6. Most software applications that run on
modern computers are multithreaded.
7. An application typically is implemented as a
separate process with several threads of control
Example: word processor (MS Word)
The MS-Word process could involve many threads:
1. Interaction with the keyboard § Display
of characters on the display page
2. Regularly saving file to disk
3. Controlling spelling and grammar Etc.
4. All these threads would share the same Multithreaded server architecture
document
Another example is a web server - Multiple threads allow for multiple requests to be satisfied
simultaneously, without having to service requests sequentially or to fork off separate processes for every
incoming request.
Benefits To Multi-threading:
There are four major categories of benefits to multi-threading:
1. Responsiveness - One thread may provide rapid response while other threads are blocked or
slowed down doing intensive calculations.
2. Resource sharing - By default threads share common code, data, and other resources, which allows
multiple tasks to be performed simultaneously in a single address space.
3. Economy – Allocating memory and resources for process creation is costly. Because threads share
resources of the process to which they belong, it is more economical to create and context-switches
threads.
Types of Thread
1. There are two types of threads:
1. User Threads
2. Kernel Threads
2. User threads are above the kernel and without kernel support. These are the threads that
application programmers use in their programs.
1. Implementation of User Level thread is done by a thread library and is easy.
2. Example of User Level threads: Java thread, POSIX threads.
3. Kernel threads are supported within the kernel of the OS itself. All modern OSs support kernel-level
threads, allowing the kernel to perform multiple simultaneous tasks and/or to service multiple kernel
system calls simultaneously.
1. While the Implementation of the kernel-level thread is done by the operating system and
is complex.
2. Example of Kernel level threads: Window Solaris.
Multithreading Models
1. The user threads must be mapped to kernel threads, by one of the following strategies:
1. Many to One Model
2. One to One Model
3. Many to Many Model
Many-To-One Model
• In the many-to-one model, many user-level threads are all mapped onto a
single kernel thread.
• Thread management is handled by the thread library in user space, which
is very efficient.
• However, the entire process will block if a thread makes a blocking system call.
• Because a single kernel thread can operate only on a single CPU, the many-to-one model does not
allow individual processes to be split across multiple CPUs.
• The disadvantage is when we considers multiprocessor system so this cannot be considered
because only one kernel is present so we can’t achieve the parallelism
Thread libraries
• Thread libraries provide programmers with an API for creating and managing threads.
• Thread libraries may be implemented either in user space or in kernel space. The former involves
API functions implemented solely within user space, with no kernel support. The latter involves
system calls, and requires a kernel with thread library support.
• There are three main thread libraries in use today:
1. POSIX Pthreads - may be provided as either a user or kernel library, as an extension to
the POSIX standard.
2. Win32 threads - provided as a kernel-level library on Windows systems.
3. Java threads - Since Java generally runs on a Java Virtual Machine, the implementation of
threads is based upon whatever OS and hardware the JVM is running on, i.e. either Pthreads
or Win32 threads depending on the system.
Threading Issues
• There are a variety of issues to consider with multithreaded programming
1. Semantics of fork() and exec() system calls
2. Thread cancellation
3. Signal handling
4. Thread pooling
5. Thread-specific data
Which version of fork() must be used totally depends upon the application.
Next system call i.e. exec() system call when invoked replaces the program along with all its threads
with the program that is specified in the parameter to exec(). Typically the exec() system call is
lined up after the fork() system call.
Here the issue is if the exec() system call is lined up just after the fork() system call then
duplicating all the threads of parent process in the child process by fork() is useless.
As the exec() system call will replace the entire process with the process provided to exec() in the
parameter.
Thread Cancellation
Thread cancellation involves terminating a thread before it has completed.
A thread that is to be cancelled is often referred to as the target thread.
Cancellation of a target thread may occur in two different scenarios:
1. Asynchronous cancellation:
One thread immediately terminates the target thread.
2. Deferred cancellation.
The target thread periodically checks whether it should terminate, allowing it an opportunity to
terminate itself in an orderly fashion.
The issue related to the target threads are listed below:
1. What if the resources had been allotted to the cancel target thread?
2. What if the target thread is terminated when it was updating the data, it was sharing with
some other thread.
Here the asynchronous cancellation of the thread where a thread immediately cancels the target
thread without checking whether it is holding any resources or not creates troublesome.
However, in deferred cancellation, the thread that indicates the target thread about the
cancellation, the target thread crosschecks its flag in order to confirm that it should it be cancelled
immediately or not. The thread cancellation takes place where they can be cancelled safely such
points are termed as cancellation points by Pthreads.
Signal Handling
A signal is used in UNIX systems to notify a process that a particular event has occurred. A signal
may be received either synchronously or asynchronously,
Synchronous signals are delivered to the same process that performed the operation that caused the
signal
Examples of synchronous signal include illegal memory access and division by 0.
Asynchronous Signal are generated by an event external to a running process, that process receives
the signal asynchronously.
Thread Pool
When a user requests for a webpage to the server, the server creates a separate thread to service the
request. Although the server also has some potential issues. Consider if we do not have a bound on
the number of actives thread in a system and would create a new thread for every new request then it
would finally result in exhaustion of system resources.
The idea is to create a finite amount of threads when the process starts. This collection of threads is
referred to as the thread pool.
A thread pool is a group of threads that have been pre-created and are available to do work as
needed
1. Threads may be created when the process starts
2. A thread may be kept in a queue until it is needed
3. After a thread finishes, it is placed back into a queue until it is needed again
4. Avoids the extra time needed to spawn new threads when they’re needed
In applications where threads are repeatedly being created/destroyed thread pools might
provide a performance benefit
Example: A server that spawns a new thread each time a client connects to the system and discards that
thread when the client disconnects
Scheduling
1. Scheduling of this kind is a fundamental operating-system
function.
2. Almost all computer resources are scheduled before use.
3. The CPU is, of course, one of the primary computer resources.
Thus, its scheduling is central to operating-system design.
4. Scheduling of processes/work is done to finish the work
on time.
5. CPU–I/O Burst Cycle
6. The success of CPU scheduling depends on an observed property of processes:
7. Process execution consists of a cycle of CPU execution and I/O wait.
8. Processes alternate between these two states.
1. Process execution begins with a CPU burst. Alternating sequence of CPU and
2. That is followed by an I/O burst. I/O bursts.
9. Another CPU burst, then another I/O burst, and so on.
10. Eventually, the final CPU burst ends with a systemrequest to terminate execution
Process scheduling
The act of determining which process is in the ready state, and should be moved to
the running state is known as Process Scheduling.
CPU Scheduler
1.Short-term scheduler selects from among the processes in ready queue, and allocates the CPU to one of them
Preemptive
Preemptive scheduling is used when a process switches from running state to ready state or from waiting
state to ready state.
The resources (mainly CPU cycles)are allocated to the process for the limited amount of time and then
is taken away ,and the process is again placed back in the ready queue if that process still has CPU
burst time remaining.
The process stays in ready queue till it gets next chance to execute.
Algorithms based on preemptive scheduling are:
1. Round Robin(RR)
2. Shortest Remaining First(SRTF),
3. Priority(Preemptive version).etc
Non-Preemptive Scheduling
1. Non-Preemptive scheduling is used when a process terminates, or a process switches from running to
waiting state.
2. In this scheduling, once the resources(CPU Cycles) is allocated to a process, the process hold the CPU
till it gets terminated or it reaches a waiting state.
3. In case of non-preemptive scheduling does not interrupts a process running CPU in middle of
the executing .
4. Instead it waits still the process completes its CPU burst time and then it can allocates the CPU to
another process
Algorithms based on Non-Preemptive scheduling are :
1. Shortest Job First(SJF basically non Preemptive)
2. Priority (non-preemptive version)etc
3. First Come First Serve
Dispatcher
1. Another component involved in the CPU-scheduling function is the dispatcher.
2. The dispatcher is the module that gives control of the CPU to the process selected by the short-term
scheduler.
3. This function involves the following:
1. Switching context
2. Switching to user mode
3. Jumping to the proper location in the user program to
restart that program
Scheduling Criteria
Many criteria have been suggested for comparing CPU-scheduling algorithms. Which characteristics are used
for comparison can make a substantial difference in which algorithm is judged to be best.
The criteria include the following:
CPU utilization :
Keep the CPU as busy as possible
Throughput :
Number of processes that complete their execution per time unit
Turnaround time:
Amount of time to execute a particular process
Waiting time:
Amount of time a process has been waiting in the ready queue
Response time
Amount of time it takes from when a request was submitted until the first response is produced, not
output (for time-sharing environment)
Scheduling Algorithms
1. Different Scheduling Algorithms
2. First Come First Serve(FCFS) Scheduling
3. Shortest-Job-First(SJF) Scheduling
4. Priority Scheduling
5. Round Robin(RR) Scheduling
6. Multilevel Queue Scheduling
7. Multilevel Feedback Queue Scheduling
8. Shortest Remaining Time First (SRTF)
9. Longest Remaining Time First (LRTF)
Gantt chart
Example:
Consider the following set of processes that arrive at time 0, with the length of the CPU burst given in
milliseconds
If the processes arrive in the order P1, P2, P3, and are served in FCFS order, we get the result shown in the
following Gantt chart
Average Turn
Gantt Chart
0 3 4 9 1 17 19
The shaded box represents the idle time of CPU
Shortest-Job-First Scheduling
1. A different approach to CPU scheduling is the shortest-job-first (SJF) scheduling algorithm.
2. This algorithm associates with each process the length of the process’s next CPU burst.
3. When the CPU is available, it is assigned to the process that has the smallest next CPU burst.
4. This is the best approach to minimize waiting time.
5. If the next CPU bursts of two processes are the same, FCFS scheduling is used to break the tie.
6. A more appropriate term for this scheduling method would be the shortest-next- CPU-burst
algorithm, because scheduling depends on the length of the next CPU burst of a process, rather than
its total length.
By comparison, if we were using the FCFS scheduling scheme, the average waiting time would be 10.25
milliseconds.
Example 2:
Consider the below processes available in the ready queue for execution, with arrival time as 0 for all and
rst times.
given
Gantt chart:
Example:SJF (Preemptive)
Consider the set of 5 processes whose arrival time and burst time are given below-
If the CPU scheduling policy is SJF preemptive, calculate the average waiting time and average turn
around time. Gantt Chart:
0 1 2 5 7 13 20
Priority Scheduling
1. A priority is associated with each process, and the CPU is allocated to the process with the highest priority.
2. Equal-priority processes are scheduled in FCFS order.
3. An SJF algorithm is simply a priority algorithm where the priority (p) is theinverse of the (predicted) next
CPU burst.
4. The larger the CPU burst, the lower the priority, and vice versa.
Note:Some systems use low numbers to represent low priority; others use low numbers for high priority. This
difference can lead to confusion.
1. We assume that low numbers represent high priority.
2. Priority scheduling can be either preemptive or nonpreemptive.
3. A preemptive priority scheduling algorithm will preempt the CPU if the priority of the newly arrived
process is higher than the priority of the currently running process.
4. A nonpreemptive priority scheduling algorithm will simply put the new process at the head of
the ready queue.
Using priority scheduling, we would schedule these processes according to the following
= 41/5
=8.2 ms
Example:
Consider the set of 5 processes whose arrival time and burst time are given below-
If the CPU scheduling policy is priority non-preemptive, calculate the average waiting time and average
turn around time. (Higher number represents higher priority)
Consider the set of processes with arrival time(in milliseconds), CPU burst time (in milliseconds), and
priority(0 is the highest priority) shown below. None of the processes have I/O burst time.
Calculate the average waiting time (in milliseconds) of all the processes using preemptive priority scheduling
algorithm
0 2 5 3 4 4 5 6
3 0 9 1 7
Example:
Consider the set of 5 processes whose arrival time and burst time are given below-
If the CPU scheduling policy is priority preemptive, calculate the average waiting time and average turn
around time. (Higher number represents higher priority)
Round-Robin Scheduling
1. The round-robin (RR)
scheduling algorithm is
designed especially for
P1 P2 P3 CPU
timesharing systems.
2. It is similar to FCFS
P9 P5 P6
to switch between processes.
3. A small unit of time,
P8
called a time quantum or time
P7 slice, is defined. A time quantum
is generally from 10 to 100
milliseconds in length.
4. The ready queue is treated
as a circular queue.
5. The CPU scheduler goes
around the ready queue, allocating the CPU to each process for a time interval of up to 1 time quantum.
Implementation RR scheduling
1. we keep ready queue as a FIFO queue of processes.
2. New processes are added to the tail of the ready queue.
3. The CPU scheduler picks the first process from the ready queue, sets a timer tointerrupt after 1
time quantum, and dispatches the process.
Example:
Consider the following set of processes that arrive at time 0, with the length of the CPU burst given in
milliseconds: time quantum of 4 milliseconds
Solution:
=47/3
=15.66ms
=5.66
ms
ExampleConsider the set of 4 processes whose arrival time and burst time are given below-
If the CPU scheduling policy is Round Robin with time quantum = 2 unit, calculate the average waiting time
and average turnaround time.
0 2 4 6 8 9 11 12
Consider the set of 5 processes whose arrival time and burst time are givenbelow-
If the CPU scheduling policy is Round Robin with time quantum = 2 unit, calculate the average waiting
time and average turn around time.
1. Longest Job First (LJF): It is similar to SJF scheduling algorithm. But, in this scheduling algorithm,
we give priority to the process having the longest burst time. This is non-preemptive in nature i.e., when any
process starts executing, can’t be interrupted before complete execution.
2. Longest Remaining Time First (LRTF): It is preemptive mode of LJF algorithm in which we
give priority to the process having largest burst time remaining.
6. An example of a multilevel queue scheduling algorithm with five queues, listed below in order
of priority:
1. For example, separate queues might be used for foreground and background processes. The
foreground queue might be scheduled by an RR algorithm, while the background queue is
scheduled by an FCFS algorithm.
In addition, there must be scheduling among the queues, which is commonly implemented as fixed-priority
preemptive scheduling.
For example, the foreground queue may have
absolute priority over the background queue. Each
queue has absolute priority over lower-priority queues.
No process in the batch queue, for example, could run
unless the queues for system processes, interactive
processes, and interactive editing processes were all
empty. If an interactive editing process entered the
ready queue while a batch process was running, the
batch process would be preempted.
Another possibility is to time-slice among the
queues.
Here, each queue gets a certain portion of the CPU time,which it can then schedule among its variou
processes. For instance, in the foreground–background queue example, thesforeground queue can be given 80
percent of the CPU time for RR scheduling among its processes, while the background queue receives 20
percent of the CPU to give to its processes on an FCFS basis.
Thread Scheduling
1. The process scheduler schedules only the kernel threads.
2. User-level threads are managed by a thread library, and the kernel is unaware of them.
3. To run on a CPU, user-level threads must ultimately be mapped to an associated kernel-level
thread, although this mapping may be indirect and may use a lightweight process (LWP).
4. In this we explore scheduling issues involving user-level and kernel-level threads and offer
specific examples of scheduling for Pthreads.
Contention Scope
1. Contention scope refers to the scope in which threads compete for the use of physical CPUs.
2. On systems implementing the many-to-one and many-to-many models, the thread library schedules
user-level threads to run on an available LWP. This scheme is known as process contention scope
(PCS)
PCS, occurs, because competition occurs between threads that are part of the same process. (
This is the management / scheduling of multiple user threads on a single kernel thread, and is
managed by the thread library. )
1. Since competition for the CPU takes place among threads belonging to the same process To decide
which kernel-level thread to schedule onto a CPU, the kernel uses system-contention scope
(SCS).
2. Competition for the CPU with SCS scheduling takes place among all threads in the system.
Systems using the one-to-one model such as Windows, Linux, and Solaris, schedule threads using only SCS.
Pthread Scheduling:
1. The Portable Operating System Interface (POSIX) is a family of standards specified by the IEEE
Computer Society for maintaining compatibility between operating systems.
2. The Pthread library provides for specifying scope contention:
1. PTHREAD_SCOPE_PROCESS schedules threads using PCS, by scheduling user threads
onto available LWPs using the many-to-many model.
2. PTHREAD_SCOPE_SYSTEM schedules threads using SCS, by binding user threads to
particular LWPs, effectively implementing a one-to-one model.
3. getscope and setscope methods provide for determining and setting the scope contention respectively:
4. pthread attr setscope(pthread attr t *attr, int scope)
5. pthread attr getscope(pthread attr t *attr, int *scope)
Multiple-Processor Scheduling
When multiple processors are available, then the scheduling gets more complicated, because now there is
more than one CPU which must be kept busy and in effective use at all times.
UNIT-2 OPERATING CSE- AIML , Page
Load sharing revolves around balancing the load between multiple processors.
1. Multi-processor systems may be heterogeneous, ( different kinds of CPUs ), or homogenous, ( all the
same kind of CPU ). Even in the latter case there may be special scheduling constraints, such as devices
which are connected via a private bus to only one of the CPUs.
2. Approaches to Multiple-Processor Scheduling
3. One approach to multi-processor scheduling is asymmetric
multiprocessing, in which one processor is the master, controlling all
activities and running all kernel code, while the other runs only user
code. This approach is relatively simple, as there is no need to share
critical system data.
4. Another approach is symmetric multiprocessing, SMP, where each
processor schedules its own jobs, either from a common ready
queue or from separate ready queues for each processor.
5. Virtually all modern OSes support SMP, including XP, Win
2000, Solaris, Linux, and Mac OSX.
Processor Affinity
High cost of invalidating and repopulating caches, most SMP systems try to avoid migration of processes
from one processor to another and instead attempt to keep a process running on the same processor. This is
known as processor affinity—that is, a process has an affinity for the processor on which it is currently
running.
Process Synchronization
Process Synchronization is the task phenomenon of coordinating the execution of processes in such a way
that no two processes can have access to the same shared data and resources.
Process Synchronization is mainly when multiple processes are running together, and more than one processes
try to gain access to the same shared resource or any data at the same time.
Race Conditions
1. Several processes access and manipulate the same data concurrently
2. The outcome of the execution depends on the particular order in which the access takes place is called
Race Condition
3. Prevent race condition by Synchronization
1. Ensure only one process at a time manipulates the critical data
EXAMPLE
Critical Section
1. When more than one processes access a same code
segment that segment is known as critical section.
2. Critical section contains shared variables or resources
which are needed to be synchronized to maintain
consistency of data variable.
3. In simple terms a critical section is group of
instructions/statements or region of code that need to be executed atomically, such as accessing a
resource (file, input or output port, global data, etc.).
1. Each process must request permission to enter its critical section.
2. The section of code implementing this request is the entry section.
3. The critical section may be followed by an exit section.
Mutual Exclusion with Busy Waiting Mutual exclusion using critical regions.
Various proposals for achieving mutual exclusion, so that while one process is busy updating shared memory
in its critical region, no other process will enter its critical region and cause trouble.
These are proposals for achieving mutual exclusion:
1. Disabling Interrupts
2. Lock Variables
3. Turn Variable or Strict Alternation Approach
Working-
Scene-01:
1. Process P0 arrives.
2. It executes the lock!=0 instruction.
3. Since lock value is set to 0, so it returns value 0 to the while loop.
4. The while loop condition breaks.
5. It sets the lock value to 1 and enters the critical section.
6. Now, even if process P0 gets preempted in the middle, no other process can enter the critical section.
7. Any other process can enter only after process P0 completes and sets the lock value to 0.
Scene-02:
1. Another process P1 arrives.
2. It executes the lock!=0 instruction.
3. Since lock value is set to 1, so it returns value 1 to the while loop.
4. The returned value 1 does not break the while loop condition.
5. The process P1 is trapped inside an infinite while loop.
6. The while loop keeps the process P1 busy until the lock value becomes 0 and its condition breaks.
Scene-03:
1. Process P0 comes out of the critical section and sets the lock value to 0.
2. The while loop condition of process P1 breaks.
3. It sets the lock value to 1 and enters the critical section.
4. Now, even if process P1 gets preempted in the middle, no other process can enter the critical section.
5. Any other process can enter only after process P1 completes and sets the lock value to 0.
Failure of the Mechanism-
1. The mechanism completely fails to provide the synchronization among the processes.
2. It can not even guarantee to meet the basic criterion of mutual exclusion.
Scene-02:
Another process P1 arrives.
1. It executes the lock!=0 instruction.
2. Since lock value is still 0, so it returns value 0 to the while loop.
3. The while loop condition breaks.
4. It sets the lock value to 1 and enters the critical section.
5. Now, process P1 gets preempted in the middle of the critical section.
Scene-03:
Process P0 gets scheduled again.
1. It resumes its execution.
2. Before preemption, it had already failed the while loop condition.
3. Now, it begins execution from the next instruction.
4. It sets the lock value to 1 (which is already 1) and enters the critical section.
Thus, both the processes get to present inside the critical section at the same time.
Problem of Proposal -2:Lock Variables
Suppose that one process reads the lock and sees that it is 0. Before it can set the lock to 1, another
process is scheduled, runs, and sets the lock to 1. When the first process runs again, it will also set the lock to
1, and two processes will be in their critical regions at the same time.
Now you might think that we could get around this problem by first reading out the lock value, then
checking it again just before storing into it, but that really does not help.
1. The race now occurs if the second process modifies the lock just after the first process has finished
its second check.
2. No Mutual Exclusion
Initially, two processes Pi and Pj are available and want to execute into critical section.
The turn variable is equal to i hence Pi will get the chance to enter into the critical section. The value of Pi
remains I until Pi finishes critical section.
Pi finishes its critical section and assigns j to turn variable. Pj will get the chance to
enter into the critical section. The value of turn remains j until Pj finishes its critical
section.
Working-
This synchronization mechanism works as explained in the following scenes-
Scene-01:
1. Process P0 arrives.
2. It executes the turn!=0 instruction.
3. Since turn value is set to 0, so it returns value 0 to the while loop.
4. The while loop condition breaks.
5. Process P0 enters the critical section and executes.
6. Now, even if process P0 gets preempted in the middle, process P1 can not enter the critical section.
7. Process P1 can not enter unless process P0 completes and sets the turn value to 1.
Scene-02:
1. Process P1 arrives.
1. It executes the turn!=1 instruction.
2. Since turn value is set to 0, so it returns value 1 to the while loop.
3. The returned value 1 does not break the while loop condition.
4. The process P1 is trapped inside an infinite while loop.
5. The while loop keeps the process P1 busy until the turn value becomes 1 and its condition breaks.
Scene-03:
1. Process P0 comes out of the critical section and sets the turn value to 1.
2. The while loop condition of process P1 breaks.
3. Now, the process P1 waiting for the critical section enters the critical section and execute.
4. Now, even if process P1 gets preempted in the middle, process P0 can not enter the critical section.
5. Process P0 can not enter unless process P1 completes and sets the turn value to 0.
Problem of Proposal -3 : Turn Variable or Strict Alternation Approach
Characteristics-
1. The characteristics of this synchronization mechanism are-
2. It ensures mutual exclusion.
3. It follows the strict alternation approach.
4. It does not guarantee progress since it follows strict alternation approach.
5. It ensures bounded waiting since processes are executed turn wise one by one and each process
is guaranteed to get a chance.
6. It ensures processes does not starve for the CPU.
7. It is architectural neutral since it does not require any support from the operating system.
8. It is deadlock free.
9. It is a busy waiting solution which keeps the CPU busy when the process is actually waiting.
Peterson's Solution
1. Peterson's Solution is a classic software-based solution to the critical section problem. It is
unfortunately not guaranteed to work on modern hardware, due to vagaries of load and
store operations.
2. Peterson's solution is based on two processes
3. Suppose system contains more than two process then peterson’s solution not sufficient
4. Peterson's solution requires two shared data items:
1. int turn - Indicates whose turn it is to enter into the critical section. If turn = = i, then process
i is allowed into their critical section.
2. boolean flag[ 2 ] - Indicates when a process wants to enter into their critical section. When
process i wants to enter their critical section, it sets flag[ i ] to true.
Note that the instruction "turn = j" is atomic, that is it is a single machine instruction which cannot be
interrupted.
Mutex Locks
1. The hardware solutions presented above are often
difficult for ordinary programmers to access,
particularly on multi-processor machines, and
particularly because they are often platform-dependent.
2. Therefore most systems offer a software API
equivalent called mutex locks or simply mutexes. ( For
mutual exclusion )
3. The terminology when using mutexes is to acquire a lock prior to entering a critical section,
and to release it when exiting
1. Just as with hardware locks, the acquire step will block the
process if the lock is in use by another process, and both the
acquire and release operations are atomic.
2. Acquire and release can be implemented as shown here
Semaphore Usage
Types of Semaphores:
1. The value of a binary semaphore can range only between 0 and 1. In some systems the Binary
Semaphore is called as Mutex locks, because, as they are locks to provide the mutual exclusion
2. The value of a counting semaphore can range over an unrestricted domain. Counting semaphores can
be used to control access to a given resource consisting of a finite number of instances
3. The process that wish to use a resource must performs the wait() operation ( count is decremented )
4. The process that releases a resource must performs the signal() operation ( count is incremented )
5. When the count for the semaphore is 0 means that all the resources are being used by some
processes. Otherwise resources are available for the processes to allocate .
6. When a process is currently using a resource means that it blocks the resource until the count becomes
> 0.
1. For example:
1. – Let us assume that there are two processes p0 and p1 which consists of two statements s0 &
s1 respectively.
2. – Also assume that these two processes are running concurrently such that process p1 executes
the statement s1 only after process p0 executes the statement s0.
Disadvantages of Semaphores
1. While a process is in its critical section, any other process that tries to enter its critical section must
loop continuously in the entry code.
2. Busy waiting wastes CPU cycles that some other process might be able to use productivity
3. This type of semaphore is also called spinlock because the process “spins“ waiting for the lock.
4. To overcome the need for busy waiting ,we can modify the definition of the wait () and signal
() semaphore operations.
5. When a process executes the wait () operation and finds that the semaphore value is not positive, it
must wait.
6. How rather than engaging in busy waiting, the process can block itself.
7. The block operation places a process into a waiting queue associated with the semaphore, and the state of
the process is switched to the waiting state.
8. The control is transferred to the CPU scheduler, which selects another process to executes.
Deadlocks and Starvation
The implementation of semaphore with waiting queue may result in the
situation where two or more processes are waiting for an event is called as
Deadlocked.
• To illustrate this, let us assume two processes P0 and P1 each
accessing two semaphores S and Q which are initialized to 1 :-
Readers-Writers Problem
1.A database is to be shared among several concurrent processes.
9.1. mutex, a semaphore (initialized to 1) which is used to ensure mutual exclusion when readcount is
upadated i.e., when any reader enters or exit from the critical section.
10. 2.Wrt , a semphore (initialized to 1) common to both reader and writer processes.
11. 3.readcount , an integer variable (initialized to 0) that keeps tracks of how many processes are
currently reading the object.
wait (mutex) ;
readcount - - ; //a reader wants to leave
if (readcount == 0) // no reader is left in the critical section
signal (wrt) ; //writers can enter
signal (mutex) ; //reader leave
}
Dining-Philosophers Problem
1. Consider five philosophers who spend their lives thinking
and eating.
2. When a philosopher thinks, she does not interact with
her colleagues.
• Allow a philosopher to pick up her chopsticks only if both chopsticks areavailable (to do this, she must
pick them up in a critical section).
• Use an asymmetric solution—that is, an odd-numbered philosopher picks up first her left chopstick and
then her right chopstick, whereas an even numbered philosopher picks up her right chopstick and then her left
chopstick.
Monitors
• A high-level abstraction that provides a convenient and
effective mechanism for processsynchronization.
• A procedure can access only those variables that are declared in
a monitor and formal parameters
• Only one process may be active within the monitor at a time
Syntax of the monitor :-
monitor monitor-name
{
// shared variable declarations
procedure P1 (…) { …. }
…
Schematic view of a Monitor
procedure Pn (…) {……}
Initialization code ( ….) { … }
…
}