chapter 2 operating system concepts · 2019-03-11 · operating system concepts 2.33 southeast...
TRANSCRIPT
Southeast University 2.1
Chapter 2 Operating System Concepts
Operating System Concepts
张竞慧 办公室:计算机楼366室
电邮:[email protected] 主页:http://cse.seu.edu.cn/PersonalPage/zjh/
电话:025-52091017
Southeast University 2.2 Operating System Concepts
Outline
Four fundamental OS concepts Operating System Components Operating System Services System Calls System Structure Virtual Machines
Southeast University 2.3
Four fundamental OS concepts Thread
Single unique execution context Program Counter, Registers, Execution Flags, Stack
Address Space Programs execute in an address space that is distinct from the
memory space of the physical machine
Process An instance of an executing program is a process consisting of an
address space and one or more threads of control
Dual Mode operation/Protection Only the “system” has the ability to access certain resources The OS and the hardware are protected from user programs and
user programs are isolated from one another by controlling the translation from program virtual addresses to machine physical addresses
Southeast University 2.4
OS Bottom Line: Run Programs
Load instruction and data segments of executable file into memory
Create stack and heap “Transfer control to it” Provide services to it While protecting OS and it
int main() { … ; }
edito
r
com
pile
r
Program Source Executable
foo.c a.out
Load
&
Exe
cute
0x000…
0xFFF…
instructions
data
instructions
data
heap
stack
Memory
Processor
registers
PC:
OS
Southeast University 2.5
The instruction cycle
PC:
Registers
ALU
Instruction fetch
Decode
Execute
Memory
instruction
next
decode
data
Processor
Southeast University 2.6
Fetch Exec
R0 …
R31 F0 …
F30 PC
… Data1 Data0
Inst237 Inst236
… Inst5 Inst4 Inst3 Inst2 Inst1 Inst0
Addr 0
Addr 232-1
What happens during program execution?
Execution sequence: Fetch Instruction at PC Decode Execute (possibly using registers) Write results to registers/mem PC = Next Instruction(PC) Repeat
PC PC PC PC
Southeast University 2.7
First OS Concept: Thread of Control
Thread: Single unique execution context Program Counter, Registers, Execution Flags, Stack
A thread is executing on a processor when it is resident in the processor registers.
PC register holds the address of executing instruction in the thread.
Certain registers hold the context of thread Stack pointer holds the address of the top of stack
Other conventions: Frame Pointer, Heap Pointer, Data May be defined by the instruction set architecture or by
compiler conventions Registers hold the root state of the thread. The rest is “in memory”
Southeast University 2.8
Second OS Concept: Program’s Address Space
Address space ⇒ the set of accessible addresses + state associated with them: For a 32-bit processor there are 232 = 4
billion addresses
What happens when you read or write to an address? Perhaps Nothing Perhaps acts like regular memory Perhaps ignores writes Perhaps causes I/O operation
(Memory-mapped I/O)
Perhaps causes exception (fault)
0x000…
0xFFF…
code
Static Data
heap
stack
Southeast University 2.9
Address Space: In a Picture
Processor registers
PC:
0x000…
0xFFF…
Code Segment
Static Data
heap
stack
instruction SP:
What’s in the code segment? Data? What’s in the stack segment? How is it allocated? How big is it?
What’s in the heap segment? How is it allocated? How big?
Southeast University 2.10
Multiprogramming - Multiple Threads of Control
OS
Proc 1
Proc 2
Proc n …
code
Static Data
heap
stack
code
Static Data
heap
stack
code
Static Data
heap
stack
Southeast University 2.11
How can we give the illusion of multiple processors?
vCPU3 vCPU2 vCPU1
Shared Memory Assume a single processor. How do we provide the illusion
of multiple processors? Multiplex in time!
Each virtual “CPU” needs a structure to hold: Program Counter (PC), Stack Pointer (SP) Registers (Integer, Floating point, others…?)
How switch from one virtual CPU to the next? Save PC, SP, and registers in current state block Load PC, SP, and registers from new state block
What triggers switch? Timer, voluntary yield, I/O, other things
vCPU1 vCPU2 vCPU3 vCPU1 vCPU2
Time
Southeast University 2.12
Third OS Concept: Process Process: execution environment with Restricted Rights
Address Space with One or More Threads Owns memory (address space) Owns file descriptors, file system context, … Encapsulate one or more threads sharing process resources
Why processes? Protected from each other! OS Protected from them Navigate fundamental tradeoff between protection and efficiency Processes provides memory protection Threads more efficient than processes (later)
Application instance consists of one or more processes
Southeast University 2.13
Protection Operating System must protect itself from user programs Reliability: compromising the operating system generally
causes it to crash Security: limit the scope of what processes can do Privacy: limit each process to the data it is permitted to access Fairness: each should be limited to its appropriate share
It must protect User programs from one another Primary Mechanism: limit the translation from program address
space to physical memory space Can only touch what is mapped in
Additional Mechanisms: Privileged instructions, in/out instructions, special registers syscall processing, subsystem implementation (e.g., file access rights, etc)
Southeast University 2.14
Fourth OS Concept: Dual Mode Operation
Hardware provides at least two modes: “Kernel” mode (or “supervisor” or “protected”) “User” mode: Normal programs executed
What is needed in the hardware to support “dual mode” operation? a bit of state (user/system mode bit) Certain operations / actions only permitted in
system/kernel mode In user mode they fail or trap
User->Kernel transition sets system mode AND saves the user PC Operating system code carefully puts aside user state then
performs the necessary operations Kernel->User transition clears system mode AND restores
appropriate user PC return-from-interrupt
Southeast University 2.15
For example: UNIX System Structure
User Mode
Kernel Mode
Hardware
Applications
Standard Libs
Southeast University 2.16
User/Kernal Mode
User Mode
Kernel Mode
Full HW access Limited HW access
exec
syscall
exit rtn
interrupt
rfi
exception
Southeast University 2.17
3 types of Mode Transfer Syscall Process requests a system service, e.g., exit Like a function call, but “outside” the process Like a Remote Procedure Call (RPC) – for later
Interrupt External asynchronous event triggers context switch eg. Timer, I/O device Independent of user process
Trap or Exception Internal synchronous event in process triggers context switch e.g., Protection violation (segmentation fault), Divide by zero,
… All 3 are an UNPROGRAMMED CONTROL TRANSFER Where does it go?
Southeast University 2.18
How do we get the system target address of the “unprogrammed
control transfer?”
Southeast University 2.19
Interrupt Vector
Where else do you see this dispatch pattern?
interrupt number (i)
intrpHandler_i () { …. }
Address and properties of each interrupt handler
Southeast University 2.20
Simple B&B: User => Kernel
OS
Proc 1
Proc 2
Proc n …
code
Static Data
heap
stack
code
Static Data
heap
stack
code
Static Data
heap
stack
0000…
FFFF…
1000…
1100…
3000…
3080…
Base 1000 …
1100… Bound
xxxx… uPC
regs
sysmode
…
0
PC
0000…
FFFF…
00FF… How to return to
system?
0000 1234
Southeast University 2.21
Simple B&B: Interrupt
OS
Proc 1
Proc 2
Proc n …
code
Static Data
heap
stack
code
Static Data
heap
stack
code
Static Data
heap
stack
0000…
FFFF…
1000…
1100…
3000…
3080…
Base 1000 …
1100 … Bound
0000 1234 uPC
regs
sysmode
…
1
PC
0000…
FFFF…
00FF… How to save
registers and set up system stack?
IntrpVector[i]
Southeast University 2.22
Simple B&B: Switch User Process
OS
Proc 1
Proc 2
Proc n …
code
Static Data
heap
stack
code
Static Data
heap
stack
code
Static Data
heap
stack
0000…
FFFF…
1000…
1100…
3000…
3080…
Base 3000 …
0080 … Bound
0000 0248 uPC
regs
sysmode
…
1
PC
0000…
FFFF…
00D0… How to save
registers and set up system stack?
0001 0124
1000 …
1100 …
0000 1234
regs 00FF…
RTU
Southeast University 2.23
Simple B&B: “resume”
OS
Proc 1
Proc 2
Proc n …
code
Static Data
heap
stack
code
Static Data
heap
stack
code
Static Data
heap
stack
0000…
FFFF…
1000…
1100…
3000…
3080…
Base 3000 …
0080 … Bound
xxxx xxxx uPC
regs
sysmode
…
0
PC
0000…
FFFF…
00D0… How to save
registers and set up system stack?
000 0248
1000 …
1100 …
0000 1234
regs 00FF…
RTU
Southeast University 2.24
What’s wrong with this simplistic address translation mechanism?
Fragmentation: Kernel has to somehow fit whole processes into
contiguous block of memory After a while, memory becomes fragmented!
Southeast University 2.25
Virtual Address Translation
Simpler, more useful schemes too! Give every process the illusion of its own BIG
FLAT ADDRESS SPACE Break it into pages More on this later
Southeast University 2.26
Providing Illusion of Separate Address Space: Load new Translation Map on Switch
Prog 1 Virtual
Address Space 1
Prog 2 Virtual
Address Space 2
Code Data Heap Stack
Code Data Heap Stack
Data 2
Stack 1
Heap 1
OS heap & Stacks
Code 1
Stack 2
Data 1
Heap 2
Code 2
OS code
OS data Translation Map 1 Translation Map 2
Physical Address Space
Southeast University 2.27 Operating System Concepts
Outline
Four fundamental OS concepts Operating System Components Operating System Services System Calls System Structure Virtual Machines
Southeast University 2.28 Operating System Concepts
Common Operating System Components
Process Management Main Memory Management File Management I/O System Management Secondary-Storage Management Protection System Command-Interpreter System
Southeast University 2.29 Operating System Concepts
Process Management A process is a program in execution.
The operating system is responsible for the following activities in connection with process management. Process creation and deletion. process suspension and resumption. Provision of mechanisms for: process synchronization process communication Deadlock handling
Southeast University 2.30 Operating System Concepts
Main-Memory Management
Memory is a large array of words or bytes, each with its own address. It is a repository of quickly accessible data shared by the CPU and I/O devices.
Main memory is a volatile storage device. It loses its contents in the case of system failure.
Southeast University 2.31 Operating System Concepts
Main-Memory Management (Cont.)
The operating system is responsible for the following activities in connections with memory management: Keep track of which parts of memory are
currently being used and by whom. Decide which processes to load when
memory space becomes available. Allocate and deallocate memory space as
needed.
Southeast University 2.32
Virtual Memory
Virtual memory allows programs to address memory from a logical point of view Without regard to the limits of physical memory
Southeast University 2.33 Operating System Concepts
File Management
There are different types of physical media to store information. Each of them has its own characteristics and physical organization
Operating System provides a uniform logical view of information storage, i.e., file.
A file is a collection of related information defined by its creator. Commonly, files represent programs (both source and object forms) and data.
Southeast University 2.34 Operating System Concepts
File Management
The operating system is responsible for the following activities in connections with file management: File creation and deletion. Directory creation and deletion. Support of primitives for manipulating files and
directories. Mapping files onto secondary storage. File backup on stable (nonvolatile) storage
media.
Southeast University 2.35
Figure 1-15. (a) Before mounting, the files on the CD-ROM are not accessible. (b) After mounting, they are part of the file hierarchy.
Files
Southeast University 2.36
Example: Mounting iso file under Linux
Operating System Concepts
mkdir -p /mnt/disk mount -o loop disk1.iso /mnt/disk umount /mnt/disk
Southeast University 2.37 Operating System Concepts
Secondary-Storage Management
Since main memory (primary storage) is volatile and too small to accommodate all data and programs permanently, the computer system must provide secondary storage to back up main memory.
Most modern computer systems use disks as the principle on-line storage medium, for both programs and data.
Southeast University 2.38 Operating System Concepts
Secondary-Storage Management (Cont.)
The operating system is responsible for the following activities in connection with disk management: Free space management Storage allocation Disk scheduling
Southeast University 2.39 Operating System Concepts
I/O System Management
The I/O subsystem consists of: A buffer-caching system A general device-driver interface Drivers for specific hardware devices
Southeast University 2.40 Operating System Concepts
Protection System Protection refers to a mechanism for
controlling access by programs, processes, or users to both system and user resources.(take file access as example)
The protection mechanism must: distinguish between authorized and unauthorized
usage. specify the controls to be imposed and means for
enforcement.
Southeast University 2.41 Operating System Concepts
Command-Interpreter System Many commands are given to the operating
system by control statements which deal with: process creation and management I/O handling secondary-storage management main-memory management file-system access protection networking
Southeast University 2.42 Operating System Concepts
Command-Interpreter System (Cont.)
The program that reads and interprets control statements is called variously:
command-line interpreter shell (in UNIX)
Its function is to get and execute the next
command statement.
Southeast University 2.43 Operating System Concepts
Outline
Four fundamental OS concepts Operating System Components Operating System Services System Calls System Structure Virtual Machines
Southeast University 2.44 Operating System Concepts
Core Operating System Services
Program execution – system capability to load a program into memory and to run it.
I/O operations – since user programs cannot execute I/O operations directly, the operating system must provide some means to perform I/O.
File-system manipulation – program capability to read, write, create, and delete files.
Communications – exchange of information between processes
Error detection – ensure correct computing by detecting errors in the CPU and memory hardware, in I/O devices, or in user programs.
Southeast University 2.45 Operating System Concepts
Additional Operating System Functions
Additional functions exist not for helping the user, but rather for ensuring efficient system operations. • Resource allocation – allocating resources to multiple
users or multiple jobs running at the same time. • Accounting – keep track of and record which users
use how much and what kinds of computer resources for account billing or for accumulating usage statistics.
• Protection – ensuring that all access to system resources is controlled.
Southeast University 2.46 Operating System Concepts
Outline
Four fundamental OS concepts Operating System Components Operating System Services System Calls System Structure Virtual Machines
Southeast University 2.47 Operating System Concepts
System Calls
System calls provide the interface between a running program and the operating system. Generally available as assembly-language
instructions. Languages defined to replace assembly
language for systems programming allow system calls to be made directly (e.g., C, C++)
Southeast University 2.48 Operating System Concepts
Southeast University 2.49 Operating System Concepts
Example of System Calls System call sequence to copy the contents of
one file to another file
Southeast University 2.50 Operating System Concepts
System Call Implementation Typically, a number associated with each
system call System-call interface maintains a table indexed
according to these numbers The system call interface invokes intended
system call in OS kernel and returns status of the system call and any return values
Southeast University 2.51 Operating System Concepts
Use of A System Call to Perform I/O
Southeast University 2.52 Operating System Concepts
System Call Implementation (Cont.)
The caller need know nothing about how the system call is implemented Just needs to obey API and understand what OS
will do as a result call Most details of OS interface hidden from
programmer by API Managed by run-time support library (set of functions
built into libraries included with compiler)
Southeast University 2.53 Operating System Concepts
API – System Call – OS Relationship
Southeast University 2.54 Operating System Concepts
Standard C Library Example
C program invoking printf() library call, which calls write() system call
Southeast University 2.55 Operating System Concepts
System Call Parameter Passing
Often, more information is required than simply identity of desired system call Exact type and amount of information vary
according to OS and call Three general methods used to pass
parameters to the OS
Southeast University 2.56 Operating System Concepts
System Call Parameter Passing (Cont.)
Simplest: pass the parameters in registers In some cases, may be more parameters than
registers Parameters stored in a block, or table, in
memory, and address of block passed as a parameter in a register This approach taken by Linux and Solaris
Parameters placed, or pushed, onto the stack by the program and popped off the stack by the operating system
Block and stack methods do not limit the number or length of parameters being passed
Southeast University 2.57
System Calls for the Linux 2.2 Kernel
On the left are the numbers of the system calls. This number will be put in register %eax.
On the right are the types of values to be put into the remaining registers before calling the trap 'int 0x80'.
Operating System Concepts
Southeast University 2.58
Figure 1-17. The 11 steps in making the system call read(fd, buffer, nbytes).
System Calls
Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
Southeast University 2.59
Procedure Call and Stack
Southeast University 2.60 Operating System Concepts
Types of System Calls
Process control File management Device management Information maintenance Communications
Southeast University 2.61
System Calls for Process Management
Some of the major POSIX system calls for process management
Southeast University 2.62
Some of the major POSIX system calls for file management
System Calls for File Management (1)
Southeast University 2.63
Some of the major POSIX system calls for file management
System Calls for File Management (2)
Southeast University 2.64
Figure 1-18. Some of the major POSIX system calls.
Miscellaneous System Calls
Southeast University 2.65 Operating System Concepts
Outline
Four fundamental OS concepts Operating System Components Operating System Services System Calls System Structure Virtual Machines
Southeast University 2.66 Operating System Concepts
UNIX UNIX – limited by hardware functionality, the
original UNIX operating system had limited structuring. The UNIX OS consists of two separable parts Systems programs The kernel Consists of everything below the system-call interface
and above the physical hardware Provides the file system, CPU scheduling, memory
management, and other operating-system functions; a large number of functions for one level
Southeast University 2.67
Description of UNIX
Southeast University 2.68 Operating System Concepts
UNIX System Structure
Southeast University 2.69
Microkernel Architecture
Most early OS are a monolithic kernel Most OS functionality resides in the kernel.
A microkernel assigns only a few essential functions to the kernel Address spaces Interprocess communication (IPC) Basic scheduling
Southeast University 2.70 Operating System Concepts
Microkernel System Structure
Moves as much from the kernel into “user” space
Communication takes place between user modules using message passing
Southeast University 2.71 Operating System Concepts
Microkernel System Structure (Cont.)
Benefits: Easier to extend a microkernel Easier to port the operating system to new
architectures More reliable (less code is running in kernel
mode) More secure
Detriments: Performance overhead of user space to kernel
space communication
Southeast University 2.72 Operating System Concepts
Modules
Most modern operating systems implement kernel modules Uses object-oriented approach Each core component is separate Each talks to the others over known interfaces Each is loadable as needed within the kernel
Overall, similar to layers but with more flexible
Southeast University 2.73
Modular Monolithic Kernel
Although monolithic, the kernel is structures as a collection of modules Loadable modules An object file which can be linked and unlinked at
run time Characteristics: Dynamic Linking Stackable modules
Southeast University 2.74
Linux Kernel Components
Southeast University 2.75 Operating System Concepts
Outline
Four fundamental OS concepts Operating System Components Operating System Services System Calls System Structure Virtual Machines
Southeast University 2.76 Operating System Concepts
Virtual Machines
A virtual machine takes the layered approach to its logical conclusion. It treats hardware and the operating system kernel as though they were all hardware
A virtual machine provides an interface identical to the underlying bare hardware
The operating system creates the illusion of multiple processes, each executing on its own processor with its own (virtual) memory
Southeast University 2.77 Operating System Concepts
Virtual Machines (Cont.)
The resources of the physical computer are shared to create the virtual machines CPU scheduling can create the appearance that
users have their own processor Spooling and a file system can provide virtual
card readers and virtual line printers A normal user time-sharing terminal serves as
the virtual machine operator’s console
Southeast University 2.78 Operating System Concepts
Virtual Machines (Cont.)
Non-virtual Machine Virtual Machine
Southeast University 2.79 Operating System Concepts
Virtual Machines (Cont.)
The virtual-machine concept provides complete protection of system resources since each virtual machine is isolated from all other virtual machines. This isolation, however, permits no direct sharing of resources.
Southeast University 2.80 Operating System Concepts
Virtual Machines (Cont.)
A virtual-machine system is a perfect vehicle for operating-systems research and development. System development is done on the virtual machine, instead of on a physical machine and so does not disrupt normal system operation.
The virtual machine concept is difficult to implement due to the effort required to provide an exact duplicate to the underlying machine
Southeast University 2.81 Operating System Concepts
VMware Architecture
Southeast University 2.82
Homework 1
VMware虚拟机Player: http://pan.baidu.com/s/1gdq7MBT
Fedora 7虚拟机镜像: http://pan.baidu.com/s/1dD76SQt
Operating System Concepts