Gentle Introduction to Operating System - PowerPoint PPT Presentation

About This Presentation
Title:

Gentle Introduction to Operating System

Description:

Anthony K. H. Tung( Associate Professor Department of Computer Science Gentle Introduction to Operating System http://www.comp.nus.edu.sg/~atung – PowerPoint PPT presentation

Number of Views:129
Avg rating:3.0/5.0
Slides: 69
Provided by: coml152
Category:

less

Transcript and Presenter's Notes

Title: Gentle Introduction to Operating System


1
Gentle Introduction to Operating System
  • Anthony K. H. Tung(???)
  • Associate Professor
  • Department of Computer Science

http//www.comp.nus.edu.sg/atung http//www.renre
n.com/profile.do?id313870900_ua_flag93w
2
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

3
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

4
What is OS?
  • As the name implied, operating system is a
    software system that operate something? What is
    that something?
  • somethinghardware
  • 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

5
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.
6
Eg. of Instruction Set for Hard Disk
  • Physically the disk is control by electronic
    circuit

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
instruction sector no. memory loc. 1
0 0 1 0 1 1 1 0 1 1 0
7
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
numbers, 2, 5, 8, -1 letters, 3, 6, 1023, -1
. . .
Sector 1
20, 4, 100,
Sector 2
Sector 3
File directory sector
A, Z, B, H, .
Sector 4
900, 1000, 50,
Sector 5
B, C, I, A, .
Sector 6
Sector 7
54, 20, 10,.
Sector 8
Sector 9
. .
O, R, Q, G
Sector 1023
Sector 1024
8
Storing Data as Files(II)
  • How to read the file numbers?

End marker
filename
numbers, 2, 5, 8, -1 letters, 3, 6, 1023, -1
. . .
Step 1 10, Sector 1, Mem. Location 20 // Read
Sector 1
// into memory location 20 Step 2 Search
location 20 for filenamenumbers Step 3
sector_num first sector of numbers Step 3
While (sector_numltgt-1) do Step 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
Sector 1
20, 4, 100,
Sector 2
Sector 3
A, Z, B, H, .
Sector 4
900, 1000, 50,
Sector 5
B, C, I, A, .
Sector 6
Sector 7
54, 20, 10,.
Sector 8
Sector 9
. .
O, R, Q, G
Sector 1023
Sector 1024
9
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?

10
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
Open File
0001-005
Close File
0006-0020
0021-0028
Get System Data
Get/Set File Attribute
0029-0035
OS Services
Read file
0036-0042
Write file
0043-0055
Allocate Memory
0056-0068
Write to Screen
0069-0080
Write to Printer
0036-0042
. . For (i0ilt10i) write
file(numbers,) . . .
1000-2000
Application Program
11
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
int i5, j6, k7
1000
ii1
1001
1002
jj2
Program
kij
1003
i0
1004
end
1005
i
CPU
1006
j
1007
variables
PC 1000
1008
k
At the start, PC point to start of program
12
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
int i5, j6, k7
1000
ii1
1001
1002
jj2
Program
kij
1003
i0
1004
end
1005
i
CPUint i5, j6, k7
5
1006
j
6
1007
variables
PC 1001
7
1008
k
Fetch instruction at 1000 and execute. Increment
PC to 1001
13
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
int i5, j6, k7
1000
ii1
1001
1002
jj2
Program
kij
1003
i0
1004
end
1005
i
CPUii1
6
1006
j
6
1007
variables
PC 1002
7
1008
k
Fetch instruction at 1001 and execute. Increment
PC to 1002
14
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
int i5, j6, k7
1000
ii1
1001
1002
jj2
Program
kij
1003
i0
1004
end
1005
i
CPUjj2
6
1006
j
8
1007
variables
PC 1003
7
1008
k
Fetch instruction at 1002 and execute. Increment
PC to 1003
15
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
int i5, j6, k7
1000
ii1
1001
1002
jj2
Program
kij
1003
i0
1004
end
1005
i
CPUkij
0
1006
j
8
1007
variables
PC 1005
14
1008
k
Fetch instruction at 1004 and execute. Increment
PC to 1005 and end program
16
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

Memory address
Open File
0001-005
Close File
0006-0020
0021-0028
Get System Data
Get/Set File Attribute
0029-0035
OS Services
Read file
0036-0042
Write file
0043-0055
CPU
Allocate Memory
0056-0068
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 . .
Write to Screen
0069-0080
PC 0043
Write to Printer
0036-0042
Interrupt Vector Table
. . For (i0ilt10i) write
file(numbers,) . . .
1000-2000
Application Program
17
Let see how a software interrupt happen now!
(User Mode)
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
Open File
0001-005
Close File
0006-0020
0021-0028
Get System Data
Get/Set File Attribute
0029-0035
OS Services
Read file
0036-0042
Write file
0043-0055
CPU
Allocate Memory
0056-0068
Write to Screen
0069-0080
PC 1002
Write to Printer
0036-0042
Interrupt Vector Table
Let say the application program is running and
the PC is pointing at location 1002
1002
Application Program
INT 01, numbers
1003
ii1
18
Let see how a software interrupt happen now!
(User Mode)
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
Open File
0001-005
Close File
0006-0020
0021-0028
Get System Data
Get/Set File Attribute
0029-0035
OS Services
Read file
0036-0042
Write file
0043-0055
CPUINT 01, numbers
Allocate Memory
0056-0068
Write to Screen
0069-0080
PC 1003
Write to Printer
0036-0042
Interrupt Vector Table
It load in the instruction and see that it is an
interrupt command(INT) asking for interrupt
service 01
1002
Application Program
INT 01, numbers
1003
ii1
19
Let see how a software interrupt happen now!
(User Mode)
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
Open File
0001-005
Close File
0006-0020
0021-0028
Get System Data
Get/Set File Attribute
0029-0035
OS Services
Read file
0036-0042
Write file
0043-0055
CPUINT 01, numbers
Allocate Memory
0056-0068
Write to Screen
0069-0080
PC 1003
Write to Printer
0036-0042
Interrupt Vector Table
It look up the IVT and see that the service Open
File is locate at address 0001
1002
Application Program
INT 01, numbers
1003
ii1
20
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 0029
05 Read file 0036
06 Write file 0043
Memory address
Open File
0001-005
Close File
0006-0020
0021-0028
Get System Data
Get/Set File Attribute
0029-0035
OS Services
Read file
0036-0042
Write file
0043-0055
CPU
Allocate Memory
0056-0068
Write to Screen
0069-0080
PC 0001
Write to Printer
0036-0042
Interrupt Vector Table
It then set PC to 0001 and start fetching
instructions from the Open File service there
1002
Application Program
INT 01, numbers
1003
ii1
21
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 0029
05 Read file 0036
06 Write file 0043
Memory address
Open File
0001-005
Close File
0006-0020
0021-0028
Get System Data
Get/Set File Attribute
0029-0035
OS Services
Read file
0036-0042
Write file
0043-0055
CPU End of Open File
Allocate Memory
0056-0068
Write to Screen
0069-0080
PC 0005
Write to Printer
0036-0042
Interrupt Vector Table
Assuming we reach the end of Open File routine,
then how do we know where we should continue next?
1002
Application Program
INT 01, numbers
1003
ii1
22
Context(I) User Mode
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
Open File
0001-005
Close File
0006-0020
0021-0028
Get System Data
Get/Set File Attribute
0029-0035
OS Services
Read file
0036-0042
Write file
0043-0055
CPUINT 01, numbers
Allocate Memory
0056-0068
Write to Screen
0069-0080
PC 1003
Write to Printer
0036-0042
Interrupt Vector Table
Actually, when an interrupt happens, we need to
save the context of the application program
before we jump to the interrupt service routine!
1002
Application Program
INT 01, numbers
1003
ii1
Context stack
Program XXX 1003
23
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 0029
05 Read file 0036
06 Write file 0043
Memory address
Open File
0001-005
Close File
0006-0020
0021-0028
Get System Data
Get/Set File Attribute
0029-0035
OS Services
Read file
0036-0042
Write file
0043-0055
CPU End of Open File
Allocate Memory
0056-0068
Write to Screen
0069-0080
PC 0005
Write to Printer
0036-0042
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
Application Program
INT 01, numbers
1003
ii1
Context stack
Program XXX 1003
24
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 0029
05 Read file 0036
06 Write file 0043
Memory address
Open File
0001-005
Close File
0006-0020
0021-0028
Get System Data
Get/Set File Attribute
0029-0035
OS Services
Read file
0036-0042
Write file
0043-0055
CPU
Allocate Memory
0056-0068
Write to Screen
0069-0080
PC 1003
Write to Printer
0036-0042
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
Application Program
INT 01, numbers
1003
ii1
Context stack
Program XXX 1003
25
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
address
Program AAA 0700
Context stack
Process ZZZ 0500
Process YYY 2500
Program XXX 1003
26
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

27
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?
28
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
29
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
30
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.
31
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?
32
Caching Interrupt (Motivation)
  • Concurrent reading and calculating

(180ms) idle
(205ms) idle
( 25ms) Sum first 50 No.
( 25ms) Sum next 50 No.
CPU
hardware disk interrupt
Initialize command
Harkware disk interrupt
Initialize command 2
Disk
Initialize (5ms)
Read 50 No. (100ms)
Transfer 50 No. (100ms)
Initialize 2 (5ms)
Read 50 No. (100ms)
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
33
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)

For (i0 ilt100i) fscanf(input,"d",numberi
)
producer
Total 0 For (i0 ilt100i)
numberi-1
Fork()
For (i0 ilt100i) while numberi-1
TotalTotalnumberi)
consumer
34
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

35
Who handle hardware interrup?
Memory address
  • 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
0001-005
Close File
0006-0020
0021-0028
Get System Data
Get/Set File Attribute
0029-0035
Read file
0036-0042
Write file
0043-0055
Handle disk interrupt
0056-0068
Scheduler
0069-0080
Write to Printer
0036-0042
Interrupt Vector Table
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
36
Single User, Multiple Programs
(180ms) idle
(205ms) idle
( 25ms) Sum first 50 No.
( 25ms) Sum next 50 No.
CPU
hardware disk interrupt
Initialize command
Harkware disk interrupt
Initialize command 2
Disk
Initialize (5ms)
Read 50 No. (100ms)
Transfer 50 No. (100ms)
Initialize 2 (5ms)
Read 50 No. (100ms)
Transfer 50 No. (100ms)
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)
37
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

38
Multitasking/Time Sharing Using Clock
Memory address
CPU initially running Process XXX at location
1003 Clock Interrupt Occur!
Open File
0001-005
Close File
0006-0020
0021-0028
Scheduler
Get/Set File Attribute
0029-0035
. . .
. . .
CPUInstruction from XXX
PC 1003
1000-2000
Process XXX
2001-2800
Program YYY
2801-3500
Process ZZZ
Context stack
Program YYY 2200
Process ZZZ 3010
39
Multitasking/Time Sharing Using Clock
Memory address
Context of Process XXX saved and Scheduler loaded
Open File
0001-005
Close File
0006-0020
0021-0028
Scheduler
Get/Set File Attribute
0029-0035
. . .
. . .
CPUScheduler
PC 0021
1000-2000
Process XXX
2001-2800
Program YYY
Process XXX 1003
2801-3500
Process ZZZ
Context stack
Program YYY 2200
Process ZZZ 3010
40
Multitasking/Time Sharing Using Clock
Memory address
Scheduler select Program YYY to be ran next
Open File
0001-005
Close File
0006-0020
0021-0028
Scheduler
Get/Set File Attribute
0029-0035
. . .
. . .
CPUScheduler
PC 0021
1000-2000
Process XXX
Process XXX 1003
2001-2800
Program YYY
Context stack
Program YYY 2200
2801-3500
Process ZZZ
Process ZZZ 3010
41
Multitasking/Time Sharing Using Clock
Memory address
Scheduler select Program YYY to be ran next
Open File
0001-005
Close File
0006-0020
0021-0028
Scheduler
Get/Set File Attribute
0029-0035
. . .
. . .
CPUInstruction from YYY
PC 2200
1000-2000
Process XXX
2001-2800
Program YYY
Process XXX 1003
2801-3500
Process ZZZ
Context stack
Process ZZZ 3010
42
Multitasking/Time Sharing Using Clock
Memory address
Then after a certain period, clock interrupt
occur again
Open File
0001-005
Close File
0006-0020
0021-0028
Scheduler
Get/Set File Attribute
0029-0035
. . .
. . .
CPUInstruction from YYY
PC 2600
1000-2000
Process XXX
2001-2800
Program YYY
Process XXX 1003
2801-3500
Process ZZZ
Context stack
Process ZZZ 3010
43
Multitasking/Time Sharing Using Clock
Memory address
Context of Programm YYY saved Scheduler is
loaded again
Open File
0001-005
Close File
0006-0020
0021-0028
Scheduler
Get/Set File Attribute
0029-0035
. . .
. . .
CPUScheduler
PC 0021
1000-2000
Process XXX
2001-2800
Program YYY
Process XXX 1003
2801-3500
Process ZZZ
Context stack
Program YYY 2600
Process ZZZ 3010
44
Multitasking/Time Sharing Using Clock
Memory address
Scheduler decide to load Process ZZZ next
Open File
0001-005
Close File
0006-0020
0021-0028
Scheduler
Get/Set File Attribute
0029-0035
. . .
. . .
CPUInstruction from ZZZ
PC 3010
1000-2000
Process XXX
2001-2800
Program YYY
2801-3500
Process ZZZ
Process XXX 1003
Context stack
Program YYY 2600
45
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!

46
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 dont 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

47
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)
48
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?
49
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
50
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)
Wang Fei update record first
Write Record (Yapeng, BC345, 250)
Read Record (Yapeng, BC345, 150)
Wang Fei Deposit 100
51
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)
Follow by Yapengs update
Write Record (Yapeng, BC345, 180)
They lost 100!
Read Record (Yapeng, BC345, 150)
Wang Fei Deposit 100
52
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)
  • Solution add a lock, no others can access a
    record when it is locked

locked
53
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)
  • Solution add a lock, no others can access a
    record when it is locked

locked
Read Record (Yapeng, BC345, 150)
Wang Fei Deposit 100
54
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)
  • Solution add a lock, no others can access a
    record when it is locked

locked
Read Record (Yapeng, BC345, 150)
Read Record (Yapeng, BC345, 180)
Wang Fei Deposit 100
55
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
Write Record (Yapeng, BC345, 180)
Read Record (Yapeng, BC345, 150)
  • Now Yapeng want to transfer 50 to Wang Fei and
    Wang Fei want to transfer 100 to Yapeng

locked
locked
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)
Wang Fei Deposit 100
56
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
Write Record (Yapeng, BC345, 180)
Read Record (Yapeng, BC345, 150)
  • Both transactions cannot continue because they
    are holding each others record

locked
locked
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)
Wang Fei Deposit 100
57
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
Open File
0001-005
Close File
0006-0020
0021-0028
Scheduler
Get/Set File Attribute
0029-0035
. . .
1000-2000
Process XXX
2001-2800
Program YYY
2801-3500
Process ZZZ
58
Memory Process Management
  • Imagine Program YYY is the following program

Memory address
Open File
0001-005
main() for (int i2ilt100i)
if IsPrime(i) printf(i is
prime,i)
Boolean IsPrime(int N) int IsPrime1
for (int i2iltNi) if (N mod i0)
return FALSE return TRUE
Close File
0006-0020
0021-0028
Scheduler
Get/Set File Attribute
0029-0035
. . .
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

59
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

60
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
61
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!
62
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

63
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

64
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 15575ms 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

65
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

66
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?

67
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/m5
    12techa.htm

68
Questions and Contact
  • atung_at_comp.nus.edu.sg
  • http//www.comp.nus.edu.sg/atung
  • http//www.renren.com/akhtung
Write a Comment
User Comments (0)
About PowerShow.com