0% found this document useful (0 votes)
16 views23 pages

OSM2

This document covers various aspects of process scheduling in operating systems, including types of schedulers (long-term, short-term, and medium-term) and their roles in managing CPU utilization. It discusses process creation, termination, and context switching, along with several CPU scheduling algorithms such as FCFS, SJF, and Priority Scheduling. The document emphasizes the importance of efficient scheduling to minimize waiting time and maximize CPU utilization.

Uploaded by

tanishabarik345
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views23 pages

OSM2

This document covers various aspects of process scheduling in operating systems, including types of schedulers (long-term, short-term, and medium-term) and their roles in managing CPU utilization. It discusses process creation, termination, and context switching, along with several CPU scheduling algorithms such as FCFS, SJF, and Priority Scheduling. The document emphasizes the importance of efficient scheduling to minimize waiting time and maximize CPU utilization.

Uploaded by

tanishabarik345
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 23

OPERATINGSYSTEMS

LectureNotes
Module II

Prepared by
Dr. RayaguruAkshaya Kumar Das
Module-II
Process Scheduling, Process coordination- Synchronization, Semaphores, monitors,
Hardware synchronization, Deadlocks, Methods for handling deadlocks

Process Scheduling
The act of determining which process in the ready state should be moved to the running state is
known as Process Scheduling.
The prime aim of the process scheduling system is to keep the CPU busy all the time and to deliver
minimum response time for all programs. For achieving this, the scheduler must apply appropriate
rules for swapping processes IN and OUT of CPU.
Schedulers fell into one of the two general categories :
1. Non pre-emptive scheduling. When the currently executing process gives up the CPU
voluntarily.
2. Pre-emptive scheduling. When the operating system decides to favour another process,
pre-empting the currently executing process.
Scheduling Queues
 All processes when enters 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 for each I/O device available.
 Process migration between the various queues.
Ready Queue And Various I/O Device Queues

 A common representation of process scheduling is a queueing diagram. Each rectangular


box in the diagram represents a queue. 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 in the ready queue until it is
selected for execution and is given the CPU. 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 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.
In the first two cases, the process eventually switches from the waiting state to the ready state, and
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.
Representation of Process Scheduling

Types of Schedulers
There are three types of schedulers available :
1. Long Term Scheduler :
Long term scheduler runs less frequently. Long Term Schedulers decide which program must get
into the job queue. From the job queue, the Job Processor, selects processes and loads them into the
memory for execution. Primary aim of the Job Scheduler is to maintain a good degree of
Multiprogramming. An optimal degree of Multiprogramming means the average rate of process
creation is equal to the average departure rate of processes from the execution memory.
3. Short Term Scheduler :
This is also known as CPU Scheduler and runs very frequently. The primary aim of this scheduler
is to enhance CPU performance and increase process execution rate.
3. Medium Term Scheduler :
During extra load, this scheduler picks out big processes from the ready queue for some time, to
allow smaller processes to execute, thereby reducing the number of processes in the ready queue.
Processes can be described as either:
 I/O-bound process – spends more time doing I/O than computations,
 CPU-bound process – spends more time doing computations and few I/O operations.
An efficient scheduling system will select a good mix of CPU-bound processes and I/O bound
processes.
 If the scheduler selects more I/O bound process, then I/O queue will be full and ready
queue will be empty.
 If the scheduler selects more CPU bound process, then ready queue will be full and I/O
queue will be empty.

Time sharing systems employ a medium-term scheduler. It swaps out the process from ready queue
and swap in the process to ready queue. When system loads get high, this scheduler will swap one
or more processes out of the ready queue for a few seconds, in order to allow smaller faster jobs to
finish up quickly and clear the system.
Advantages of medium-term scheduler –
 To remove process from memory and thus reduce the degree of multiprogramming
(number of processes in memory).
 To make a proper mix of processes (CPU bound and I/O bound)

 Short-term scheduler is invoked very frequently (milliseconds)  (must be fast).


 Long-term scheduler is invoked very infrequently (seconds, minutes)  (may be slow).
 The long-term scheduler controls the degree of multiprogramming.

Comparison among Schedulers

S.N. Long-Term Scheduler Short-Term Scheduler Medium-Term


Scheduler
1 It is a job scheduler It is a CPU scheduler It is a process swapping
scheduler.
2 Speed is lesser than short Speed is fastest Speed is in between both
term Scheduler among other two short and long term
scheduler.
3 It controls the degree of It provides lesser It reduces the degree of
multiprogramming control over degree of multiprogramming.
multiprogramming

4 It is almost absent or It is also minimal in It is a part of Time sharing


minimal in time sharing time sharing system systems.
system

5 It selects processes from It selects those It can re-introduce the


pool and loads them into processes process into memory and
memory for execution which are ready to execution can be continued.
execute

Context Switch
A context switch is the mechanism to store and restore the state or context of a CPU in Process
Control block so that a process execution can be resumed from the same point at a later time.
Using this technique, a context switcher enables multiple processes to share a single CPU.
Context switching is an essential part of a multitasking operating system features.
When the scheduler switches the CPU from executing one process to execute another, the state
from the current running process is stored into the process control block. After this, the state for
the process to run next is loaded from its own PCB and used to set the PC, registers, etc. At that
point, the second process can start executing.
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
When the process is switched, the following information is stored for later use.
Program Counter
Scheduling information
Base and limit register value
Currently used register
Changed State
I/O State information
Accounting information
Operations on Process

Process Creation
Through appropriate system calls, such as fork or spawn, processes may create other processes. The
process which creates other process, is termed the parent of the other process, while the created
sub-process is termed its child.
Each process is given an integer identifier, termed as process identifier, or PID. The parent PID
(PPID) is also stored for each process.
On a typical UNIX systems the process scheduler is termed as sched, and is given PID 0. The first
thing done by it at system start-up time is to launch init, which gives that process PID 1. Further
Init launches all the system daemons and user logins, and becomes the ultimate parent of all other
processes.
A child process may receive some amount of shared resources with its parent depending on system
implementation. To prevent runaway children from consuming all of a certain system resource,
child processes may or may not be limited to a subset of the resources originally allocated to the
parent.
There are two options for the parent process after creating the child:
 Wait for the child process to terminate before proceeding. Parent process makes a wait ()
system call, for either a specific child process or for any particular child process, which
causes the parent process to block until the wait() returns. UNIX shells normally wait for
their children to complete before issuing a new prompt.
 Run concurrently with the child, continuing to process without waiting. When a UNIX shell
runs a process as a background task, this is the operation seen. It is also possible for the
parent to run for a while, and then wait for the child later, which might occur in a sort of a
parallel processing operation

Process creation in nutshell


 Parent process create children processes, which, in turn create other processes, forming a tree of

processes.
 Resource sharing
 Parent and children share all resources.
 Children share subset of parent’s resources.
 Parent and child share no resources.
 Execution
 Parent and children execute concurrently.
 Parent waits until children terminate.
 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.
Process Termination
By making the exit(system call), typically returning an int, processes may request their own
termination. This int is passed along to the parent if it is doing a wait(), and is typically zero on
successful completion and some non-zero code in the event of any problem.
Processes may also be terminated by the system for a variety of reasons, including :
 The inability of the system to deliver the necessary system resources.
 In response to a KILL command or other unhandled process interrupts.
 A parent may kill its children if the task assigned to them is no longer needed i.e. if the need
of having a child terminates.
 If the parent exits, the system may or may not allow the child to continue without a parent
(In UNIX systems, orphaned processes are generally inherited by init, which then proceeds
to kill them.)

When a process ends, all of its system resources are freed up, open files flushed and closed, etc.
The process termination status and execution times are returned to the parent if the parent is waiting
for the child to terminate, or eventually returned to init if the process already became an orphan.
The processes which are trying to terminate but cannot do so because their parent is not waiting for
them are termed zombies. These are eventually inherited by init as orphans and killed off.
Process termination in nutshell
 Process executes last statement and asks the operating system to delete it (exit)
 Output data from child to parent (via wait)
 Process’ resources are deallocated by operating system
 Parent may terminate execution of children processes (abort)
 Child has exceeded allocated resources
 Task assigned to child is no longer required
 If parent is exiting
o Some operating system do not allow child to continue if its parent terminates
o All children terminated - cascading termination
CPU Scheduling Algorithm:
• CPU scheduling is the process whereby the OS selects from among processes in the ready queue,
and allocates the CPU to one of them. The main objective is to maximum CPU utilization
SCHEDULING CRITERIA
• A good scheduling algorithm is one which:
– Maximizes CPU utilization
• keep the CPU as busy as possible
– Maximizes Throughput
• # of processes that complete their execution per time unit
– Minimizes Turnaround Time
• amount of time to execute a particular process
– Minimizes Waiting Time
• amount of time process has been waiting in ready queue
– Minimizes Response Time
• amount of time it takes from when a
request was submitted until the first
response is produced (for time-sharing
environment)
– Eliminates Starvation
• when a process never or rarely executes
due to the algorithm

• Maximum CPU utilization obtained with


multiprogramming
• CPU–I/O Burst Cycle – Process execution consists of a
cycle of CPU execution and I/O wait
• CPU burst followed by I/O burst
• CPU burst distribution is of main concern
CPU Brut time
CPU Scheduling deals with the problem of deciding which of the processes in the ready queue is to be
allocated first to the CPU. A Process Scheduler schedules different processes to be assigned to the
CPU based on particular scheduling algorithms. There are six popular process scheduling
algorithms which we are going to discuss in this chapter:
1. First-Come, First-Served (FCFS) Scheduling
2. Shortest-Job-First (SJF) Scheduling
3. Priority Scheduling
4. Shortest Remaining Time
5. Round Robin(RR) Scheduling
6. Multiple-Level Queues Scheduling
These algorithms are either non-preemptiveor preemptive. Non-preemptive algorithms are
designed so that once a process enters the running state, it cannot be preempted until it completes
its allotted time, whereas the preemptive scheduling is based on priority where a scheduler may
preempt a low priority running process anytime when a high priority process enters into a ready
state.

First Come, First Served (FCFS)


 Jobs are executed on first come, first served basis. 
 It is a non-preemptive scheduling algorithm. 
 Easy to understand and implement. 
 Its implementation is based on FIFO queue. 
 Poor in performance, as average wait time is high.
Consider the example
Process Burst Time
P1 24
P2 3
P3 3
 Suppose that the processes arrive in the order: P1 , P2 , P3
The Gantt Chart for the schedule is:

P1 P2 P3
0 24 27 30

 Waiting time for P1 = 0; P2 = 24; P3 = 27


Average waiting time: (0 + 24 + 27)/3 = 17
Version 2
Suppose that the processes arrive in the order:
P2 , P3 , P1
 The Gantt chart for the schedule is:

P2 P3 P1
0 3 6 30

 Waiting time for P1 = 6; P2 = 0; P3 = 3



Average waiting time: (6 + 0 + 3)/3 = 3

Much better than previous case

Convoy effect - short process behind long process
o Consider one CPU-bound and many I/O-bound processes
Shortest-Job-First (SJf) Scheduling
This is also known as shortest job Next, or SJN.
This is a non-preemptive scheduling algorithm.
Best approach to minimize waiting time.
Easy to implement in Batch systems where required CPU time is known in advance.
Impossible to implement in interactive systems where the required CPU time is not known.
The processer should know in advance how much time a process will take.

ProcessArrival Time Burst Time
P1 0.0 6
P2 2.0 8
P3 4.0 7
P4 5.0 3
 SJF scheduling chart

P4 P1 P3 P2
0 3 9 16 24

 Average waiting time = (3 + 16 + 9 + 0) / 4 = 7


Priority Scheduling
 Priority scheduling is a non-preemptive algorithm and one of the most common scheduling
algorithms in batch systems.
 Each process is assigned a priority. Process with highest priority is to be executedfirst and
so on.
 Processes with same priority are executed on first come first served basis.
 Priority can be decided based on memory requirements, time requirements or anyother
resource requirement.

 Each process is assigned a priority.


 Process with highest priority is to be executedfirst and so on. 
 Smallest integer is high prority
 Preemptive
 Nonpreemptive

 Processes with same priority are executed on first come first served basis.
 Priority can be decided based on memory requirements, time requirements or
anyother resource requirement.
 SJF is priority scheduling where priority is the inverse of predicted next CPU burst time
 Problem Starvation– low priority processes may never execute
 Solution Aging– as time progresses increase the priority of the process
EXAMPLE
Process Burst Time Priority
P1 10 3
P2 1 1
P3 2 4
P4 1 5
P5 5 2
 Priority scheduling Gantt Chart

 Average waiting time = 8.2


Shortest Remaining Time
 Shortest remaining time (SRT) is the preemptive version of the SJN algorithm.
 The processor is allocated to the job closest to completion but it can be preempted by
anewer ready job with shorter time to completion.
 Impossible to implement in interactive systems where required CPU time is not known.
 It is often used in batch environments where short jobs need to be given preference.
 Now we add the concepts of varying arrival times and preemption to the analysis
ProcessA Arrival TimeT Burst Time
P1 0 8
P2 1 4
P3 2 9
P4 3 5
 Preemptive SJF Gantt Chart

P1 P2 P4 P1 P3
0 1 5 10 17 26

 Average waiting time = [(10-1)+(1-1)+(17-2)+5-3)]/4 = 26/4 = 6.5 msec


Round Robin(RR) Scheduling
 Round Robin is a preemptive process scheduling algorithm.
 Each process is provided a fix time to execute; it is called a quantum.
 Once a process is executed for a given time period, it is preempted and other
processexecutes for a given time period.
 Context switching is used to save states of preempted processes.

Example of RR with Time Quantum = 4(q)


Process Burst Time
P1 24
P2 3
P3 3
The Gantt chart is:
P1 P2 P3 P1 P1 P1 P1 P1
0 4 7 10 14 18 22 26 30
 Typically, higher average turnaround than SJF, but better response
 q should be large compared to context switch time
 q usually 10ms to 100mili sec,
 context switch < 10 microsec
Multiple-Level Queues Scheduling

Multiple-level queues are not an independent scheduling algorithm. They make use of otherexisting
algorithms to group and schedule jobs with common characteristics.
Multiple queues are maintained for processes with common characteristics.
Each queue can have its own scheduling algorithms.
Priorities are assigned to each queue.
For example, CPU-bound jobs can be scheduled in one queue and all I/O-bound jobs in
anotherqueue. The Process Scheduler then alternately selects jobs from each queue and assigns
them tothe CPU based on the algorithm assigned to the queue.

 Ready queue is partitioned into separate queues, eg:


 foreground (interactive)
 background (batch)
 Process permanently in a given queue
 Each queue has its own scheduling algorithm:
 foreground – RR
 background – FCFS
 Scheduling must be done between the queues:
 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


 20% to background in FCFS

Multilevel Feedback Queue


 A process can move between the various queues; aging can be implemented this way
 Multilevel-feedback-queue scheduler defined by the following parameters:
 number of queues
 scheduling algorithms for each queue
 method used to determine when to upgrade a process
 method used to determine when to demote a process
 method used to determine which queue a process will enter when that process needs
service
Exmple
 Three queues:
 Q0 – RR with time quantum 8 milliseconds
 Q1 – RR time quantum 16 milliseconds
 Q2 – FCFS
 Scheduling
 A new job enters queue Q0which is servedFCFS
 When it gains CPU, job receives 8 milliseconds
 If it does not finish in 8 milliseconds, job is moved to queue Q1
 At Q1 job is again served FCFS and receives 16 additional milliseconds
 If it still does not complete, it is preempted and moved to queue Q2

Cooperating Processes
 Independent process cannot affect or be affected by the execution of another process.
 Cooperating process can affect or be affected by the execution of another process
 Advantages of process cooperation
o Information sharing
o Computation speed-up
o Modularity
o Convenience
Co-operation among processes are allowed for following reasons –
 Information Sharing - There may be several processes which need to access the same file.
So the information must be accessible at the same time to all users.
 Computation speedup - Often a solution to a problem can be solved faster if the problem
can be broken down into sub-tasks, which are solved simultaneously (particularly when
multiple processors are involved.)
 Modularity - A system can be divided into cooperating modules and executed by sending
information among one another.
 Convenience - Even a single user can work on multiple tasks by information sharing.
Cooperating processes require some type of inter-process communication.This is allowed by two
models:
1. Shared Memory systems
2. Message passing systems.

Sl No Shared Memory Message passing


1. A region of memory is shared by Message exchange is done among the
communicating processes, into which the processes by using objects.
information is written and read
2. Useful for sending large block of data Useful for sending small data.
3. System call is used only to create shared System call is used during every read
memory and write operation.
4. Message is sent faster, as there are no Message is communicated slowly.
system calls

 Shared Memory is faster once it is set up, because no system calls are required and access
occurs at normal memory speeds. Shared memory is generally preferable when large
amounts of information must be shared quickly on the same computer.
 Message Passing requires system calls for every message transfer, and is therefore slower,
but it is simpler to set up and works well across multiple computers. Message passing is
generally preferable when the amount and/or frequency of data transfers is small.

Shared Memory Systems


 A region of shared-memory is created within the address space of a process, which needs to
communicate. Other process that needs to communicate uses this shared memory.
 The form of data and position of creating shared memory area is decided by the process.
Generally, a few messages must be passed back and forth between the cooperating processes
first in order to set up and coordinate the shared memory access.
 The process should take care that the two processes will not write the data to the shared
memory at the same time.

PROCESS SYNCHRONIZATION
 A cooperating process is one that can affect or be affected by other processesexecuting in
the system. Cooperating processes can either directly share a logicaladdress space (that is,
both code and data) or be allowed to share data only throughfiles or messages.
 Concurrent access to shared data may result in data inconsistency. To maintain data
consistency, various mechanisms is required to ensure the orderly execution ofcooperating
processes that share a logical address space.
Producer- Consumer Problem


 Producer-Consumer Example Using Shared Memory
 This is a classic example, in which one process is producing data and another process is
consuming the data.
 The data is passed via an intermediary buffer (shared memory). The producer puts the data
to the buffer and the consumer takes out the data from the buffer. A producer can produce
one item while the consumer is consuming another item. The producer and consumer must
be synchronized, so that the consumer does not try to consume an item that has not yet been
produced. In this situation, the consumer must wait until an item is produced.
There are two types of buffers into which information can be put –
 Unbounded buffer
 Bounded buffer

 With Unbounded buffer, there is no limit on the size of the buffer, and so on the data
produced by producer. But the consumer may have to wait for new items.
 With bounded-buffer – As the buffer size is fixed. The producer has to wait if the buffer is
full and the consumer has to wait if the buffer is empty.

This example uses shared memory as a circular queue. The in and out are two pointers to the array.
Note in the code below that only the producer changes "in", and only the consumer changes "out".

First the following data is set up in the shared memory area:

 The producer process –


Note that the buffer is full when [ (in+1) % BUFFER_SIZE == out]
• The consumer process –
Note that the buffer is empty when [ in == out]

Interprocess Communication – Message Passing


Inter-process communication or inter-process communication (IPC) refers specifically to the
mechanisms an operating system provides to allow the processes to manage shared data.
 Message passing systems uses system calls for "send message" and "receive message".
 A communication link must be established between the cooperating processes before
messages can be sent.
 There are three methods of creating the link between the sender and the receiver-
o Direct or indirect communication (naming)
o Synchronous or asynchronous communication (Synchronization)
o Automatic or explicit buffering.
1. Naming
Processes that want to communicate must have a way to refer to each other. They can use either
direct or indirect communication.
a) Direct communicationthe sender and receiver must explicitly know each other’s name. The
syntax for send() and receive() functions are as follows-
• send(P, message) – send a message to process P
• receive(Q, message) – receive a message from process Q

Properties of communication link:


• A link is established automatically between every pair of processes that wants to
communicate. The processes need to know only each other's identity to communicate.
• A link is associated with exactly one pair of communicating processes
• Between each pair, there exists exactly one link.

Types of addressing in direct communication –


• Symmetric addressing – the above-described communication is symmetric
communication. Here both the sender and the receiver processes have to name each other to
communicate.
• Asymmetric addressing – Here only the sender’s name is mentioned, but the receiving data
can be from any system.

send (P, message) --- Send a message to process P


receive (id, message). Receive a message from any process
Disadvantages of direct communication– any changes in the identifier of a process, may have to
change the identifier in the whole system (sender and receiver), where the messages are sent and
received.
b) Indirect communicationuses shared mailboxes, or ports.
A mailbox or port is used to send and receive messages. Mailbox is an object into which messages
can be sent and received. It has a unique ID. Using this identifier messages are sent and received.
Two processes can communicate only if they have a shared mailbox. The send and receive
functions are –
• send (A, message) – send a message to mailbox A
• receive (A, message) – receive a message from mailbox A

Properties of communication link:


 A link is established between a pair of processes only if they have a shared mailbox
 A link may be associated with more than two processes
 Between each pair of communicating processes, there may be any number of links, each
link is associated with one mailbox.
 A mail box can be owned by the operating system. It must take steps to –
o create a new mailbox
o send and receive messages from mailbox
o delete mailboxes.

2. Synchronization
The send and receive messages can be implemented as either blocking or non-blocking.
 Blocking (synchronous) send - sending process is blocked (waits) until the message is
received by receiving process or the mailbox.
 Non-blocking (asynchronous) send - sends the message and continues (does not wait)
 Blocking (synchronous) receive - The receiving process is blocked until a message is
available
 Non-blocking (asynchronous) receive - receives the message without block. The received
message may be a valid message or null.

3. Buffering
When messages are passed, a temporary queue is created. Such queue can be of three capacities:
 Zero capacity – The buffer size is zero (buffer does not exist). Messages are not stored in
the queue. The senders must block until receivers accept the messages.
 Bounded capacity- The queue is of fixed size(n). Senders must block if the queue is full.
After sending ‘n’ bytes the sender is blocked.
 Unbounded capacity - The queue is of infinite capacity. The sender never blocks.

Race Conditions
In operating systems, processes that are working together share some common storage
(mainmemory, file etc.) that each process can read and write. When two or more processes are
readingor writing some shared data and the final result depends on who runs precisely when, are
calledrace conditions. Concurrently executing threads that share data need to synchronize
theiroperations and processing in order to avoid race condition on shared data. Only one
„customer‟thread at a time should be allowed to examine and update the shared variable.
Race conditions are also possible in Operating Systems. If the ready queue is implemented as
alinked list and if the ready queue is being manipulated during the handling of an interrupt,
theninterrupts must be disabled to prevent another interrupt before the first one completes.
Ifinterrupts are not disabled than the linked list could become corrupt.
Critical Section

How to avoid race conditions?


The key to preventing trouble involving shared storage is find some way to prohibit more thanone
process from reading and writing the shared data simultaneously. That part of the programwhere the
shared memory is accessed is called the Critical Section. To avoid race conditions andflawed
results, one must identify codes in Critical Sections in each thread. The characteristicproperties of
the code that form a Critical Section are
 Codes that reference one or more variables in a “read-update-write” fashion while any of
thosevariables is possibly being altered by another thread.
 Codes that alter one or more variables that are possibly being referenced in “read-updata-
write”fashion by another thread.
 Codes use a data structure while any part of it is possibly being altered by another thread.
 Codes alter any part of a data structure while it is possibly in use by another thread.
Here, the important point is that when one process is executing shared modifiable data in itscritical
section, no other process is to be allowed to execute in its critical section. Thus, theexecution of
critical sections by the processes is mutually exclusive in time.
Mutual Exclusion
A way of making sure that if one process is using a shared modifiable data, the other processeswill
be excluded from doing the same thing.Formally, while one process executes the shared variable,
all other processes desiring to do so atthe same time moment should be kept waiting; when that
process has finished executing theshared variable, one of the processes waiting; while that process
has finished executing theshared variable, one of the processes waiting to do so should be allowed
to proceed. In thisfashion, each process executing the shared data (variables) excludes all others
from doing sosimultaneously. This is called Mutual Exclusion.Note that mutual exclusion needs to
be enforced only when processes access shared modifiabledata - when processes are performing
operations that do not conflict with one another theyshould be allowed to proceed concurrently.
Mutual Exclusion Conditions
If we could arrange matters such that no two processes were ever in their critical
sectionssimultaneously, we could avoid race conditions. We need four conditions to hold to have a
goodsolution for the critical section problem (mutual exclusion).
 No two processes may at the same moment inside their critical sections.
 No assumptions are made about relative speeds of processes or number of CPUs.
 No process should outside its critical section should block other processes.
 No process should wait arbitrary long to enter its critical section.
Proposals for Achieving Mutual Exclusion
The mutual exclusion problem is to devise a pre-protocol (or entry protocol) and a post-protocol
(or exist protocol) to keep two or more threads from being in their critical sections at the same
time. Tanenbaum examine proposals for critical-section problem or mutual exclusion problem.
Problem
When one process is updating shared modifiable data in its critical section, no other process
should allowed to enter in its critical section.
Proposal 1 -Disabling Interrupts (Hardware Solution)
Each process disables all interrupts just after entering in its critical section and re-enable all
interrupts just before leaving critical section. With interrupts turned off the CPU could not be
switched to other process. Hence, no other process will enter its critical and mutual exclusion
achieved.
Conclusion
Disabling interrupts is sometimes a useful interrupts is sometimes a useful technique within the
kernel of an operating system, but it is not appropriate as a general mutual exclusion mechanism
for users process. The reason is that it is unwise to give user process the power to turn off
interrupts.
Proposal 2 - Lock Variable (Software Solution)
In this solution, we consider a single, shared, (lock) variable, initially 0. When a process wants to
enter in its critical section, it first test the lock. If lock is 0, the process first sets it to 1 and then
enters the critical section. If the lock is already 1, the process just waits until (lock) variable
becomes 0. Thus, a 0 means that no process in its critical section, and 1 means hold your horses -
some process is in its critical section.
Conclusion
The flaw in this proposal can be best explained by example. Suppose process A sees that the lock

is 0. Before it can set the lock to 1 another process B is scheduled, runs, and sets the lock to 1.
When the process A runs again, it will also set the lock to 1, and two processes will be in their
critical section simultaneously.
Proposal 3 - Strict Alteration
In this proposed solution, the integer variable 'turn' keeps track of whose turn is to enter the
critical section. Initially, process A inspect turn, finds it to be 0, and enters in its critical section.
Process B also finds it to be 0 and sits in a loop continually testing 'turn' to see when it becomes
1.Continuously testing a variable waiting for some value to appear is called the Busy-Waiting.
Conclusion
Taking turns is not a good idea when one of the processes is much slower than the other.
Suppose process 0 finishes its critical section quickly, so both processes are now in their
noncritical section. This situation violates above mentioned condition 3.
Using Systems calls 'sleep' and 'wakeup'
Basically, what above mentioned solution do is this: when a processes wants to enter in its
critical section , it checks to see if then entry is allowed. If it is not, the process goes into tight
loop and waits (i.e., start busy waiting) until it is allowed to enter. This approach waste CPUtime.
Now look at some interprocess communication primitives is the pair of steep-wakeup.
oSleep: It is a system call that causes the caller to block, that is, be suspended until some other
process wakes it up.
oWakeup: It is a system call that wakes up the process.
Both 'sleep' and 'wakeup' system calls have one parameter that represents a memory address used to
match up 'sleeps' and 'wakeups' .
The Bounded Buffer Producers and Consumers
The bounded buffer producers and consumers assumes that there is a fixed buffer size i.e., a
finite numbers of slots are available.
Statement
To suspend the producers when the buffer is full, to suspend the consumers when the buffer is
empty, and to make sure that only one process at a time manipulates a buffer so there are no race
conditions or lost updates.
As an example how sleep-wakeup system calls are used, consider the producer-consumer
problem also known as bounded buffer problem.
Two processes share a common, fixed-size (bounded) buffer. The producer puts information into
the buffer and the consumer takes information out.
Trouble arises when
1. The producer wants to put a new data in the buffer, but buffer is already full.
Solution: Producer goes to sleep and to be awakened when the consumer has removed data.
2. The consumer wants to remove data the buffer but buffer is already empty.
Solution: Consumer goes to sleep until the producer puts some data in buffer and wakes consumer up.
Conclusion
This approaches also leads to same race conditions we have seen in earlier approaches. Race
condition can occur due to the fact that access to 'count' is unconstrained. The essence of the
problem is that a wakeup call, sent to a process that is not sleeping, is lost.
Semaphore & Monitor
Definition of Semaphore
Being a process synchronization tool, Semaphore is an integer variable S. This integer
variable S is initialized to the number of resources present in the system. The value of
semaphore S can be modified only by two functions wait() and signal() apart from initialization.
The wait() and signal() operation modifies the value of the semaphore S indivisibly. Which
means when a process is modifying the value of the semaphore, no other process can
simultaneously modify the value of the semaphore. Further, the operating system distinguishes
the semaphore in two categories Counting semaphores and Binary semaphore.
In Counting Semaphore, the value of semaphore S is initialized to the number of resources
present in the system. Whenever a process wants to access the shared resources, it performs
wait() operation on the semaphore which decrements the value of semaphore by one. When it
releases the shared resource, it performs a signal() operation on the semaphore which
incrementsthe value of semaphore by one. When the semaphore count goes to 0, it means all
resources are occupied by the processes. If a process need to use a resource when semaphore
count is 0, it executes wait() and get blocked until a process utilizing the shared resources
releases it and the value of semaphore becomes greater than 0.
In Binary semaphore, the value of semaphore ranges between 0 and 1. It is similar to mutex
lock, but mutex is a locking mechanism whereas, the semaphore is a signalling mechanism. In
binary semaphore, if a process wants to access the resource it performs wait() operation on the
semaphore and decrements the value of semaphore from 1 to 0. When process releases the
resource, it performs a signal() operation on the semaphore and increments its value to 1. If the
value of the semaphore is 0 and a process want to access the resource it performs wait()
operation and block itself till the current process utilizing the resources releases the resource.
Definition of Monitor
To overcome the timing errors that occurs while using semaphore for process synchronization,
the researchers have introduced a high-level synchronization construct i.e. the monitor type. A
monitor type is an abstract data type that is used for process synchronization.
Being an abstract data type monitor type contains the shared data variables that are to be shared
by all the processes and some programmer-defined operations that allow processes to execute in
mutual exclusion within the monitor. A process can not directly access the shared data variable
in the monitor; the process has to access it through procedures defined in the monitor which
allow only one process to access the shared variables in a monitor at a time.
The syntax of monitor is as follow:
1. monitormonitor_name
2. {
3. //shared variable declarations
4. procedure P1 ( . . . ) {
5. }
6. procedure P2 ( . . . ) {
7. }
8. procedurePn ( . . . ) {
9. }
10. initialization code ( . . . ) {
11. }
12. }
A monitor is a construct such as only one process is active at a time within the monitor. If other
process tries to access the shared variable in monitor, it gets blocked and is lined up in the queue
to get the access to shared data when previously accessing process releases it.
Conditional variables were introduced for additional synchronization mechanism. The
conditional variable allows a process to wait inside the monitor and allows a waiting process
to resume immediately when the other process releases the resources.
The conditional variable can invoke only two operation wait() and signal(). Where if a process
P invokes a wait() operation it gets suspended in the monitor till other process Q invoke
signal() operation i.e. a signal() operation invoked by a process resumes the suspended process.
Key Differences Between Semaphore and Monitor
1. The basic difference between semaphore and monitor is that the semaphore is an integer
variable S which indicate the number of resources available in the system whereas, the monitor
is the abstract data type which allows only one process to execute in critical section at a time.
2. The value of semaphore can be modified by wait() and signal() operation only. On the other
hand, a monitor has the shared variables and the procedures only through which shared
variables can be accessed by the processes.

3. In Semaphore when a process wants to access shared resources the process performs wait()
operation and block the resources and when it release the resources it performs signal()
operation. In monitors when a process needs to access shared resources, it has to access them
through procedures in monitor.
4. Monitor type has condition variables which semaphore does not have.
Questions
1. What is operating system? Explain multiprogramming and time-sharing system.
2. Explain dual mode operating in operating system with a neat block diagram.
3. What are system calls? Briefly print out its types.
4. What is Interprocess communication? Explain direct and indirect communication
with respect to message passing system.
5. Analyze modular kernel approach with layered approach with a neat sketch.
6. List and explain the services provided by OS for the user and efficient operation of
system.
7. Illustrate with a neat sketch, the process states and process control block.
8. Discuss the methods to implement message passing IPC in detail.
9. With a neat diagram, explain the concept of virtual machines.
10. Define the following terms
• Virtual Machine
• CPU scheduler
• System call
• Context switch
11. What is Interprocess communication? Explain direct and indirect communication
with respect to message passing system.
12. Describe a mechanism for enforcing memory protection in order to prevent a
program from modifying the memory associated with other programs.
13. What are the tradeoffs inherent in handheld computers?
14. Distinguish between the client-server and peer-to-peer models of distributed
systems.
15. Some computer systems do not provide a privileged mode of operation in
hardware. Is it possible to construct a secure operating system for these computer
systems? Give arguments both that it is and that it is not possible.
16. What are the main differences between operating systems for mainframe
computers and personal computers?
17. Identify several advantages and several disadvantages of open-source operating
systems. Include the types of people who would find each aspect to be an advantage
or a disadvantage.
18. How do clustered systems differ from multiprocessor systems? What is required
for two machines belonging to a cluster to cooperate to provide a highly available
service?
19. What is the main difficulty that a programmer must overcome in writing an
operating system for a real-time environment.

You might also like