OS Complete
OS Complete
Introduction
Slides Courtesy: Dr. Barsha Mitra
BITS Pilani CSIS Department, BITS Pilani, Hyderabad Campus
Hyderabad Campus
Handout Overview
Program that manages computer’s hardware
Acts as an intermediary between computer user and computer h/w
Several types, each type optimising different aspects
mainframe operating systems
personal computer (PC) operating systems
operating systems for mobiles platforms
(Question, what are the characteristics of these platforms which
the OS needs to optimise for?)
OS is a resource allocator
Manages all resources
Decides between conflicting requests for efficient and fair resource use
OS is a control program
Controls execution of user programs to prevent errors and improper
use of the computer
device controller
is a hardware
component that
works as a bridge
between the
hardware device
and the operating
system or an
application
program
kept in memory
One job is selected and run via job
scheduling
When it has to wait for I/O, OS
switches to another job
Choice of Interface
exec()
loads a binary file into memory and starts execution
destroys previous memory image
call to exec() does not return unless an error occurs
wait()
parent can issue wait() to move out of ready queue until the child is done
ordinary pipe can’t be accessed from outside the process that created it
parent process creates a pipe and uses it to communicate with a child process that it
creates via fork()
child inherits the pipe from its parent process like any other file
Queueing Diagram
Data parallelism – distributes subsets of the same data across multiple cores,
same operation on each subset
Many-to-One
One-to-One
Many-to-Many
JOINING 10
THREADS
Signals are used in UNIX systems to notify a process that a particular event has
occurred
The signal is delivered to a process
When delivered, signal handler is used to process signals
Synchronous and asynchronous signals
Synchronous signals
illegal memory access, div. by 0
delivered to the same process that performed the operation generating the signal
Asynchronous signals
generated by an event external to a running process
the running process receives the signal asynchronously
Ctrl + C, timer expiration
Most multithreaded versions of UNIX allow a thread to specify which signals it will
accept and which it will block
In some cases, an asynchronous signal may be delivered only to those threads that
are not blocking it
Signals need to be handled only once, a signal is delivered only to the first thread
found that is not blocking it
default type
If thread has cancellation disabled, cancellation remains pending until thread enables it
Default type is deferred
Cancellation only occurs when thread reaches cancellation point
Establish cancellation point by calling pthread_testcancel()
If cancellation request is pending, cleanup handler is invoked to release any
acquired resources
Thread-local storage (TLS) allows each thread to have its own copy
of data
Application runs an upcall handler on this new virtual processor, which saves the state of the blocking thread
and relinquishes the virtual processor on which the blocking thread is running
Upcall handler then schedules another thread that is eligible to run on an available virtual processor
When the event that the blocking thread was waiting for occurs, the kernel makes another upcall to the
thread library informing it that the previously blocked thread is now eligible to run
Upcall handler for this event requires a virtual processor, and kernel may allocate a new virtual processor or
preempt one of the user threads and run the upcall handler on its virtual processor
After marking the unblocked thread as eligible to run, the application schedules an eligible thread to run
on an available virtual processor BITS Pilani, Hyderabad Campus
Scheduler Activations
Source: Scheduler Activations: Effective Kernel Support for the User-Level Management of Parallelism, THOMAS E. ANDERSON, BRIAN N.
BERSHAD, EDWARD D. LAZOWSKA, and HENRY M. LEVY, 1992
Source: Scheduler Activations: Effective Kernel Support for the User-Level Management of Parallelism, THOMAS E. ANDERSON, BRIAN N.
BERSHAD, EDWARD D. LAZOWSKA, and HENRY M. LEVY, 1992
Source: Scheduler Activations: Effective Kernel Support for the User-Level Management of Parallelism, THOMAS E. ANDERSON, BRIAN N.
BERSHAD, EDWARD D. LAZOWSKA, and HENRY M. LEVY, 1992
Associate with each process the length of its next CPU burst
Use these lengths to schedule the process with the shortest time
Use FCFS in case of tie
SJF is optimal – gives minimum average waiting time for a given
set of processes
The difficulty is knowing the length of the next CPU request
For long-term (job) scheduling in a batch system, use the process time
limit that a user specifies when the job is submitted
fixed priority
preemptive scheduling
among queues
Three queues:
Q0 – RR with time quantum 8 milliseconds
Q1 – RR time quantum 16 milliseconds
Q2 – FCFS
Scheduling
A new process enters queue Q0 which is uses RR
When it gains CPU, job receives 8 milliseconds
If it does not finish in 8 milliseconds, job is moved to queue
Q1
At Q1 process is again served using RR and receives 16
additional milliseconds
If it still does not complete, it is preempted and moved to
queue Q2 BITS Pilani, Hyderabad Campus
CFS
Red-Black Tree
Every node has a color either red or black.
The root of the tree is always black.
There are no two adjacent red nodes (A red
node cannot have a red parent or red child).
Every path from a node (including root) to any of
its descendants NULL nodes has the same
number of black nodes.
All leaf nodes are black nodes.
Self balancing: sub-trees are rotated when the
properties are violated
Search, insert, delete: O(log n)
do {
flag[i] = true;
turn = j; Provable that the 3 CS requirement are met:
while (flag[j] && turn = = j); 1. Mutual exclusion is preserved
critical section 2. Progress requirement is satisfied
flag[i] = false; 3. Bounded-waiting requirement is met
remainder section
} while (true);
Pj
Pi
BITS Pilani, Hyderabad Campus
Synchronization Hardware
Many systems provide hardware support for implementing the critical
section code
All solutions below based on idea of locking
Protecting critical regions via locks
Uniprocessors – could disable interrupts
Currently running code would execute without preemption
Generally too inefficient on multiprocessor systems
Modern machines provide special atomic hardware instructions
Atomic = non-interruptible
Either test memory word and set value
Or swap contents of two memory words
do {
acquire lock
critical section
release lock
remainder section
} while (TRUE);
do {
waiting[i] = true; while ((j != i) && !
key = true; waiting[j])
while (waiting[i] j = (j + 1) % n;
&& key) if (j == i)
key = lock = false;
test_and_set(&lock); else
waiting[i] = false;
waiting[j] = false;
/* critical section
/* remainder section */
*/
j = (i + 1) % n; } while (true);
acquire() {
while (!available) Adv. of Spinlock:
; /* busy wait */ 1. In a multiprocessor system no context switch is
available = false;;
required when a process must wait on a lock
}
release() {
2. useful when locks are to be held for short
available = true; times
} 3. on a multiprocessor system, one thread can
do {
spin on one processor and the other may
acquire lock
critical section execute CS on another processor
release lock Disadv. of Spinlock: busy waiting wastes CPU
remainder section
cycles
} while (true);
signal(semaphore *S) {
S->value++;
if(S->value <= 0){
wait(semaphore *S) { remove a process P from S->list;
S->value--; wakeup(P);
if(S->value < 0){ }
add this process to S->list; }
block();
}
}
do {
do {
...
wait(full);
/* produce item in next_produced */
wait(mutex);
...
...
wait(empty);
/*remove item from buffer to next_consumed*/
wait(mutex);
...
...
signal(mutex);
/* add next_produced to the buffer */
signal(empty);
...
...
signal(mutex);
/*consume the item in next_consumed */
signal(full);
...
} while (true); BITS Pilani, Hyderabad Campus
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
First readers-writers prob. no reader is kept waiting unless a writer has
already obtained the permission to use the shared obj., writers may starve
Second readers-writers prob. if a writer is waiting to access the shared obj.
no new readers may start, readers may starve
BITS Pilani, Hyderabad Campus
Readers-Writers Problem
Shared Data
Semaphore rw_mutex initialized to 1, common to both readers and writers, acts
as a mutual exclusion semaphore for writers, used by first reader that enters CS or last
reader that exits CS, not used by other readers
Integer read_count initialized to 0, keeps track of how many processes are reading
the shared obj.
// eat
signal (chopstick[i] );
signal (chopstick[ (i + 1) % 5] );
// think
} while (TRUE);
No Preemption –
If a process that is holding some resources requests another resource that cannot
be immediately allocated to it, then all resources currently being held are released
Preempted resources are added to the list of resources for which the process is
waiting
Process will be restarted only when it can regain its old resources, as well as the
new ones that it is requesting
When a process P1 requests some resources and they are allocated to some other
waiting process P2, then preempt the desired resources from P2 and give them to P1
If the resources are not allocated to a waiting process, then P1 must wait
While waiting P1’s resources may be preempted
Circular Wait – impose a total ordering of all resource types, and require
that each process requests resources in an increasing order of enumeration
define a 1:1 function F : R → N (N is set of natural nos.)
Say a process P has requested a no. of instances of Ri
Later, Pi can request resources of type Rj iff F(Rj) > F(Ri)
Alternatively, if Pi requests an instance of Rj then Pi must have released all
instances of Ri s.t. F(Ri) >= F(Rj)
Several instances of same resource type must be requested for in a single
request
Proof by contradiction
BITS Pilani, Hyderabad Campus
Deadlock Avoidance
When a process gets all its resources it must return them in a finite
amount of time
P2 3 0 2 9 0 2 P2 6 0 0
P3 2 1 1 2 2 2 P3 0 1 1
P4 0 0 2 4 3 3 P4 4 3 1
The system is in a safe state since the sequence < P1, P3, P4, P2, P0> satisfies safety
criteria BITS Pilani, Hyderabad Campus
Resource-Request Algorithm
Requesti = request vector for process Pi. If Requesti [j] = k then process Pi wants k instances
of resource type Rj
1. If Requesti <= Needi go to step 2. Otherwise, raise error condition, since process
has exceeded its maximum claim
2. If Requesti <= Available, go to step 3. Otherwise Pi must wait, since resources are
not available
3. Pretend to allocate requested resources to Pi by modifying the state as follows:
Available = Available – Requesti ;
Allocationi = Allocationi + Requesti;
Needi = Needi – Requesti ;
If safe: the resources are allocated to P
i
If unsafe: P must wait, and the old resource-allocation state is restored
i
P2 3 0 2 9 0 2 P2 6 0 0
P3 2 1 1 2 2 2 P3 0 1 1
P4 0 0 2 4 3 3 P4 4 3 1
Executing safety algorithm shows that sequence < P1, P3, P4, P0, P2> satisfies safety
requirement
Can request for (3,3,0) by P4 be granted?
Can request for (0,2,0) by P0 be granted?
BITS Pilani, Hyderabad Campus
Banker’s Algorithm Example:
P1 Requests (1,0,2)
Check that Request <= Available (that is, (1,0,2) <= (3,3,2): true
Process Allocation Max Available Process Need
A B C A B C A B C A B C
P0 0 1 0 7 5 3 2 3 0 P0 7 4 3
P1 3 0 2 3 2 2 P1 0 2 0
P2 3 0 2 9 0 2 P2 6 0 0
P3 2 1 1 2 2 2 P3 0 1 1
P4 0 0 2 4 3 3 P4 4 3 1
Executing safety algorithm shows that sequence < P1, P3, P4, P0, P2> satisfies safety
requirement
Can request for (3,3,0) by P4 be granted? NO: resources unavailable
Can request for (0,2,0) by P0 be granted? NO: results in unsafe state
BITS Pilani, Hyderabad Campus
Deadlock Detection
Allow system to enter deadlock state
Detection algorithm
Recovery scheme
Available: A vector of length m indicates the number of available resources of each type.
Allocation: An n × m matrix defines the number of resources of each type currently
allocated to each thread.
Request: An n × m matrix indicates the current request of each thread. If Request[i][j] equals
k, then thread Ti is requesting k more instances of resource type Rj.
Algorithm:
1) Let Work and Finish be vectors of length m and n, respectively. Initialize Work = Available. For i = 0, 1, ..., n–
1, if Allocationi ≠ 0, then Finish[i]= false. Otherwise, Finish[i]= true.
2) Find an index i such that both
a) Finish[i]== false
b) Requesti ≤ Work
3)If no such i exists, go to step 4.
Rollback – return to some safe state, restart process for that state, total
rollback or partials
Worst-fit: Allocate the largest hole; must also search entire list
Produces the largest leftover hole
First-fit and best-fit better than worst-fit in terms of speed and storage
utilization
BITS Pilani, Hyderabad Campus
Fragmentation
n = 2 and m = 4
32-byte memory and 4-byte pages
P a ge # F ra m e #
• Memory structures for paging can get huge using straight-forward methods
• Cost a lot
• Don’t want to allocate that contiguously in main memory
• Hierarchical Paging
• Hashed Page Tables
• Inverted Page Tables
Pager guesses which pages will be used before swapping out again
Pager brings in only those pages into memory
How to determine that set of pages?
Need new MMU functionality to implement demand paging
If pages needed are already memory resident
If page needed and not memory resident
Need to detect and load the page into memory from storage
Without changing program behavior
Without programmer needing to change code
7 0 1 2 0 3 0 4 2 3 0 3 2 1 2 0 1 7 0 1
7 7 7 2 2 2 2 2 7
0 0 0 0 4 0 0 0
1 1 3 3 3 1 1
7 7 7 2 2 4 4 4 0 1 1 1
0 0 0 0 0 0 3 3 3 0 0
1 1 3 3 2 2 2 2 2 7
Equal allocation – For example, if there are 100 frames (after allocating
frames for the OS) and 5 processes, give each process 20 frames
Keep some as free frame buffer pool
Local replacement – each process selects from only its own set of
allocated frames
More consistent per-process performance
But possibly underutilized memory
If a process does not have “enough” pages, the page-fault rate is
very high
Page fault to get page
Replace existing frame
But quickly need replaced frame back
This leads to:
Low CPU utilization
Operating system thinking that it needs to increase the degree of
multiprogramming
Another process added to the system
Page-Fault Frequency
(PFF)
Define an upper and
lower limits of page
fault rate
If page fault rate is
too low -> take away
a frame
If page fault rate is
too high ->
allocate one more
frame
• Naming problem
• Grouping problem
index table
Need index table
Random access
Dynamic access without external
fragmentation, but have overhead of index
block
BITS Pilani, Hyderabad Campus
Free Space Management
File system maintains free-space list to track available blocks/clusters
(Using term “block” for simplicity)
Bit vector or bit map (n blocks)
0 1 2 n-1
…
1 block[i] free
bit[i] =
0 block[i] occupied
Counting
Because space is frequently contiguously used and freed
Keep address of first free block and count of following free blocks
Free space list then has entries containing addresses and counts
• Blocking
• Nonblocking
• Error Handling
• OS can recover from disk read, device unavailable, transient write failures
• Retry a read or write, for example
• Most return an error number or code when I/O request fails
• System error logs hold problem reports
• I/O Protection
• User process may accidentally or purposefully attempt to disrupt normal
operation via illegal I/O instructions
• All I/O instructions defined to be privileged
• I/O must be performed via system calls
BITS Pilani, Hyderabad Campus
Thank You
int main() {
if (fork() || fork())
fork();
printf("GATE\n");
return 0;
}
Choose the most appropriate answer with respect to the following code snippet. Assume all required header files have been included.
1. int main()
2. {
4. char buf[30];
10 return 0;
11. }
int main() {
int pfds[2];
char buf[1000];
pipe(pfds);
if (!fork()) {
} else {
printf("%s", buf);
wait(NULL);
return 0;
}
Which of the following is a possible output from line 10?
Assume all required header files have been included.
a) segmentation fault
b) parent
c) cphairlednt
d) Error because two processes can’t write to the same
end of the pipe simultaneously