Os Unit Ii
Os Unit Ii
Os Unit Ii
Mr.V.Yuvaraj
Assistant Professor – Department of Computer Applications
Dr. N.G.P. ARTS AND SCIENCE COLLEGE
Dr. N.G.P.-KALAPATTI ROAD
COIMBATORE-641 048
Tamil Nadu, India
Mobile: +917502919891,
E-mail: yuvaraj.v@drngpasc.ac.in
Dr. NGPASC
COIMBATORE | INDIA
Chapter 2: Processes
• Process Concept
• Process Scheduling
• Operations on Processes
• Basic Concepts: Scheduling Criteria-
Scheduling Algorithms
• Synchronization: Background - The
Critical - Section Problem -Semaphores.
Dr. NGPASC
COIMBATORE | INDIA
Process Concept
Dr. NGPASC
COIMBATORE | INDIA
Process State
Dr. NGPASC
COIMBATORE | INDIA
Diagram of Process State
Dr. NGPASC
COIMBATORE | INDIA
Process Control Block (PCB)
Information associated with each process
(also called task control block)
• Process state – running, waiting, etc
• Program counter – location of instruction
to next execute
• CPU registers – contents of all process-
centric registers
• CPU scheduling information- priorities,
scheduling queue pointers
• Memory-management information –
memory allocated to the process
• Accounting information – CPU used, clock
time elapsed since start, time limits
• I/O status information – I/O devices
allocated to process, list of open files
Dr. NGPASC
COIMBATORE | INDIA
CPU Switch From Process to Process
Dr. NGPASC
COIMBATORE | INDIA
Threads
Dr. NGPASC
COIMBATORE | INDIA
Process Representation in Linux
Dr. NGPASC
COIMBATORE | INDIA
Process Scheduling
Dr. NGPASC
COIMBATORE | INDIA
Ready Queue And Various I/O Device Queues
Dr. NGPASC
COIMBATORE | INDIA
Representation of Process Scheduling
Dr. NGPASC
COIMBATORE | INDIA
Schedulers
• Short-term scheduler (or CPU scheduler) – selects which process should be
executed next and allocates CPU
– Sometimes the only scheduler in a system
– Short-term scheduler is invoked frequently (milliseconds) (must be fast)
• Long-term scheduler (or job scheduler) – selects which processes should be
brought into the ready queue
– Long-term scheduler is invoked infrequently (seconds, minutes) (may be
slow)
– The long-term scheduler controls the degree of multiprogramming
• Processes can be described as either:
– I/O-bound process – spends more time doing I/O than computations, many
short CPU bursts
– CPU-bound process – spends more time doing computations; few very long
CPU bursts
• Long-term scheduler strives for good process mix
Dr. NGPASC
COIMBATORE | INDIA
Addition of Medium Term Scheduling
Medium-term scheduler can be added if degree of
multiple programming needs to decrease
Remove process from memory, store on disk,
bring back in from disk to continue execution:
swapping
Dr. NGPASC
COIMBATORE | INDIA
Multitasking in Mobile Systems
• Some mobile systems (e.g., early version of iOS) allow only
one process to run, others suspended
• Due to screen real estate, user interface limits iOS provides
for a
– Single foreground process- controlled via user interface
– Multiple background processes– in memory, running, but not on
the display, and with limits
– Limits include single, short task, receiving notification of events,
specific long-running tasks like audio playback
• Android runs foreground and background, with fewer limits
– Background process uses a service to perform tasks
– Service can keep running even if background process is suspended
– Service has no user interface, small memory use
Dr. NGPASC
COIMBATORE | INDIA
Context Switch
• 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
• Context of a process represented in the PCB
• Context-switch time is overhead; the system does
no useful work while switching
– The more complex the OS and the PCB the longer the
context switch
• Time dependent on hardware support
– Some hardware provides multiple sets of registers per
CPU multiple contexts loaded at once
Dr. NGPASC
COIMBATORE | INDIA
Operations on Processes
Dr. NGPASC
COIMBATORE | INDIA
Process Creation
Dr. NGPASC
COIMBATORE | INDIA
Process Creation (Cont.)
• 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
Dr. NGPASC
COIMBATORE | INDIA
C Program Forking Separate Process
Dr. NGPASC
COIMBATORE | INDIA
Creating a Separate Process via Windows API
Dr. NGPASC
COIMBATORE | INDIA
Process Termination
Dr. NGPASC
COIMBATORE | INDIA
Process Termination
• Some operating systems do not allow child to exists if its parent
has terminated. If a process terminates, then all its children must
also be terminated.
– cascading termination. All children, grandchildren, etc. are
terminated.
– The termination is initiated by the operating system.
• The parent process may wait for termination of a child process by
using the wait()system call. The call returns status
information and the pid of the terminated process
pid = wait(&status);
• If no parent waiting (did not invoke wait()) process is a zombie
• If parent terminated without invoking wait , process is an
orphan
Dr. NGPASC
COIMBATORE | INDIA
Dr. NGPASC
COIMBATORE | INDIA
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
Dr. NGPASC
COIMBATORE | INDIA
Histogram of CPU-burst Times
Dr. NGPASC
COIMBATORE | INDIA
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
Dr. NGPASC
COIMBATORE | INDIA
Dispatcher
Dr. NGPASC
COIMBATORE | INDIA
Scheduling Criteria
Dr. NGPASC
COIMBATORE | INDIA
First- Come, First-Served (FCFS) Scheduling
Dr. NGPASC
COIMBATORE | INDIA
FCFS Scheduling (Cont.)
Suppose that the processes arrive in the order:
P2 , P3 , P1
• The Gantt chart for the schedule is:
P2 P3 P1
0 3 6 30
Dr. NGPASC
COIMBATORE | INDIA
Shortest-Job-First (SJF) Scheduling
Dr. NGPASC
COIMBATORE | INDIA
Example of SJF
• Commonly, α set to ½
• Preemptive version called shortest-remaining-time-first
Dr. NGPASC
COIMBATORE | INDIA
Prediction of the Length of the Next CPU Burst
Dr. NGPASC
COIMBATORE | INDIA
Examples of Exponential Averaging
• =0
– n+1 = n
– Recent history does not count
• =1
– n+1 = tn
– Only the actual last CPU burst counts
• If we expand the formula, we get:
n+1 = tn+(1 - ) tn -1 + …
+(1 - )j tn -j + …
+(1 - )n +1 0
Dr. NGPASC
COIMBATORE | INDIA
Example of Shortest-remaining-time-first
P1 P2 P4 P1 P3
0 1 5 10 17 26
Dr. NGPASC
COIMBATORE | INDIA
Priority Scheduling
Dr. NGPASC
COIMBATORE | INDIA
Example of Priority Scheduling
Dr. NGPASC
COIMBATORE | INDIA
Example of RR with Time Quantum = 4
Process Burst Time
P1 24
P2 3
P3 3
• The Gantt chart is:
P1 P2 P3 P1 P1 P1 P1 P1
0 4 7 10 14 18 22 26 30
Dr. NGPASC
COIMBATORE | INDIA
Time Quantum and Context Switch Time
Dr. NGPASC
COIMBATORE | INDIA
Turnaround Time Varies With The Time Quantum
Dr. NGPASC
COIMBATORE | INDIA
Multilevel Queue
Dr. NGPASC
COIMBATORE | INDIA
Multilevel Feedback Queue
Dr. NGPASC
COIMBATORE | INDIA
Example of Multilevel Feedback Queue
• Three queues:
– Q0 – RR with time quantum 8 milliseconds
– Q1 – RR time quantum 16 milliseconds
– Q2 – FCFS
• 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
Dr. NGPASC
COIMBATORE | INDIA
Multiprocess Architecture – Chrome Browser
Dr. NGPASC
COIMBATORE | INDIA
Background
while (true) {
/* produce an item in next produced */
Dr. NGPASC
COIMBATORE | INDIA
Consumer
while (true) {
while (counter == 0)
; /* do nothing */
next_consumed = buffer[out];
out = (out + 1) % BUFFER_SIZE;
counter--;
/* consume the item in next consumed */
}
Dr. NGPASC
COIMBATORE | INDIA
Race Condition
register1 = counter
register1 = register1 + 1
counter = register1
Dr. NGPASC
COIMBATORE | INDIA
Critical Section Problem
Dr. NGPASC
COIMBATORE | INDIA
Algorithm for Process Pi
do {
Dr. NGPASC
COIMBATORE | INDIA
Solution to Critical-Section Problem
• 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
Dr. NGPASC
COIMBATORE | INDIA
Semaphore Implementation
Dr. NGPASC
COIMBATORE | INDIA
Semaphore Implementation with no Busy waiting
Dr. NGPASC
COIMBATORE | INDIA
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);
}
}
Dr. NGPASC
COIMBATORE | INDIA
Deadlock and Starvation