Professional Documents
Culture Documents
Process Description
Modern computers work in a multitasking environment in which they execute multiple programs
simultaneously. These programs cooperate with each other and share the same resource, such as
memory and cpu. An operating system manages all processes to facilitate proper utilization of
these resources.
Important concept: decomposition. Given hard problem, chop it up into several simpler problems
that can be solved separately. A big size program should be broken down into processes to be
executed.
What is a process?
Process States:
Forking: want to make a copy of existing process (e.g., UNIX uses fork ( ) function).
Make sure process to be copied is not running and has all state saved.
Make a copy of code, data, and stack.
Copy PCB of source into new process.
Make process known to dispatcher/scheduler.
The PCB contains important information about the specific process including:
The current state of the process i.e., whether it is ready, running, waiting, or whatever.
Unique identification of the process in order to track "which is which" information.
A pointer to parent process.
Similarly, a pointer to child process (if it exists).
The priority of process (a part of CPU scheduling information).
Pointers to locate memory of processes.
A register save area.
The PCB is a certain store that allows the operating systems to locate key information about a
process. Thus, the PCB is the data structure that defines a process to the operating systems.
Process table: collection of all process control blocks for all processes.
How can several processes share one CPU? OS must make sure that processes do not interfere
with each other. This means
Making sure each gets a chance to run inside the cpu (fair scheduling).
Making sure they do not modify each other's state (protection).
Context Switching
Most modern computer systems allow more than one process to be executed simultaneously.
This is called Multitasking systems.
Context switching means switching the cpu to different processes. It requires saving of the state
of the current process into the PCB and load the saved PCB of the previous or new process.
Dispatcher (also called Short Term Scheduler): inner-most portion of the OS that runs processes
without interference: Scheduler supports and facilitates the following activities
OS needs some agent to facilitate the state saving and restoring code.
All machines provide some special hardware support for saving and restoring state:
Process Scheduling:
When more than one process is running, the operating system must decide which one should first
run. The part of the operating system concerned with this decision is called the scheduler, and
algorithm it uses is called the scheduling algorithm.
In multitasking and uniprocessor system scheduling is needed because more than one process is
in ready and waiting state. A certain scheduling algorithm is used to get all the processes to run
correctly. The processes should be in such a manner that no processes must be made to wait for a
long time.
General Goals
Fairness:
Fairness is important under all circumstances. A scheduler makes sure that each process gets its
fair share of the CPU and no process can suffer indefinite postponement/delay. Not giving
equivalent or equal time is not fair.
Efficiency:
Scheduler should keep the system (or in particular CPU) busy 100% of the time when
possible. If the CPU and all the Input/Output devices can be kept running all the time, more work
gets done per second than if some components are idle.
Response Time:
A scheduler should minimize the response time for interactive user.
Turnaround:
A scheduler should minimize the time batch users must wait for an output of executing
process. It is Cpu time + Wait time
Chapter: 2 Process Management 2010 E.C Page 4
Operating Systems DMU
Throughput:
A scheduler should maximize the number of jobs processed per unit time.
The Scheduling algorithms can be divided into two categories with respect to how they deal with
interrupts.
A scheduling discipline is non preemptive if once a process has been given the CPU, the CPU
cannot be taken away from that process until the assigned process completes its execution.
1. In non preemptive system, short jobs are made to wait by longer jobs..
2. In non preemptive system, response times are more predictable, maximum, because
incoming high priority jobs cannot displace waiting jobs.
3. In non preemptive scheduling, a scheduler executes jobs in the following two situations.
a. When a process switches from running state to the waiting state.
b. When a process terminates.
Preemptive Scheduling:
The strategy of allowing processes that are logically running to be temporarily suspended is
called Preemptive Scheduling and it is contrast to the "run to completion" method.
Scheduling Algorithms
CPU Scheduling deals with the problem of deciding which of the processes in the ready queue is
to be allocated the CPU.
First-In-First-Out (FIFO)
Run-to-Completion
Run-Until-Done
FCFS scheme is not useful in scheduling interactive users because it cannot guarantee good
response time. The code for FCFS scheduling is simple to write and understand. One of the
major drawbacks of this scheme is that the average time is often quite long.
Example:
Shortest-Job-First (SJF) is a non-preemptive discipline in which waiting job (or process) with the
smallest estimated run-time-to-completion is run first. In other words, when CPU is available, it
is assigned to the process that has smallest next CPU burst.
The SJF algorithm favors for short jobs (or processes) at the expense of longer ones. Since the
SJF scheduling algorithm gives the minimum average time for a given set of processes, it is
probably optimal.
Example:
If a new process arrives with a shorter next cpu time than what is left of the currently executing
process, then the new process get the cpu.
The SRT is the preemtive counterpart of SJF and useful in time-sharing environment.
In SRT scheduling, the process with the smallest estimated left run-time to completion is
run next, including new arrivals.
In SJF scheme, once a job begins executing, it run to completion.
In SRT scheme, a running process may be preempted/ interrupted by a new arrival
process with shortest estimated remaining/left run-time.
The algorithm SRT has higher overhead than its counterpart SJF.
The SRT must keep track of the elapsed time of the running process and must handle
occasional preemptions.
In this scheme, arrival of small processes will run almost immediately. However, longer
jobs have even longer mean waiting time.
Example:
One of the simplest, fairest and most widely used algorithms is round robin (RR).
In the round robin scheduling, processes are dispatched in a FIFO manner but are given a limited
amount of CPU time called a time-slice or a quantum.
If a process does not complete before its CPU-time expires, the CPU is preempted/interrupted
and given to the next process waiting in a queue. The preempted process is then placed at the
back of the ready list.
Round Robin Scheduling is preemptive (at the end of time-slice) therefore it is effective in time-
sharing environments in which the system needs to guarantee reasonable response times for
interactive users.
The only interesting issue with round robin scheme is the length of the quantum. Setting the
quantum too short causes too many context switches and lower the CPU efficiency. On the other
hand, setting the quantum too long may cause poor response time and approximates FCFS. So
the slice time should not be too short and too long.
Example:
Priority Scheduling
The basic idea is straightforward: each process is assigned a priority, and priority is allowed to
run. Equal-Priority processes are scheduled in FCFS order or in RR. The shortest-Job-First (SJF)
algorithm is a special case of general priority scheduling algorithm.
SJF algorithm is also a priority algorithm where the priority is the inverse of the (predicted) next
CPU burst. That is, the longer the CPU burst, the lower the priority and vice versa
Example:
In the case of uniprocessor systems, processes interact on the bases of the degree to which they
are aware of each other’s existence.
1. Processes unaware of each other: The processes are not working together; the OS
needs to be concerned about competition for resources (cpu or memory).
2. Processes directly aware each other: Processes that are designed to work jointly on
the same program or application. These processes exhibit cooperation but
competition for shared variables (global variables).
One of the benefits of multitasking is that several processes can be made to cooperate in order to
achieve their ends. To do this, they must do one of the following.
Waiting: Some processes wait for other processes to give a signal before continuing. All
these are issues of synchronization/communication.
Since processes frequently need to communicate with other processes therefore, there is a need
for a well-structured communication. And the interaction or communication between two or
more processes is termed as Interprocess communication (IPC).Information sharing between
processes or IPC achieved through shared variable ,by passing through shared file and by using
OS services provided for this purpose like OS signals(system calls like interrupts).
When processes co-operate each other’s there is a problem of how to synchronize cooperating
processes. For example, suppose two processes modify the same file. If both processes tried to
write simultaneously the result would be a senseless mixture. We must have a way of
synchronizing processes, so that even concurrent processes must stand in line to access shared
data serially without any interference or competition or racing.
Race Conditions
In operating systems, processes that are working together share some common storage (main
memory, cpu, file etc.) that each process can read and write. When two or more processes are
reading or writing some shared data and the final result depends on who runs precisely when,
are called race conditions. Concurrently executing processes that share data need to synchronize
their operations and processing in order to avoid race (competition) condition on shared data or
shared resource. Only one process at a time should be allowed to examine and update the shared
variable.
Chapter: 2 Process Management 2010 E.C Page 9
Operating Systems DMU
Critical Section
The key to preventing trouble involving shared storage is find some way to prohibit more than
one process from reading and writing the shared data simultaneously. That part of the program
where the shared memory is accessed is called the Critical Section. To avoid race conditions and
flawed/unsound results, one must identify codes in Critical Sections in each process.
Here, the important point is that when one process is executing shared modifiable data in its
critical section, no other process is to be allowed to execute in its critical section. Thus, the
execution 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 processes
will be excluded from doing the same thing.
Formally, while one process executes the shared variable, all other processes desiring to do so at
the same time should be kept waiting; when that process has finished executing the shared
variable, one of the processes waiting to do so should be allowed to proceed. In this fashion, each
process executing the shared data (variables) excludes all others from doing so simultaneously.
This is called Mutual Exclusion; this approach can avoid race condition.
Note that mutual exclusion needs to be enforced only when processes access shared modifiable
data - when processes are performing operations that do not conflict with one another they
should be allowed to proceed concurrently.
In order to achieving mutual exclusion one should develop a pre-protocol (or entry protocol) and
a post-protocol (or exist protocol) to keep two or more processes from being in their critical
sections at the same time. When one process is updating shared modifiable data in its critical
section, no other process should allow entering in its critical section.
Disabling Interrupts:
Each process disables all interrupts just after entering in its critical section and re-enables 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.
// Process A // Process B
disable_interrupt(); disable_interrupt();
Cal_interest() withdraw()
{ {
} }
enable_interrupt(); enable_interrupt();
Lock Variable
In this solution, we consider a single, shared, (lock) variable, initially 0. When a process wants to
enter in its critical section, it first tests 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, lock= 0 means that no process in its critical section, and lock= 1 means some
process uses the critical section.
{ If(lock==0)
{ lock=1;return true;
Basically, what above mentioned solution do is: when a process wants to enter in its critical
section, it checks to see if the 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 CPU-time.
Sleep ( )
o It is a system call that causes the caller to block, that is, be suspended/sleep until some
other process wakes it up.
Wakeup ( )
o It is a system call that wakes up the process.
The Bounded Buffer Producers and Consumers example: an example how sleep-wakeup system
calls are used.
The bounded buffer producers and consumers assume that there is a fixed buffer size i.e., a finite
numbers of slots is available.
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.
Two processes share a common, fixed-size (bounded) buffer. The producer puts information into
the buffer and the consumer takes information out.
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.
Semaphores
A semaphore is a protected variable whose value can be accessed and altered only by the
operations down ( ) and up ( ) and initialization operation called 'Semaphoiinitislize'.
The down (or wait or sleep ) operation on semaphores S, written as down(S) or wait (S) operates
as follows:
The up (or signal or wakeup ) operation on semaphore S, written as up(S) or signal (S), operates
as follows:
Operations down and up are done as single, indivisible, atomic action. It is guaranteed that once
a semaphore operation has stared, no other process can access the semaphore until operation has
completed. Mutual exclusion on the semaphore, S, is enforced within down(S) and up(S).
If several processes attempt a down(S) simultaneously, only one process will be allowed to
proceed. The other processes will be kept waiting, but the implementation of down and up
guarantees that processes will not suffer indefinite postponement/delay.
Monitor
Some languages have special class-environments for dealing with mutual exclusion. Such an
environment is called a monitor.
A procedure or function defined under the umbrella of a monitor can only access those
shared memory locations declared within that monitor and vice-versa.