Os Lecture1
Os Lecture1
Operating Systems
COMP2006
Overview
Lecture 1
2
COMP2006 Operating Systems
Prerequisites:
Unix and C Programming
3
Textbooks
Required Text:
Silberschatz, Galvin, and Gagne, Operating System Concepts, 9th
edition, John-Wiley & Sons, 2013; OR 10th edition
4
Overview
Reference
Chapter 1-2, and Section 13.1-13.2
Topics:
Operating system purposes, components and services.
OS structures
5
What is an OS?
An Operating System (OS) is a PROGRAM that acts as an
interface between a USER of a computer system and the
COMPUTER HARDWARE
– OS manages the computer hardware, and provides a basis for running
application programs
There is no universally accepted definition of OS and what is part
of OS and what is not
Kernel – a part of OS: a program that is running all the time on
the computer
– Other programs are considered as system/application programs
6
OS Components
Process Management
Main-memory management
Secondary-storage management
File Management
I/O System Management
Protection System
Networking (Distributed Systems) – NOT in COMP2006
Command- interpreter System – NOT in COMP2006
7
OS Main Purposes
1) To provide an environment for a user to execute programs
conveniently and efficiently
– Users can be people, machines, or other computers
– Efficient operation of computer system is important in a large-expensive
(multi million Dollars) multi-user computer
– Convenience and efficiency are often contradictory
• Aims for convenience sacrifice efficiency
• In the past → efficiency is more important
• Now → users need more conveniences?
2) To simulate features not available on hardware
– OS provides features factually not provided by the hardware, e.g., virtual
disks, virtual memory, virtual machines, etc.
3) To control all computer resources and provide the base at which
the application programs can be written and run
8
Why do we study operating systems?
You may not be involved in creating / modifying operating
systems, however, most of your code runs on top of operating
systems
– Thus, good knowledge on how operating systems work will help you
write proper, efficient, effective, and secure programs.
Knowing the fundamentals of operating systems, how they
manage computer hardware, and what they provide to
applications is important to you who
– create / modify operating systems
– write programs that run on operating systems
– use operating systems
9
Abstract of Computer System
User 1 User 2 User 3 User n
Operating System
Computer Hardware
• CPU
• Memory
• Input/Output (I/O)
11
PC Motherboard (from Silberschatz, et al.)
12
Computer System Organization (cont. )
A general-purpose computer system contains:
– One or more Central Processing Unit (CPU): the ‘brain’ of the system
– Memory system
– Input/output (I/O) devices, e.g., disks, printers
CPU, I/O devices, and Memory unit are connected by a system bus
– All signals between them flow through the bus
Each device type (e.g., disk drive, video display, etc.) is managed by
a device controller that has a local buffer
– Each device controller has a device driver (part of OS)
– CPU moves data from/to main memory to/from local buffer
An I/O operation is from the device to the local buffer of a controller.
– Device controller informs CPU that it has finished its operation by causing an
interrupt
– I/O devices and CPU can execute concurrently
Access to memory is managed by a memory controller
13
Computer System Organization (cont. )
Computer needs an initial program (called bootstrap) to start
running when it is powered on, and rebooted
14
Computer System Organization (cont. )
The kernel runs all the time and provides services to the system
and its users
System programs are loaded and start running at boot time
System programs that are running are called system processes or system
daemon that run when the kernel is running
– In Linux, the first system process is called init or systemd that has
process ID or PID = 1.
It runs other daemons
– Once the system is fully booted, it waits for some events to occur
Thus, an operating system is interrupt-driven
The system knows when there occurs an event via interrupts either from
hardware or software (called trap or exception)
– Hardware generates interrupt by sending signal to CPU via bus
– Software sets interrupt using a system call (also called a monitor call)
15
Interrupt Mechanism
CPU stops what it is doing when it receives an interrupt
– It goes to a specific location to execute the interrupt service routine (ISR)
– The location is stored in an interrupt vector table
The table is in low memory area – a protected area that can be accessed only by OS
– Interrupt vector table contains the addresses/pointers of all ISRs
– Each interrupt is known by its interrupt number/vector
Interrupts with lower numbers have higher priority
CPU computes the location from the interrupt number
– E.g., multiply interrupt number with a fixed value, e.g., 4 → the starting address of ISR for interrupt
40 is 40 * 4 = 160
17
Interrupt Controller
Efficient interrupt handling is required for good system
performance.
– A server may handle hundreds of thousands of interrupts per second
Use interrupt-controller hardware
– It can defer interrupt handling during critical processing
– It provides efficient dispatch to interrupt handler
– It supports multi-level interrupts with priority
CPU can defer a low priority interrupt when a higher priority
interrupt is being served
– It is also possible to stop the execution of an interrupt to serve an incoming
higher priority interrupt
18
OS interaction with the interrupt mechanism
At boot time: OS probes the hardware buses to see what devices
are present and installs the corresponding interrupt-handlers into
the interrupt vector
During I/O: Various devices raise interrupts to indicate:
– They are ready to provide service
– They have completed output
– The input data are available, or a failure has been detected
During interaction with processes/programs
– Exceptions: e.g., divide by zero, accessing protected memory address,
page-fault, etc.
– System call implementation: it uses software interrupt or trap
Has lower priority than device interrupt
– Managing flow of control within the kernel
E.g., Copying data from kernel buffer to user buffer is not urgent, and thus can be done
when higher priority task, e.g., reading the data from disk to the kernel buffer is
completed 19
Storage Structure
Registers
Storage hierarchy:
Speed
Cost Primary
Cache
storage volatile
Volatility
Size/capacity
Main Memory
Nonvolatile memory
Secondary
storage
Hard-disk drives
Non-
volatile
Optical Disks
Tertiary
storage
Magnetic Tapes
20
Performance of various levels of storage
Figure 1.14 (Textbook, 10th edition)
21
Storage Structure (cont. )
CPU executes instructions only from a main memory
– Main memory: the only large storage media that CPU can access directly
Memory keeps both programs and data (von Neumann Architecture)
Execution cycle in Von Neumann Architecture
1) Fetch an instruction from memory and stores it in a register
The register is called instruction register (IR)
The address/pointer of the next instruction to be fetched/executed is stored in
the CPU’s Program Counter (PC)
2) Decode the instruction
3) If the instruction needs operands, fetch the operands from memory, and
store them in internal registers
4) Execute the instruction, and store the results in memory
5) Repeat the steps
22
Storage Structure (cont. )
General purpose computer uses rewritable memory for its main
memory, called random access memory (RAM)
– Usually implemented in dynamic RAM (DRAM)
Other type of memory: Read only Memory (ROM), cannot be changed, is used
to store static program, e.g., bootstrap
EEPROM: cannot be changed frequently, contains mostly static programs
Memory can be viewed as a one-dimensional array of bytes
– Its size is determined by the number of bits used to address each location
in the memory
Each byte has an address; the address starts from 0 to 2n – 1, for n bit address
E.g., address with 32 bits means the size of memory is 232 = 4 Giga bytes
Note: one word is the CPU’s native unit of data. A computer with 64-bit registers
usually has 64-bit words. A computer usually executes intructions with operands of size
words, rather than bytes.
23
Storage Structure (cont. )
Unit standard by the International Electrotechnical Commission (IEC)
Binary Decimal
210=1024 KiB kibibyte 1000 KB kilobyte
Note: The textbook and this slide uses 210 bytes = KB, 220 bytes = MB, etc.
24
Storage Structure (cont. )
Secondary storage: extension of main memory
– It provides non-volatile storage and large capacity
– The most common secondary storage is hard disk
Volatile storage loses its contents when it is not powered
Registers, cache and main memory are volatile
Solid state disk, magnetic disk, optical disk and magnetic tape are non-
volatile
– Solid state disk uses DRAM in normal operation and stores its contents to a
hidden magnetic disk when it is powered off
– Non-volatile RAM (NVRAM) uses DRAM with backup battery
Cache and registers are smaller but faster storage
– CPU has a limited number of internal registers (access time: 0.25-0.5 ns)
and limited sized cache (access time: 0.5 – 25 ns)
25
Storage Structure (cont.)
Magnetic disk: rigid metal or glass platters covered with magnetic recording
material
– Disk surface is logically divided into circular tracks, which are subdivided into
sectors
The set of tracks that are in one arm position forms a cylinder
A disk uses its disk head to read/write stored data
– Disk speed has two parts:
Transfer rate: the rate at which data flow between the drive and computer (in
MB per second).
Random access time = seek time + rotational latency (in millisecond)
– Seek time: the time to move the disk arm to the desired cylinder
– Rotational latency: the time for the desired sector to rotate to the disk head
Magnetic tape: its access time is about a thousand times slower than magnetic
disk
– It is mainly used for backup – tertiary storage
26
Disk Mechanism
Figure 11.1 (Textbook, 10th edition)
27
I/O - Basic concepts
An I/O device communicates with a computer system by sending signals over a
cable or wireless via a port
– CPU communicates with the controller by reading and writing bit patterns in these
registers using special I/O instructions
28
I/O Structure
Figure 13.1 (Textbook)
29
Some I/O Port addresses
Fig. 12.2 (Textbook, 10th edition)
31
Basic interrupt I/O mechanism
1. Device controller raises an interrupt by asserting a signal on the
CPU’s interrupt request line
32
Interrupt-drive I/O cycle
CPU I/O controller
1
Device driver initiates I/O
Initiates I/O
2
CPU executing
CPU checks for interrupts between
3
instructions
6
7
CPU resumes processing of
interrupted task
33
Direct memory access (DMA)
DMA is used to improve the performance for large data transfers.
– Use a special-purpose processor (DMA controller)
– DMA bypasses CPU to transfer data directly between I/O device and memory
– Concurrently, CPU goes on with other work
After finishing the entire transfer, DMA controller interrupts the CPU
– Thus, DMA uses only one interrupt for the whole data transfer
34
Steps in DMA Transfer
Fig. 13.5 (textbook)
35
Computer-System Architecture
36
Multiprocessor (cont. )
37
Multiprocessor (cont.)
Two types of multiprocessor system:
– Asymmetric multiprocessing
– Symmetric multiprocessing (SMP)
Asymmetric multiprocessing:
– Use a master processor to schedule and allocate work to (slave) processors
– Each slave processor waits for instruction from the master or has predefined
task
– More common in extremely large systems
38
Multiprocessor Systems (cont. )
39
Multiprocessor Systems (cont. )
41
Time-Sharing/Multitasking Systems
Goal: to provide interactive use of computer system at reasonable
cost
It is a variant of multiprogramming where user input is from on-
line terminal
CPU is multiplexed among jobs in memory frequently (1 sec?)
so that users can interact with their running program.
It needs these additional OS features:
– On-line communication between users and system
When OS finishes the execution of one command, it seeks the next control
statement from the user’s keyboard
– On-line file system, for users to access data and code
Today, most systems provide both batch processing and time
sharing
42
Real-time Systems
It has well-defined fixed-time constraint –
– The system is functional only if it completes within the time constraint
Hard real-time system
❖ It guarantees that tasks complete on time
❖ It is often used as a control device in a dedicated application
❖ E.g., controlling scientific experiments, medical imaging systems, industrial
control systems, and some display systems
❖ It has limited or no secondary storage
❖ data is stored in short-term memory, or in ROM.
Soft real-time system
❖ It sets a critical-time task higher priority over others until it completes
❖ It is useful in applications (multimedia)
43
Virtualization
Virtualization technology allows an OS to run as applications on
other OS
– It is a software that includes an emulation that is used when the type of
source CPU is different from that of the target CPU
e.g., it allows applications compiled for an IBM power CPU to run on Intel CPU
44
OS Operation
In multiprogramming, OS must ensure that an incorrect (or malicious) program
cannot cause other programs to execute incorrectly
Many programming errors are detected by the hardware, and the errors are
normally handled by the OS
Dual-mode operation
Hardware provides at least two modes of operations:
45
OS Operation (cont.)
Dual-mode operation (cont.)
The hardware provides some privileged instructions, i.e., machine
instructions that may cause harm
– E.g., the MSB of the machine code of the instruction is a bit ‘0’
A privileged instruction can be executed only in monitor mode
At system boot-time the hardware starts in monitor mode: OS is loaded
OS starts user processes in user mode
– A user process could never gain control of the computer in monitor mode
When an interrupt or fault occurs hardware switches to monitor mode
monitor user
Interrupt/fault
46
OS Operation (cont.)
I/O
All I/O instructions are privileged instructions
How does the user program perform I/O ? Use system call
– A system call usually takes the form of a trap to a specific location in the interrupt
vector
– Control passes through the interrupt vector to an interrupt service routine in the
OS, and the mode bit is set to a monitor mode.
– The monitor verifies that the parameters are correct and legal, executes the request,
and returns control to the instruction following the system call.
47
OS Operation (cont.)
Memory
OS must provide memory protection at least for the interrupt vector and the
Interrupt Service Routine
– Only OS can access these memory areas.
Two registers can be used to determine the range of legal addresses each
program can access:
– Base register: It holds the starting address of physical memory address for the
program
– Limit register: It contains the size of the memory range for the program
Memory area outside the defined range for a program is protected, i.e., the
program is not allowed access
– OS has unrestricted access to monitor and user memory
Instructions to load base and limit registers are privileged instructions. Why?
48
OS Operation (cont.)
CPU
OS must prevent one user program using CPU all the time
– E.g., due to program getting stuck in an infinite loop, or non-fair users
49
System Calls
A system call is a user interface to OS services
– It is used to ask the OS to execute some instructions that can be run only in monitor mode, e.g.,
I/O instruction
System call is available in assembly-language or in high level languages, e.g., C
– E.g., fork () is a system call to ask OS to create a new process
Application Program Interface (API): a set of functions available to an application
programmer
– An API typically invokes some system calls for the application programmers
– Examples of API: Windows API, POSIX API, Java API
– API is more portable and simpler to use
50
System Calls (cont. )
A system call is known by its number
– System call interface maintains a vector table which stores pointers to system call routines
System call interface intercepts each API and calls all corresponding system calls to handle the API
System call result/termination:
– Normal termination (exit or return)
– Terminate current program and return to the command interpreter
– Abnormal termination (trap) ⎯ program error
1) Process control
– End, abort (running program); Load, execute; Create, terminate; Signal event, etc.
– Examples: fork(), exit(), wait(), etc.
2) File management
– Create, delete; Open, close; Read, write, reposition; Get and set file attributes, etc.
– Examples: open(), read(), write(), close(), etc.
3) Device management (memory, tape drives etc.)
– Request device; Release device; Read, write, reposition, etc.
– Examples: ioctl(), read(), write(), etc.
4) Information maintenance
– Get or set time or date; Get or set process attributes, etc.
– Examples: getpid(), alarm(), sleep(), etc.
5) Communications
– Create, delete communication connection; Send and receive messages, etc.
– Examples: pipe(), shmget(), mmap(), etc.
6) Protection
– Resource access control
– Examples: chmod(), umask(), chown(), etc.
52
System programs or utilities
Provide a convenient environment for program development and execution
– Most users’ view of the OS is defined by system programs, not the actual system calls
– Some of them are simple user interface to system calls
53
OS Structures
A system as large and complex as a modern OS must be designed carefully
such that it can function properly and be modified easily
Various structures:
– Simple Structure/Monolithic
– Layered Approach
– Microkernels
– Modules
54
Monolithic
(the users) • The original Unix was limited
•Shells and commands by hardware functionality
•Compilers and interpreters
• Put all kernel functionality
•System libraries
into a single static binary file
System call interface to the kernel
that runs in kernel mode
Signals terminal File system CPU scheduling
• The UNIX OS consists of two
handling Swapping block I/O Page replacement
separable parts:
Character I/O system Disk/tape drivers Demand paging
Terminal drivers Virtual memory
• System programs
• Kernel
Kernel interface to the hardware
Terminal controllers Device controllers Memory controllers
Terminals Disks and tapes Physical memory
Kernel consists of everything below the system-call interface and above the physical
hardware
– It provides the file system, CPU scheduling, memory management, and other operating-system
functions
– Disadv: it contains many functions in one level - difficult to implement, maintain, and extend
– Adv: little overhead in system call, and fast communication within kernel processes
55
Layered approach
OS is divided into a several layers (levels) Six layers
– Each layer is built on top of lower layers
– The bottom layer (layer 0) is the hardware, and the
Layer 5: User programs
highest (layer N) is the user interface Layer 4: Buffering for input and
– It was first used in the THE (Technische Hogeschool output devices
Eindhoven) operating system
Layer 3: Operator-console device
Layers are selected such that each layer uses
driver
functions (operations) and services of only
lower-level layers Layer 2: Memory management
Advantage: Modularity - easy to debug Layer 1: CPU scheduling
Disadvantage: less efficient, hard to define
level functionality Layer 0: Hardware
56
Microkernel
It has smaller-sized kernel
– It provides minimal services such as process and memory management, and communication
– It puts non-essential components of kernel as system/user programs
The kernel functions as a communication facility between a client and the OS
services using message passing
Advantages:
Easier to extend OS
More portable
Disadvantages:
Using message passing increases system function overhead, and thus reduces
performance
57
Microkernel (illustration from textbook)
58
Modules
Modular kernel
– It uses Object-oriented programming techniques
The kernel has a set of core components
– Additional modules (e.g., Scheduling, device drivers, file systems) can be added during
boot/run time to kernel
It is like a layered system but more flexible
– Any module can call any other module
It is like a microkernel but more efficient
– Modules do not need message passing
59
Linux System Structure (from textbook)
• Hybrid structure: monolithic plus modular design
➢ It uses glibc to communicate with the system call interface
60