Operating System new
Operating System new
Lecture 1
Purpose of a Computer
System
Computer systems consist of
software and hardware that
are combined to provide a tool
to solve specific problems in
an efficient manner
Execute programs
Layered View of a
Computer System
Computer System
Hardware
Integer Control Keyboard Mouse
Unit Unit
CD
Floating Point
Unit
Cache
Processor
System Bus
HD
Mem
Bus
4. Synchronize executions of
cooperating processes
4/4
Allow a user to manage his/her files
and directories properly
1. User view of directory structure
P1
P2
P1
…
P2
OS Features Needed for
Multiprogramming
SPOOLing (Simultaneous Peripheral Operation On-Line):
Spooling is the process of temporary storage of data for
use and execution by a device, program, or system.
Memory management : multiple programs can run
simultaneously without interfering with each other's
data. The OS allocates separate memory spaces for
each program, so they don't overlap or access each
other's information.
CPU scheduling: This feature decides which program
gets to use the CPU (brain of the computer) and for how
long. It's like a traffic cop, ensuring that each program
gets a fair share of CPU time, so they can run efficiently
and complete tasks.
Time-sharing Systems
• Interactive system with multiprogramming:
Allows multiple users to interact with the computer
simultaneously, executing multiple programs at the
same time.-
• Reasonable response time:
Resume
Interrupt Handling
Interrupt transfers control to the
interrupt service routine, generally,
through the interrupt vector, which
contains addresses of all the
interrupt service routines.
Interrupt architecture must save the
address of the instruction after the
interrupted instruction and the CPU
state so that execution of the
interrupted process may continue
after the interrupt has been serviced.
Interrupt Handling …
Incoming interrupts are disabled while another
interrupt is being processed to prevent lost
interrupts.
An operating system is interrupt driven.
Whenever an interrupt occurs, the controller
completes the execution of the current instruction
and starts the execution of an Interrupt Service
Routine (ISR) or Interrupt Handler. ISR tells the
processor or controller what to do when the
interrupt occurs. The interrupts can be either
hardware interrupts or software interrupts.
Hardware Protection
Dual-Mode Operation
I/O Protection
Memory Protection
CPU Protection
Dual-Mode Operation
Sharing system resources requires
operating system to ensure that an
incorrect program cannot cause other
programs to execute incorrectly.
Provide hardware support to
differentiate between at least two
modes of operations.
User mode – execution done on behalf
of a user.
Monitor mode (also kernel mode or
system mode) – execution done on
behalf of operating system.
Dual-Mode Operation …
Mode bit added to computer
hardware to indicate the current
mode: monitor (0) or user (1).
When an interrupt or fault occurs
hardware switches to monitor mode.
Interrupt/fault
monitor user
set user mode
interrupt occurs.
Timer commonly used to implement time
sharing.
Time also used to compute the current time.
Load-timer is a privileged instruction(because
load timer value isn’t decided by user)
OS Components
Process management
Main memory management
Secondary storage management
I/O system management
File management
Protection system
Networking
Command-line interpreter (shells)
Operating System
Services
Services for user and users of
programs:
Program execution
I/O Operations
File System Manipulation
Communications between
processes/users
Error detection and handling
Operating System
Services …
Services for efficient system
operation:
Resource management
Accounting
Protection
OS Kernel
Users
Applications
Operating System API(Application Program Interface,
AUI(Application User Interface
Interrupt Trap
System Calls
User processes must not be given
open access to the kernel code
The system call interface layer
contains entry point in the kernel
code
Any user or application request that
involves access to any system
resource must be handled by the
kernel code
Types Of System Calls
Process Control
File Management
Device Management
Information
maintenance
Communications
System Call Execution
The user program makes a call to a
library function.
Library routine puts appropriate
parameters at a well-known place
(registers, stack, or a table in memory).
The trap instruction is executed to
change mode from user to kernel.
Control goes to operating system.
Operating system determines which
system call is to be carried out.
Semantics of System
Call Execution …
Kernel indexes the dispatch table,
which contains pointers to service
routines for system calls.
Service routine is executed and
return parameter or error code
placed at well-known places (usually
a CPU register).
Control given back to user program.
Library function executes the
instruction following trap.
System Call …
Process
Library Call
System Call
trap
Dispatch Table
Service
Code
Kernel
Code
Operating System
Structures
Simple Structure
Layered Approach
Microkernels
Virtual Machines
Simple Structures …
UNIX consists of two separable
parts, the kernel and the system
programs.
Every thing below the system call
interface and above the physical
hardware is the kernel.
An enormous amount of functionality
combined in one level, UNIX is
difficult to enhance as changes in
one section could adversely affect
other areas.
Simple Structures
MS-DOS : written to provide the most functionality in the
least space, so it was not divided into modules carefully
UNIX System Structure
Layered Approach
The OS is broken up into a number of
layers
Bottom layer is hardware and the topmost
layer ( layer N) is the user interface
A typical layer consists of data structures
and a set of routines to service the layer
above it
THE operating system by Dijkstra
IBM’s OS/2
Layered Approach …
Layered Approach …
Modularity
Each layer uses functions and services of
only lower layers
Simplifies debugging and system
verification.
The major difficulty with layered approach
is careful definition of layers, because a
layer can only use the layers below it
Less efficient than other approaches
Microkernel
Structures the operating system by
removing all non-essential components
from the kernel and implementing them as
system and user level programs
Smaller kernel
Main function is to provide a
communication facility between client
programs and the various services that are
also running in the user space.
Microkernel …
Easier to extend the OS—new services are
added to user space and consequently do not
require modification of the kernel and/or its
recompilation
Easier to maintain operating system code
(enhancement, debugging, etc.)
OS is easier to port from one hardware to
another
More security and reliability
Mach, MacOS X Server, QNX, OS/2, and
Windows NT
Windows NT Client-Server
Structure
Virtual Machines
CPU scheduling and virtual memory
techniques used to emulate hardware of
the underlying machine, on which user can
install an operating system that the virtual
machine supports
On a time-sharing system with virtual
machine support, users may be working on
different operating systems
Pioneered by IBM VM operating system
that ran CMS, a single-user interactive
operating system
Virtual Machines …
Difficult to implement.
System development done without
disrupting normal system operation.
Virtual Machines …
assigned to a processor.
running: Instructions are being
executed.
waiting: The process is waiting for
execution.
Process States
Process Control Block
(PCB)
Process information and attributes
Process state
Program counter
CPU registers
CPU scheduling information
Memory-management information
Accounting information
I/O status information
Per process file table
Process ID (PID)
Parent PID, etc.
Process Control Block
(PCB)
CPU Switch From Process
to Process
Process Scheduling
Queues
Job queue – set of all processes in the
system.
Ready queue – set of all processes
residing in main memory, ready and
waiting to execute.
Device queues – set of processes
waiting for I/O devices.
Process migration between the
various queues.
Queues in the OS
Queues in a Computer
System
Schedulers
Long term scheduler
Short term scheduler
Medium term scheduler
Long Term Scheduler
Long-term scheduler (or job scheduler) –
selects processes from the job pool to 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.
More processes, smaller percentage of time
each process is executed
Short Term Scheduler
Short-term scheduler (or CPU scheduler) –
selects which process should be executed
next and allocates it the CPU through the
dispatcher.
Short-term scheduler is invoked very frequently
(milliseconds) (must be fast).
Invoked when following events occur
CPU slice of the current process finishes
Current process needs to wait for an event
Clock interrupt
I/O interrupt
System call
Signal
Medium Term Scheduler
Also known as swapper
Selects an in-memory process and swaps
it out to the disk temporarily
Swapping decision is based on several
factors
Arrival of a higher priority process but no
memory available
Poor mix of jobs
Memory request of a process cannot be met
Addition of Medium Term
Scheduling
Context Switch
When CPU switches to another process,
the system must save the state (context)
of the ‘current’ (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;
typically in microseconds
Process Creation
Parent process create children processes,
which, in turn create other processes,
forming a tree of processes.
Resource sharing
Parent and children share all resources.
Children share a subset of parent’s resources.
Parent and child share no resources.
Execution
Parent and children execute concurrently.
Parent waits until children terminate.
Process Creation …
Address space
Child duplicate of parent.
Child has a program loaded onto it.
UNIX examples
fork system call creates a new process
exec system call used after a fork to
replace the process’ memory image with
a new executable.
Processes Tree on a UNIX
System
Process Termination
Process executes the last statement and
requests the operating system to terminate
it (exit).
Output data from child to parent (via wait).
Process resources are deallocated by the
operating system, to be recycled later.
Process Termination …
Parent may terminate execution of
children processes (abort).
Child has exceeded allocated resources
(main memory, execution time, etc.).
Parent needs to create another child but has
reached its maximum children limit
Task performed by the child is no longer
required.
Parent exits.
Operating system does not allow child to continue
if its parent terminates.
Cascaded termination
Process Management
in UNIX/Linux
Important process-related
UNIX/Linux system calls
fork
wait
exec
exit
fork()
When the fork system call is executed, a
new process is created which consists of a
copy of the address space of the parent.
This mechanism allows the
parent process to communicate
easily with the child process.
SYNOPSIS
#include <sys/types.h>
#include <unistd.h>
pid_t fork(void);
fork() ...
The return code for fork is zero for the
child process and the process identifier
of child is returned to the parent
process.
On success, both processes continue
execution at the instruction after the
fork call.
On failure, -1 is returned to the parent
process and errno is set appropriately
to indicate the reason of failure; no
child is created
fork()—Sample Code
main()
{
int pid;
Parent Process pid = 1234
...
pid = fork();
if (pid == 0) {
/* Code for child */ Child Process pid = 0
...
}
else {
/* Code for parent */
...
}
... Kernel Space
}
fork()—Inherits from
the Parent
The child process inherits the
following attributes from the parent:
Environment
Open file descriptor table
Signal handling settings
Nice value
Current working directory
Root directory
File mode creation mask (umask)
Etc.
fork()—Child Differs
from the Parent
The child process differs from the
parent process:
Different process ID (PID)
Different parent process ID (PPID)
Child has its own copy of parent’s
file descriptors
Etc.
fork()—Reasons for
Failure
Maximum number of processes
allowed to execute under one user
has exceeded
Maximum number of processes
allowed on the system has exceeded
Not enough swap space