OSM2
OSM2
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
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)
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
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
P1 P2 P3
0 24 27 30
P2 P3 P1
0 3 6 30
P4 P1 P3 P2
0 3 9 16 24
P1 P2 P4 P1 P3
0 1 5 10 17 26
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.
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.
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.
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".
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
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.