Gentle Introduction to Operating System
Anthony K. H. Tung(鄧锦浩)Associate Professor
Department of Computer Science
http://www.comp.nus.edu.sg/~atunghttp://www.renren.com/profile.do?id=313870900&_ua_flag=93w
Objectives Operating System in SOC is a second year
course(CS2106) which have prerequisites of either CS1104/ CS2100 : Computer Organization EE2007: Microprocessor Systems
Aim of this talk: Provide some hardware background so that you
can understand OS better Explain “why” instead of just “how” and
“what”? Provide a framework that can merge different
concepts together
Approach We will design an OS together, starting from
a simple one and finally to a complex one: Single user, single process Single user, multiple processes Multiple users, multiple processes
While designing, we will take into consideration various desiderata Efficiency Robustness to changes Robustness to errors Consistency Isolation
What is OS?
As the name implied, “operating system” is a software system that operate something? What is that “something”?
“something”=hardware Why do we need to put in a software
layer that operate the hardware? To appreciate something, we need to
ask ourselves what is something do not exist…
In the beginning…
There is no such thing as an OS, each user write their own program to control the hardware which have their own control code or instruction set
Example: hard disk Have many tracks Each track have many
sectors Data are stored in each
sector as binary bits1010010100….
Eg. of Instruction Set for Hard Disk
Physically the disk is control by electronic circuit
instruction sector no. memory loc. 1 0 0 1 0 1 1 1 0 1 1 0
Code Description Binary
00 Reset Disk Drives 00
01 Check Drive Status 01
02 Read Sectors From Drive 10
03 Write Sectors To Drive 11
Storing Data as Files When there was no OS(i.e. no fgets,
fopen…), programmers have to organize the sectors into files in their programs
Example: Use the first sector as file directory and indicate what are the sectors use to store data for each file. Figure on the right show file directory for two files, “numbers” and “letters”
“numbers”, 2, 5, 8, -1“letters”, 3, 6, 1023, -1 . . .
20, 4, 100, …………
A, Z, B, H, ……….
900, 1000, 50, …………
B, C, I, A, ……….
54, 20, 10,………….
O, R, Q, G …………
Sector 2
Sector 3
Sector 4
Sector 5
Sector 6
Sector 7
Sector 8
Sector 9
Sector 1023
Sector 1024
Sector 1
filename
End marker
File directory sector
.
.
Storing Data as Files(II)
How to read the file “numbers”?
“numbers”, 2, 5, 8, -1“letters”, 3, 6, 1023, -1 . . .
20, 4, 100, …………
A, Z, B, H, ……….
900, 1000, 50, …………
B, C, I, A, ……….
54, 20, 10,………….
O, R, Q, G …………
Sector 2
Sector 3
Sector 4
Sector 5
Sector 6
Sector 7
Sector 8
Sector 9
Sector 1023
Sector 1024
Sector 1
filename
End marker
Step 1: 10, Sector 1, Mem. Location 20 // Read Sector 1 // into memory location 20Step 2: Search location 20 for filename=“numbers”;Step 3: sector_num = first sector of “numbers”;Step 3: While (sector_num<>-1) doStep 4: { 10, Sector sector_num, Mem. Location 21;Step 5: process data in mem. Location 21;Step 6: }
Note: “10” is the hardware code for reading a sector of the disk
.
.
What is the problem of such an approach?
Difficulty in programming. Have to remember the command code and repeat the same thing in every program.
Different programmers might implement the file differently. What if one of them resign?
Not robust to change: What if a different brand of disk with different instruction code is used? i.e. “01” is used to read a sector instead?
Having an OS avoid these problems
In its most basic form, OS provide a list of routines or services that are called by application programs to perform certain tasks through software interrupts
Details of such call involve three concepts: Program Counter (PC) Interrupt Vector Table
(IVT) Context Switch
Open File
Close File
Get System Data
Get/Set File Attribute
Read file
Write file
Allocate Memory
Write to Screen
Write to Printer
. .For (i=0;i<10;i++) write file(“numbers”,…) . . .
0001-005
0021-0028
0029-0035
0036-0042
0043-0055
0056-0068
0069-0080
0036-0042
Memory address
0006-0020
1000-2000
OS Services
ApplicationProgram
Program Counter (PC)
The program counter(PC) is a register that store the address of the next instruction to be executed by the CPU
int i=5, j=6, k=7;
i=i+1;
j=j+2;
k=i+j;
i=0;
end
1000
1002
1003
1004
1005
1006
1007
1008
Memory address
1001
Program
variables
ij
kPC: 1000
CPU:
At the start, PC point to start of program
Program Counter (PC)
The program counter(PC) is a register that store the address of the next instruction to be executed by the CPU
int i=5, j=6, k=7;
i=i+1;
j=j+2;
k=i+j
i=0;
end
5
6
7
1000
1002
1003
1004
1005
1006
1007
1008
Memory address
1001
Program
variables
ij
kPC: 1001
CPU:int i=5, j=6, k=7;
Fetch instruction at 1000 and execute. Increment PC to 1001
Program Counter (PC)
The program counter(PC) is a register that store the address of the next instruction to be executed by the CPU
int i=5, j=6, k=7;
i=i+1;
j=j+2;
k=i+j
i=0;
end
6
6
7
1000
1002
1003
1004
1005
1006
1007
1008
Memory address
1001
Program
variables
ij
kPC: 1002
CPU:i=i+1
Fetch instruction at 1001 and execute. Increment PC to 1002
Program Counter (PC)
The program counter(PC) is a register that store the address of the next instruction to be executed by the CPU
int i=5, j=6, k=7;
i=i+1;
j=j+2;
k=i+j
i=0;
end
6
8
7
1000
1002
1003
1004
1005
1006
1007
1008
Memory address
1001
Program
variables
ij
kPC: 1003
CPU:j=j+2
Fetch instruction at 1002 and execute. Increment PC to 1003
Program Counter (PC)
The program counter(PC) is a register that store the address of the next instruction to be executed by the CPU
int i=5, j=6, k=7;
i=i+1;
j=j+2;
k=i+j
i=0;
end
0
8
14
1000
1002
1003
1004
1005
1006
1007
1008
Memory address
1001
Program
variables
ij
kPC: 1005
CPU:k=i+j;
Fetch instruction at 1004 and execute. Increment PC to 1005 and end program
Interrupt Vector Table(IVT)
During interrupt, the program counter(PC) must be set to point to the address of the OS service in order to execute the routine there. How do the program know where is the address?
IVT: store the location of each interrupt service
Open File
Close File
Get System Data
Get/Set File Attribute
Read file
Write file
Allocate Memory
Write to Screen
Write to Printer
. .For (i=0;i<10;i++) write file(“numbers”,…) . . .
0001-005
0021-0028
0029-0035
0036-0042
0043-0055
0056-0068
0069-0080
0036-0042
Memory address
0006-0020
1000-2000
OS Services
ApplicationProgram
PC: 0043
CPU:Interrupt No.
Service Address
01 Open File 0001
02 Close File 0006
03 Get SD 0021
04 Get/Set File Attribute
0029
05 Read file 0036
06 Write file 0043
07 ..
Interrupt Vector Table
Let see how a software interrupt happen now! (User Mode)
Open File
Close File
Get System Data
Get/Set File Attribute
Read file
Write file
Allocate Memory
Write to Screen
Write to Printer
INT 01, “numbers”
0001-005
0021-0028
0029-0035
0036-0042
0043-0055
0056-0068
0069-0080
0036-0042
Memory address
0006-0020
1002
OS Services
ApplicationProgram
PC: 1002
CPU:
Interrupt No.
Service Address
01 Open File 0001
02 Close File 0006
03 Get SD 0021
04 Get/Set File Attribute
0029
05 Read file 0036
06 Write file 0043
Interrupt Vector Table
Let say the application program is running and the PC is pointing at location 1002
i=i+1;1003
Let see how a software interrupt happen now! (User Mode)
Open File
Close File
Get System Data
Get/Set File Attribute
Read file
Write file
Allocate Memory
Write to Screen
Write to Printer
INT 01, “numbers”
0001-005
0021-0028
0029-0035
0036-0042
0043-0055
0056-0068
0069-0080
0036-0042
Memory address
0006-0020
1002
OS Services
ApplicationProgram
PC: 1003
CPU:INT 01, “numbers”
Interrupt No.
Service Address
01 Open File 0001
02 Close File 0006
03 Get SD 0021
04 Get/Set File Attribute
0029
05 Read file 0036
06 Write file 0043
Interrupt Vector Table
It load in the instruction and see that it is an interrupt command(INT) asking for interrupt service 01
i=i+1;1003
Let see how a software interrupt happen now! (User Mode)
Open File
Close File
Get System Data
Get/Set File Attribute
Read file
Write file
Allocate Memory
Write to Screen
Write to Printer
INT 01, “numbers”
0001-005
0021-0028
0029-0035
0036-0042
0043-0055
0056-0068
0069-0080
0036-0042
Memory address
0006-0020
1002
OS Services
ApplicationProgram
PC: 1003
CPU:INT 01, “numbers”
Interrupt No.
Service Address
01 Open File 0001
02 Close File 0006
03 Get SD 0021
04 Get/Set File Attribute
0029
05 Read file 0036
06 Write file 0043
Interrupt Vector Table
It look up the IVT and see that the service “Open File” is locate at address 0001
i=i+1;1003
Let see how a software interrupt happen now! (Kernel Mode)
Open File
Close File
Get System Data
Get/Set File Attribute
Read file
Write file
Allocate Memory
Write to Screen
Write to Printer
INT 01, “numbers”
0001-005
0021-0028
0029-0035
0036-0042
0043-0055
0056-0068
0069-0080
0036-0042
Memory address
0006-0020
1002
OS Services
ApplicationProgram
PC: 0001
CPU:
Interrupt No.
Service Address
01 Open File 0001
02 Close File 0006
03 Get SD 0021
04 Get/Set File Attribute
0029
05 Read file 0036
06 Write file 0043
Interrupt Vector Table
It then set PC to 0001 and start fetching instructions from the “Open File” service there
i=i+1;1003
Let see how a software interrupt happen now! (Kernel Mode)
Open File
Close File
Get System Data
Get/Set File Attribute
Read file
Write file
Allocate Memory
Write to Screen
Write to Printer
INT 01, “numbers”
0001-005
0021-0028
0029-0035
0036-0042
0043-0055
0056-0068
0069-0080
0036-0042
Memory address
0006-0020
1002
OS Services
ApplicationProgram
PC: 0005
CPU: End of Open File
Interrupt No.
Service Address
01 Open File 0001
02 Close File 0006
03 Get SD 0021
04 Get/Set File Attribute
0029
05 Read file 0036
06 Write file 0043
Interrupt Vector Table
Assuming we reach the end of “Open File” routine, then how do we know where we should continue next?
i=i+1;1003
Context(I): User Mode
Open File
Close File
Get System Data
Get/Set File Attribute
Read file
Write file
Allocate Memory
Write to Screen
Write to Printer
INT 01, “numbers”
0001-005
0021-0028
0029-0035
0036-0042
0043-0055
0056-0068
0069-0080
0036-0042
Memory address
0006-0020
1002
OS Services
ApplicationProgram
PC: 1003
CPU:INT 01, “numbers”
Interrupt No.
Service Address
01 Open File 0001
02 Close File 0006
03 Get SD 0021
04 Get/Set File Attribute
0029
05 Read file 0036
06 Write file 0043
Interrupt Vector TableActually, when an interrupt happens, we need to save the context of the application program before we jump to the interrupt service routine!
i=i+1;1003
Program XXX 1003Context stack
Context(II): Kernel Mode
Open File
Close File
Get System Data
Get/Set File Attribute
Read file
Write file
Allocate Memory
Write to Screen
Write to Printer
INT 01, “numbers”
0001-005
0021-0028
0029-0035
0036-0042
0043-0055
0056-0068
0069-0080
0036-0042
Memory address
0006-0020
1002
OS Services
ApplicationProgram
PC: 0005
CPU: End of Open File
Interrupt No.
Service Address
01 Open File 0001
02 Close File 0006
03 Get SD 0021
04 Get/Set File Attribute
0029
05 Read file 0036
06 Write file 0043
Interrupt Vector Table
So when read the end of “Open File”, we look at the context stack and jump back to the place where interrupt occurs!
i=i+1;1003
Program XXX 1003Context stack
Context(III): return from interrupt(User mode again!)
Open File
Close File
Get System Data
Get/Set File Attribute
Read file
Write file
Allocate Memory
Write to Screen
Write to Printer
INT 01, “numbers”
0001-005
0021-0028
0029-0035
0036-0042
0043-0055
0056-0068
0069-0080
0036-0042
Memory address
0006-0020
1002
OS Services
ApplicationProgram
PC: 1003
CPU:
Interrupt No.
Service Address
01 Open File 0001
02 Close File 0006
03 Get SD 0021
04 Get/Set File Attribute
0029
05 Read file 0036
06 Write file 0043
Interrupt Vector Table
So when read the end of “Open File”, we look at the context stack and jump back to the place where interrupt occurs!
i=i+1;1003
Program XXX 1003Context stack
Context(IV): Final Notes
Note that in a context stack, there can be many entries as if many different processes/programs are running at the same time and issues different interrupts at different point in time
Program XXX 1003
Context stack Process YYY 2500
Process ZZZ 0500
Program AAA 0700
program address
Making OS more efficient
Since the OS provide a set of routines/services that are called by all application programs, it make sense to ensure that these routines/services are efficient so that the whole computer system is efficient as a whole
We will next look at two concepts that make the OS more efficient in using the CPU: Caching Hardware interrupts
Caching(Motivation)
CPUDisk
We wish to read 100 numbers from the disk and sum them up. These are the time that is need for each operations:
Initialize hard disk: 5msRead 1 number: 2 msTransfer 1 number: 2 msSum 2 numbers: 0.5ms
How long will it take to finish what we want to do?
Initialize hard disk: 5ms
Read 1 number: 2 ms
Transfer 1 number: 2 ms
Sum 2 numbers: 0.5 ms
Caching(Motivation)
CPUDisk
Assuming no cache/bufferInitialize 100 times for 100 numbers: 5 x 100 ms = 500 msRead 100 numbers: 2 x 100ms = 200msTransfer 100 numbers: 2 x 100ms = 200msSum 100 numbers: 100 x 0.5 = 50msTotal = 950ms
Initialize hard disk: 5ms
Read 1 number: 2 ms
Transfer 1 number: 2 ms
Sum 2 numbers: 0.5 ms
Caching(Motivation)
CPU
Disk
Assuming we DO have cache that can store all 100 numbersInitialize 1 times for 100 numbers: 5 x 1 ms = 5 msRead 100 numbers: 2 x 100ms = 200msTransfer 100 numbers: 2 x 100ms = 200msSum 100 numbers: 100 x 0.5 = 50msTotal = 455ms
Initialize hard disk: 5ms
Read 1 number: 2 ms
Transfer 1 number: 2 ms
Sum 2 numbers: 0.5 ms
100, 29, 50, 200,…, 90
Local buffer of 100 numbers
100, 29, 50, 200,…, 90
CPU Memory Cache
Caching(Motivation)
CPU
Disk
Because of this, data on hard disk are stored as a block called “sector”. One sector can contain one set of numbers. Only the first read of the sector will cause access to the disk. If the sector is stored in the memory cache, subsequent read will be from the cache.
Initialize hard disk: 5ms
Read 1 number: 2 ms
Transfer 1 number: 2 ms
Sum 2 numbers: 0.5 ms
100, 29, 50, 200,…, 90
Local buffer of 100 numbers
100, 29, 50, 200,…, 90
CPU Memory Cache
Caching(Motivation)
CPU
Disk
Can we do better? If we are summing N numbers, does it always make sense to have a local buffer or sector of N numbers for the disk?
How about first loading 50 numbers, let the CPU start to compute and then CURRENTLY load the other 50 numbers?
Initialize hard disk: 5ms
Read 1 number: 2 ms
Transfer 1 number: 2 ms
Sum 2 numbers: 0.5 ms
100, 29, 50, 200,…, 90
Local buffer of 100 numbers
100, 29, 50, 200,…, 90
CPU Memory Cache
Caching & Interrupt (Motivation)
Concurrent reading and calculating
CPU
Initialize hard disk: 5msRead 1 number: 2 msTransfer 1 number: 2 msSum 2 numbers: 0.5ms
Disk
(205ms)idle
Initialize (5ms)
hardware disk interrupt
Read 50 No. (100ms)
Transfer 50 No. (100ms)
Initialize command
( 25ms)Sum first 50 No.
Initialize 2 (5ms)
Harkware disk interrupt
Read 50 No. (100ms)
Initialize command 2
Transfer 50 No. (100ms)
( 25ms)Sum next 50 No.
(180ms)idle
idle (25ms)
Total time = 205ms + 205ms + 25ms = 435ms
Time is saved since summing the first 50 numbers is done concurrent with disk I/O
What if we divide the numbers into 4 blocks of 25 numbers ? Optimal number of blocks?
Interrupt and Currency The concurrent execution in previous example is
possible because of hardware interrupt AND caching. The CPU leave the task of reading data to the hard
disk and perform other task(adding number). The disk inform the CPU using interrupt when it complete its reading and transferring of data
Notice that we have more from single program, single process to single program with 2 processes (multi-threaded)
Total = 0;For (i=0; i<100;i++) number[i]=-1;
For (i=0; i<100;i++) fscanf(input,"%d",&number[i]);
For (i=0; i<100;i++){ while number[i]==-1; Total=Total+number[i]);}
Fork()
producer
consumer
Memory Hierarchy for Caching
In general, CPU only read data from the processor registers and CPU cache
Cache miss result in fetching from the next layer
Pipelining(流水线 ) In general, caching
and buffering serve to coordinate devices with different speed
Who handle hardware interrup? Same as in software
interrupt! Hardware interrupt sent
signal to CPU which will then give control to one of the OS interrupt handling routine
Open File
Close File
Get System Data
Get/Set File Attribute
Read file
Write file
Handle disk interrupt
Scheduler
Write to Printer
0001-005
0021-0028
0029-0035
0036-0042
0043-0055
0056-0068
0069-0080
0036-0042
Memory address
0006-0020
Interrupt No.
Service Address
01 Open File 0001
02 Close File 0006
03 Get SD 0021
04 Get/Set File Attribute
0029
05 Read file 0036
06 Write file 0043
Interrupt Vector Table
Single User, Multiple Programs
CPU
Disk
(205ms)idle
Initialize (5ms)
hardware disk interrupt
Read 50 No. (100ms)
Transfer 50 No. (100ms)
Initialize command
( 25ms)Sum first 50 No.
Initialize 2 (5ms)
Harkware disk interrupt
Read 50 No. (100ms)
Initialize command 2
Transfer 50 No. (100ms)
( 25ms)Sum next 50 No.
(180ms)idle
idle (25ms)
Notice that the CPU still have idle time in the ADD_100_NUMBER example earlier. What can we do with these idle time?
Answer: Swap the context of other programs in and run (Just like in Windows)
Clock interrupt
A hardware implemented clock issue a clock interrupt at regular interval. Eg. intel process give a clock interrupt to the OS every 15ms. This can be changed by the OS which can use this clock as an “alarm clock”
This “alarm clock” is used to wake up a Scheduler which will select one of the programs to be ran in the CPU
Multitasking/Time Sharing Using Clock
Open File
Close File
Scheduler
Get/Set File Attribute
.
.
.
Process XXX
0001-005
0021-0028
0029-0035
.
..
Memory address
0006-0020
1000-2000PC: 1003
CPU:Instruction from XXX
Program YYY2001-2800
Context stack Program YYY 2200
Process ZZZ 3010
Process ZZZ2801-3500
CPU initially running Process XXX at location 1003
Clock Interrupt Occur!
Multitasking/Time Sharing Using Clock
Open File
Close File
Scheduler
Get/Set File Attribute
.
.
.
Process XXX
0001-005
0021-0028
0029-0035
.
..
Memory address
0006-0020
1000-2000PC: 0021
CPU:Scheduler
Program YYY2001-2800
Context stack Program YYY 2200
Process ZZZ 3010
Process ZZZ2801-3500
Context of Process XXX saved and Scheduler loaded
Process XXX 1003
Multitasking/Time Sharing Using Clock
Open File
Close File
Scheduler
Get/Set File Attribute
.
.
.
0001-005
0021-0028
0029-0035
.
..
Memory address
0006-0020
PC: 0021
CPU:Scheduler
Context stack Program YYY 2200
Process ZZZ 3010
Scheduler select Program YYY to be ran next
Process XXX 1003Process XXX1000-2000
Program YYY2001-2800
Process ZZZ2801-3500
Multitasking/Time Sharing Using Clock
Open File
Close File
Scheduler
Get/Set File Attribute
.
.
.
0001-005
0021-0028
0029-0035
.
..
Memory address
0006-0020
PC: 2200
CPU:Instruction from YYY
Context stack Process ZZZ 3010
Scheduler select Program YYY to be ran next
Process XXX 1003
Process XXX1000-2000
Program YYY2001-2800
Process ZZZ2801-3500
Multitasking/Time Sharing Using Clock
Open File
Close File
Scheduler
Get/Set File Attribute
.
.
.
0001-005
0021-0028
0029-0035
.
..
Memory address
0006-0020
PC: 2600
CPU:Instruction from YYY
Context stack Process ZZZ 3010
Then after a certain period, clock interrupt occur again
Process XXX 1003
Process XXX1000-2000
Program YYY2001-2800
Process ZZZ2801-3500
Multitasking/Time Sharing Using Clock
Open File
Close File
Scheduler
Get/Set File Attribute
.
.
.
0001-005
0021-0028
0029-0035
.
..
Memory address
0006-0020
PC: 0021
CPU:Scheduler
Context stack
Process ZZZ 3010
Context of Programm YYY saved
Scheduler is loaded again
Process XXX 1003
Program YYY 2600
Process XXX1000-2000
Program YYY2001-2800
Process ZZZ2801-3500
Multitasking/Time Sharing Using Clock
Open File
Close File
Scheduler
Get/Set File Attribute
.
.
.
Process XXX
0001-005
0021-0028
0029-0035
.
..
Memory address
0006-0020
1000-2000PC: 3010
CPU:Instruction from ZZZ
Program YYY2001-2800
Context stack
Process ZZZ2801-3500
Scheduler decide to load Process ZZZ next
Process XXX 1003
Program YYY 2600
Multitasking/Time Sharing
Note that sometimes program are swapped out by the scheduler not because its time slice is used up but because it is waiting for some input/output (eg. reading disk) and is thus idle
This is made known to the OS because input/output are also handled by the OS through software interrupts!
Isolation: Security and Protection Since multiple programs are at
different stage of their execution concurrently in the system, care must be take care that they are isolated from each other i.e. they don’t affect each other through writing/reading from each others memory or file.
Again, this can be control by the OS since all these are done through its interrupt services
Data Consistency
Name Account No.
Savings
XXX XXX XXXX
XXX XXX XXXX
Yapeng BC345 $100
XXX XXX XXXX
XXX XXX XXXX
XXX XXX XXXX
XXX XXX XXXX
Bank Data Yapeng Deposit $50
Read Record(Yapeng, BC345, $100)
Data Consistency
Name Account No.
Savings
XXX XXX XXXX
XXX XXX XXXX
Yapeng BC345 $150
XXX XXX XXXX
XXX XXX XXXX
XXX XXX XXXX
XXX XXX XXXX
Bank Data Yapeng Deposit $50
Read Record(Yapeng, BC345, $100)
Write Record(Yapeng, BC345, $150)
Very simple?
Data Consistency
Name Account No.
Savings
XXX XXX XXXX
XXX XXX XXXX
Yapeng BC345 $150
XXX XXX XXXX
XXX XXX XXXX
XXX XXX XXXX
XXX XXX XXXX
Data Yapeng Deposit $30
Read Record(Yapeng, BC345, $150)
Read Record(Yapeng, BC345, $150)
Now both Yapeng and his wife Wang Fei deposit money concurrently
Wang Fei Deposit $100
Data Consistency
Name Account No.
Savings
XXX XXX XXXX
XXX XXX XXXX
Yapeng BC345 $250
XXX XXX XXXX
XXX XXX XXXX
XXX XXX XXXX
XXX XXX XXXX
Data Yapeng Deposit $30
Read Record(Yapeng, BC345, $150)
Read Record(Yapeng, BC345, $150)
Wang Fei update record first
Wang Fei Deposit $100
Write Record(Yapeng, BC345, $250)
Data Consistency
Name Account No.
Savings
XXX XXX XXXX
XXX XXX XXXX
Yapeng BC345 $180
XXX XXX XXXX
XXX XXX XXXX
XXX XXX XXXX
XXX XXX XXXX
Database Yapeng Deposit $30
Read Record(Yapeng, BC345, $150)
Read Record(Yapeng, BC345, $150)
Follow by Yapeng’s update
Wang Fei Deposit $100
Write Record(Yapeng, BC345, $180)
They lost $100!
Data Consistency
Name Account No.
Savings
XXX XXX XXXX
XXX XXX XXXX
Yapeng BC345 $150
XXX XXX XXXX
XXX XXX XXXX
XXX XXX XXXX
XXX XXX XXXX
Database Yapeng Deposit $30
Read Record(Yapeng, BC345, $150)
Solution: add a lock, no others can access a record when it is locked
locked
Data Consistency
Name Account No.
Savings
XXX XXX XXXX
XXX XXX XXXX
Yapeng BC345 $150
XXX XXX XXXX
XXX XXX XXXX
XXX XXX XXXX
XXX XXX XXXX
Database Yapeng Deposit $30
Read Record(Yapeng, BC345, $150)
Read Record(Yapeng, BC345, $150)
Wang Fei Deposit $100
Solution: add a lock, no others can access a record when it is locked
locked
Data Consistency
Name Account No.
Savings
XXX XXX XXXX
XXX XXX XXXX
Yapeng BC345 $180
XXX XXX XXXX
XXX XXX XXXX
XXX XXX XXXX
XXX XXX XXXX
Database Yapeng Deposit $30
Read Record(Yapeng, BC345, $150)
Read Record(Yapeng, BC345, $150)
Wang Fei Deposit $100
Solution: add a lock, no others can access a record when it is locked
locked
Write Record(Yapeng, BC345, $180)
Read Record(Yapeng, BC345, $180)
Data Deadlock
Name Account No.
Savings
XXX XXX XXXX
XXX XXX XXXX
Yapeng BC345 $150
XXX XXX XXXX
Wang Fei BC987 $1000
XXX XXX XXXX
XXX XXX XXXX
Database
Read Record (Yapeng, BC345, $150)Read Record(Wang Fei, BC987,$1000)Write Record (Yapeng, BC345, $100)Write Record (Wang Fei, BC345, $1050)
Wang Fei Deposit $100
Now Yapeng want to transfer $50 to Wang Fei and Wang Fei want to transfer $100 to Yapenglocked
Write Record(Yapeng, BC345, $180)
Read Record(Yapeng, BC345, $150)
Read Record(Wang Fei, BC987,$1000)Read Record (Yapeng, BC345, $150)Write Record (Yapeng, BC345, $250)Write Record (Wang Fei, BC345, $900)
Read Record(Wang Fei, BC987, $1000)
locked
Data Deadlock
Name Account No.
Savings
XXX XXX XXXX
XXX XXX XXXX
Yapeng BC345 $150
XXX XXX XXXX
Wang Fei BC987 $1000
XXX XXX XXXX
XXX XXX XXXX
Database
Read Record (Yapeng, BC345, $150)Read Record(Wang Fei, BC987,$1000)Write Record (Yapeng, BC345, $100)Write Record (Wang Fei, BC987, $1050)
Wang Fei Deposit $100
Both transactions cannot continue because they are holding each other’s recordlocked
Write Record(Yapeng, BC345, $180)
Read Record(Yapeng, BC345, $150)
Read Record(Wang Fei, BC987,$1000)Read Record (Yapeng, BC345, $150)Write Record (Yapeng, BC345, $200)Write Record (Wang Fei, BC987, $900)
Read Record(Wang Fei, BC987, $1000)
locked
Memory & Process Management
Memory is an important resource since processes need to be load into memory to be ran and there might not be enough memory to store all the processes and their data
Thus, memory and process management come hand in hand
Processes/programs are also a type of “data” to the OS
Open File
Close File
Scheduler
Get/Set File Attribute
.
.
.
0001-005
0021-0028
0029-0035
Memory address
0006-0020
Process XXX1000-2000
Program YYY2001-2800
Process ZZZ2801-3500
Memory & Process Management
Imagine Program YYY is the following program:
Open File
Close File
Scheduler
Get/Set File Attribute
.
.
.
0001-005
0021-0028
0029-0035
Memory address
0006-0020
Process XXX1000-2000
Program YYY2001-2800
Process ZZZ2801-3500
main(){ for (int i=2;i<100;i++) { if IsPrime(i) printf(“%i is prime”,i); }}
Boolean IsPrime(int N){ int IsPrime=1; for (int i=2;i<N;i++) { if (N mod i==0) return FALSE } return TRUE}
How do main() know the location of IsPrime()? Essentially set by OS when it load Program YYY. Program YYY might be moved to different location
Relative Address: Call function that is 10 locations behind
Virtual Memory
Virtual Memory is analogy to virtual money
The bank told you that you have $10,000 with them but You do not know the
physical location The bank might stored
$10,000 gold bar and not dollar notes
In fact the bank might not have so much money
Virtual Memory vs File Management
Virtual Memory File Management
Make use of disk to store more processes/data
Make use of disk to store more data
Transparent to the processes
Processes are aware of the existence of file
Need special hardware (MMU) for implementation
Just make use of ram and disk. Handle by OS.
Need to swap in/out both processes and data
Need to swap in/out only the data
Many concepts are however similar and have the same reasoning
Virtual Memory
Each process is divided into pages (similar to sector/block for disk/file) and bring into the memory as a whole unit. Assumption of locality:
When an instruction(data) in a page (block) is accessed, other instructions (data) in the same page is likely to be accessed
Need to monitor each virtual page is mapped to which physical frame But this is also in the
main memory which require memory access!
Virtual Memory (Paging hardware: TLB)
Translation look-aside buffers (TLBs) Similar to the concept of caching. Part of the page table is cached in the TLB which have faster access
time than main memory RAM If page (block) not in memory(invalid bit set), issue a page fault through hardware interrupt (by MMU). An
interrupt handler from the OS will then be activated to do the swapping
Virtual Memory(Page Replacement)
If there is no free frame in the physical memory, need to move a page to the disk.
Page Replacement Policy FIFO(First In, First Out): Throw out the
earliest page that came in. Need to monitor the time a page is brought in
LRU(Least Recently Used): Throw out the page that last access time is furthest away. Need to monitor the time a page is last access
Virtual Memory(Thrashing)
Example: A process XXX is given a time slice of 5 clock tick/interrupts to be executed in the CPU. Assuming 1 clock interrupt occur every 15ms, this mean XXX is given 15*5=75ms to use the CPU.
Assuming page fault for XXX and it take 100ms to load in the page for XXX. Then XXX has not enough time to be executed! Same thing will happen the next time process XXX is scheduled to be ran.
Happened when many processes is in the system and each process get little time slices and memory
Summary: Looking back
Why do we rely on OS to manage hardware, resources etc.? Answer: Because everything go through the OS in the
form of hardware and software interrupts and this make OS the center of the universe
Relook at the important concepts Software Interrupts Caching(for data) Hardware Interrupts
Disk interrupt Clock interrupt (Time Slicing) MMU interrupt (Page Fault)
Virtual Memory Paging for both process and data
Question to think about
Software interrupts seems to be similar to function call in your C programming. What is the different between interrupt handler and function in C?
Useful Reading and References
History of operating system http://en.wikipedia.org/wiki/History
_of_operating_systems Interrupts in DOS
http://www.8bs.com/othrdnld/manuals/512/512tech/m512techa.htm
Questions and Contact
[email protected] http://www.comp.nus.edu.sg/
~atung http://www.renren.com/akhtung