Operating System: Institute of Technology and Management
Operating System: Institute of Technology and Management
OPERATING SYSTEM
22MCA102
Course Outcomes:
CO1: Explore operating system concepts
CO2: Apply the suitable OS algorithm for any given use case
CO3: Analyse the file concepts, memory management and disk scheduling
techniques
CO4: Explore Linux features and commands
CO5: Build shell scripts using Linux commands and language constructs
Mr. Dwarakanath G V
Assistant Professor, Dept. of MCA, BMSIT&M
[email protected]
13/05/2024
Mobile: 9916155597 1
BMS INSTITUTE OF TECHNOLOGY AND MANAGEMENT
Module 2: Processes
BMS INSTITUTE OF TECHNOLOGY AND MANAGEMENT
Chapter : Processes
Process Concept
Process Scheduling
Scheduling Criteria
Scheduling Algorithms
The Critical-section Problem
Semaphores
Classic Problems of Synchronization
Synchronization Examples
BMS INSTITUTE OF TECHNOLOGY AND MANAGEMENT
Objectives
To introduce the notion of a process -- a program in execution,
which forms the basis of all computation
To describe the various features of processes, including scheduling,
creation and termination, and communication
To introduce the critical-section problem
To present both software and hardware solutions of the critical-
section problem.
BMS INSTITUTE OF TECHNOLOGY AND MANAGEMENT
Process Concept
An operating system executes a variety of programs:
Batch system – jobs
Time-shared systems – user programs or tasks
Textbook uses the terms job and process almost interchangeably
Process – a program in execution; process execution must progress in
sequential fashion
Multiple parts
The program code, also called text section
Current activity including program counter, processor registers
Stack containing temporary data
Function parameters, return addresses, local variables
Data section containing global variables
Heap containing memory dynamically allocated during run time
BMS INSTITUTE OF TECHNOLOGY AND MANAGEMENT
Basic Concepts
Scheduling Criteria
Scheduling Algorithms
BMS INSTITUTE OF TECHNOLOGY AND MANAGEMENT
Objectives
To introduce CPU scheduling, which is the basis for multiprogrammed
operating systems
To describe various CPU-scheduling algorithms
To discuss evaluation criteria for selecting a CPU-scheduling algorithm
for a particular system
To examine the scheduling algorithms of several operating systems
BMS INSTITUTE OF TECHNOLOGY AND MANAGEMENT
Basic Concepts
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
BMS INSTITUTE OF TECHNOLOGY AND MANAGEMENT
Histogram of CPU-burst Times
BMS INSTITUTE OF TECHNOLOGY AND MANAGEMENT
CPU Scheduler
Short-term scheduler selects from among the processes in ready
queue, and allocates the CPU to one of them
Queue may be ordered in various ways
CPU scheduling decisions may take place when a process:
1. Switches from running to waiting state
2. Switches from running to ready state
3. Switches from waiting to ready
4. Terminates
Scheduling under 1 and 4 is nonpreemptive
All other scheduling is preemptive
Consider access to shared data
Consider preemption while in kernel mode
Consider interrupts occurring during crucial OS activities
BMS INSTITUTE OF TECHNOLOGY AND MANAGEMENT
Dispatcher
Dispatcher module gives control of the CPU to the process
selected by the short-term scheduler; this involves:
switching context
switching to user mode
jumping to the proper location in the user program to restart
that program
Dispatch latency – time it takes for the dispatcher to stop one
process and start another running
BMS INSTITUTE OF TECHNOLOGY AND MANAGEMENT
Scheduling Criteria
CPU utilization – keep the CPU as busy as possible
Throughput – # of processes that complete their execution per time
unit
Turnaround time – amount of time to execute a particular process
Waiting time – amount of time a process has been waiting in the ready
queue
Response time – amount of time it takes from when a request was
submitted until the first response is produced, not output (for time-
sharing environment)
BMS INSTITUTE OF TECHNOLOGY AND MANAGEMENT
P1 P2 P3
0 24 27 30
P2 P3 P1
0 3 6 30
P4 P1 P3 P2
0 3 9 16 24
Can be done by using the length of previous CPU bursts, using exponential
averaging
The CPU is allocated to the process with the highest priority (smallest
integer highest priority)
Preemptive
Nonpreemptive
P1 P2 P3 P1 P1 P1 P1 P1
0 4 7 10 14 18 22 26 30
Scheduling
A new job enters queue Q0 which is served
FCFS
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
BMS INSTITUTE OF TECHNOLOGY AND MANAGEMENT
Chapter : Process
Synchronization
BMS INSTITUTE OF TECHNOLOGY AND MANAGEMENT
Chapter : Process Synchronization
Background
The Critical-Section Problem
Peterson’s Solution
Semaphores
Classic Problems of Synchronization
Monitors
Synchronization Examples
Alternative Approaches
BMS INSTITUTE OF TECHNOLOGY AND MANAGEMENT
Objectives
To present the concept of process synchronization.
To introduce the critical-section problem, whose solutions can be
used to ensure the consistency of shared data
To present both software and hardware solutions of the critical-
section problem
To examine several classical process-synchronization problems
To explore several tools that are used to solve process
synchronization problems
BMS INSTITUTE OF TECHNOLOGY AND MANAGEMENT
Background
Processes can execute concurrently
May be interrupted at any time, partially completing execution
Concurrent access to shared data may result in data inconsistency
Maintaining data consistency requires mechanisms to ensure the
orderly execution of cooperating processes
Illustration of the problem:
Suppose that we wanted to provide a solution to the consumer-
producer problem that fills all the buffers. We can do so by having
an integer counter that keeps track of the number of full buffers.
Initially, counter is set to 0. It is incremented by the producer after it
produces a new buffer and is decremented by the consumer after it
consumes a buffer.
BMS INSTITUTE OF TECHNOLOGY AND MANAGEMENT
Producer
while (true) {
/* produce an item in next produced */
The variable turn indicates whose turn it is to enter the critical section
The flag array is used to indicate if a process is ready to enter the
critical section. flag[i] = true implies that process Pi is ready!
BMS INSTITUTE OF TECHNOLOGY AND MANAGEMENT
Algorithm for Process Pi
do {
flag[i] = true;
turn = j;
while (flag[j] && turn = = j);
critical section
flag[i] = false;
remainder section
} while (true);
BMS INSTITUTE OF TECHNOLOGY AND MANAGEMENT
Peterson’s Solution (Cont.)
Provable that the three CS requirement are met:
1. Mutual exclusion is preserved
Pi enters CS only if:
either flag[j] = false or turn = i
2. Progress requirement is satisfied
3. Bounded-waiting requirement is met
BMS INSTITUTE OF TECHNOLOGY AND MANAGEMENT
Semaphore
Synchronization tool that provides more sophisticated ways (than Mutex locks) for
process to synchronize their activities.
Semaphore S – integer variable
Can only be accessed via two indivisible (atomic) operations
wait() and signal()
Originally called P() and V()
wait(S) {
while (S <= 0)
; // busy wait
S--;
}
Definition of the signal() operation
signal(S) {
S++;
}
BMS INSTITUTE OF TECHNOLOGY AND MANAGEMENT
Semaphore Usage
Counting semaphore – integer value can range over an unrestricted domain
Binary semaphore – integer value can range only between 0 and 1
Same as a mutex lock
Can solve various synchronization problems
Consider P1 and P2 that require S1 to happen before S2
Create a semaphore “synch” initialized to 0
P1:
S1;
signal(synch);
P2:
wait(synch);
S2;
Can implement a counting semaphore S as a binary semaphore
BMS INSTITUTE OF TECHNOLOGY AND MANAGEMENT
Semaphore Implementation
Must guarantee that no two processes can execute the wait() and
signal() on the same semaphore at the same time
Thus, the implementation becomes the critical section problem where
the wait and signal code are placed in the critical section
Could now have busy waiting in critical section implementation
But implementation code is short
Little busy waiting if critical section rarely occupied
Note that applications may spend lots of time in critical sections and
therefore this is not a good solution
BMS INSTITUTE OF TECHNOLOGY AND MANAGEMENT
Semaphore Implementation with no Busy waiting
With each semaphore there is an associated waiting queue
Each entry in a waiting queue has two data items:
value (of type integer)
pointer to next record in the list
Two operations:
block – place the process invoking the operation on the
appropriate waiting queue
wakeup – remove one of processes in the waiting queue and
place it in the ready queue
typedef struct{
int value;
struct process *list;
} semaphore;
BMS INSTITUTE OF TECHNOLOGY AND MANAGEMENT
Implementation with no Busy waiting (Cont.)
wait(semaphore *S) {
S->value--;
if (S->value < 0) {
add this process to S->list;
block();
}
}
signal(semaphore *S) {
S->value++;
if (S->value <= 0) {
remove a process P from S->list;
wakeup(P);
}
}
BMS INSTITUTE OF TECHNOLOGY AND MANAGEMENT
Deadlock and Starvation
Deadlock – two or more processes are waiting indefinitely for an event that
can be caused by only one of the waiting processes
Let S and Q be two semaphores initialized to 1
P0 P1
wait(S); wait(Q);
wait(Q); wait(S);
... ...
signal(S); signal(Q);
signal(Q); signal(S);
do {
...
/* produce an item in next_produced */
...
wait(empty);
wait(mutex);
...
/* add next produced to the buffer */
...
signal(mutex);
signal(full);
} while (true);
BMS INSTITUTE OF TECHNOLOGY AND MANAGEMENT
Bounded Buffer Problem (Cont.)
The structure of the consumer process
Do {
wait(full);
wait(mutex);
...
/* remove an item from buffer to next_consumed */
...
signal(mutex);
signal(empty);
...
/* consume the item in next consumed */
...
} while (true);
BMS INSTITUTE OF TECHNOLOGY AND MANAGEMENT
Readers-Writers Problem
A data set is shared among a number of concurrent processes
Readers – only read the data set; they do not perform any updates
Writers – can both read and write
Problem – allow multiple readers to read at the same time
Only one single writer can access the shared data at the same time
Several variations of how readers and writers are considered – all involve
some form of priorities
Shared Data
Data set
Semaphore rw_mutex initialized to 1
Semaphore mutex initialized to 1
Integer read_count initialized to 0
BMS INSTITUTE OF TECHNOLOGY AND MANAGEMENT
Readers-Writers Problem (Cont.)
The structure of a writer process
do {
wait(rw_mutex);
...
/* writing is performed */
...
signal(rw_mutex);
} while (true);
BMS INSTITUTE OF TECHNOLOGY AND MANAGEMENT
Readers-Writers Problem (Cont.)
The structure of a reader process
do {
wait(mutex);
read_count++;
if (read_count == 1)
wait(rw_mutex);
signal(mutex);
...
/* reading is performed */
...
wait(mutex);
read count--;
if (read_count == 0)
signal(rw_mutex);
signal(mutex);
} while (true);
BMS INSTITUTE OF TECHNOLOGY AND MANAGEMENT
Readers-Writers Problem Variations
First variation – no reader kept waiting unless writer has
permission to use shared object
Second variation – once writer is ready, it performs the write
ASAP
Both may have starvation leading to even more variations
Problem is solved on some systems by kernel providing reader-
writer locks
BMS INSTITUTE OF TECHNOLOGY AND MANAGEMENT
Dining-Philosophers Problem
// eat
signal (chopstick[i] );
signal (chopstick[ (i + 1) % 5] );
// think
} while (TRUE);
What is the problem with this algorithm?
BMS INSTITUTE OF TECHNOLOGY AND MANAGEMENT
Dining-Philosophers Problem Algorithm (Cont.)
Deadlock handling
Allow at most 4 philosophers to be sitting simultaneously
at the table.
Allow a philosopher to pick up the forks only if both are
available (picking must be done in a critical section.
Use an asymmetric solution -- an odd-numbered
philosopher picks up first the left chopstick and then the
right chopstick. Even-numbered philosopher picks up first
the right chopstick and then the left chopstick.
BMS INSTITUTE OF TECHNOLOGY AND MANAGEMENT
Problems with Semaphores
Incorrect use of semaphore operations: