How to read the file

advertisement
Anthony K. H. Tung(鄧锦浩)
Associate Professor
Department of Computer Science
Gentle Introduction to Operating
System
http://www.comp.nus.edu.sg/~atung
http://www.renren.com/profile.do?id=313870900&_ua_flag=93
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 bits
1010010100….
Eg. of Instruction Set for Hard Disk

Physically the disk is control by electronic
circuit
instruction sector no.
1 0
010111
memory loc.
011 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”
End marker
filename
Sector 1
“numbers”, 2, 5, 8, -1
“letters”, 3, 6, 1023, -1
.
.
.
Sector 2
20, 4, 100, …………
Sector 3
A, Z, B, H, ……….
Sector 4
Sector 5
900, 1000, 50, …………
Sector 6
B, C, I, A, ……….
Sector 7
Sector 8
54, 20, 10,………….
Sector 9
.
.
Sector 1023
Sector 1024
O, R, Q, G …………
File
directory
sector
Storing Data as Files(II)

How to read the file
“numbers”?
Step 1: 10, Sector 1, Mem. Location 20 // Read Sector 1
// into memory location 20
Step 2: Search location 20 for filename=“numbers”;
Step 3: sector_num = first sector of “numbers”;
Step 3: While (sector_num<>-1) do
Step 4: {
10, Sector sector_num, Mem. Location 21;
Step 5:
process data in mem. Location 21;
Step 6: }
End marker
filename
Sector 1
“numbers”, 2, 5, 8, -1
“letters”, 3, 6, 1023, -1
.
.
.
Sector 2
20, 4, 100, …………
Sector 3
A, Z, B, H, ……….
Sector 4
Sector 5
900, 1000, 50, …………
Sector 6
B, C, I, A, ……….
Sector 7
Note: “10” is the hardware code for reading a sector of the disk
Sector 8
54, 20, 10,………….
Sector 9
.
.
Sector 1023
Sector 1024
O, R, Q, G …………
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
Memory
address
0001-005
Open File
0006-0020
Close File
0021-0028
Get System Data
0029-0035
Get/Set File Attribute
0036-0042
Read file
0043-0055
Write file
0056-0068
Allocate Memory
0069-0080
Write to Screen
0036-0042
Write to Printer
1000-2000
.
.
For (i=0;i<10;i++)
write file(“numbers”,…)
.
.
.
OS Services
Application
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
Memory
address
1000
int i=5, j=6, k=7;
1001
i=i+1;
1002
j=j+2;
1003
k=i+j;
1004
i=0;
1005
end
CPU:
1006
PC: 1000
1007
1008
At the start, PC point to start
of program
Program
i
j
k
variables
Program Counter (PC)

The program counter(PC) is a
register that store the address
of the next instruction to be
executed by the CPU
Memory
address
1000
int i=5, j=6, k=7;
1001
i=i+1;
1002
j=j+2;
1003
k=i+j
1004
i=0;
1005
end
CPU:int i=5, j=6, k=7;
1006
5
PC: 1001
1007
6
1008
7
Fetch instruction at 1000
and execute.
Increment PC to 1001
Program
i
j
k
variables
Program Counter (PC)

The program counter(PC) is a
register that store the address
of the next instruction to be
executed by the CPU
Memory
address
1000
int i=5, j=6, k=7;
1001
i=i+1;
1002
j=j+2;
1003
k=i+j
1004
i=0;
1005
end
CPU:i=i+1
1006
6
PC: 1002
1007
6
1008
7
Fetch instruction at 1001
and execute.
Increment PC to 1002
Program
i
j
k
variables
Program Counter (PC)

The program counter(PC) is a
register that store the address
of the next instruction to be
executed by the CPU
Memory
address
1000
int i=5, j=6, k=7;
1001
i=i+1;
1002
j=j+2;
1003
k=i+j
1004
i=0;
1005
end
CPU:j=j+2
1006
6
PC: 1003
1007
8
1008
7
Fetch instruction at 1002
and execute.
Increment PC to 1003
Program
i
j
k
variables
Program Counter (PC)

The program counter(PC) is a
register that store the address
of the next instruction to be
executed by the CPU
Memory
address
1000
int i=5, j=6, k=7;
1001
i=i+1;
1002
j=j+2;
1003
k=i+j
1004
i=0;
1005
end
CPU:k=i+j;
1006
0
PC: 1005
1007
8
1008
14
Fetch instruction at 1004
and execute.
Increment PC to 1005 and
end program
Program
i
j
k
variables
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?
Service
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
0001-005
Open File
0006-0020
Close File
0021-0028
Get System Data
0029-0035
Get/Set File Attribute
0036-0042
Read file
0043-0055
Write file
CPU:
0056-0068
Allocate Memory
PC: 0043
0069-0080
Write to Screen
0036-0042
Write to Printer
IVT: store the location of each
interrupt service
Interrupt
No.
Address
.
.
Memory
address
OS Services
Interrupt Vector Table
1000-2000
.
.
For (i=0;i<10;i++)
write file(“numbers”,…)
.
.
.
Application
Program
Let see how a software interrupt
happen now! (User Mode)
Interrupt
No.
Service
Address
01
Open File
0001
02
Close File
0006
0001-005
Open File
03
Get SD
0021
0006-0020
Close File
04
Get/Set File Attribute
0029
0021-0028
Get System Data
05
Read file
0036
0029-0035
Get/Set File Attribute
06
Write file
0043
0036-0042
Read file
0043-0055
Write file
CPU:
0056-0068
Allocate Memory
PC: 1002
0069-0080
Write to Screen
0036-0042
Write to Printer
Memory
address
OS Services
Interrupt Vector Table
Let say the application
program is running
and the PC is pointing
at location 1002
1002
1003
INT 01, “numbers”
i=i+1;
Application
Program
Let see how a software interrupt
happen now! (User Mode)
Interrupt
No.
Service
Address
01
Open File
0001
02
Close File
0006
0001-005
Open File
03
Get SD
0021
0006-0020
Close File
04
Get/Set File Attribute
0029
0021-0028
Get System Data
05
Read file
0036
0029-0035
Get/Set File Attribute
06
Write file
0043
0036-0042
Read file
0043-0055
Write file
CPU:INT 01, “numbers”
0056-0068
Allocate Memory
PC: 1003
0069-0080
Write to Screen
0036-0042
Write to Printer
Memory
address
OS Services
Interrupt Vector Table
It load in the instruction
and see that it is an
interrupt command(INT)
asking for interrupt
service 01
1002
1003
INT 01, “numbers”
i=i+1;
Application
Program
Let see how a software interrupt
happen now! (User Mode)
Interrupt
No.
Service
Address
01
Open File
0001
02
Close File
0006
0001-005
Open File
03
Get SD
0021
0006-0020
Close File
04
Get/Set File Attribute
0029
0021-0028
Get System Data
05
Read file
0036
0029-0035
Get/Set File Attribute
06
Write file
0043
0036-0042
Read file
0043-0055
Write file
CPU:INT 01, “numbers”
0056-0068
Allocate Memory
PC: 1003
0069-0080
Write to Screen
0036-0042
Write to Printer
Memory
address
OS Services
Interrupt Vector Table
It look up the IVT and see
that the service “Open
File” is locate at address
0001
1002
1003
INT 01, “numbers”
i=i+1;
Application
Program
Let see how a software interrupt
happen now! (Kernel Mode)
Interrupt
No.
Service
Address
01
Open File
0001
Memory
address
0001-005
Open File
02
Close File
0006
03
Get SD
0021
0006-0020
Close File
04
Get/Set File Attribute
0029
0021-0028
Get System Data
05
Read file
0036
0029-0035
Get/Set File Attribute
06
Write file
0043
0036-0042
Read file
0043-0055
Write file
CPU:
0056-0068
Allocate Memory
PC: 0001
0069-0080
Write to Screen
0036-0042
Write to Printer
OS Services
Interrupt Vector Table
It then set PC to 0001 and
start fetching instructions
from the “Open File”
service there
1002
1003
INT 01, “numbers”
i=i+1;
Application
Program
Let see how a software interrupt
happen now! (Kernel Mode)
Interrupt
No.
Service
Address
01
Open File
0001
02
Close File
0006
03
Get SD
0021
04
Get/Set File Attribute
05
06
Memory
address
0001-005
0006-0020
Open File
0029
0021-0028
Get System Data
Read file
0036
0029-0035
Get/Set File Attribute
Write file
0043
0036-0042
Read file
0043-0055
Write file
CPU: End of Open File
0056-0068
Allocate Memory
PC: 0005
0069-0080
Write to Screen
0036-0042
Write to Printer
Assuming we reach the end
of “Open File” routine, then
how do we know where we
should continue next?
Close File
OS Services
Interrupt Vector Table
1002
1003
INT 01, “numbers”
i=i+1;
Application
Program
Context(I): User Mode
Interrupt
No.
Service
Address
01
Open File
0001
02
Close File
0006
0001-005
Open File
03
Get SD
0021
0006-0020
Close File
04
Get/Set File Attribute
0029
0021-0028
Get System Data
05
Read file
0036
0029-0035
Get/Set File Attribute
06
Write file
0043
0036-0042
Read file
0043-0055
Write file
CPU:INT 01, “numbers”
0056-0068
Allocate Memory
PC: 1003
0069-0080
Write to Screen
0036-0042
Write to Printer
Actually, when an interrupt
happens, we need to save
the context of the application
program before we jump to
the interrupt service routine!
Memory
address
OS Services
Interrupt Vector Table
1002
1003
Context
stack
Program XXX
INT 01, “numbers”
i=i+1;
1003
Application
Program
Context(II): Kernel Mode
Interrupt
No.
Service
Address
01
Open File
0001
02
Close File
0006
03
Get SD
0021
04
Get/Set File Attribute
05
06
Memory
address
0001-005
0006-0020
Open File
0029
0021-0028
Get System Data
Read file
0036
0029-0035
Get/Set File Attribute
Write file
0043
0036-0042
Read file
0043-0055
Write file
CPU: End of Open File
0056-0068
Allocate Memory
PC: 0005
0069-0080
Write to Screen
0036-0042
Write to Printer
Close File
OS Services
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!
1002
1003
Context
stack
INT 01, “numbers”
Application
Program
i=i+1;
Program XXX
1003
Context(III): return from interrupt
(User mode again!)
Interrupt
No.
Service
Address
01
Open File
0001
02
Close File
0006
03
Get SD
0021
04
Get/Set File Attribute
05
06
Memory
address
0001-005
0006-0020
Open File
0029
0021-0028
Get System Data
Read file
0036
0029-0035
Get/Set File Attribute
Write file
0043
0036-0042
Read file
0043-0055
Write file
CPU:
0056-0068
Allocate Memory
PC: 1003
0069-0080
Write to Screen
0036-0042
Write to Printer
So when read the end of
“Open File”, we look at the
context stack and jump
back to the place where
interrupt occurs!
Close File
OS Services
Interrupt Vector Table
1002
1003
Context
stack
INT 01, “numbers”
Application
Program
i=i+1;
Program XXX
1003
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
Context
stack
address
Program AAA
0700
Process ZZZ
0500
Process YYY
2500
Program XXX
1003
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)
Read 1 number: 2 ms
Sum 2 numbers:
0.5 ms
Initialize hard disk: 5ms
Transfer 1 number: 2 ms
CPU
Disk
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: 5ms
Read 1 number: 2 ms
Transfer 1 number: 2 ms
Sum 2 numbers: 0.5ms
How long will it take to finish what we want to do?
Caching(Motivation)
Read 1 number: 2 ms
Sum 2 numbers:
0.5 ms
Initialize hard disk: 5ms
Transfer 1 number: 2 ms
CPU
Disk
Assuming no cache/buffer
Initialize 100 times for 100 numbers: 5 x 100 ms = 500 ms
Read 100 numbers: 2 x 100ms = 200ms
Transfer 100 numbers: 2 x 100ms = 200ms
Sum 100 numbers: 100 x 0.5 = 50ms
Total = 950ms
Caching(Motivation)
Read 1 number: 2 ms
Sum 2 numbers:
0.5 ms
Initialize hard disk: 5ms
Disk
CPU
Transfer 1 number: 2 ms
100, 29, 50, 200,…, 90
100, 29, 50, 200,…, 90
CPU Memory Cache
Local buffer of 100
numbers
Assuming we DO have cache that can store all 100 numbers
Initialize 1 times for 100 numbers: 5 x 1 ms = 5 ms
Read 100 numbers: 2 x 100ms = 200ms
Transfer 100 numbers: 2 x 100ms = 200ms
Sum 100 numbers: 100 x 0.5 = 50ms
Total = 455ms
Caching(Motivation)
Read 1 number: 2 ms
Sum 2 numbers:
0.5 ms
Initialize hard disk: 5ms
Disk
CPU
Transfer 1 number: 2 ms
100, 29, 50, 200,…, 90
100, 29, 50, 200,…, 90
CPU Memory Cache
Local buffer of 100
numbers
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.
Caching(Motivation)
Read 1 number: 2 ms
Sum 2 numbers:
0.5 ms
Initialize hard disk: 5ms
Disk
CPU
Transfer 1 number: 2 ms
100, 29, 50, 200,…, 90
100, 29, 50, 200,…, 90
CPU Memory Cache
Local buffer of 100
numbers
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?
Caching & Interrupt (Motivation)

Concurrent reading and calculating
(205ms)
idle
( 25ms)
Sum first 50 No.
(180ms)
idle
( 25ms)
Sum next 50 No.
CPU
Initialize
command
Disk
Initialize
(5ms)
Read 50 No.
(100ms)
hardware
disk
interrupt
Transfer 50 No.
(100ms)
Initialize
command 2
Initialize 2
(5ms)
Read 50 No.
(100ms)
Harkware
disk
interrupt
Transfer 50 No.
(100ms)
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?
Initialize hard disk: 5ms
Read 1 number: 2 ms
Transfer 1 number: 2 ms
Sum 2 numbers: 0.5ms
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]);
producer
Fork()
For (i=0; i<100;i++)
{ while number[i]==-1;
Total=Total+number[i]);}
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
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
Memory
address
0001-005
Open File
0006-0020
Close File
0021-0028
Get System Data
0029-0035
Get/Set File Attribute
0036-0042
Read file
0043-0055
Write file
0056-0068
Handle disk interrupt
0069-0080
Scheduler
0036-0042
Write to Printer
Interrupt Vector Table
Single User, Multiple Programs
(205ms)
idle
( 25ms)
Sum first 50 No.
(180ms)
idle
( 25ms)
Sum next 50 No.
CPU
Initialize
command
Disk
Initialize
(5ms)
Read 50 No.
(100ms)
hardware
disk
interrupt
Transfer 50 No.
(100ms)
Initialize
command 2
Initialize 2
(5ms)
Read 50 No.
(100ms)
Harkware
disk
interrupt
Transfer 50 No.
(100ms)
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)
idle
(25ms)
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
CPU initially running Process
XXX at location 1003
Clock Interrupt Occur!
Memory
address
0001-005
Open File
0006-0020
Close File
0021-0028
Scheduler
0029-0035
Get/Set File Attribute
.
.
.
.
.
.
CPU:Instruction from XXX
PC: 1003
Context
stack
Program YYY
2200
Process ZZZ
3010
1000-2000
Process XXX
2001-2800
Program YYY
2801-3500
Process ZZZ
Multitasking/Time Sharing Using Clock
Context of Process XXX saved
and Scheduler loaded
Memory
address
0001-005
Open File
0006-0020
Close File
0021-0028
Scheduler
0029-0035
Get/Set File Attribute
.
.
.
.
.
.
CPU:Scheduler
PC: 0021
Context
stack
Process XXX
1003
Program YYY
2200
Process ZZZ
3010
1000-2000
Process XXX
2001-2800
Program YYY
2801-3500
Process ZZZ
Multitasking/Time Sharing Using Clock
Scheduler select Program YYY
to be ran next
Memory
address
0001-005
Open File
0006-0020
Close File
0021-0028
Scheduler
0029-0035
Get/Set File Attribute
.
.
.
.
.
.
CPU:Scheduler
PC: 0021
Context
stack
Process XXX
1003
Program YYY
2200
Process ZZZ
3010
1000-2000
Process XXX
2001-2800
Program YYY
2801-3500
Process ZZZ
Multitasking/Time Sharing Using Clock
Scheduler select Program YYY
to be ran next
Memory
address
0001-005
Open File
0006-0020
Close File
0021-0028
Scheduler
0029-0035
Get/Set File Attribute
.
.
.
.
.
.
CPU:Instruction from YYY
PC: 2200
Context
stack
Process XXX
1003
Process ZZZ
3010
1000-2000
Process XXX
2001-2800
Program YYY
2801-3500
Process ZZZ
Multitasking/Time Sharing Using Clock
Then after a certain period,
clock interrupt occur again
Memory
address
0001-005
Open File
0006-0020
Close File
0021-0028
Scheduler
0029-0035
Get/Set File Attribute
.
.
.
.
.
.
CPU:Instruction from YYY
PC: 2600
Context
stack
Process XXX
1003
Process ZZZ
3010
1000-2000
Process XXX
2001-2800
Program YYY
2801-3500
Process ZZZ
Multitasking/Time Sharing Using Clock
Context of Programm YYY saved
Scheduler is loaded again
Memory
address
0001-005
Open File
0006-0020
Close File
0021-0028
Scheduler
0029-0035
Get/Set File Attribute
.
.
.
.
.
.
CPU:Scheduler
PC: 0021
Context
stack
Process XXX
1003
Program YYY
2600
Process ZZZ
3010
1000-2000
Process XXX
2001-2800
Program YYY
2801-3500
Process ZZZ
Multitasking/Time Sharing Using Clock
Scheduler decide to load
Process ZZZ next
Memory
address
0001-005
Open File
0006-0020
Close File
0021-0028
Scheduler
0029-0035
Get/Set File Attribute
.
.
.
.
.
.
CPU:Instruction from ZZZ
PC: 3010
Context
stack
Process XXX
1003
Program YYY
2600
1000-2000
Process XXX
2001-2800
Program YYY
2801-3500
Process ZZZ
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
Yapeng Deposit $50
Bank Data
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
Read Record
(Yapeng, BC345, $100)
Data Consistency
Yapeng Deposit $50
Bank Data
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
Read Record
(Yapeng, BC345, $100)
Write Record
(Yapeng, BC345, $150)
Very simple?
Data Consistency
Yapeng Deposit $30
Data
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
Read Record
(Yapeng, BC345, $150)
Now both Yapeng and
his wife Wang Fei
deposit money
concurrently
Read Record
(Yapeng, BC345, $150)
Wang Fei Deposit $100
Data Consistency
Yapeng Deposit $30
Data
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
Read Record
(Yapeng, BC345, $150)
Write Record
(Yapeng, BC345, $250)
Wang Fei update
record first
Read Record
(Yapeng, BC345, $150)
Wang Fei Deposit $100
Data Consistency
Yapeng Deposit $30
Database
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
Read Record
(Yapeng, BC345, $150)
Write Record
(Yapeng, BC345, $180)
Follow by Yapeng’s
update
They lost $100!
Read Record
(Yapeng, BC345, $150)
Wang Fei Deposit $100
Data Consistency
Yapeng Deposit $30
Database
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
Read Record
(Yapeng, BC345, $150)
locked

Solution: add a lock, no others
can access a record when it is
locked
Data Consistency
Yapeng Deposit $30
Database
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
Read Record
(Yapeng, BC345, $150)
locked

Solution: add a lock, no others
can access a record when it is
locked
Read Record
(Yapeng, BC345, $150)
Wang Fei Deposit $100
Data Consistency
Yapeng Deposit $30
Database
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
Read Record
(Yapeng, BC345, $150)
Write Record
(Yapeng, BC345, $180)
locked

Solution: add a lock, no others
can access a record when it is
locked
Read
ReadRecord
Record
(Yapeng,
(Yapeng,BC345,
BC345,$180)
$150)
Wang Fei Deposit $100
Data Deadlock
Read Record (Yapeng, BC345, $150)
Read Record(Wang Fei, BC987,$1000)
Write Record (Yapeng, BC345, $100)
Write Record (Wang Fei, BC345, $1050)
Database
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
Read Record
(Yapeng, BC345, $150)
Write Record
(Yapeng, BC345, $180)

locked
locked
Read Record
(Wang Fei, BC987, $1000)
Now Yapeng want to transfer
$50 to Wang Fei and Wang
Fei want to transfer $100 to
Yapeng
Read Record(Wang Fei, BC987,$1000)
Read Record (Yapeng, BC345, $150)
Write Record (Yapeng, BC345, $250)
Write Record (Wang Fei, BC345, $900)
Wang Fei Deposit $100
Data Deadlock
Read Record (Yapeng, BC345, $150)
Read Record(Wang Fei, BC987,$1000)
Write Record (Yapeng, BC345, $100)
Write Record (Wang Fei, BC987, $1050)
Database
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
Read Record
(Yapeng, BC345, $150)
Write Record
(Yapeng, BC345, $180)

locked
Both transactions cannot
continue because they are
holding each other’s record
locked
Read Record
(Wang Fei, BC987, $1000)
Read Record(Wang Fei, BC987,$1000)
Read Record (Yapeng, BC345, $150)
Write Record (Yapeng, BC345, $200)
Write Record (Wang Fei, BC987, $900)
Wang Fei Deposit $100
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
Memory
address
0001-005
Open File
0006-0020
Close File
0021-0028
Scheduler
0029-0035
Get/Set File Attribute
.
.
.
1000-2000
Process XXX
2001-2800
Program YYY
2801-3500
Process ZZZ
Memory & Process Management

Imagine Program YYY is the
following program:
Boolean IsPrime(int N)
{ int IsPrime=1;
for (int i=2;i<N;i++)
{ if (N mod i==0)
return FALSE
}
return TRUE
}
main()
{ for (int i=2;i<100;i++)
{ if IsPrime(i)
printf(“%i is prime”,i);
}
}

Memory
address
0001-005
Open File
0006-0020
Close File
0021-0028
Scheduler
0029-0035
Get/Set File Attribute
.
.
.
Relative Address:
Call function that is
10 locations behind
1000-2000
Process XXX
2001-2800
Program YYY
2801-3500
Process ZZZ
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
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_o
perating_systems
 Interrupts in DOS
 http://www.8bs.com/othrdnld/manuals/5
12/512tech/m512techa.htm

Questions and Contact
atung@comp.nus.edu.sg
 http://www.comp.nus.edu.sg/~atung
 http://www.renren.com/akhtung

Download