ECE: Introduction To Computer Systems Instructor: Maria Striki Spring 2021
ECE: Introduction To Computer Systems Instructor: Maria Striki Spring 2021
ECE: Introduction To Computer Systems Instructor: Maria Striki Spring 2021
2
What is an Operating System
3
Operating System Definition
OS is a resource allocator
Manages all resources
Decides between conflicting requests for efficient and fair resource use
OS is a control program
Controls execution of programs to prevent errors and improper use of
the computer
Operating System Concepts – 9th Edition 1.4 Silberschatz, Galvin and Gagne ©2013
Quiz Question 1: What Is An Operating System?
Answer:
5
Quiz Question 1
Answer: d
6
Quiz Question 2
a) System calls
b) API
c) Library
d) Assembly instructions
Answer: 7
Quiz Question 2
a) System calls
b) API
c) Library
d) Assembly instructions
Answer: a 8
Quiz Question 3
a) DTrace
b) DLocate
c) DMap
d) DAdd
Answer: 9
Quiz Question 3
Which facility dynamically adds probes to a running system, both
in user processes and in the kernel?
a) DTrace
b) DLocate
c) DMap
d) DAdd
Answer: a
DTrace is a comprehensive dynamic tracing framework created by Sun Microsystems for
troubleshooting kernel and application problems on production systems in real time.
Originally developed for Solaris, then ported to several Unix-like systems. DTrace can be
used to get a global overview of a running system, such as: amount of memory, CPU time,
filesystem and network resources used by the active processes, a log of the arguments with
which a specific function is being called, or a10list of the processes accessing a specific file.
What Is An Operating System?
application (user)
operating system
hardware
1. A software layer between the hardware and the application programs/users which
provides a virtual machine interface: easy and safe.
2. A resource manager that allows programs/users to share the hardware resources:
fair and efficient. It controls execution of programs to avoid improper use of the
computer
3. A set of utilities to simplify application development and execution
4. A program that acts as an intermediary between a user of a computer and the
computer hardware
11
Operating System Definition (Cont.)
“The one program running at all times on the computer” is the kernel.
Everything else is either
a system program (ships with the operating system) , or
an application program.
Operating System Concepts – 9th Edition 1.12 Silberschatz, Galvin and Gagne ©2013
What are the goals of an Operating System?
13
Computer System Structure
Operating system
Controls and coordinates use of hardware among various
applications and users
Application programs – define the ways in which the system resources are
used to solve the computing problems of the users
Word processors, compilers, web browsers, database systems,
video games
Users
People, machines, other computers
14
Four Components of a Computer System
15
Why Do We Want An OS?
16
Mechanism And Policy
application (user)
hardware
Policies: the procedures that guide the selection of a certain course of action
among alternatives (e.g. the replacement policy for the buffer cache)
Want to separate mechanisms and policies as much as possible
Different policies may be needed for different operating environments
17
Examples of Various Levels of Abstraction
application (user)
hardware
18
System layers: interface, implementation, interface, ...
Parallel Applications
Abstractions for describing Abstractions for “Programming
concurrent, parallel, or describing model”
independent computation communication (way of thinking about
things)
Language or
library
Compiler and/or parallel runtime Primitives
/mechanisms
OS system
call API
Operating system
Hardware
Architecture
Micro-architecture (hardware implementation) (HW/SW boundary)
pthread_create()
x86-64
modern multi-core CPU
ISPC compiler
21
Basic Computer Structure and Machine
Abstraction of Module Functionalities
to the User
22
Basic computer structure
CPU Memory
memory bus
I/O bus
23
Computer System Organization
Computer-system operation
One or more CPUs, device controllers connect through common
bus providing access to shared memory
Concurrent execution of CPUs and devices competing for
memory cycles
Operating System Concepts – 9th Edition 1.24 Silberschatz, Galvin and Gagne ©2013
Virtual Machine Abstractions
26
Major Issues In OS Design
27
Storage Management
28
Storage Structure
Main memory – only large storage media that the CPU can
access directly
Random access
Typically volatile
30
Storage-Device Hierarchy
31
Caching
Only one interrupt is generated per block, rather than the one
interrupt per byte
33
How a Modern Computer Works
34
Memory Layout for Multiprogrammed System
Operating System Concepts – 9th Edition 1.35 Silberschatz, Galvin and Gagne ©2013
Architectural Refresher
36
Architectural Refresher
37
Topics
38
Conceptual Model
Addresses of
memory cells Memory contents
0
CPU
1
+ 2
- 3
4
"big byte array"
* 5
/ 6
7
8
9
39
Operating System Perspective
40
Fetch-Decode-Execute
42
Simple Fictitious Example Encoding
44
The Program Counter
45
The Program Counter
46
Conceptual Model
Memory
0 operator Instruction 0
CPU
1 operand 1 @ memory
+ 2 operand 2
address 0
Arithmetic - 3
4
destination
Units
* 5
Instruction 1
@ memory
/ 6 address 4
Program 7
4
Counter 8
9
47
Memory Indirection
48
Memory Indirection
50
Example: While Loop
Only memory operations possible in "register-only" machines are load from and store
to memory. Save instruction space too 52
Register Machine Model
Memory
0
CPU
1
Arithmetic Units +,-,*,/ 2
3
Logic Units <,>,!= 4
Program Counter 8 5
register 0 24 6
7
register 1 100 8
register 2 9
18
53
Typical Instructions
54
Abstracting the Machine
55
Abstracting the Machine
56
Virtual Machines vs. Operating Systems
OS OS OS
VM VM VM … App
OS
Machine
Rutgers University 57
Operating System Structure
Operating System Concepts – 9th Edition 1.58 Silberschatz, Galvin and Gagne ©2013
Process Management
A process is a program in execution. It is a unit of work within the system.
Program is a passive entity, process is an active entity.
Process needs resources to accomplish its task
CPU, memory, I/O, files, Initialization data
Process termination requires reclaim of any reusable resources
Rutgers University 60
Architecture Features for the OS
Rutgers University 61
Processor Modes
Rutgers University 62
Operating-System Operations
Interrupt driven (hardware and software).
If there are no processes to execute, no I/O devices to service, no
users to whom to respond, OS will sit quietly waiting for something
to happen.
Events are almost always signaled by the occurrence of an
interrupt or trap or exception.
Operating System Concepts – 9th Edition 1.63 Silberschatz, Galvin and Gagne ©2013
Operating-System Operations (cont.)
Dual-mode operation allows OS to protect itself and other system
components
User mode and kernel mode
Mode bit (kernel: 0 and user:1) 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
At system boot time, hardware starts in kernel mode. OS is loaded and starts
user applications in user mode.
Whenever a trap or interrupt occurs, the hardware changes the state of the
mode bit from 1 to 0 (from user mode to kernel mode).
Whenever the OS gains control of the computer, it is in kernel mode.
System always switches to user mode before passing control to a user
program.
Operating System Concepts – 9th Edition 1.64 Silberschatz, Galvin and Gagne ©2013
Operating-System Operations (cont.)
Ensure protection by designating some instructions as privileged.
Privileged instructions can be executed only in kernel mode.
If attempt to execute privileged instruction in user mode is made, the
hardware does not execute it, but treats it as illegal and traps it to OS.
Initial control resides in the OS, where instructions are executed in kernel
mode.
When control is given back to a user application, the mode is set to user
mode.
System calls provide the means for a user program to ask the OS to
perform tasks reserved for the OS on the user program’s behalf. A sys call
takes the form of a trap to a specific location in the interrupt vector.
Operating System Concepts – 9th Edition 1.65 Silberschatz, Galvin and Gagne ©2013
Transition from User to Kernel Mode
Operating System Concepts – 9th Edition 1.66 Silberschatz, Galvin and Gagne ©2013
Common Functions of Interrupts
The operating system preserves the state of the CPU by storing registers and
the program counter (PC).
Determines which type of interrupt has occurred: separate segments of code
determine what action should be taken for each type of interrupt
Operating System Concepts – 9th Edition 1.67 Silberschatz, Galvin and Gagne ©2013
I/O Structure
Version 1: After I/O starts, control returns to user program only upon I/O
completion
Wait instruction idles the CPU until the next interrupt
Wait loop (contention for memory access)
At most one I/O request is outstanding at a time, no simultaneous I/O
processing
Version 2: After I/O starts, control returns to user program without waiting
for I/O completion
System call – request to the OS to allow user to wait for I/O completion
Device-status table contains entry for each I/O device indicating its
type, address, and state
OS indexes into I/O device table to determine device status and to
modify table entry to include interrupt
Operating System Concepts – 9th Edition 1.68 Silberschatz, Galvin and Gagne ©2013
Simple Protection Scheme
All addresses < 100 are reserved for operating system use
Mode register provided
zero = CPU is executing the OS (in system mode)
one = CPU is executing in user mode
Hardware does this check:
On every fetch, if mode bit is 1 and address < 100, then do not execute
the instruction
When accessing operands, if mode bit is 1 and operand address < 100,
do not execute the instruction
Mode register can only be set if mode is 0
Rutgers University 69
Simple Protection Model
CPU Memory
Arithmetic Units +,-,*,/ 0
Logic Units <,>,!= OS
99
Program Counter 8 100
User
101
Registers 0-31
102
Mode register 0 103
104
105
106
Rutgers University 70
Fetch-decode-execute Revised
Fetch:
if (( the PC < 100) && ( the mode register == 1)) then
Error! User tried to access the OS
else
fetch the instruction at the PC
Decode:
if (( destination register == mode) && ( the mode register == 1)) then
Error! User tried to set the mode register
< more decoding >
Execute:
if (( an operand < 100) && ( the mode register == 1) then
error! User tried to access the OS
else
execute the instruction
Rutgers University 71
Exceptions
Rutgers University 72
Fetch-decode-execute with Exceptions
Fetch:
if (( the PC < 100) && ( the mode bit == 1)) then
set the PC = 60 60 is the well
set the mode = 0 known entry point
fetch the instruction at the PC for a memory
Decode: violation
if (( destination register == mode) && ( the mode register == 1)) then
set the PC = 64
set the mode = 0 64 is the well
goto fetch known entry point
< more decoding > for a mode
Execute: register violation
< check the operands for a violation>
Note: Execute phase must check both operands and check again when performing an indirect load
Rutgers University 73
Recovering from Exceptions
The OS can figure out what caused the exception from the entry point. But
how can it figure out where in the user program the problem was?
Solution: add another register, the PC’
When an exception occurs, save the current PC to PC’ before loading the PC
with a new value
OS can examine the PC' and perform some recovery action
Stop user program and print an error message: error at address PC'
Run a debugger
Rutgers University 74
Fetch-decode-execute with Exceptions & Recovery
Fetch:
if (( the PC < 100) && ( the mode bit == 1)) then
set the PC' = PC
set the PC = 60
set the mode = 0
Decode:
if (( destination register == mode) && ( the mode register == 1)) then
set the PC' = PC
set the PC = 64
set the mode = 0
goto fetch
< more decoding >
Execute:
…
Rutgers University 75
Traps
Rutgers University 76
Fetch-decode-execute with traps
Fetch:
if (( the PC < 100) && ( the mode bit == 1)) then
< memory exception>
Decode:
if (the instruction is a trap) then
set the PC' = PC
set the PC = 68
set the mode = 0
goto fetch
if (( destination register == mode) && ( the mode bit == 1)) then
< mode exeception >
Execute:
…
Rutgers University 77
Traps
How does the OS know which service the user program wants to invoke on a trap?
User program passes the OS a number that encodes which OS service is desired
Our example machine could include the trap service ID in the opcode itself.
Most real CPUs have a convention for passing the trap code in a set of registers
E.g. the user program sets register 0 with the trap code, then executes the trap instruction
Rutgers University 78
Refresher
79
Switching from User to Kernel Mode
How to "get back" to user mode and the user's code after a trap?
Set the mode register = 1 then set the PC? What happens then?
A: Exception? Trap? What number of instruction do we jump to?
Set the PC, then set the mode bit? What happens then?
A: Exception? Trap? What number of instruction do we jump to?
Rutgers University 80
Switching from User to Kernel Mode
How to "get back" to user mode and the user's code after a trap?
Option 1: Set the mode register = 1 then set the PC?
But after the mode bit is set to user, exception!
Option 2: Set the PC, then set the mode bit?
Jump to "user-land", still in kernel mode
Rutgers University 81
Interrupts
How can we force a the CPU back into system mode if the user
program is off computing something?
Solution: Interrupts
An interrupt is an external event that causes the CPU to jump to a
known address
Rutgers University 82
Summary 1: Hardware and Software Interrupts
Interrupts are signals sent to the CPU by external devices, normally I/O. They tell the
CPU to stop its current activities and execute the appropriate part of the OS. There
are 3 types of interrupts:
Hardware Interrupts are generated by h/w devices to signal they need attention
from the OS. They may have just received some data (keystrokes on the keyboard or
a piece of data on the Ethernet card); or they just completed a task which the OS
previously requested.
Software Interrupts (traps, exceptions) when they want to request a system call to
be performed by the OS.
Exceptions are generated by the CPU itself to indicate that some error or condition
occurred for which assistance from the OS is needed.
Traps are generated by program transferring data between hard drive and memory.
Interrupts give the user better control over the computer. Otherwise, a user may
have to wait for a given application to have a higher priority over the CPU to be ran.
This ensures that the CPU will deal with the process immediately.
Rutgers University 83
Summary 2: Hardware and Software Interrupts
Rutgers University 84
Summary 3: Interrupts
X86 Assembler example: It moves the system-call name into the EAX
register, the pointer to the parameters into the EBX register of the
CPU and then emits software interrupt no 42. The interrupt
handling will do the switch to kernel mode. The interrupt number is
looked up in the Interrupt Descriptor Table (IDT) and invokes the
function that's registered there, the syscall handler. This handler
executes in kernel mode. On return to the user mode the code will
move the content of EAX into the variable RET….
Rutgers University 85
The Clock
Rutgers University 86
Simple Interrupt Model
CPU Memory
Arithmetic Units +,-,*,/ OS
User
Logic Units <,>,!=
Program Counter 8
PC' Interrupt line Clock
Registers 0-31
Reset line
Mode register 0
Rutgers University 87
Fetch-decode-execute with Interrupts
Fetch:
if (the clock interrupt line == 1) then
set the PC' = PC
set the PC = 72
set the mode = 0
goto fetch
if (( the PC < 100) && ( the mode bit == 1)) then
< memory exception >
fetch next instruction
Decode:
if (the instruction is a trap) then
< trap exception >
if (( destination register == mode) && ( the mode bit == 1)) then
< mode exeception >
<more decoding>
Execute: …
Rutgers University 88
Entry Points
What are the "entry points" for our little example machine?
60: memory access violation
64: mode register violation
68: User-initiated trap
72: Clock interrupt
Each entry point is typically a jump to some code block in the OS
All real OS’es have a set of entry points for exceptions, traps and
interrupts
Sometimes they are combined and software has to figure out what happened.
Rutgers University 89
Saving and Restoring Context
Rutgers University 90
Questions for class (True-False)
Only a trap with mode bit set to 1 is the way to transition from
user to kernel mode to service a request.
Ans: T/F?
Rutgers University 91
Input and Output
Rutgers University 92
A Simple I/O device
Rutgers University 93
Basic Network I/O
CPU Memory
0
Arithmetic Units +,-,*,/
Logic Units <,>,!= 98 Transmit Reg.
Program Counter 8 99 Receive Reg.
Registers 0-31
Clock
Mode register 0 Interrupt line
Reset line
Rutgers University 94
Why Memory-Mapped Registers
Rutgers University 95
Homework:
When does the processor check whether an interrupt has occurred?
Why do we need to map the registers on Network Interface Cards
(NICs) to the main memory?
96
Status Registers
Rutgers University 97
Polled I/O
To Transmit:
While (status register bit 0 == 1); // wait for card to be ready
TX register = data;
Status reg = status reg | 0x1; // tell card to TX (set bit 0 to 1)
Naïve Receive:
While (status register bit 1 != 1); // wait for data to arrive
Data = RX register; // Are we correct? You check…
Status reg = status reg & 0x01; // tell card got data (clear bit 1)
Cant' stall OS waiting to receive!!! ; // Are we correct? You check…
Rutgers University 99
Polling vs. Interrupts
Size of the transfer becomes the key factor in when to use PIO
vs. DMA
Given:
A load costs 100 CPU "cycles“ (time units)
A store costs 50 CPU “cycles” (time units).
To process an interrupt costs 2000 instructions, each an average of 2 cycles
To send a packet via PIO costs 1 load + 1 store per byte
To send via DMA costs 4 loads + interrupt
Find the packet size where transmitting via DMA costs less CPU
cycles than PIO
Disk drives:
Present the CPU a linear array of fixed-sized blocks that are persistent
across power cycles
Network cards:
Allow the CPU to send and receive discrete units of data (packets) across
a wire, fiber or radio
Packet sizes 64-8000 bytes are typical
Graphics adapters:
Present the CPU with a memory that is turned into pixels on a screen
How does a machine start running the operating system in the first
place?
The process of starting the OS is called booting
Sequence of hardware + software event form the boot protocol
(3) The boot loader can then load the rest of the operating system
from disk. Note that this point the OS still is not running
The boot loader can know about multiple operating systems
The boot loader can know about multiple versions of the OS
112
Memory Management
Operating System Concepts – 9th Edition 1.113 Silberschatz, Galvin and Gagne ©2013
Storage Management
OS provides uniform, logical view of information storage
Abstracts physical properties to logical storage unit - file
Each medium is controlled by device (i.e., disk drive, tape drive)
Varying properties include access speed, capacity, data-transfer
rate, access method (sequential or random)
File-System management
Files usually organized into directories
Access control on most systems to determine who can access what
OS activities include
Creating and deleting files and directories
Primitives to manipulate files and directories
Mapping files onto secondary storage
Backup files onto stable (non-volatile) storage media
Operating System Concepts – 9th Edition 1.114 Silberschatz, Galvin and Gagne ©2013
Mass-Storage Management
Usually disks used to store data that does not fit in main memory or data that
must be kept for a “long” period of time
Proper management is of central importance
Entire speed of computer operation hinges on disk subsystem and its
algorithms
OS activities
Free-space management
Storage allocation
Disk scheduling
Operating System Concepts – 9th Edition 1.115 Silberschatz, Galvin and Gagne ©2013
Performance of Various Levels of Storage
Operating System Concepts – 9th Edition 1.116 Silberschatz, Galvin and Gagne ©2013
Computing Environments - Traditional
Operating System Concepts – 9th Edition 1.117 Silberschatz, Galvin and Gagne ©2013
Computing Environments - Mobile
Operating System Concepts – 9th Edition 1.118 Silberschatz, Galvin and Gagne ©2013
Computing Environments – Distributed
Distributed computiing
Collection of separate, possibly heterogeneous, systems
networked together
Network is a communications path, TCP/IP most common
– Local Area Network (LAN)
– Wide Area Network (WAN)
– Metropolitan Area Network (MAN)
– Personal Area Network (PAN)
Network Operating System provides features between
systems across network
Communication scheme allows systems to exchange
messages
Illusion of a single system
Operating System Concepts – 9th Edition 1.119 Silberschatz, Galvin and Gagne ©2013
Computing Environments – Client-Server
Client-Server Computing
Dumb terminals supplanted (superseded) by smart PCs
Many systems now provide servers, responding to requests
generated by clients
Compute-server system provides an interface to client to
request services (i.e., database)
File-server system provides interface for clients to store
and retrieve files
Operating System Concepts – 9th Edition 1.120 Silberschatz, Galvin and Gagne ©2013
Computing Environments - Peer-to-Peer
Operating System Concepts – 9th Edition 1.121 Silberschatz, Galvin and Gagne ©2013
Computing Environments - Virtualization
Operating System Concepts – 9th Edition 1.122 Silberschatz, Galvin and Gagne ©2013
Computing Environments - Virtualization
Operating System Concepts – 9th Edition 1.123 Silberschatz, Galvin and Gagne ©2013
Computing Environments - Virtualization
Operating System Concepts – 9th Edition 1.124 Silberschatz, Galvin and Gagne ©2013
Computing Environments – Cloud Computing
Operating System Concepts – 9th Edition 1.125 Silberschatz, Galvin and Gagne ©2013
Computing Environments – Cloud Computing
Operating System Concepts – 9th Edition 1.126 Silberschatz, Galvin and Gagne ©2013
Computing Environments – Real-Time Embedded Systems
Operating System Concepts – 9th Edition 1.127 Silberschatz, Galvin and Gagne ©2013