OS-Unit II Notes
OS-Unit II Notes
TOPICS
Program Process
Program is a passive entity such as file containing Process is an active entity with a program counter
list of instructions stored on disk specifying the next instruction to execute
Hard disk stores the programs and these programs Requires resources such as memory, IO devices
doesn’t require resources and CPU
Process in Memory
A process generally consists of a process stack which consists of code, data, heap and
stack sections
The compiled program code is stored in text section.
The local variables and function return values are stored in stack section
Stack and heap start at the opposite ends and grow towards each other
The Process changes its state multiple times throughout its life cycle.
New: It is the initial state of a process. This is the state when the process has just been created.
Ready: In the ready state, the process is waiting to be assigned the processor by the short-term
scheduler, so that it can run. This state is immediately after the new state for the process.
Running: The process is said to be in running state when the process instructions are being
executed by the processor. This is done once the process is assigned to the processor using the
short-term scheduler.
Waiting: The process is in blocked state if it is waiting for some event to occur. This event may
be I/O as the I/O events are executed in the main memory and don't require the processor. After
the event is complete, the process again goes to ready state.
Terminated: The process is terminated once it finishes its execution. In the terminated state, the
process is removed from main memory and its process control block is also deleted.
Logically, the 'Running' and 'Ready' states are similar. In both cases the process is willing to
run, only in the case of 'Ready' state, there is temporarily no CPU available for it. The ‘Waiting’
state is different from the 'Running' and 'Ready' states in that the process cannot run, even if the
CPU is available.
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.
Context-switch time is overhead; the system does no useful work while switching.
Time dependent on hardware support
The context of a process is represented in the PCB of the process;
it includes
value of CPU registers
process-state and
memory-management information.
Disadvantages:
1) Context-switch time is pure overhead, because the system does no useful work while
switching.
2) Context-switch times are highly dependent on hardware support.
Process scheduler selects the available process for program execution on CPU.
The process state consists of everything necessary to resume the process execution if it is
somehow put aside temporarily.
Schedulers:
The following are the different type of schedulers
1. Long-term scheduler (or job scheduler) – selects which processes should be brought
into the ready queue.
Long-term scheduler is invoked very infrequently (seconds, minutes) (may be slow)
The long-term scheduler controls the degree of multiprogramming
2. Short-term scheduler (or CPU scheduler) – selects which process should be executed
next and allocates CPU.
Short-term scheduler is invoked very frequently (milliseconds) (must be fast)
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
Why long-term scheduler should select a good process mix of I/O-bound and CPU-bound
processes ?
i. If all processes are I/O bound, then
Ready-queue will almost always be empty, and
Short-term scheduler will have little to do.
ii. If all processes are CPU bound, then
I/O waiting queue will almost always be empty (devices will go unused) and
System will be unbalanced.
3. Medium-term schedulers-
Some time-sharing systems have medium-term scheduler
The scheduler removes processes from memory and thus reduces the degree of
multiprogramming.
Later, the process can be reintroduced into memory, and its 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 scheduler
dynamically.
Process Operations
1) Process Creation and
2) Process Termination
Process Creation
A process may create a new process via a create-process system-call.
The creating process is called a parent-process.
The new process created by the parent is called the child-process (Sub-process).
OS identifies processes by pid (process identifier), which is typically an integer-number.
A process needs following resources to accomplish the task:
CPU time
memory and
I/O devices.
Child-process may
get resources directly from the OS or
get resources of parent-process. This prevents any process from overloading the system.
The child-process is a duplicate of the parent-process (it has the same program and data
as the parent). -fork()
The child-process has a new program loaded into it. -exec()
In UNIX, each process is identified by its process identifier (pid), which is a unique integer.
Getpid() – retrieves the process id for that process
Fork()-
• A new process is created by the fork() system-call
• The new process consists of a copy of the address-space of the original process.
• After fork() ,both parent and the child, have the same memory image, the same
environment strings and the same open files
•
• Both the parent and the child continue execution with one difference:
o The return value for the fork() is zero for the new (child) process.
o The return value for the fork() is nonzero pid of the child for the parent-process.
o The return value for the fork() is non-negative, if child process is unsuccessful
Exec()-
Exec() system-call is used after a fork() system-call by one of the two processes to
replace the process's memory-space with a new program.
The parent can issue wait() system-call to move itself off the ready-queue.
Exec() – Replaces the entire current process with a new program. It loads the program
into current process space and run it from entry point.
Process Termination
A process terminates when it finishes executing its last statement and informs OS to delete it by
using exit() system call. At that point it will return a value to parent process through wait()
system call.
Its resources are returned to the system, it is purged from any system lists or tables, and its
process control block (PCB) is erased i.e., the PCB's memory space is returned to a free memory
pool. The new process terminates the existing process, usually due to following reasons:
Normal Exist Most processes terminate because they have done their job. This call is existing in
UNIX.
Error Exist When process discovers a fatal error. For example, a user tries to compile a program
that does not exist.
Fatal Error An error caused by process due to a bug in program for example, executing an
illegal instruction, referring non-existing memory or dividing by zero.
Killed by another Process A process executes a system call telling the Operating Systems to
terminate some other process. In UNIX, this call is kill.
In some systems when a process kills all processes it created are killed as well (UNIX does not
work this way).
Some OS doesn’t allow a child to exist, if its parent has terminated, so it has to kill all its
children, this phenomenon is called cascading termination. In UNIX, if parent terminates, all its
children will have a new parent i.e., init process. So that children still have a parent to send the
status.
Message Passing
Figure 9 a. Message Passing b. Shared Memory
Message Passing provides a mechanism to communicate and synchronize their actions
without sharing the same address space
IPC facility provides two operations:
send(message)
receive(message)
Message size can be fixed or variable size.
If P and Q wish to communicate, they need to exchange messages using send/receive via
communication link. This Communication link can be implemented using
1. Direct or Indirect communication
2. Synchronous or Asynchronous
3. Automatic or Explicit buffering
Each process must explicitly name the Messages are sent to/received from
recipient/sender. mailboxes (or ports).
A link is established automatically between A link is established between a pair of
every pair of processes that want to processes only if both members have a
communicate. The processes need to know shared mailbox.
only each other‟s identity to communicate. A link may be associated with more than
A link is associated with exactly two two processes.
processes. A number of different links may exist
Exactly one link exists between each pair between each pair of communicating
of processes. processes.
The sending process is blocked until the The sending process sends the message
message is received by the receiving process and resumes operation.
or by the mailbox.
The receiver blocks until a message is The receiver retrieves either a valid
available. message or a null.
Shared Memory
Every process executes in its own address space & OS tries to prevent one process from
other process memory
Shared memory requires two or more processes agree to remove this restriction
Exchange information by reading & writing data in the shared areas
OS doesn’t control the data & location determined but the processes are responsible for it
Process are responsible that they don’t write to the same location simultaneously
Eg for Producer-Consumer problem:
Producer process produces the information
Consumer process consumes the information
Eg: Compiler produces assembly code, which is consumed by assembler
#define Buffer_size 10
typedef struct {
---------
}item;
item buffer[Buffersize];
int in =0;
int out=0;
// in- points to next free positionin buffer
//out- first full position in buffer
//Producer Code:
item nextProduced;
while( true ) {
/* Produce an item and store it in nextProduced */
nextProduced = makeNewItem( . . . );
/* Wait for space to become available */
while( ( ( in + 1 ) % BUFFER_SIZE ) == out )
; /* Do nothing */
/* And then store the item and repeat the loop. */
buffer[ in ] = nextProduced;
in = ( in + 1 ) % BUFFER_SIZE;
}
//Consumer code:
item nextConsumed;
while( true ) {
/* Wait for an item to become available */
while( in == out )
; /* Do nothing */
/* Get the next available item */
nextConsumed = buffer[ out ];
out = ( out + 1 ) % BUFFER_SIZE;
/* Consume the item in nextConsumed
( Do something with it ) */
}
CPU Scheduler
This scheduler selects a waiting-process from the ready-queue and allocates CPU to the
waiting-process.
The ready-queue could be a FIFO, priority queue, tree and list.
The records in the queues are generally process control blocks (PCBs) of the processes.
CPU Scheduling
1. When a process switches from the running state to the waiting state. For ex; I/O request.
2. When a process switches from the running state to the ready state. For ex: when an
interrupt occurs.
3. When a process switches from the waiting state to the ready state. For ex: completion of
I/O.
4. When a process terminates.
Scheduling under 1 and 4 is non-preemptive.
All other scheduling is preemptive
Once the CPU has been allocated to a process, the process keeps the CPU until it releases the
CPU either
by terminating or
by switching to the waiting state.
This is driven by the idea of prioritized computation.
Processes that are runnable may be temporarily suspended
Disadvantages:
Incurs a cost associated with access to shared-data.
Affects the design of the OS kernel.
Dispatcher
It gives control of the CPU to the process selected by the short-term scheduler.
The function involves:
Switching context
Switching to user mode &
Jumping to the proper location in the user program to restart that program.
It should be as fast as possible, since it is invoked during every process switch.
Dispatch latency means the time taken by the dispatcher to stop one process and start another
running.
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. Note that
giving equivalent or equal time is not fair. Think of safety control and payroll at a nuclear plant.
Policy Enforcement
The scheduler has to make sure that system's policy is enforced. For example, if the local
policy is safety then the safety control processes must be able to run whenever they want to,
even if it means delay in payroll processes.
Efficiency
Scheduler should keep the system (or in particular CPU) busy cent percent 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 time batch users must wait for an output.
Throughput
A scheduler should maximize the number of jobs processed per unit time.
A little thought will show that some of these goals are contradictory. It can be shown that
any scheduling algorithm that favours some class of jobs hurts another class of jobs. The
amount of CPU time available is finite, after all.
The Scheduling algorithms can be divided into two categories with respect to how they deal
with clock interrupts.
Nonpreemptive Scheduling
A scheduling discipline is nonpreemptive if, once a process has been given the CPU, the
CPU cannot be taken away from that process.
In nonpreemptive system, short jobs are made to wait by longer jobs but the
overall treatment of all processes is fair.
In nonpreemptive system, response times are more predictable because incoming high
priority jobs cannot displace waiting jobs.
In nonpreemptive scheduling, a schedular executes jobs in the following two situations.
o When a process switches from running state to the waiting state.
o When a process terminates.
Preemptive Scheduling
A scheduling discipline is preemptive if, once a process has been given the CPU can take
away.
The strategy of allowing processes that are logically runnable to be temporarily
suspended is called Preemptive Scheduling and it is contrast to the "run to completion"
method.
CPU Scheduling deals with the problem of deciding which of the processes in the ready queue is
to be allocated the CPU. Following are some scheduling algorithms we will study.
1. FCFS Scheduling.
2. Shortest job first
3. Priority Scheduling
4. Round Robin Scheduling.
5. Multilevel Queue Scheduling.
6. Multilevel Feedback Queue Scheduling.
Scheduling
Algorithms
Non-
Preemptive
Preemeptive
First-In-First-Out (FIFO)
Run-to-Completion
Run-Until-Done
CONVOY EFFECT:
Processes that need shorter CPU time are blocked by one process holding the CPU for long time.
This leads to Poor utilization of resource and poor performance
Preemptive SJF
If the new process has a shorter next CPU burst than what is left of the executing process, that
process is preempted. It is also known as SRTF scheduling (Shortest-Remaining-Time-First).
Example (for non-preemptive SJF): Consider the following set of processes, with the length of
the CPU-burst time given in milliseconds.
Figure 15SHORTEST REMAINING JOB FIRST (SRJF)/SHORTEST REMAINING TIME NEXT–Preemptive Example 1
Figure 16SHORTEST REMAINING JOB FIRST (SRJF)/SHORTEST REMAINING TIME NEXT–Preemptive Example 2
Internally-defined priorities
Externally-defined priorities
For example:
Categories
Non Preemptive
Advantage:
Indefinite blocking, where low-priority processes are left waiting indefinitely for CPU. Solution: Aging
is a technique of increasing priority of processes that wait in system for a long time.
Example: Consider the following set of processes, assumed to have arrived at time 0, in the order P1,
P2, ..., P4, with the length of the CPU-burst time given in milliseconds.
Preemptive
The CPU is preempted if the priority of the newly arrived process is higher than the priority of the
currently running process
Figure 19PRIORITY BASED Preemptive (Example1)
Round-Robin Scheduling
Designed especially for timesharing systems.It is similar to FCFS scheduling, but with preemption.
A small unit of time is called a time quantum (or time slice).
To implement:
Advantage:
Disadvantage:
If each queue has absolute priority over lower-priority unless the queue for the highest-priority
processes were process in the batch queue could run unless the queues interactive editing
processes will all empty.
Possibility II
If there is a time slice between the queues then each queue gets a certain amount of CPU times,
which it can then schedule among the processes in its queue. For instance;
If multiple CPUs are available, the scheduling problem becomes more complex.
Two approaches:
Asymmetric Multiprocessing
The basic idea is:
A master server is a single processor responsible for all scheduling decisions, I/O processing
and other system activities.
The other processors execute only user code.
Advantage:
This is simple because only one processor accesses the system data structures, reducing the
need for data sharing.
Symmetric Multiprocessing
The basic idea is:
Each processor is self-scheduling.
To do scheduling, the scheduler for each processor
Examines the ready-queue and Selects a process to execute.
Restriction: We must ensure that two processors do not choose the same process and that
processes are not lost from the queue.
Processor Affinity
In SMP systems, Migration of processes from one processor to another are avoided and
instead processes are kept running on same processor. This is known as processor affinity.
o Soft Affinity – When an operating system has a policy of attempting to keep a process
running on the same processor but not guaranteeing it will do so, this situation is called
soft affinity.
o Hard Affinity – Hard Affinity allows a process to specify a subset of processors on
which it may run. Some systems such as Linux implements soft affinity but also provide
some system calls like sched_setaffinity() that supports hard affinity.
Load Balancing
Load Balancing is the phenomena which keeps the workload evenly distributed across all
processors in an SMP system. Load balancing is necessary only on systems where each
processor has its own private queue of process which are eligible to execute. Load balancing
is unnecessary because once a processor becomes idle it immediately extracts a runnable
process from the common run queue. On SMP(symmetric multiprocessing), it is important to
keep the workload balanced among all processors to fully utilize the benefits of having more
than one processor else one or more processor will sit idle while other processors have high
workloads along with lists of processors awaiting the CPU.
There are two general approaches to load balancing :
1. Push Migration – In push migration a task routinely checks the load on each processor and
if it finds an imbalance then it evenly distributes load on each processors by moving the
processes from overloaded to idle or less busy processors.
2. Pull Migration – Pull Migration occurs when an idle processor pulls a waiting task from a
busy processor for its execution.
Multi-Threaded Programming
Overview
A thread is a basic unit of CPU utilization; it comprises a
thread ID,
a program counter,
a register set, and
a stack.
It shares with other threads belonging to the same process its code section, data
section, and other operating-system resources, such as open files and signals.
A traditional (or heavyweight:) process has a single thread of control.
If a process has multiple threads of control, it can perform more than one task at a
time.
Figure 4.1 illustrates the difference between a traditional single threaded process and a multi-
threaded process.
Multithreading models
User threads are supported above the kernel and are managed without kernel support,
Kernel threads are supported and managed directly by the operating system.
1. Many-to-One Model
The many-to-one model maps many user-level threads to one kernel thread.
Thread management is done by the thread library in user space,
It is efficient; but the entire process will block if a thread makes a blocking system
call.
one thread can access the kernel at a time,
Multiple threads are unable to run in parallel on multiprocessors.
2. One-to-One Model
The one-to-one model maps each user thread to a kernel thread.
It provides more concurrency than the many-to-one model by allowing another
Thread to run when a thread makes a blocking system call;
It also allows multiple threads to run in parallel on multiprocessors.
The only drawback to this model is that creating a user thread requires creating the
corresponding kernel thread. Because the overhead of creating kernel threads can
burden the performance of an application,
EX: Linux, along with the family of Windows operating systems, implement the
one-to-one model.
3.Many-to-Many Model
The many-to-many model multiplexes many user-level threads to a smaller or equal number of
kernel threads.
The number of kernel threads may be specific to either a particular application or a particular
machine
Developers can create many user threads that are necessary and corresponding kernel threads can
run in parallel on a multiprocessor.
when a thread performs a blocking system call, the kernel can schedule another thread for
execution.
Two level model also allows a user level thread to be bound to a kernel thread.
Thread Libraries
A Thread Libraries provides the programmer with an API for creating and managing
threads.
Threading Issues
The fork() and exec() System Calls
The fork () system call is used to create a separate, duplicate process.
UNIX has 2 versions:
a) fork()- one that duplicates all threads.
b)fork()-duplicates only the thread that invoked the fork() system call.
Exec() if a thread invokes the exec() system call, the program specified in the parameter to exec () will
replace the entire process-including
all threads.
If exec() is called immediately after forking, then duplicating all threads is
unnecessary, as the program specified in the parameters to exec() will replace
the process.
Cancellation
The thread cancellation is the task of terminating a thread before it has completed.