Operating System (Chapter 2)
Operating System (Chapter 2)
Submitted By
Krishna Dev Thapa Magar
Introduction of Process
➢ A process can be thought of as program in execution.
➢ A process will need certain resources such as CPU time, memory, files and I/O devices to
accomplish its task.
➢ A process is the unit of work in modern time sharing system.
➢ System consists of collection of processes, OS processes to execute OS code and user processes
to execute user code.
➢ The OS is responsible for the following activities in connection with process management:
❑ The creation and deletion of both user and system processes.
❑ Scheduling of the processes
❑ Communication and deadlock handling for the processes
Program and process (Difference/imp)
➢ A process is basically the program in execution and is more than program code which is
sometimes known as text section.
➢ When we execute the program, it becomes a process which perform all task mentioned in the
program.
➢ So, process is just a instance of executing program including current value of program
counter(PC), register and variables.
➢ When a program is loaded into the memory and it becomes a process, it can be divided into 4
sections; stack, heap, text and data. The structure of process in the memory is shown in figure
below:
➢ The process stack contains temporary data such as functions parameters, local variables and written
address.
➢ Heap is dynamically allocated memory to a process during its run time.
➢ Data section contains the global and static variable .
➢ Text section contains program code.
Hence, from the above analogy process is activity of some kind which has program, input,
output and state. A single processor may be shared among several process with some
scheduling algorithm.
Process state(imp)
➢ A process may be in one of three states. The following figure shows the state diagram showing the three
states.
➢ A process may be in
❑ Process blocks for input
❑ Scheduler picks another process
❑ Scheduler picks this process
❑ Input becomes available
➢ Running
❑ Running process is the process that is actually using the CPU at that time. Only one process can be
in running state at any one time(assuming a single processor machine).
➢ Ready
❑ A process that is ready is runnable but cannot get access to the CPU due to other process using it.
Any number of processes can be in ready state at the same time.
➢ Blocked
❑ A blocked process is unable to run until some external event has taken place. For example, it may
be waiting for data to be retrieved from a disk.
✓ The running process can either move to blocked state and ready state. Running process is moved to
blocked state when operating system discovers that process cannot continue right now i.e. when it need
to wait for an external event such as input from keyboard.
✓ Running process go to the ready state when scheduler allows other process to use the CPU.
✓ A ready process only move to a running state when scheduler decide to provide CPU to that process
✓ Blocked process can only move to a ready state when external event for which a process is waiting happens.
Implementation of process
➢ To implement the process model, the OS maintains a table called process table with one entry per process.
➢ Some author call these entries process control block (PCB).
➢ This entry contains information about process state including its program counter, memory allocation, status
of open files, its accounting and scheduling information and everything else about the process that must be
same when the process is switched from running to ready or blocked state so that it can be restarted later.
➢ Process creation:
❑ There are four principle events that cause process to be created.
❖ System initialization
❖ Execution of process creation system call by running process
❖ User request to create a new process
❖ Initiation of batch job
❑ In interactive system, user can start a program by typing a command or clicking an icon.
❑ Taking either of these action start a new process and run the selected program in it.
❑ The last situation in which process are created applies only to batch system.
❑ When the OS decide that it has the resources to run another job in a batch it create a new process and
run the next job from input queue in it.
❑ The process may create several new process via. a create system call during the course of execution.
❑ The creating process is called parent process and new process are called children of that process.
❑ Each of these new process in turn may create other process, forming the tree of process or hierarchy of
the processes.
➢ Process termination
❑ After the process has been created, it starts running and does whatever its job is.
❑ Sooner or later the new process will terminate usually due to one of the following conditions:
❖ Normal exit
❖ Error exit
❖ Fatal exit
❖ Killed by other process
❑ The first reason for termination of the process is because they have done their work.
❑ The second reason for termination is that process discovers a fatal error.
❑ Third reason for termination is an error caused by the process due to the program bug. Eg: Referencing
non-existing memory, executing an illegal instruction etc.
❑ The fourth reason, the process might terminate is that process execute system call telling the OS to kill
some other process.
Threads
➢ Threads like process are mechanism to allow the program to do more than one thing at a time.
➢ Conceptually, a thread also called light weighted process exist within the process, is a basic unit of CPU
utilization.
➢ It consists of program counter, register set and stack to keep the track of which instruction to execute next,
to hold the current working variable and execution history respectively.
➢ The thread within the same process shares code section, data section and other OS resources such as open
file etc.
Multi-threading
➢ The term multi-threading is used to describe the situation of allowing multiple threads in the same process.
➢ All the threads in the same process shares same address process, global variables, set of open files etc.
➢ If a process has multiple threads of control it can perform more than one task at a time.
➢ The following figure illustrates the difference between single threaded and multi-threaded process.
Accounting information
➢ Fig: first column list some items shared by all threads in process
➢ The second column list some item provided to each thread
Benefits of multithreading
➢ Responsiveness
➢ Resource sharing
➢ Economy
➢ Scalability
➢ Advantages:
❑ User level thread package can be implemented on an operating system that does not support
thread.
❑ Thread switching is faster because it does not require trapping to the kernel.
❑ They allow each process to have its own customized scheduling algorithm.
➢ Disadvantages:
❑ Allowing a thread to use blocking system call cause the entire process blocked.
❑ If a thread causes a page fault, the kernel not even knowing about the existence of threads,
naturally blocks the entire process until the disk I/O is complete even though other thread might be
run able.
❑ Within a process there are no clock interrupt, making it impossible to schedule the thread in the
fashion that takes turn.
❑ Lack of co-ordination between kernel and threads.
➢ Implementing threads kernel
❑ The second approach of implementing thread is to having the kernel know about thread and manage it,
no run time system is needed.
❑ The following figure illustrates the implementation of thread in kernel.
Assignments:
➢ Differentiate between process and thread.
➢ Why thread is necessary? In which circumstances user level thread is better than kernel level thread?
➢ Describe how multithreading improves performance over single threaded solution.
➢ Does window have the concept of process hierarchy? Explain.
➢ Differentiate between program and process.
Process Scheduling (Imp)
➢ The process scheduling is the activity of the process manager that handles the removal of running
process from the CPU and selection of another process on the basis of particular strategy.
➢ Process scheduling refers to which process is given control of the CPU and how long.
Scheduling queues
➢ The operating system maintains the following important process scheduling queue:
❑ Job queue
✓ This queue keeps all processes in the system.
❑ Ready queue
✓ This queue keeps a set of all processes residing in the main memory and are ready and
waiting to execute.
✓ A new process is initially put in the ready queue. It waits there until it is selected for
execution or dispatched.
❑ Device queue
✓ The list of processes waiting for particular I/O device is called a device queue.
✓ Each device has it’s own device queue.
Scheduler
➢ When a computer is multi-programmed, it frequently has multiple processes competing for the CPU
at the same time. This situation occurs whenever two or more of them are simultaneously in ready
state.
➢ If only one CPU is available the choice has to be made which process to run next. The part of
operating system that makes the choice is called the scheduler and algorithm it uses it called
scheduling algorithm.
Types of scheduler
➢ Long term scheduler
➢ Short term scheduler
➢ Medium scheduler
➢ Medium scheduler
❑ The key idea behind a medium-term scheduler is that some times it can be advantageous to
remove the process from memory and thus reduce the multiprogramming.
❑ Later the process can be reintroduce into memory, and it’s execution can be continued where
it left off. This scheme is called swapping.
❑ The process is swapped out and is later swapped in by the medium term scheduler.
Context switch
➢ Switching the CPU to another process requires performing a state save of the current process and
state restore of different process. This task is known as context switch.
➢ When context switch occurs, the kernel saves the context of the old process in it’s PCB and loads
the saved context of new process schedule to run.
➢ Context switch time is pure overhead, because the system does no useful work, while switching. It’s
speed varies from machine to machine, depending on the memory speed, the number of register
and the existence of special instruction etc.
➢ Some process spend most of their time in computing such processes are called compute bound or
CPU bound process. Compute bound process typically have long CPU burst and thus infrequent I/O
waits.
➢ An I/O bound process is one that spend more of it’s time doing I/O than it spend doing computation.
I/O bound processes have short CPU bursts and thus frequent I/O wait.
➢ In above figure(a) represents compute bound process and figure(b) represents I/O bound process.
When to schedule?
A key issue related to scheduling is when to make scheduling decisions. There are variety of situations in
which scheduling is needed. Some of these situation are:
➢ When a new process is created.
➢ When a process terminates (Running to terminate)
➢ When process blocks in I/O or for some other reason (running to wait)
➢ When an I/O interrupt occurs (waiting to ready)
➢ When quantum expire (running to ready)
Non-preemptive Vs preemptive scheduling
Non-preemptive
➢ Under non-preemptive scheduling, once a CPU has been allocated to a process, the process keeps
the CPU until it release the CP|U either by terminating or by switching to the waiting state.
➢ In other word, A non-preemption scheduling algorithm picks a process to run and then just lets it run
until it blocks or until it voluntarily release the CPU.
➢ In non-preemption scheduling short job are made to wait by longer job i.e. no priority.
➢ Useful in real time and batch system. Example: FCFS
➢ In non-preemption scheduling process cannot be interrupted during it’s execution.
Preemptive
➢ In contrast to non-preemptive scheduling, a preemptive scheduling algorithm picks a process and let
it run for maximum of some time. If it still running at the end of time interval, it is suspended and the
scheduler picks another process to run if one is available.
➢ During preemption scheduling requires having a clock interrupt. If no clock is available non-
preemptive scheduling is only option.
➢ Preemptive scheduling is useful in a system in which high priority process requires rapid attention.
➢ Useful in time sharing system for guarantying acceptable response time. Eg: Round robin scheduling
➢ In preemptive scheduling process can be interrupted during it’s execution.
Scheduling criteria
Different scheduling algorithms have different properties and the choice of particular algorithm may favor
one class of processes over another. Many criteria have been suggested for comparing scheduling
algorithms. The criteria include the following:
➢ CPU utilization
Keep the CPU as busy as possible
➢ Throughput
Number of processes that are complete per time unit
➢ Turn around time
The interval from the time of submission of process to time of completion.
➢ Waiting time
Amount of time that a process spend waiting in the ready queue. Sum of the period waiting in the
ready queue.
➢ Response time
Time from the submission of a request until the first response is produced. This response time is the
time it takes to start responding, not the time it takes to output the response.
It is desirable to maximize CPU utilization and throughput and to minimize turn around time, waiting time
and response time.
Categories of scheduling algorithm
In different environment different scheduling algorithm are needed. This situation arises because
different application areas have different goals. The scheduler optimized for one may not be optimal
for another area. Three environment worth distinguishing are:
Batch
In batch systems, there are no users impatiently waiting at terminal for a quick response. Consequently,
non-preemptive algorithms, or preemptive algorithms with long time periods for each process are
often acceptable. This approach reduces process switches and thus improves performance.
Interactive
In an environment with interactive user, preemption is essential to keep one process from hogging the CPU
and denying service to others. Preemption is needed to prevent this behaviour.
Real time
In a real time system normally preemption is not needed because the processes know that they may not
run for long period of time and usually do their work and block quickly.
Scheduling criteria
Different scheduling to design a scheduling algorithm, it is necessary to have some idea of what the good
algorithm should do. Some goals depends on the environment (batch, interactive or real time) but there
are also some that are desirable in all cases.
➢ All system
Fairness: Giving each process a fair share of CPU
Policy enforcement: seeing that stated policy is carried out.
Balance: keeping all part of the system busy
➢ Batch system
Throughput: Maximize job per hour
Turnaround time: Minimize time between submission and termination
CPU utilization: keep the CPU busy all the time
➢ Interactive systems
Response time: respond to request quickly
Proportionally: meet user expectation
➢ Real time systems
Meeting deadline: avoid losing data
Predictability: avoid quality degradation in multimedia systems
Scheduling algorithms
➢ Process scheduling deals with problem of deciding which of the processes in the ready queue is to be
allocated the CPU.
➢ Algorithm used by the scheduler to select a process from a ready queue to run next is called scheduling
algorithm.
➢ There are many different process scheduling algorithm.
➢ Problems of FCFS
❑ Average waiting time under FCFS policy is often quite long.
❑ No guarantee of good response time
❑ Not suitable for time sharing and interactive system, where each process get share of CPU at
regular interval.
❑ There is a convery effect as other process wait for the one big process to get of the CPU. This
effect results in lower CPU and device utilization.
➢ Advantage of FCFS
❑ It is easy to understand and equally easy to program.
❑ It is also fair, in the sense that allocating scarce resource to a people who are willing to stand on
line first is fair.
2. Shortest Job first (SJF) scheduling
➢ It is also non-preemption algorithm that assumes run time are known in advance.
➢ It selects a process with shortest burst time if CPU burst of two processes are same. FCFS scheduling is
used to break the tie.
➢ In this algorithm, the scheduling decision policies are based upon the CPU burst time.
➢ The SJF scheduling algorithm is provably optimal in that it gives the minimum average waiting time for
a given set of processes.
➢ Advantages
❑ Reduces the average waiting time over FCFS.
❑ Favour short jobs at the cost of long job.
➢ Problems
❑ We do not know the burst time of a process before it start.
❑ Although SJF is optimal it is difficult to implement.
❑ Not applicable in time sharing system.
3. Shortest remaining time next (SRTF)Scheduling
➢ It is the preemption version of shortest job first algorithm.
➢ With this algorithm, the scheduler always chooses the process whose remaining runtime is shortest.
➢ In this algorithm runtime of the process is known in advance.
➢ In this scheduling, when a new job arrives, the total time is compared to the current process’s
remaining time. If new job needs less time to finish than the current process, the current process is
suspended and new process is started.
➢ Advantages
❑ Low average waiting time than SJF.
❑ Useful in the time sharing system.
➢ Problems
❑ Very high overhead than SJF.
❑ Requires additional computation
❑ Favours short jobs, long job can be victims of starvation.
4. Round Robin (RR) Scheduling
➢ It is one of the oldest, simplest and most widely used scheduling algorithm.
➢ It is similar to FCFS scheduling, but preemption is added to enable the system to switch between the
processes.
➢ In this scheduling, each process is assigned a time interval called it’s quantum, during which it is
allowed to run. If the process is still running at the end of quantum, the running process is preempted
and new process is allowed to run. The preempted process is placed at the back of ready or run able
list. If the process is blocked or finished before the quantum has elapsed, the CPU switching is done.
➢ Round Robin is easy to implement. All the scheduler needs to do is maintain a list of runnable process.
When the process uses up its quantum it is put on the end of the list as in figure below:
➢ Advantages
❑ Round Robin is effective in time sharing system.
❑ Fair treatment of all processes
❑ Good response time for short processes.
❑ Low average waiting time when process length vary widely.
➢ Problems
❑ Performance depends on quantum size so care must be taken in choosing a quantum value.
❑ Processing overhead is there in handling clock interrupt.
❑ Throughput is low of time quantum is small.
Performance of RR scheduling
➢ The interesting issue with round robin is the length of the quantum.
➢ Switching from one process to another requires a certain amount of time.
➢ Setting the quantum too short causes too many processes switches and lower the CPU efficiency, but
setting it too long may cause poor response to short interactive requests.
➢ Large value of quantum may cause Round Robin behaves like FCFS policy.
➢ A quantum around 20-50 millisecond is reasonable for many general processes. The key idea for
choosing the optimal quantum size is that 80% of the CPU burst should be shorter than quantum.
5. Priority Scheduling
➢ In priority scheduling, a priority is associated with each process and the CPU is allocated to the process
with the highest priority.
➢ Equal priority process are scheduled in FCFS order.
➢ Priorities are generally indicated by some fixed range of number however there is no general
agreement on whether 0 is the highest or lowest priority. Some system use low number to represent
low priority and other use low number for higher priority.
➢ Priority scheduling can be either preemption or not preemption.
➢ In preemption priority scheduling, when a process arrives at the ready queue it’s priority is compared
with the priority of the currently running process and preempt the CPU if the priority of the newly
arrived process is higher than the priority of currently running process.
➢ A non-preemption priority scheduling does not preempt the concurrently running process until the
process does not complete or block i.e. no preemption is allowed in non-preemptive priority
scheduling.
Why priority scheduling?
➢ On a PC, there may be multiple processes some of them more important than others.
➢ For example, a process sending electronic mail in background should be assigned a lower priority
than a process displaying a video film on the screen in real time.
➢ So at that time there is a need of priority scheduling to execute the highest priority process first.
Problem in priority scheduling
➢ A major problem with priority scheduling is that some process may never run.
➢ There may always be higher priority job that get assigned the CPU. This is known as indefinite
blocking or starvation.
➢ In heavily loaded computer system, a steady steam of higher priority processes can prevent low
priority process from ever getting the CPU.
➢ On solution to the problem of indefinite blocking or starvation is, scheduler may decrease the
priority of currently running process at each clock tick.
➢ If the action cause its priority to drop below that of the next highest process, a process switch
occurs.
➢ Alternatively, each process may be assigned a maximum time quantum that is allowed to run.
➢ When this quantum is used up, the next highest priority process is given a chance to run.
Assigning a priority
➢ Priorities can be assigned dynamically (internally) or statically (externally) to the process.
➢ Priorities can be assigned dynamically by the system to achieve certain system goal.
➢ For example some processes are highly I/O bound and spend must of their time waiting for I/O to complete.
➢ Whenever such a process want CPU, it should be given the CPU immediately, to let it start it’s next I/O request, which
can then proceed in parallel with another process actually computing.
➢ Internally defined priorities use some measurable quantities to compute the priority of the process.
➢ For example, time limit, memory requirement, number of open files etc. have been used in computing priorities.
➢ External or statically priorities are set by criteria outside the operating system, such as importance of the process, the
type and amount of fund being paid for computer use and often political factor.
➢ For example on the military computer, processes started by generals might begin at priority 100, process started by
cornels at 90 and so on.
Multilevel queue scheduling
➢ A multilevel queue scheduling algorithm partitions the ready queue into several separate queues to
later for different process type.
➢ The process are permanently assigned to one queue, generally based on some property of the process.
➢ Each queue has its own scheduling algorithm. For example separate queue might be used for
interactive and batch processes and interactive process are scheduled by RR while batch processes are
scheduled by FCFS algorithms.
➢ The scheduler has to decide which queue to run. Either higher priority queue can be processed until
they are empty before the lower priority queue are executed or each queue can be given certain
amount CPU, which it can then schedule among its various processes.
Example
➢ No process is batch queue could run unless the queue for system process, interactive process were all
empty.
➢ If an interactive process entered the ready queue while batch process was running, the batch process
will be preempted.
Multilevel feedback queue scheduling (MFQ)
➢ The multilevel feed back queue scheduling implements multilevel queues having the different priority
to each level and allows the process to move between the queues.
➢ If a process uses to much CPU time, it will be moved to lower priority queue.
➢ In addition, a process that waits too long in lower priority queue may be moved to higher priority
queue, to prevents starvation.
Example
➢ Consider a multilevel feedback queue scheduler with three queues numbered 0 to 2 with quantum size
8,12 and 32.
➢ The scheduler first executes all processes in queue 0. Only when queue 0 is empty it will execute
process in queue 1 and process in queue 2 executes only if queue 0 and queue 1 are empty. A process in
queue 1 will in turn be preempted by process arriving for queue 0.
➢ A process entering the ready queue is put in queue 0 and executes for 8 millisecond. If it does not finish
with in this time it moves to the tail of queue 1.
➢ If queue 0 is empty, the process at the head of queue 1 start execute with 16msec quantum. If it does
not complete it is preempted and put into queue 2.
➢ Process in queue 2 are run on an FCFS order but are run only when queues 0 and 1 are empty.
Parameter for defining Multilevel feedback queue scheduler
In general, a multilevel feedback queue scheduler is defined by the following parameters:
➢ Number of queue.
➢ The scheduling algorithm for each queue
➢ The method used to determine when to upgrade a process to a higher priority queue
➢ The method used to determine when to demote a process to lower priority queue.
➢ The method used to determine which queue process will enter when that process needs service.
Example
Consider the following set of processes with the length of CPU burst time given in millisecond
➢ Process executing concurrently in the operating system may be either independent processes or
cooperating processes.
➢ 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.
➢ A process is cooperating if it can affect or be affected by the other process executing in the system
clearly any process that shares data with other processes is cooperating processes.
➢ Computation speedup
If we want a particular task to run faster, we must break it into subtask, each of which will be
executing in parallel with the other.
➢ Modularity
We may want to construct the system in modular fashion, dividing the system functions into separate
processes.
➢ Convenience
Even an individual user may work on many task at the same time. For instance, a user may be editing,
printing and compiling in parallel.
Interprocess communication
➢ Interprocess communication(IPC) is the mechanism that allow a process to exchange data and
information. Cooperating process requires IPC to complete their task.
➢ There are two fundamental model of interprocess communication:
❑ Shared memory
❑ Message passing
❑ Shared memory
➢ In the shared memory model of IPC, a region of memory that is shared by cooperating process is
established.
➢ Process can exchange information by reading and writing data to the shared region in shared
memory model.
➢ Shared memory allows maximum speed and convenience of communication.
➢ Shared memory is faster than message passing, as message passing system are typically
implemented using system calls and thus requires the more time consuming task of kernel
intervention.
➢ The following figure depict shared memory interprocess communication model:
❑ Message passing
➢ In message passing model, communication takes place by means of message exchange between
the cooperating process i.e. message passing provides a mechanism to allow processes to
communicate without sharing the same address space and is particularly useful in distributed
environment, where the communicating processes may reside on different computers connected
by a network.
➢ Message passing facility provides at least two operations:
❖ Send (message)
❖ Receive (message)
➢ If a process P and Q want to communicate, they need to establish a communication link between
them and exchange message via send and receive.
Send (P, message) = Send message to process P
Receive (Q, message) = receive message from Q
➢ Message passing system are typically implemented using system call and thus requires a more
time consuming task of kernel intervention. The message passing communication model is
depicted by following figure:
Interprocess communication issues
Process frequently needs to communicate with other processes to complete their task. The
mechanism of passing information from one process to another process is called interprocess communication.
(Imp) There are three main issues related to IPC. They are:
➢ How one process can pass information to another process.
➢ How to make sure two or more processes do not get in each others way when engaging in shared
resources.
➢ How to maintain the proper sequence when dependencies are present.
➢ Situation where two or more processes are reading or waiting some shared data and the final result
depends on who runs precisely when, are called race condition.
➢ In other word, race condition is a situation where two or more processes read or writes some shared
data concurrently and final result depends on particular order in which the access takes place.
➢ To see how interprocess communication work in practice and how race condition arises.
➢ Lets us consider simple and common example: a print spooler
❑ When a process want to print a file, it enters the file name in spooler directory.
❑ Another process the printer daemon periodically checks to see if there are any files to be printed
and if there are, it prints them and removes their name from the directory.
❑ Consider the situation given in fig. below where ‘in’ point to the next free slot in the directory and
‘out’ point to the next file to be printed are two shared variables.
❑ Let process A and process B decide they want to queue a file for printing.
❑ Let process A reads ‘in’ i.e. 7 but before inserting the name of documents to be printed in index 7
clock interrupt occurs and CPU decide to schedule process B.
❑ Now, process B also reads ‘in’ and get 7. At this instance, both process think that next available
slot is 7.
❑ Process B now continues to run it stores the name of its file in slot 7 and update ‘in’ to be 8.
❑ After sometimes process A runs again, starting from the place it left off and it finds in=7 and write
its file name in slot 7, erasing the name that process B just put there and it update the value of ‘in’
to be 8.
❑ The printer daemon will not notice anything wrong but process B will never receive any output.
Situation like this are called race condition.
Possibility of race
➢ Many concurrent processes read the same data.
➢ One process reading and another process writing same data.
➢ Two or more processes writing same data.
Avoiding the Race condition (Solution of race condition)
➢ One way to prevent from race condition is to find some way to prohibit more than one process from
reading and writing the shared data at the same time i.e. we need mutual exclusion.
➢ (imp) Mutual exclusion is some way of making sure that if one process is using a shared variable or
shared file the other process will be excluded from doing the same thing.
Critical section / Critical region (Why execution of critical section is mutually exclusive?)
➢ Sometimes a process has to access shared memory or files that can lead to a race condition.
➢ The part of the process that access to shared variable, shared memory or shared file is called critical
region or critical section.
➢ One way to avoid race condition is not to allow two processes in their critical section at the same time
i.e. when one process is executing its critical section no other process is allowed to execute its critical
section. So, it avoid the race condition execution of critical section by the process is mutually exclusive.
Critical section problem (CSP)
➢ Critical section problem is to design a protocol so that no two processes are executing in their critical
section at the same time.
➢ In short, problem of avoiding a race condition is called critical section problem.
➢ Each process must request a permission to enter its critical section. The section of code implementing
this request is entry section and critical section is followed by exit section.
➢ The general structure of process is
while (true)
{
entry section
critical section
exit section
Remainder section
}
Good solution of race condition
➢ If we could arrange the process such that no two process were in their critical section at the same time,
we could avoid the race.
➢ Although this requirement avoid race condition, in fact to provide the good solution to the problem of
race condition we need 4 conditions to hold.
1. No two processes may be simultaneously inside their critical section (i.e. Mutual exclusion)
2. No assumption may be made about speed or number of CPU’s.
3. No process running outside its critical section may block other processes to enter its critical
section.
4. No process should have to wait forever to enter its critical section.
➢ In abstract sense, the behaviour we want is shown in figure below:
2. Lock variable
✓ Lock variable is a software solution to achieve mutual exclusion.
✓ It is the binary shared variable whose value is either 0 or 1. Initially, 0 is assigned to a lock
variable.
✓ When process want to enter its critical region, it first test the lock variable.
✓ If the lock is zero, the process set it to 1 and enter the critical region.
✓ If the lock is already 1, the process just waits until it become 0. Thus, a zero means no
process is in its critical region and 1 means that some process is in its critical region.
While(lock !=0); //wait//
{
lock = 1;
critical section ();
lock = 0;
remainder section ();
}
✓ Problem
It again suffer from race condition.
❖ Suppose that one process read the lock and sees that it is zero but before it can set the
lock to 1, another process is schedule, runs and set the lock to 1.
❖ When the first process runs again it can also set the lock to 1 and two processes will be in
their critical section at the same time (i.e. lock variable violates mutual exclusion
condition)
3. Strict Alternation
✓ The third approach to critical section problem is strict alternation.
✓ In this approach, process share the common integer variable turn. If turn=0 then process P0 is
allowed to execute in its critical section, if turn = 1, process P1 is allowed to execute in its critical
section.
Process 0
while (true)
{
while (turn !=0); //wait//
critical section ();
turn = 1;
remainder section ();
}
Process 0
while (true)
{
while (turn !=0); //wait//
critical section ();
turn = 1;
remainder section ();
}
Process 1
while (true)
{
while (turn !=1); //wait//
critical section ();
turn = 0;
remainder section ();
}
✓ Assume the variable turn is initially set to zero and process P0 inspect turn and find that turn is 0
and it is allowed to enter its critical section.
✓ If process P1 tries to run, it will find that turn is zero and will have to wait until turn becomes 1.
✓ When process P0 exit its critical section it sets turn to 1 which allow the process P1 to enter its
critical region.
✓ Again, if process P0 tries to enter its critical section it will be blocked as turn is becomes to 0.
Hence, this approach preserve mutual exclusion condition.
✓ Problems:
❖ This approach has the problem of strict alternation.
❖ Consider the following sequence of event.
❖ Process 0 (P0) runs, enters its critical section and exit by setting turn to 1. Process P0 is
now in its non-critical section or remainder section. Assume this remainder section takes
long time.
❖ Process P1 which is much faster process now runs and once it has left its critical section
turn is set to 0.
❖ Process P1 executes its non-critical section very quickly and return on the top of the
procedure.
❖ The situation is now that process P0 is in its non-critical section and process P1 is waiting
for turn to be set to 1.
❖ From this observation we can see that strict alternation violates the condition “No process
running outside its critical section may block other processes.”
4. Peterson’s solution
✓ It is the solution to the mutual exclusion problem that does not require strict alternation but still
uses the idea of lock variable with the concept of taking turn.
✓ The Peterson’s solution consists of two procedure shown below:
# define FALSE 0
# define TRUE 1
# define N 2 /*number of process*/
int turn; /*whose turn is it */
int interested[n]; /*all values initially o(FALSE)*/
void enter_region (int process) /*process is 0 or 1*/
{
leave_region;
MOV lock, #0 /*store 0 in lock*/
RET /*return to caller i.e. leave critical region*/
➢ Full is initially zero, empty is initially equal to number of slot in the buffer i.e. N and Mutex is initially 1. If
each process does the down Mutex before entering its critical region and up just after leaving it,
mutual exclusion is guaranteed.
➢ This solution ensure that producer stops running when buffer is full and consumer stops running when
buffer is empty.
Use of semaphore
➢ To deal with n process critical section problem.
➢ To solve the synchronization problem
✓ For example: Two concurrently running processes P1 with statement S1 and P2 with statement
S2. Suppose we require that S2 must be executed after S1 has completed. This problem can be
implemented by using common semaphore synchronization initialized to 0 as
semaphore synchronize=0
P1:s1
up(synchronize)
P2:down(synchronize)
S2
Criticality using semaphore
➢ We should be too much careful about the order of down operation when we use the semaphore.
➢ For Eg: suppose that two downs in the producer’s code were reversed in order then mutex was
decremented before empty.
➢ If the buffer were completely full, the producer would block with mutex set to 0.
➢ Consequently, the next time the consumer tried to access the buffers, it would do a down on mutex
which is 0 and blocked too.
➢ Hence, both processes would block forever and no more work would be done and deadlock arises.
➢ So, using the semaphore by the programmer is critical.
Monitor
➢ Semaphore is very useful for solving critical section problem only if programmer use them properly. If
anyone process fails to proper use of semaphore cause the subtle error i.e. deadlock will be arises.
➢ To make it easier to write correct program, Brinch and Hoare purposed a higher level synchronization
primitive called a monitor.
➢ A monitor is a collection of procedure, variable and data structure that are grouped together in a
special kind of module or package.
➢ Process may call the procedure in a monitor whenever they want to but they cannot directly access
the monitors internal data structures.
➢ Monitor have an important property that only one process can be active in a monitor at any instant.
➢ Typically, when a process call a monitor procedure, the few instruction of producer will check to see if
any other process is currently active within the monitor. If so, the calling process will be suspended
until the other process has left the monitor. If no, other process is using the monitor, the calling
process may enter.
➢ The skeleton of producer consumer problem with monitor is given below:
Monitor Producer consumer
condition full, empty;
integer count;
Procedure insert(item : integer)
begin
if count = N then wait (full)
insert_item (item)
count = count+1
if count = 1 then signal (empty)
end:
function remove (item : integer)
begin
if count = 0 then wait (empty);
remove item (item)
count=count-1
if count = N-1 then signal (full)
end:
end monitor:
Producer producer
begin
while true do
begin
item=produce item;
[Link](item)
end
end;
Producer consumer
begin
while true do
begin
item = Producer [Link]
consumer item(item);
end
end;
Disadvantage
➢ Lack of implementation in most commonly used programming language like C and C++.
➢ Not applicable for distributed system consisting of multiple CPU’s with its own private memory
connected by LAN.
Message Passing
➢ Message passing system have many challenging problems and design issues that do not arise with
semaphore or monitors. Some issues are:
❑ Message can be lost by the network(solution for this is receiver can send special
acknowledgement message as soon as message has been received)
❑ Message is received correctly but acknowledgement back to the sender is lost
❑ Authenticate problem
❑ Performance is also issue when sender and receiver are on the same machine.
Producer consumer problem using message passing
➢ The producer consumer problem can be solved with message passing and no shared memory. The
solution using message passing is presented below:
#define N 10
void producer(void)
{
int item;
message m; /*message buffer*/
while (TRUE)
{
item=Produce_item();
receive(consumer, &m); /*wait for an empty to arrive*/
build_message(&m, item)
send(consumer, &m)
}
}
void consumer(void)
{
int item, I;
for ( I = 0; I < N; I++)
{
send(producer, &m) /*send N empty message*/
}
item=extract_item(&m)
send(producer, &m);
consume_item(item)
}
Classical IPC problem (Imp)
➢ This problem was posed by Dijkstra. The problem can be stated quite simply as follows:
❑ Five philosopher are sited around a circular table. Each philosopher has a plate of spaghetti and
philosopher needs two forks to eat it.
❑ Between each pair of plate there is one fork as shown in figure below:
Solution 2
❑ After taking the left fork the program checks for right fork, if it is not available the philosopher
puts down the left one, wait for sometime and repeat the whole process.
❑ Problem: With a little bit of bad luck, all the philosopher could start the process of picking left
fork simultaneously, seeing that their right fork is unavailable, putting down their left fork,
waiting sometime and picking up their left fork again simultaneously and so on forever. In this
case starvation occurs.(i.e. all program continue to run and definitely but fails to make any
progress)
Solution 3
❑ Using the binary semaphore Mutex. Before starting to acquire a fork philosopher would down
on Mutex, after placing the fork philosopher would do up Mutex.
❑ Problem: This solution is adequate (i.e. no deadlock and starvation is there) but not perfect i.e.
only one philosopher can be eating at any instant.
Solution 4 (write in exam)
❑ The perfect solution using semaphore for each philosopher is presented below:
#define N 5
#define LEFT(i)
#define RIGHT(i+1)%N
#define THINKING 0
#define HUNGRY 1
#define EATING 2
typedef semaphore;
int state[N]
semaphore Mutex=1
semaphore S[N]
void philosopher(int i)
{
while (TRUE)
{
think();
takefork(i);
eat();
put_fork(i);
}
}
Void take_forks(int i)
{
down (&Mutex)
state[i]=HUNGRY
test[i];
up (&Mutex)
down (&S[i])
}
Void put_fork(i)
{
down (&Mutex);
state[i]=THINKING;
test(LEFT)
test(RIGHT)
up(Mutex)
}
Void test(i)
{
if(state[i]==HUNGRY&&state[LEFT]!=EATING&&state[RIGHT]!=EATING)
{
state[i]=EATING;
up(&s[i])
}
}
❑ Here we use an array state to keep track of whether the philosopher is eating, thinking or
hungry. A philosopher may move in eating state if neither neighbor is eating.
❑ This solution uses an array of semaphore one per philosopher so hungry philosopher can block
if the needed forks are busy.
Sleeping Barber Problem
❑ Problem: The analogy is based on a hypothetical barber shop with one barber, one barber chair, and
n chairs for waiting for customers if there are any to sit on the chair.
➢ If there is no customers, then the barber sleeps in his own chair.
➢ When a customer arrives, he has to wake up the barber.
➢ If there are many customers and the barber is cutting a customer’s hair, then the remaining
customers either wait if there are empty chairs in the waiting room or they leave if no chairs
are empty.
Solution:
❑ It include three semaphores.
➢ First for customer, which counts the number of customers
presents in the waiting room (customer in the barber chair
is not included because he is not waiting).
➢ Second for barber, 0 or 1 is used to tell whether barber is
idle or working.
➢ Third mutex, is used to provide the mutual exclusion which
is required for the process to execute.
Algorithm
Barber {
while(true) {
down(Customers); /* waits for a customer (sleeps). */
down(Seats); /* mutex to protect the number of available seats.*/
FreeSeats++; /* a chair gets free.*/
up(Barber); /* bring customer for haircut.*/
up(Seats); /* release the mutex on the chair.*/
if(FreeSeats > 0) {
FreeSeats- -; /* sitting down.*/
up(Customers); /* notify the barber. */
up(Seats); /* release the lock */
down(Barber); /* wait in the waiting room if barber is busy. */
// customer is having hair cut
} else {
up(Seats); /* release the lock */
// customer leaves
}
}
}