Operating System Tutorial
Source: [Link]
Outline
Introduction
Process and Threads
Deadlocks and Process Synchronization
Scheduling
Interrupts & System Calls
Memory Management
What is an Operating System?
A program that acts as an intermediary
between a user of a computer and the
computer hardware.
Operating system goals:
z Execute user programs and make solving
user problems easier.
z Make the computer system convenient to
use.
Use the computer hardware in an efficient
manner.
Four Components of a Computer System
Operating-System Operations
Dual-mode operation allows OS to protect
itself and other system components
z User mode and kernel mode
z Mode bit provided by hardware
Provides ability to distinguish when system is running
user code or kernel code
Some instructions designated as privileged, only
executable in kernel mode
System call changes mode to kernel, return from call
resets it to user
Transition from User to Kernel Mode
Process Concept
Process
z a program in execution
z process execution must progress in sequential
fashion
A process includes:
z program counter
z stack
z data section
Process Control Block (PCB)
Process State Diagram
CPU Switch From Process to Process
Representation of Process Scheduling
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
Cooperating Processes
Independent process cannot affect or be
affected by the execution of another
process
Cooperating process can affect or be
affected by the execution of another
process
Advantages of process cooperation
z Information sharing
z Computation speed-up
z Modularity
z Convenience
Producer-Consumer Problem
Paradigm for cooperating processes,
producer process produces
information that is consumed by a
consumer process
z unbounded-buffer places no practical limit
on the size of the buffer
z bounded-buffer assumes that there is a
fixed buffer size
Interprocess Communication (IPC)
Mechanism for processes to communicate and to
synchronize their actions
Message system – processes communicate with each
other without resorting to shared variables
IPC facility provides two operations:
z send(message) – message size fixed or variable
z receive(message)
If P and Q wish to communicate, they need to:
z establish a communication link between them
z exchange messages via send/receive
Threads
A flow of control in a process
has access to entire process (incl. other threads)
potentially parallel execution
minimal state, so low overheads
All threads within a process
z share the same state and same memory space
z use different stack
z and can communicate with each other directly, because they
share the same variables.
Single and Multithreaded Processes
Benefits
Responsiveness
Resource Sharing
Economy
Utilization of MP Architectures
Process vs Thread
Process Thread
Address Space Not Shared Shared
Stack Not Shared Not Shared
Communication IPC Direct
State Different Same
Information
Context Switch Large Small
Overhead
CPU Scheduler
Selects from among the processes in memory that are
ready to execute, and allocates the CPU to one of
them
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
Dispatcher
Dispatcher module gives control of the CPU to the
process selected by the short-term scheduler; this
involves:
z switching context
z switching to user mode
z 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
Scheduling
Optimization Criteria
z Max CPU utilization
z Min turnaround time
z Min waiting time
Few Algorithms
z First Come First Serve
z Shortest Job First
z Priority based
z Round Robin
Deadlock: Bridge Crossing Example
Deadlock Characterization
Deadlock can arise if four conditions hold simultaneously.
Mutual exclusion
Hold and wait
No preemption
Circular wait
Methods for Handling Deadlocks
Ensure that the system will never enter a deadlock
state.
Allow the system to enter a deadlock state and
then recover.
Deadlock Prevention
Restrain the ways request can be made.
Mutual Exclusion
Hold and Wait
z Require process to request and be allocated all its
resources before it begins execution
z or allow process to request resources only when the
process has none
No Preemption
Circular Wait
Interrupt Handling
The operating system preserves the state of the
CPU by storing registers and the program counter.
Determines which type of interrupt has occurred:
z polling
z vectored interrupt system
Separate segments of code determine what action
should be taken for each type of interrupt
Interrupt Timeline
Common Functions of Interrupts
Interrupt transfers control to the interrupt service
routine generally, through the interrupt vector, which
contains the addresses of all the service routines.
Interrupt architecture must save the address of the
interrupted instruction.
Incoming interrupts are disabled while another
interrupt is being processed to prevent a lost
interrupt.
A trap is a software-generated interrupt caused either
by an error or a user request.
An operating system is interrupt driven.