You are on page 1of 354

1

Nguyn l H iu Hnh
Trnh by: Quc Huy
B mn Khoa Hc My Tnh
Khoa CNTT HBK HN
2
Nguyn l H iu Hnh
Thi lng : 60 tit.
V tr mn hc : sau Tin Hc i
Cng
Mc ch mn hc: trang b kin thc v
h iu hnh, khai thc h thng hiu
qu hn.
3
Ti liu tham kho

Ting Anh:
Operating System Concept and Design Milan
Milenkovic
Modern Operating System Adrew S.
Tanenbaum
Ting Vit:
Bi ging h iu hnh Nguyn Thanh Tng
Lp trnh hp ng v my vi tnh IBM PC-
Peter Norton
Cm nang Lp trnh h thng Micheal Tischer
4
Ni dung mn hc
Chng 1: Cc khi nim c s
Chng 2: Qun l tin trnh
Chng 3: Qun l Processor
Chng 4: Qun l b nh
Chng 5: Qun l thit b ngoi vi v file
Chng 6: H thng nhiu Processors
Chng 7: virus my tnh
Chng 8: Tng quan v cc loi h iu
hnh
5
Chng 1: Cc khi nim c bn
v h iu hnh
Bi 1: Cu trc phn lp ca h
thng tnh ton
Bi 2: nh ngha v phn loi h
iu hnh
Bi 3: Cc tnh cht v nguyn tc
xy dng h iu hnh
Bi 4: Mt s hnh thi giao tip
ngi - my
6
Bi 1: Cu trc phn lp ca h
thng tnh ton
1.H thng tnh ton
gm : mi trng ca h thng v
ngi s dng.
Platform, gm c: phn cng my tnh ,
phn mm my tnh
Phn cng: - my tnh
- kt ni my tnh
- truyn thng my tnh
7
1.1 Phn cng my tnh
Kin trc chung ca my tnh
My tnh gm nhng thnh phn ?

- B vo ra
- B nh
- B vi x l
- ng truyn
8
B vo ra
B vo ra lm nhim v nhn d liu, cc
thng tin vo t cc vt mang tin nh a
t, bn phm.., chuyn n b nh qua
ng truyn di s iu khin ca b
x l,
ngc li chuyn thng tin t b nh qua
ng truyn ti cc vt mang tin, qu
trnh ny cng chu s iu khin ca b
nh.
9
B nh
lu tr thng tin trong qu trnh my tnh
x l
2 dng lu tr:
Tm thi RAM (Random Access Memory):
B nh truy xut ngu nhin
lu tr d liu v chng trnh trong qu trnh thao tc
v tnh ton
Bn vng ROM (Read Only Memory):
B nh ch c thng tin
lu tr cc chng trnh h thng, chng trnh iu
khin vic nhp xut c s
10
B nh
Nguyn tc Voneuman
B nh my tnh gm nhiu ngn nh
c nh a ch.
Khi x l th theo a ch ch khng x
l theo ni dung
11
B x l
Central Processor Unit (CPU)
Thc hin 2 cng vic:
iu khin hot ng ca cc thit b khc
Ch bin thng tin
Gm cc thnh phn:
Khi iu khin (Control Unit - CU)
Khi tnh ton s hc v logic (Arithmetic
Logic Unit ALU)
Cc thanh ghi (Register)
Cc ng truyn thng tin (internal bus)
B nh tng tc Cache
12
1.2 Kt ni my tnh
My tnh c kt ni vi h thng khc tng
hiu nng s dng
Vd:
My tnh kt ni vi my tnh -> mng my tnh.
m bo truyn thng: s dng cc giao thc
(protocol)
Mng my tnh phn loi thnh:
LAN (Local Area Network)
WAN (Wide Area Network)
GAN (Global Area Network)
Internet

13
1.3 Truyn thng my tnh
Tn hiu tng t (Analog Signal)
Truyn lin tc
Ch truyn c trn mt ng truyn
Chm, d b nhiu
Khi b ngt s phi truyn li t u
Tn hiu s (Digital Signal)
Thng tin c s ha
ng gi thnh cc gi tin (packet)
C th truyn song song trn nhiu ng
truyn
M ha, bo mt
14
2. Phn mm
Software: l cc phng thc hay
cch thc iu khin phn cng.
nh ngha: l mt h thng cc
chng trnh cho php khai thc v
m rng kh nng ca my tnh.
15
Phn loi phn mm
Tiu ch thng mi
Publicware: l nhng phn mm s dng ph
bin trong cng ng
Shareware:l phn mm c php dng th
trong mt gii hn no
Licence ware: bt buc phi mua v ng k
bn quyn
Freeware: t do sao chp, sa i
Tiu ch pht trin phn mm
Phn mm ngun ng
Phn mm m ngun m
16
Phn loi phn mm
Kin trc
ng dng ngi dng (User Application)
ng dng h thng (System
Application)
H iu hnh
BIOS
17
3. Ngi s dng
Chia lm 3 lp:
Lp trnh vin (Programmer)
Ngi s dng cui cng (end-user)
Thao tc vin (Operator)

18
4. Ti nguyn ca h thng
tnh ton
Bao gm:
Tt c cc ngoi vi
B nh
B x l
Cc chng trnh ca h thng

19
4. Ti nguyn ca h thng
tnh ton
Chia thnh 2 loi c bn:
Ti nguyn hu hn kh nng phc v
VD:
Ti nguyn v hn kh nng phc v
VD:
20
Bi 2. nh ngha v phn loi
h iu hnh
1. Mt vi nh ngha h iu hnh
T quan im ca ngi s dng:H
iu hnh l tp hp cc chng trnh,
phc v khai thc h thng tnh ton
mt cch d dng, thun tin
T quan im ca ngi qun l: H
iu hnh l mt tp cc chng trnh
phc v qun l cht ch v s dng ti
u cc ti nguyn ca h thng tnh
ton
21
Mt vi nh ngha h iu hnh
nh ngha t quan im cn b k thut: H
iu hnh l h thng chng trnh, trang b cho
mt my tnh c th to ra mt my logic mi
vi cc ti nguyn v kh nng mi.
nh ngha t quan im k s lp trnh h
thng: H iu hnh l h thng m hnh ha,
m phng cc hot ng ca my, ca ngi s
dng v ca thao tc vin, hot ng trong ch
i thoi, nhm to mi trng khai thc
thun tin v qun l ti u cc ti nguyn ca
h thng tnh ton
22
2. Phn loi h iu hnh
H iu hnh n mc ch, a mc
ch
H iu hnh n mc ch: ch phc v
cho mt chc nng duy nht trn my
tnh, gii quyt bi ton xc nh.
H iu hnh a mc ch: mt thi
im cho php thc hin mt chng
trnh phc v nhiu yu cu khc nhau
23
2. Phn loi h iu hnh
H iu hnh tp trung v phn tn
HH tp trung : trong h thng my tnh ch c mt
h iu hnh duy nht ci t vo my ch, h
iu hnh ny c chc nng khi ng v cung cp
dch v cho cc my tnh khc. Cc my ny ch yu
vo ra cc d liu. Vic x l tp trung vo my ch.
HH phn tn: Trong h thng my tnh, mi my c
h iu hnh ring khi ng c lp v c chc
nng khc nhau. H iu hnh ti my ch c gi l
h iu hnh phn tn v: Qun l cc ti nguyn h
thng khng ch ti my ch m cn c cc my trm
trong h thng. Cha s ti nguyeen ng thi cho
nhiu my khc nhau.
24
2. Phn loi h iu hnh
H iu hnh phn chia thi gian v h iu
hnh thi gian thc.
H iu hnh phn chia thi gian (Sharing time) :
mt b x l nhng thi im khc nhau phc v
cho chng trnh khc nhau hay ni khc i thi gian
x l ny b chia s cho nhiu chng trnh do vy
tn ti cc khong thi gian c nhng chng trnh b
ri vo trng thi ch i hay trng thi khng c phc
v.
H iu hnh thi gian thc : cc thi im lin tc
trong h thng chng trnh lun c phc v bi mt
CPU v c th l nhng thi im khc nhau, mt
chng trnh s c phc v bi c b x l khc nhau.
25
3. Lch s pht trin cc
h iu hnh
Th h 1 (1943-1955)
Th h 2 (1955-1965)
Th h 3 (1965-1980)
Th h 4 (1980 n nay)
26
Th h 1 (1943-1955)
Howard Aiken Havard v John von
Neuman Princeton xy dng thnh cng
my tnh
Dng cc ng chn khng vi hn 10000 ng
Lp trnh: thit lp cc cng tc
Ngn ng lp trnh: ngn ng my
Tc chm
i thit k my cng l
i lp trnh
i ch to my
Qun tr h thng
Thao tc h thng
27
A Famous Remark (1943)
"I think there is a world market
for maybe five computers."
Thomas Watson, chairman of IBM

28
Th h 2 (1955-1965)
Thit b bn dn ra i ( Transistors)
Lp trnh: trn phiu c l
Ngn ng lp trnh: FORTRAN & Assembly
Cng vic: mang tnh khoa hc
My tnh: Mainframes
H iu hnh: X l theo l Batch system

Cc trng i hc bt u mua my tnh (gi
my ln n hng triu $)

C s phn chia gia ngi thit k, ngi ch
to, ngi vn hnh, ngi lp trnh v chuyn
vin bo tr.
29
Punched Card

30
H thng x l theo l
- Mang phiu n my IBM 1401 (good at card
reading)
- c ni dung phiu ra bng t
- Cho bng t vo my IBM 7094 thc hin tnh ton
- Cho bng t vo my IBM 1401 in kt qu xut ra
ngoi


31
A Famous Remark (1957)
"I have traveled the length and
breadth of this country and
talked with the best people, and
I can assure you that data
processing is a fad that won't
last out the year.
The editor in charge of business books
for Prentice Hall
32
Th h 3 (1965-1980)
My tnh s dng cc mch tch hp (Integrated Circuit
IC)
Lp trnh: Bng t
Ngn ng lp trnh: FORTRAN & Assembly
Cng vic: khoa hc v thng mi
My tnh: IBM 360, DEC PDPs
H iu hnh: Multiprogramming/timesharing, spooling
Chng trnh h iu hnh di c triu dng hp ng v do hng
ngn lp trnh vin thc hin
Cc h iu hnh pht trin:
MULTICS (cha ca cc h iu hnh hin i)
UNIX (System V, BSD)
POSIX (bi IEEE)
MINIX (bi Tanenbaum)
Linux (derived from MINIX
33
A Famous Remark (1977)
There is no reason anyone
would want a computer in their
home.
Ken Olson, president, chairman and
founder of Digital Equipment Corp.

34
Th h 4 1980 n nay
S dng cng ngh Very large scale
instruction - VLSI
Lp trnh: bc cao
Ngn ng lp trnh: C/C++, Java,
My tnh: PC
H iu hnh: Windows, MacOS,
Linux

35
Bi 3. Tnh cht v nguyn tc xy
dng h iu hnh
1. Tnh cht chung ca h iu hnh:

A) Tin cy v chun xc
B) An ton cao
C) K tha v thch nghi
D) Hiu qu
E) Thun tin
36
Tin cy
Mi cng vic trong h thng u phi c
kim tra:
Kim tra mi trng iu kin thc hin,
Kim tra kt qu thc hin,
Nhiu chc nng KT: chuyn giao cho
phn cng.
V d: Lnh COPY A:F1.TXT B:
Sau khi KT c php, bt u thc hin
lnh. Ln lt h thng s KT g v c th
c thng bo no?

37
Kt CARD I/O,
Tn ti a?
Thit b in t a?
ng c a?
Kh nng truy nhp ca a?
Kh nng truy nhp a?
Tn ti file F1.TXT?
Kh nng truy nhp file?
. . . . . . . .

38
An ton cao
Hn ch truy nhp khng hp thc,
Hn ch nh hng sai st v tnh
hay c ,
Bo v:
Nhiu mc,
Nhiu cng c,
Nhiu thi im v giai on khc nhau.
39
K tha v thch nghi

t
Thi im to
OS
K tha
Thch nghi
40
i vi vic nng cp, tnh k tha l
bt buc
Cc thao tc, thng bo phi khng
c thay i, nu c th khng ng
k v phi c hng dn c th.
m bo tnh tha k s duy tr v
pht trin i ng ngi s dng
41
Hiu qu cao
Ti nguyn h thng phi c khai
thc trit
m bo duy tr hot ng ca ton
h thng
42
Thun tin
H thng phi d dng s dng
C nhiu mc hiu qu khc nhau
H thng tr gip phi phong ph,
a dng
Tra cu, hng dn, tooltip

43
2.NGUYN L T CHC V
HOT NG
Nguyn l m un,
Nguyn l ph chc nng,
Nguyn l Macroprocessor,
Nguyn l bng tham s iu khin,
Nguyn l gi tr chun,
Nguyn l 2 loi tham s.

44
NGUYN L M UN
M un :cc n v chng trnh,
chng trnh con, lp (class), i
tng (object)...
Mi m un phi c tnh cht:
c lp tng i v mt chc nng

M
0
I
0
I
1
O
0
M
1
I
2
O
1
M
2
I
3
O
2
45
NGUYN L M UN
Mi cng vic m un CT c lp,
Cc m un lin kt vi nhau
thng qua Input/Output:



Cc m un c nhm theo chc
nng thnh phn h thng.

M
0
I
0
I
1
O
0
M
1
I
2
O
1
M
2
I
3
O
2
46
NGUYN L PH CHC NNG
Mi cng vic trong h thng thng thng c
th thc hin bng nhiu cch vi nhiu cng c
khc nhau,
L do:
Mi m un c hiu ng ph chc nng,
Ngi dng c quyn khai thc mi hiu ng ph
khng ph thuc vo vic cng b,
Vai tr:
m bo thun tin cho ngi dng,
m bo an ton chc nng ca h thng,
V d: In mt file.

47
NGUYN L MACROPROSSECOR
Trong OS khng c sn chng trnh gii
quyt vn
Khi cn thit: H thng to ra CT v thc
hin CT to ra:








Nguyn l ny p dng vi c bn thn
ton b OS: Trn a ch c cc thnh
phn. Khi cn cc thnh phn c lp rp
thnh H IU HNH (Np h thng).

Vn Lp trnh Thc hin
Kt
qu
Macroprocessor
48
NGUYN L MACROPROSSECOR
Mi h iu hnh u s dng
nguyn l ny trong i thoi ngi-
my trn ngn ng vn hnh.
su trong phn tch c php ca
cc h thng l khc nhau
c p dng rng ri trong cc h
c s d liu
49
NGUYN L BNG THAM S IU KHIN
Mi i tng trong OS Bng tham s
(Control Table, Control Block),
H thng khng bao gi tham chiu ti i
tng vt l m ch tham chiu ti bng
tham s iu khin tng ng.
Vi cc a t, CD bng tham s ghi
phn u Vng h thng (System Area),
Vi cc files Header.
50
Cu trc file nh kiu
Header
Cc bn ghi
Gii m header
Cc trng ca
header
G
i

i

m

n

g
h
i
51
Mt s loi bng tham s :
Cho WINDOWS: Win.ini,
Cho MS DOS: Config.sys,
Cho WINWORD: Winword.ini,
Bng tham s cu hnh h thng:
phc v cho mi h iu hnh: lu
tr trong CMOS,

52
NGUYN L GI TR CHUN
Cch gi khc: Nguyn tc ngm nh
(Default),
H thng chun b bng gi tr cho cc
tham s - bng gi tr chun,
Khi hot ng: nu tham s thiu gi tr
OS ly t bng gi tr chun.
Vai tr ca nguyn l:
Thun tin: khng phi nhc li nhng gi tr
thng dng,
Ngi dng khng cn bit y d hoc su v
h thng.
53
Nguyn l gi tr chun
Tc ng ln gi tr
tham s hoc bng
gi tr chun:
Startup,
Autoexec.bat,
Control Panel
V d: c:\csdl>dir
Tham s thiu gi
tr:
a?
Th mc?
Xem g?
Quy cch a ra?
Ni ra?
54
NGUYN L 2 LOI THAM S
2 loi tham s:
Tham s v tr (Position Parameters),
Tham s kho (Keyword Param.).




Tham s kho theo trnh t tu .
C:\CSDL\DIR A:\ /S /P /ON /L /W
Tham s
v tr
Tham s kho
55
Nguyn tc bo v nhiu mc
Mi ti nguyn c c trng vt l
khc nhau -> phng php v mc
bo v, cch thc bo v khc
nhau -> xy dng nhiu lp bo v
an xen, h tr ln nhau mi c th
ngn chn cc s can thip tri php
vo ti sn h thng.
VD: Hng ro an ninh mng
56
Nguyn l khi to khi ci t
(nguyn l sinh generate)
sinh ra (khi to) mt h thng trc ht
phi la chn cc m un chng trnh t
kho m un sn c sao cho p ng c
cc yu cu ngi dng, ph hp cu hnh
k thut ca my
T chc lu tr v gn kt cc m un
chn to thnh h thng mong mun
Do phn mm Setup thc hin
57
3.Thnh phn ca h iu hnh
Thnh phn ca h thng iu khin
Thnh phn ca h thng phc v
58
4.Kin trc phn lp
Giao tip ngi -my
Kernel User mode
Kernel iu phi
BIOS
Gc ngi s dng
59
4.Kin trc phn lp
Giao tip ngi my
Qun l ti nguyn
Qun l thit b
DRIVER
BIOS
Gc ngi lp trnh h thng
60
Kin trc h iu hnh DOS

61
Kin trc Unix
62
Kin trc phn lp ca THE

63
5.Cc dch v ca h iu hnh
Thi hnh chng trnh : h thng phi c kh
nng np chng trnh vo b nh v thi hnh
n. Chng trnh phi chm dt thi hnh theo
cch thng thng hay bt thng (c li).
Thao tc nhp xut : Mt chng trnh thi hnh
c th yu cu nhp xut. Nhp xut ny c th
l tp tin hay thit b. i vi thit b c mt hm
c bit c thi hnh. tng hiu qu, ngi
s dng khng truy xut trc tip cc thit b
nhp xut m thng qua cch thc do h iu
hnh cung cp.
64
Cc dch v ca h iu hnh
Thao tc trn h thng tp tin
Thng tin : Thng tin c th c ci t
qua chia x b nh, hoc bng k thut
chuyn thng ip.
Pht hin li : h iu hnh phi c kh
nng bo li. Li xy ra c th do CPU, b
nh, trong thit b nhp xut, hay trong
cc chng trnh. i vi mi dng li, h
iu hnh s c cch gii quyt tng ng
65
6.Li gi h thng
System calls
cung cp mt giao tip gia tin
trnh v h iu hnh.
Mt s h thng cho php li gi h
thng c thc hin t cp lp trnh
ngn ng cp cao, nh cc hm v
li gi hm. N c th pht sinh li
gi t cc th tc hay gi trc tip
inline
66
Bi 4. Mt s hnh thi giao tip
ngi - my
Dng lnh (Command line)
Bng chn (Menu)
Biu tng (Icon)
67
1.Dng lnh
Khi nim:Theo nguyn l macro,
thun tin cho ngi dng trong h
iu hnh xy dng b Macro tng
ng vi cc chc nng ca HH. Mi
macro c quy cch:
Tn Macro ton hng v tham s
VD: DIR C:\ *.* / P
Tn ca macro c tnh gi nh.
68
Dng lnh (tip)
Nguyn tc xy dng: xy dng mt
h thng macro trong giao tip gia
ngi s dng vi my tnh, cc
macro ny c phn cp thnh cc
nhm ng dng
69
u nhc im
- n gin nhng c t chc
- Khng i hi cu hnh my phc tp
- Vic gn thng s vo chng trnh l
ht sc n gin.
Hn ch:
- Tc a lnh vo chm so vi cc
hnh thi khc.
- Ngi s dng phi nh tp lnh cng
knh.
- Khng thun tin cho ngi s dng
hn ch v ngn ng giao tip
70
2. Bng chn

- Menu: bng chn 1 mc, mi mc chn (pad),
cc mc chn c th sp t ty tin trn ca
s.
chn c th dng
+ Cc thit b tr: con chut, bt t, tay (i hi
phi c mn hnh cm ng).
+ Phm tt (hotkey) : gn vi mi chc nng
+ phm dch chuyn: tri, phi v enter
71
2. Bng chn
- Popup:l mt hp chn, mi mc chn
gi l 1 bar (thanh), cc bar c th t .
chn c th dng
+ Cc thit b tr: con chut, bt t, tay
(i hi phi c mn hnh cm ng).
+ Phm tt (hotkey) : gn vi mi chc
nng
+ phm dch chuyn: ln, xung v
enter
- Menu_Popup
Kt hp gia Menu vi popup.

72
Nguyn tc xy dng: s dng k thut
phn tch t trn xung, chia nh chc
nng ri nhm cc chc nng.
Qu trnh chia nh dng li khi nhn c
chc nng mc n gin nht, khi ta
xy dng mc 1 ng vi menu. Mi nhm
con mc 2 tr xung th ta gn vi 1
popup
73
nh gi:
u im :
Khng i hi cu hnh my phc tp,
khng phi nh tp lnh.
Tc a lnh vo nhanh hn.
Hn ch:
Vn cha thun tin vi nhng ngi hn
ch v ngn ng giao tip.
Khng gn c tham s trc tip cho tr-
ng trnh phi quay tr li ch dng
lnh.
74
3) Biu tng
Icon
Gm nh km theo li ch:
Anh: c trng cho chc nng
V d

Desktop (bn lm vic) : mn hnh lu biu t-
ng cc chc nng thng dng nht
75
3) Biu tng
Ca s: cc c trng thanh tiu , gc ca s bao
gi c cng c nt iu khin, 2 thanh cun dch
chuyn ni dung trong ca s. Ca s cho php
zoom, di chuyn c trn mn hnh (float). Mu (mu
nn, mu k t, mu bin); v tr ca s (ta cc
nh x1,y1 v x2,y2)
Ca s l mt vng lm vic, tng ng vi mt hay mt
nhm cc chc nng. Khi ca s tng ng vi 1 chc
nng th ca s cha cc biu tng.
Ti mt thi im c th chn nhiu chc nng (m
nhiu ca s trn mn hnh). Cc ca s c th trnh
by theo 3 cch (Xp ngi/ gi nhau; chng (overlap),
dn tri (title)).

76
Nguyn tc t chc
Tng t bng chn
77
nh gi
u: + Khng phi nh lnh
+ Thn thin d s dng
+ Nhp lnh nhanh
+ Chn nhiu lnh ng thi
Nhc:
+ i hi cu hnh my cao (b nh ln, card
mn hnh, tc my cao, cng dung ln ln)
+ Khng gn trc tip tham s cho chng
trnh
78
Chng 2: Qun l tin trnh
Bi 1. nh ngha v phn loi tin
trnh

Bi 2. Cc k thut iu

Bi 3. Tc nghn (Deadlock)
79
Bi 1. nh ngha v
phn loi tin trnh

1.nh ngha tin trnh
Trng thi ca mt h thng tnh ton ti
mt thi im c hiu l mt b thng
tin ca tt c cc thnh phn trong h
thng tnh ton.
Mt dy chuyn trng thi ca h thng
tnh ton k hiu l S0, S1,..Sn trong
S0 l trng thi u, Sn l trng thi kt
thc th ta gi l mt tin trnh.
80
1.nh ngha tin trnh
S
0
S
1
S
2
S
3
S
4
S
5
. . . . S
n-1
S
n
S
n+1
. . . .
A
B
Vic chuyn trng thi h thng tnh ton phi da
vo mt s kin pht sinh trong h thng gi l hin
tng ngt.
81
Tin trnh l mt chng trnh ang
x l, s hu mt con tr lnh, tp
cc thanh ghi v cc bin.
hon thnh tc v ca mnh, mt
tin trnh c th cn n mt s ti
nguyn
CPU, b nh chnh, cc tp tin v thit
b nhp/xut.
82
Phn bit khi nim chng trnh
v tin trnh
Mt chng trnh l mt thc th th
ng, cha ng cc ch th iu khin
my tnh tin hnh mt tc v no .
Khi cho thc hin cc ch th ny, chng
trnh chuyn thnh tin trnh, l mt thc
th hot ng, vi con tr lnh xc nh
ch th k tip s thi hnh, km theo tp
cc ti nguyn phc v cho hot ng ca
tin trnh.
83
Cc trng thi ca tin trnh
Trng thi ca tin trnh ti mt thi im
c xc nh bi hot ng hin thi ca
tin trnh ti thi im .
nguyn nhn mt tin trnh thay i trng
thi :
phi ch mt s kin no xy ra
i mt thao tc nhp/xut hon tt
buc phi dng hot ng do ht thi gian
x l
84
Cc trng thi ca tin trnh
Mi to : tin trnh ang c to lp.
Running : cc ch th ca tin trnh ang
c x l.
Blocked : tin trnh ch c cp pht mt
ti nguyn, hay ch mt
s kin xy ra .
Ready : tin trnh ch c cp pht CPU
x l.
Kt thc : tin trnh hon tt x l.
Ti mt thi im, ch c mt tin trnh c
th nhn trng thi running trn mt b x
l bt k. Trong khi , nhiu tin trnh c
th trng thi blocked hay ready
85
Cu trc d liu khi qun l
tin trnh
Process Control Block (PCB)

86
Cu trc d liu khi qun l
tin trnh
nh danh ca tin trnh (1) : gip phn bit cc tin
trnh
Trng thi tin trnh (2): xc nh hot ng hin
hnh ca tin trnh.
Ng cnh ca tin trnh (3): m t cc ti nguyn
tin trnh ang trong qu trnh, hoc phc v cho
hot ng hin ti, hoc lm c s phc hi hot
ng cho tin trnh, bao gm cc thng tin v:
Trng thi CPU: ni dung cc thanh ghi,
quan trng nht l con tr lnh IP lu tr a
ch cu lnh k tip tin trnh s x l.
Cc thng tin ny cn c lu tr khi xy ra
mt ngt, nhm c th cho php phc hi hot
ng ca tin trnh ng nh trc khi b ngt.

87
Ng cnh ca tin trnh (3)
B x l: dng cho my c cu hnh
nhiu CPU, xc nh s hiu CPU m
tin trnh ang s dng.
B nh chnh: danh sch cc khi
nh c cp cho tin trnh.
Ti nguyn s dng: danh sch cc
ti mguyn h thng m tin trnh
ang s dng.
Ti nguyn to lp: danh sch cc
ti nguyn c tin trnh to lp.

88
Cu trc d liu khi qun l
tin trnh
Thng tin giao tip (4): phn nh cc thng tin
v quan h ca tin trnh vi cc tin trnh khc
trong h thng :
Tin trnh cha: tin trnh to lp tin trnh ny .
Tin trnh con: cc tin trnh do tin trnh ny to
lp .
u tin : gip b iu phi c thng tin la
chn tin trnh c cp CPU.
Thng tin thng k (5): thng k v hot ng
ca tin trnh
thi gian s dng CPU,
thi gian ch.
Cc thng tin ny c th c ch cho cng vic nh
gi tnh hnh h thng v d on cc tnh hung
tng lai.
89
Process Control Block
Process table
H iu hnh lu con tr ti tng PCB ca
mi tin trnh trong mt bng tin trnh ca
ton h thng hoc tng ngi dng.
Truy cp nhanh ti cc PCB
Khi mt tin trnh b dng, h iu hnh loi
b tin trnh khi bng tin trnh v gii
phng tt c cc ti nguyn ca tin trnh
90
3. Process Control Block
Process table
91
Thao tc trn tin trnh
to lp tin trnh (create)
kt thc tin trnh (destroy)
tm dng tin trnh (suspend)
ti kch hot tin trnh (resume)
thay i u tin tin trnh
92
2 - Phn loi tin trnh
Tin trnh k tip
- Hai tin trnh c gi l k tip nu
thi im bt u ca tin tnh
ny l sau thi im kt thc ca
tin trnh khc.
93
2 - Phn loi tin trnh
Tin trnh song song:
Hai tin trnh c gi l song song nu
thi im bt u ca tin tnh ny
nm gia thi im bt u v kt thc
ca tin trnh khc.
BEGIN
END
t
Begin
A
B
94
3 Phn loi tin trnh song
song
A
B
C
Z
a
b
c
z
A
B
C
Z
a
b
c
z
I
I
A
B
C
Z
b
c
z
A
B
C
Z
a
b
c
z
c lp
Quan h
thng
tin
Phn
cp
ng mc
95
Song song c lp
Trong qu trnh hot ng khng trao i
thng tin vi nhau v khng dng chung ti
nguyn.
Vic t chc tin trnh song song c
lp ch yu l bo v thng tin, tin
trnh ny khng xm phm n d liu
v chng trnh ca tin trnh khc
96
Song song khng c lp
2 tin trnh c trao i thng tin vi nhau
Hai tin trnh c th trao i thng bo vi nhau. Mt
tin trnh c th gi thng bo cho tin trnh khc.
Trong mt s trng hp tin trnh nhn thng bo phi
tr li. trao i thng tin gia cc tin trnh h
thng t chc cc vng nh chung lm hm th. Mi tin
trnh khi cn nhn thng bo c th kim tra ni dung
hm th xem c thng bo hay khng v nu c th dnh
cho tin trnh no.
Cc tin trnh c th trao i thng tin vi nhau thng
qua cc cng c cp cao qua cc tin trnh iu
khin ca h thng, thng thng l qua chong trnh
gim st (monitor). Cng c trao i cp cao cho php
ch ng gi thng tin n tin trnh nhn vo ng
thi im cn thit
97
Song song c chia s ti nguyn
dng chung

Trong qu trnh hot ng mt tin
trnh c th khi to mt tin trnh
khc hot ng song song.
Tin trnh khi to l tin trnh
chnh,
tin trnh c khi to l tin trnh
con.
C hai m hnh phn phi ti nguyn :
M hnh qun l ti nguyn tp
trung : ti nguyn c phn phi trc
tip cho c tin trnh chnh v tin
trnh con.
98
Song song c chia s ti nguyn
dng chung
M hnh qun l ti nguyn phn tn. Ti
nguyn c phn phi cho tin trnh chnh,
tin trnh con nhn ti nguyn t tin trnh
chnh.
Cch phn phi ti nguyn ny n gin nhng
khng ti u v ti nguyn phi c phn phi t
sm.
Trong bt k trng hp no, tin trnh con
nhn ti nguyn u th phi tr v ni
nhn . Do tin trnh chnh phi kt
thc sau tin trnh con.
Nu tin trnh con cha kt thc th dng cc
lnh nh WAIT, POST tm ngng tin trnh
chnh, ch tin trnh con kt thc, nhn li ti
nguyn v tr cho h iu hnh.

99
Song song ng mc
hai tin trnh lun phin s dng mt ti
nguyn. Sau khi s dng xong ti nguyn
chung, mt tin trnh bn giao li ti
nguyn ny cho tin trnh khc.
Bn thn tin trnh bn giao ti nguyn
vn c th tip tc cc cng vic ring
ca mnh trong thi gian ch nhn li ti
nguyn chung.
VD : for i :=1 to 10 do write(i) ;
100
BIU DIN TIN TRNH
SONG SONG
Gi thit: S1, S2, . . ., Sn cc cng
vic thc hin song song (Trn 1
hoc nhiu my).

S
1
S
2
S
n
. . . .
101
BIU DIN
2 cch m t ph bin:
PARBEGIN COBEGIN
S
1
; S
1
;
S
2
; S
2
;

. . . . . . . . . . . . . .
S
n
S
n
PAREND; COEND;
Cc cng vic S
i
c m t chnh xc
bng mt ngn ng lp trnh c th.

102
3. Ti nguyn gng
Trong cc loi ti nguyn ca h thng
tnh ton, da vo c im v nguyn
l hot ng, c nhng ti nguyn c xem
l v hn, hu hn ti nguyn phc v.
VD : my in c coi l hu hn ti nguyn
phc v.
103
Ti nguyn gng
Nu mt thi im no , 1 ti nguyn hu
hn kh nng phc v li c nhiu tin trnh
truy cp n th thi im ny, ta ni rng
ti nguyn trng thi gng.
Mt dy cc thi im gng k tip nhau to
thnh on gng.
Trong khong thi gian tin trnh s dng ti
nguyn gng th ta gi l tin trnh gng.
mt tin trnh c th chia lm 2 on : on
gng v on cn li.
104

105
4. iu tin trnh
m bo cc tin tnh qua on gng mt
cch hp l nht.
Ti mt thi im khng c qu 1 tin trnh
vo on gng.
khng tin trnh no chim on gng trong
thi gian v hn.
Khng c tin trnh no phi ch v hn trc
khi vo on gng
C 2 mc iu s cp v cao cp.
106
iu tin trnh
mc s cp
cc lnh iu c t ngay trong chng
trnh ngi dng,
iu cao cp
lnh iu t trong thnh phn h
iu hnh.
chng trnh iu phi cng vic v iu
phi chnh
107
Bi 2. Cc k thut iu
1. K thut iu s cp :
K thut kha trong ( n hiu)
K thut kim tra v xc lp
K thut semaphore
108
a/ Phng php kha trong
Nguyn tc t chc
nu hai hay nhiu tin trnh cng nh ghi vo mt
a ch no ca b nh trong th s k thut
ch cho php mt tin trnh lm vic, cn cc tin
trnh khc phi ch
Mi tin trnh s dng mt byte trong vng b nh
chung lm kha.
Mi khi vo c on gng byte ny c gn gi tr
bng 1
khi s dng xong ti nguyn gng, gn 0 vo byte nh
du
109
chun b vo on gng mi tin
trnh phi kim tra byte kha ca cc
tin trnh khc
Nu c byte no cha 1 th phi ch
cho n khi byte nhn gi tr 0 mi
vo on gng
110
S NGUYN L
Var c1, c2:Integer;
BEGIN c1:=0; c2 := 0;
PARBEGIN
TT1: Repeat TT1: TT2:Repeat
c1 := 1; c2 := 1;
While c2 <> 0 do ; While c1 <> 0 do ;
{on gng TT1} {on gng TT1}
c1 := 0; c2 := 0;
{Phn cn li ca TT1} {Phn cn li ca TT1}
Until false; Until false
PAREND
END.
C kh nng c 2 TT cng ch i trc on gng!
Xc lp
Kim tra
111
S NGUYN L
Nguyn nhn khng p ng yu cu
iu :
Kim tra v Xc lp 2 cng vic ring
bit,
Khong cch thi gian gia 2 cng vic,
Gia 2 cng vic: Processor c th b
chuyn sang cng vic khc.
1968: Dekker cng b gii thut iu
, kt ni Kim tra v Xc lp thnh
mt khi.
112
Gii thut Dekker
Var c1,c2,tt: Integer;
BEGIN c1 := 0; c2 := 0; tt := 1;
PARBEGIN
TT1: Repeat
c1 := 1;
While c2 = 1 do
if tt = 2 then
begin c1 := 0;
while tt = 2 do ;
c1 := 1
end;
{ on gng TT 1}
c1 := 0; tt := 2;
{ Phn cn li ca TT 1}
Until false;
TT2: Repeat
c2 := 1;
While c1 = 1 do
if tt = 1 then
begin c2 := 0;
while tt = 1 do ;
c2 := 1
end;
{ on gng TT 2}
c2 := 0; tt := 1;
{ Phn cn li ca TT 2}
Until false
PAREND
END.
113
nh gi
u im : khng i hi cng c c bit.
=> c th t chc bng ngn ng bt k v cho
loi my ty .
Nhc im :
l qu phc tp, c bit khi s tin tnh v s on
gng tng.
trc khi vo on gng tin trnh phi ch i tch
cc.
Nguyn nhn:
Khng cc b ho bin trong tin trnh,
Mi TT phi t Kim tra v xc lp
114
b.Phng php kim tra v xc lp
(TEST and SET)
C s: dng lnh my TS c t cc
my tnh th h III tr i.
TS
(Test and Set)
Lm vic vi
2 bin
Thc hin:
- 2 cng vic,
-Lin tc
Bin chung
G
Bin ring
L TS(L)
L := G;
G := 1;
115
TEST AND SET
S iu : Var l1, l2, g: Integer;
BEGIN
g := 0;
PARBEGIN
TT1: Repeat
l1 := 1;
While l1 = 1 do TS(l1);
{on gng TT1}
g := 0;
{Phn cn li ca TT1}
Until false;
TT2: Repeat
l2 := 1;
While l2 = 1 do TS(l2);
{on gng TT2}
g := 0;
{Phn cn li ca TT2}
Until false
PAREND
END.
g
l1 l2
0 1
1 0 1
1
1
1
1
1 1
0 1
0
1 1
1
1
1
1
1
116
c im:
n gin, phc tp khng tng
khi s tin trnh tng. Nguyn nhn:
Cc b ho bin v tnh lin tc ca
KT & XL,
Tn ti hin tng ch i tch cc.
Nguyn nhn: Mi TT phi t a
mnh vo on gng.

117
TEST and SET
c im:
n gin, phc tp khng tng
khi s tin trnh tng. Nguyn nhn:
Cc b ho bin v tnh lin tc ca
KT & XL,
Tn ti hin tng ch i tch cc.
Nguyn nhn: Mi TT phi t a
mnh vo on gng.


118
TEST and Set
Trong trng hp c nhiu tin trnh cng
ch kh xc nh c tin trnh no s
vo c on gng sm nht. ph thuc
vo thi im gii phng ti nguyn gng
ca tin trnh ang s dng.
Gii thut ny ch thch hp khi bit
chc chn thi gian ch i l rt nh
119
K THUT N BO
Dijsktra xut 1972.
xut:
Mi ti nguyn gng c t tng
ng vi mt bin nguyn c bit S
(Semaphore),
Ban u: S Kh nng phc v t.ng.
gng,
- 2 lnh my P(S) v V(S) thay i gi
tri ca S, mi lnh lm 2 cng vic v
lm mt cch lin tc.
120
K THUT N BO
Ni dung lnh P(S):
* Dec(s);
** If S < 0 then a TT i xp hng.

Ni dung lnh V(S):
* Inc(s);
** If S s 0 then Kch hot TT ang xp hng.

121
K THUT N BO
Thc hin:
V nhiu l do, khng th ch to MT vi 2
lnh trn,
Lnh P(S), V(S) th tc tng ng.
m bo tnh lin tc:
P(S)
V(S)
X l X l
Phong to
Processor
Gii
phng
Processor
122
K THUT N BO
S iu :

Var s:Integerl
BEGIN
s := 1;
PARBEGIN
TT1:Repeat
P(s);
{on gng TT1}
V(s);
{Phn cn li TT1}
Until false;
TT2:Repeat
P(s);
{on gng TT2}
V(s);
{Phn cn li TT2}
Until false
PAREND
END.
S=1
0
-1
0
-1
0
TT1 vo
TT2 ch
TT2 vo
TT1 ch
TT1 vo
Var s:Integerl
BEGIN
s := 1;
PARBEGIN
TT1:Repeat
P(s);
{on gng TT1}
V(s);
{Phn cn li TT1}
Until false;
TT2:Repeat
P(s);
{on gng TT2}
V(s);
{Phn cn li TT2}
Until false
PAREND
END.
S=1
0
-1
0
-1
0
TT1 vo
TT2 ch
TT2 vo
TT1 ch
TT1 vo
Var s:Integerl
BEGIN
s := 1;
PARBEGIN
TT1:Repeat
P(s);
{on gng TT1}
V(s);
{Phn cn li TT1}
Until false;
TT2:Repeat
P(s);
{on gng TT2}
V(s);
{Phn cn li TT2}
Until false
PAREND
END.
S=1
0
-1
0
-1
0
TT1 vo
TT2 ch
TT2 vo
TT1 ch
TT1 vo
123
Semaphore nh phn:
Phn ln cc ti nguyn gng c kh
nng phc v = 1 S nh phn.
P(S):
If s = 0 then Xp_hng Else s := 0;
V(S):
If dng_xp_hng = NULL then
Kch_hot
Else s := 1;
Vn t tn cc th tc P v V.
K THUT N BO
124
Semaphore
c im: mi tin trnh ch phi kim
tra quyn vo on gng mt ln
sau hoc n c vo on gng
hoc phi xp hng ch.
Khi ch tin tnh khng phi lm g ht.
Mt tin trnh no khc, sau khi ra
khi on gng s nh thc tin trnh
ch tin trnh ny vo on gng.
125
2. Cng c iu cp cao
Cng c iu cp cao nm ngoi tin
trnh ngi s dng
Ngi s dng khng bit ti nguyn g v
khi no thuc loi gng => H thng phi
c trch nhim kim tra. nhn bit v
iu .
Dng chng trnh monitor (th k) iu

126
Monitor
Cu trc c bit bao gm cc th tc, cc
bin v cu trc d liu c cc thuc tnh :
Cc bin v cu trc d liu bn trong
monitor ch c th c thao tc bi cc
th tc nh ngha bn trong monitor .
(encapsulation).
Ti mt thi im, ch c mt tin trnh
duy nht c hot ng bn trong mt
monitor (mutual exclusive).

127
Monitor
Trong mt monitor, c th nh ngha cc bin
iu kin v hai thao tc km theo l Wait v
Signal nh sau : gi c l bin iu kin c
nh ngha trong monitor:
Wait(c): chuyn trng thi tin trnh gi
sang blocked , v t tin trnh ny vo
hng i trn bin iu kin c.
Signal(c): nu c mt tin trnh ang b
kha trong hng i ca c, ti kch hot tin
trnh , v tin trnh gi s ri khi
monitor.

128
Monitor

129
Monitor
Mi thi im ch c mt tin trnh lm vic
c vi Monitor.
Monitor l mt ti nguyn lgic tng qut v l
ti nguyn gng.
Mi ln mun s dng ti nguyn mi h thng
gn Monitor vo tin trnh.
Nu c th s dng ti nguyn th tin trnh c a
vo dng xp hng ch.
Khi ti nguyn gng c gii phng, Monitor nhn
iu khin v bt tn hiu gii phng tin
trnh khi trng thi ch.
130
Monitor
Monitor khng phi l tin trnh m l mt
i tng th ng.
Monitor c kch hot khi c tin trnh
cn s dng cng c ny.
Thng thng Monitor c h thng kch hot
khi phn phi ti nguyn. Cc tin trnh ng
dng kch hot Monitor t chc iu
khi mc u tin ca n khng cho php tin
trnh trc tip lm vic vi cc th tc P(S) v
V(S)
131
Bi 3. Tc nghn (Deadlock)
1. nh ngha
2. iu kin xy ra b tc
3. Phng nga b tc
4. D bo v trnh b tc
5. Nhn bit v khc phc
132
1. nh ngha

Mt tp hp cc tin trnh c nh
ngha trong tnh trng tc nghn khi mi
tin trnh trong tp hp u ch i mt
s kin m ch c mt tin trnh khc
trong tp hp mi c th pht sinh c.
133

134
135
2.iu kin xy ra b tc
4 iu kin cn c th lm xut hin tc
nghn:
C s dng ti nguyn khng th chia s
(Mutual exclusion): Mi thi im, mt ti
nguyn khng th chia s c h thng
cp pht ch cho mt tin trnh , khi tin
trnh s dng xong ti nguyn ny, h
thng mi thu hi v cp pht ti nguyn
cho tin trnh khc.
136
S chim gi v yu cu thm ti nguyn
(Wait for): Cc tin trnh tip tc chim
gi cc ti nguyn cp pht cho n
trong khi ch c cp pht thm mt s
ti nguyn mi.
Khng thu hi ti nguyn t tin trnh
ang gi chng (No preemption): Ti
nguyn khng th c thu hi t tin
trnh ang chim gi chng trc khi tin
trnh ny s dng chng xong.
137
Tn ti mt chu k trong th cp
pht ti nguyn ( Circular wait): c t
nht hai tin trnh ch i ln nhau :
tin trnh ny ch c cp pht ti
nguyn ang b tin trnh kia chim
gi v ngc li.
Khi c 4 iu kin ny, th tc
nghn xy ra.
138
Cc phng php x l tc nghn
3 hng tip cn x l tc nghn :
S dng mt quy tc (protocol)
bo m rng h thng khng bao
gi xy ra tc nghn.
Cho php xy ra tc nghn v tm
cch sa cha tc nghn.
Hon ton b qua vic x l tc
nghn, xem nh h thng khng bao
gi xy ra tc nghn.
139
3. Phng nga b tc
Phng nga b tc : m bo sao cho khng
ng thi xy ra bn iu kin gy b tc.
khng cn phi tc ng ng thi vo c bn
iu kin.
ty tnh hnh c th tc ng nhm iu kin
ny hay nhm khc.
Vic phng nga b tc l rt tn km-> ch
p dng i vi cc h thng c xc xut xy ra
b tc ln v tn tht do b tc gy ra cng
rt ln
140
Phng nga
Ti nguyn khng th chia s :
gn nh khng th trnh c iu kin
ny v bn cht ti nguyn gn nh c
nh.
Tuy nhin i vi mt s ti nguyn c
th dng cc c ch spooling bin i
thnh ti nguyn c th chia s.
141
Phng nga
cu thm mt ti nguyn th n khng
chim gi cc ti nguyn khc. C th p
t mt trong hai c ch truy xut sau :
Tin trnh phi yu cu tt c cc ti nguyn
cn thit trc khi bt u x l
Khi tin trnh yu cu mt ti nguyn mi v b
t chi, n phi gii phng cc ti nguyn
ang chim gi , sau li c cp pht tr
li cng ln vi ti nguyn mi.
142
Phng nga
Khng thu hi ti nguyn:
cho php h thng c thu hi ti
nguyn t cc tin trnh b kho v cp
pht tr li cho tin trnh khi n thot
khi tnh trng b kha.
Tuy nhin vi mt s loi ti nguyn,
vic thu hi s rt kh khn v vi phm
s ton vn d liu
143
Phng nga
t im gc:
C nh trong CT,
Theo tc nhn ngoi
(vd: thi gian)
ng dng:
Hiu chnh CT,
Thc hin cc CT di,
Vi ton b h thng: Hibernating.


m

g

c
Dump
Thi im
ngt CT
Dump
im khi phc
thc hin CT
144
Phng nga
Tn ti mt chu k: trnh to chu k
trong th bng cch cp pht ti
nguyn theo mt s phn cp nh sau :
gi R = {R1, R2,...,Rm} l tp cc loi ti
nguyn.
Cc loi ti nguyn c phn cp t 1-N.
V d : F(a) = 2, F(my in) = 12
Cc tin trnh khi yu cu ti nguyn phi tun
th quy nh : khi tin trnh ang chim gi ti
nguyn Ri th ch c th yu cu cc ti nguyn
Rj nu F(Rj) > F(Ri).
145
4.D BO V TRNH
Mi ln phn phi mt ti nguyn:
kim tra xem vic phn phi ny c
th dn n nguy c b tc cho mt s
tin trnh no hay khng v l
nhng tin trnh no?
iu kin mi trng:
Xc xut xy ra b tc nh,
Tn tht (nu c b tc) ln.
146
D BO V TRNH
Gii thut tiu biu: Ngi ch
ngn hng.
Gi thit:
Xt 1 loi ti nguyn, s lng tstb,
n tin trnh,
Max
i
,
Ffoi
i
,
Kt
i
boolean,
True chc chn kt thc c,
False trong trng hp ngc li.
147
D BO V TRNH
ts := tstb;
{Thng k}
For i := 1 to n do
begin
clai[i] := max[i] - ffoi[i];
ts := ts - ffoi[i];
kt[i] := false
end;
{nh gi}
Flag := true;
While flag do
begin flag := false;
for i := 1 to n do
if not kt[i] and (clai[i] <= ts) then
begin
kt[i] := true;
ts := ts + ffoi[i];
flag := true
end
end;
If ts <> tstb then Kh_An_Toan;
148
D BO V TRNH
Tiu chun d bo: ngt,
Da vo Kt
i
bit cc TT c nguy c b
tc,
X l trc khi TT b b tc.
c im gii thut:
n gin,
Input: Max
i
tin cy,
Mi loi ti nguyn th tc,
Mi ln phn phi kim tra.
149
5.NHN BIT V KHC PHC
nh k kim tra cc TT ch i
pht hin b tc,
iu kin p dng:
Xc xut xy ra b tc b,
Tn tht (nu c b tc) b.
p dng vi phn ln OS trong thc
t,
Do OP m nhim.

150
Khc phc
2 la chn chnh hiu chnh tc
nghn:
nh ch hot ng ca cc tin
trnh lin quan
da trn vic thu hi li cc ti nguyn
ca nhng tin trnh b kt thc
Thu hi ti nguyn
151
nh ch hot ng ca cc tin
trnh lin quan
tt c cc tin trnh trong tnh trng tc
nghn
tng tin trnh lin quan cho n khi
khng cn chu trnh gy tc nghn : chn
c tin trnh thch hp
u tin,
thi gian x l,
s lng ti nguyn ang chim gi ,
s lng ti nguyn yu cu
...
152
Thu hi ti nguyn

Cn gii quyt 3 vn sau:
Chn la mt nn nhn: tin trnh no s
b thu hi ti nguyn ? v thu hi nhng
ti nguyn no ?
Tr li trng thi trc tc nghn: khi thu
hi ti nguyn ca mt tin trnh, cn phi
phc hi trng thi ca tin trnh tr li
trng thi gn nht trc m khng
xy ra tc nghn.
Tnh trng i ti nguyn : lm sao
bo m rng khng c mt tin trnh
lun lun b thu hi ti nguyn ?
153
Bi 4. Lung (Thread)
1.nh ngha
Qu trnh nh (lightweight process Lwp)
n v c bn ca vic s dng CPU
Mi tiu trnh x l tun t on code ca
n,
Gm:
nh danh lung (thread ID)
B m chng trnh
Tp thanh ghi
Ngn xp

154
Cc tiu trnh chia s CPU vi nhau
ging nh cch chia s gia cc tin
trnh: mt tiu trnh x l trong khi
cc tiu trnh khc ch n lt.
Mt tiu trnh cng c th to lp cc
tin trnh con, v nhn cc trng thi
khc nhau nh mt tin trnh tht
s.
Mt tin trnh c th s hu nhiu
tiu trnh.
155
Chia s vi cc lung khc thuc
cng mt tin trnh
Code
Data
OS resource
Tp tin, tn hiu
Nu tin trnh c nhiu lung iu
khin, n c th thc hin nhiu hn
mt tc v ti mt thi im
156
Tin trnh n lung v a lung
157
Cc tin trnh hon ton c lp vi nhau,
ch c th lin lc thng qua cc c ch
thng tin gia cc tin trnh m h iu
hnh cung cp.
cc tiu trnh trong cng mt tin trnh li
chia s mt khng gian a ch chung ->
cc tiu trnh c th chia s cc bin ton
cc ca tin trnh.
Mt tiu trnh c th truy xut n c cc
stack ca nhng tiu trnh khc trong
cng tin trnh.
khng c c ch bo v
158
Li ch
4 loi:
S p ng (Responsiveness) : cho
php mt chng trnh tip tc chy
thm ch nu mt phn ca n b
kha hay ang thc hin mt thao
tc di -> gia tng s p ng vi
ngi dng.
VD: trnh duyt web p ng bng 1
lung
nh c ti bng 1 lung
159
Chia s ti nguyn (Resource Sharing) :
Mc nh chia s: b nh v ti nguyn ca
tin trnh
Cho php ng dng c nhiu hot ng trong
cng khng gian a ch.
Kinh t (Economic): tn t th gian to
v qun l lung hn tin trnh
VD: Solaris 2: to qu trnh chm hn 30 ln
so vi lung
chuyn ng cnh chm hn 5 ln
160
S dng kin trc a x l (Utilization of
MP Architectures):
Tin trnh n lung ch chy trn mt CPU
Mi lung thc thi song song trn mt b x l
khc nhau -> gia tng tnh ng hnh
Trong kin trc n x l: CPU chuyn i
qua li gia cc lung nhanh -> to o gic
ca song song

161
2.User thread v kernel thread
User thread:
c h tr v ci t bi th vin lung
mc ngi dng.
Th vin h tr:To lung, lp lch, qun
l lung
c khi to v qun l nhanh, khng c
s can thip ca h thng
Li gi h thng -> gy nghn h thng
Posix Pthreads, Mach C-thread, win32
thread


162
Kernel thread
H tr trc tip bi h iu hnh
To lung, lp lch v qun l
c to v qun l chm hn user
thread
Windows NT, windows 2000,solaris
2, Tru64 Unix h tr kernel thread
163
3. M hnh a lung
3 m hnh:
Nhiu - mt
Mt - mt
Nhiu - nhiu
164
M hnh nhiu- mt
nh x nhiu lung
cp user ti mt
lung cp nhn.
Qun l lung thc
hin trong khng
gian ngi dng
Khng th chy
song song trn
nhiu b x l
165
M hnh mt mt
nh x mt lung ngi
dng ti mt lung nhn
Mt lung khc chy khi
mt lung khc thc hin
li gi h thng gy nghn
Nhiu lung chy song
song trn cc b x l
khc nhau
Chi ph cho vic to lung
nhn nh hng hot ng
ca OS -> gii hn s
lung nhn
Windows NT, windows
2000 v OS/2
166
M hnh nhiu nhiu
a hp nhiu user
thread ti s lng <
hoc = cc kernel
thread
Mt ng dng c th
c cp nhiu lung
nhn khi trong h
thng nhiu b x l
Ngi dng c th to
nhiu lung, cc lung
nhn tng ng c
th chy song song
trn b a x l
Solaris 2 v Tru64
Unix
167
Pthreads
Tham chiu ti
chun POSIX (IEEE
1003.1c) nh
ngha API cho vic
to v ng b
lung.
c t cho hnh vi
lung h iu
hnh khc nhau ci
t khc nhau

168
169
170
171
Lung Solaris 2
n bn Unix h tr lung ti cp nhn
v cp ngi dng
Pthread API
Gia user v kernel thread l mt Lwp
Mi tin trnh cha t nht mt lwp
Th vin lung a hp user thread trn
cc lwp
Mi lwp c mt kernel thread
Kernel thread hc thi tt c thao tc trong
nhn.
M hnh nhiu nhiu
172
Chng 3: Qun l Processor
Chng trnh mun thc hin th trc tin
phi np vo b nh.
Nhng nu chng trnh khng c quyn
s dng processor th vn khng thc
hin c.
processor c th c xt nh l ti nguyn
phc v cho vic thc hin chng trnh.

173
1. Processor l gc v Processor
vt l.
Ch a chng trnh phn chia thi gian mi
ngi s dng c cm gic nh ch c mt mnh
mnh l ngi duy nht s dng ton b h
thng.
Tt c cc h iu hnh thc hin song song
u do mt processor ca h thng
processor vt l iu khin.
ngi s dng nh gi hot ng processor trn
c s quan st tin trnh ca mnh thc hin ->
Processor ngi s dng quan st v nh gi :
processor lgic.
processor logic gn vi tin trnh.
174
1. Processor l gc v Processor
vt l.
TT
3
TT
1
TT
2
t
0 t
1
t
2
t
3
t
4
t
5
t
6
t
t
USER 3 LP3
USER 1 LP1
USER 2 LP2
175
1. Processor l gc v Processor
vt l.
Ch x l k tip n chng trnh:
mt tin trnh c thc hin xong mi khi to v thc hin tin
trnh khc -> c th ng nht processor logic vi processor vt
l.
km hiu qu v ti nguyn processor b lng ph nhiu.
Nng cao hiu qu khai thc ca ton h thng ni
chung:
phn chia b nh
phn phi thi gian ca processor cho nhiu tin trnh ng
thi tn ti.
i vi mi tin trnh, tc thc hin s b gim :
thi gian truy nhp b nh tng
khng c quyn s dng processor lin tc.
i vi ngi s dng: thay i tm l khai thc h thng.

176
Cc vn cn phi gii quyt
i vi cc h thng nhiu
processor

a)S lng processor logic nn to ra l bao nhiu?
b) di khong thi gian gn lin tc
processor vt l cho mt processor lgic l bao
nhiu th hp l?
c)Sau khi mt processor lgic ht quyn s
dng processor vt l th cn chn tin trnh
no phn phi processor vt l?
Cc cch gii quyt khc nhau ba vn nu
trn to ra cc chin lc iu tin trnh khc
nhau.
177
2 - Cc trng thi tin trnh
TT
Sn sng
TT
Thc hin
TT
Ch i
KT CT

Ch phn chia thi
gian
178
Mi thi im ch c mt tin trnh
ginh c processor thc hin lnh
ca mnh.
Tin trnh ny nm trong trng thi thc
hin.
Tin trnh c phn phi y mi ti
nguyn ch thiu processor-> trng thi
sn sng.
179
Nu tin trnh khng th thc hin tip c v thiu mt vi iu
kin no th n nm trng thi b ngt.
Tin trnh khng th sn sng thc hin c v s kin m n
cn cha xy ra
tin trnh gi ti mt mul nhng m un ny cha c np v
nh v trong b nh
tin trnh ang thc hin trao i vo ra v thit b vo ra
cha bo l xong php trao i ch nh.
tin trnh b ngt khng chim b nh v ch
trong mt s trng hp c th a tin trnh ra b nh ngoi.
Mt s h thng thm ch cn chuyn bt tin trnh sn sng
ra b nh ngoi v nh vy chuyn n sang trng thi b ngt.
Nhn ton cc h thng : mi thi im ch c khng qu mt
tin trnh nm trong trng thi thc hin, cn cc trng thi sn sng
v b ngt c th c nhiu tin trnh.
180
Mi tin trnh, d ang trng thi no i na
cng c lc nhn y mi ti nguyn, c th
ch tr processor vt l. Nh vy n c mt
processor logic.
Giai on cung cp v gn ti nguyn cho tin
trnh k c processor lgic gi l khi to tin
trnh.
Trong ch phn chia thi gian, tin trnh -
c khi to li mi ln ngi s dng c php
giao tip vi h thng. Tin trnh ny lp tc -
c chuyn sang trng thi sn sng.
181
Trong ch x l theo l, cng vic c t choc xp hng ch
nhp v khi to, thm ch c khi c phn phi mi ti
nguyn ch cn ch nhn processor lgic.
Gi thit mi cng vic trong ch x l l ch tng ng vi mt
tin trnh.
n mt lc no cng vic c chn, tc l h thng tin hnh
phn phi ti nguyn cho n. Mt tin trnh mi c khi to v c
chuyn trng thi sn sng.
Khi tin trnh thc hin xong h thng s ly li processor lgic v
tin trnh khng cn tn ti na. hon tt mi x l theo ng
yu cu ngi s dng cng vic c chuyn sang xp hng ch v
s trn my v hay truyn thng tin lin lc ng di. Tin trnh
ch phn chia thi gian ra thng khi h thng t trng thi thc
hin bng cch tr li h thng processor lgic.
182
Tin trnh c th ri b trng thi
thc hin v mt trong ba l do:
Tin trnh hon thnh mi vic cn
thit
tin trnh tr processor lgic v c chuyn
sang ch x l kt thc.
Tin trnh t ngt
chng hn sau khi a yu cu trao i
vo ra v phi ch cho php trao i
ny kt thc mi lm vic tip c. Tin
trnh chuyn t trng thi thc hin sang
b ngt v mi cho n khi xut
hin s kin m n ch n,
183
Tin trnh s dng processor vt l ht thi gian
ginh cho n v c chng trnh iu chuyn t
trng thi thc hin sang sn sng.
Vic chuyn trng thi tin trnh v thc cht l phn
phi li ti nguyn h thng, trong trng hp ny ch
yu l ti nguyn processor vt l. Cc ti nguyn khc
nh b nh, my in, mn hnh u c th c phn
phi li.
C ti nguyn d phn phi li
b nh tng i d
processor d phn phi li. nht.
C ti nguyn km linh hot -> kh phn phi li.
My in rt kh phn phi li


184
Khng phi mi h iu hnh u phn phi li
processor.
Nu tin trnh b ngt nm trong b nh v s kin n
ch xy ra th chng trnh iu khin s chuyn tin
trnh t trng thi b ngt sang trng thi sn sng.
Nu tin trnh b a ra b nh ngoi th n s c
a li vo b nh trong v sau chuyn sang trng thi
sn sng.
T trng thi ny tin trnh chuyn sang trng thi thc
hin khi chng trnh iu chn n.
Nu tin trnh va khi phc c th t u tin phc v
cao th c th c chuyn ngay t trng thi sn sng
sang trng thi thc hin.

185
chng trnh iu khin m nhim.
Vic phn phi v phn phi li processor vt l
chn processor lgic
Chn cng vic v xy dng tin trnh ng vi
n do chng trnh iu phi m nhim.
phn phi b nh trong
b nh ngoi
gn cc file lm vic vi tin trnh
cung cp cc ti nguyn khc
chuyn tin trnh sang trng thi sn sng.
t chc x l kt thc tin trnh.
186
chng trnh iu m nhim
chn tin trnh sn sng
phn phi processor vt l cho tin trnh.
Do tnh cht cng vic nn chng trnh iu
i khi c gi l chng trnh iu phi
cp thp , cn bn thn chng trnh iu phi
th gi l iu phi cp cao.
Khc bit c bn gia iu phi v iu l
tin trnh ch cn mt ln c iu phi nhng
c th cn c iu nhiu hn.
187
3. iu tin trnh
(scheduling).
Mt trong nhng chc nng ca chng trnh iu l
chn tin trnh thc hin, tc l chn tin trnh
sn sng v phn phi processor vt l cho n.
Mi tin trnh sn sng c gn mt th t u tin.
Th t u tin c xc nh da vo nhiu yu t khc
nhau
thi im hnh thnh tin trnh
tng thi gian tin trnh c thc hin
thi gian ngi s dng d bo kt thc.
Th t u tin ny -> u tin iu ( phn bit vi u tin
iu phi phc v cho hot ng ca chng trnh iu
phi).
Mi cch to th t u tin iu khc nhau s lm xut
hin mt phng thc iu .
188
Mc tiu iu phi
S cng bng ( Fairness) :
Cc tin trnh chia s CPU mt cch cng bng, khng
c tin trnh no phi ch i v hn c cp pht
CPU
Tnh hiu qa (Efficiency) :
H thng phi tn dng c CPU 100% thi gian.
Thi gian p ng hp l (Response time) :
Cc tiu ho thi gian hi p cho cc tng tc
ca ngi s dng
Thi gian lu li trong h thng ( Turnaround Time) :
Cc tiu ha thi gian hon tt cc tc v x l
theo l.
Thng lng ti a (Throughput ) :
Cc i ha s cng vic c x l trong mt n
v thi gian.

189
Tiu chun s lng nh gi cht lng iu l thi gian ch i x
l.
thi gian mt tin trnh trng thi sn sng ch c phn phi
processor vt l.
ngi s dng quan tm c thi gian tin trnh b ngt, thi gian
ch chng trnh iu phi x l.
Khi tin trnh b ngt -> khng c xt phn phi processor,
thi gian tin trnh nm trong trng thi ngt khng c tnh vo
thi gian ch i nh gi cht lng iu .
ch phn chia thi gian s dng tp th ngi s dng khng
quan tm n tng thi gian s dng processor m ch lu n
thi gian cn thit phc v yu cu cui cng va a vo.
i vi lp ngi s dng trong ch ny khng tn ti thng tin
cn thit nh gi thi gian phc v, cng nh khng c
cng c th hin c nhng nh gi nu c.
190
3. iu tin trnh (scheduling)
(tip).
2 ch thng dng
a. Ch mt dng xp hng
b. Ch hai dng xp hng

191
Ch mt dng xp hng

V nguyn tc cch thc iu tt nht l m bo
cho mi ngi c phc v nh nhau. -> khng c mt tin
trnh no phi ch i lu hn tin trnh khc. -> nh
gi cht lng iu da vo thi gian ch i trung
bnh.
Gi thit
h thng ang xt hot ng lu
dng xp hng cho iu phi nhim v, cho iu ,
dng xp hng tin trnh b ngt l ln
phn ng ca h thng khng thay i theo thi gian,
-> h thng hot ng trong ch n nh.
Khi i vi mi tin trnh chuyn sang trng thi sn sng
o c thi gian ch i: khong thi gian k t khi tin trnh
chuyn sang trng thi sn sng cho ti khi n chuyn sang
trng thi thc hin.
192
i vi mi tin trnh ta c th c nhiu ln
khong thi gian ny -> tnh c thi gian ch
i trung bnh ca mt tin trnh c th.
S quan st cng ln th thi gian ch i trung
bnh -> cng chnh xc.
nh gi cht lng iu = vic phn tch
gii thut iu + kt qu thc nghim
Gi thit: quan st v thng k thi gian chi i
ca tng tin trnh ch khng phi ca mt tin
trnh->thu c thi gian ch i trung bnh
chung ca h thng.
193
Chin lc phc v bnh ng l m bo cho mi tin
trnh u c mt thi gian ch i trung bnh nh nhau.
Chin lc FCFS (First come first served).
theo trnh t xut hin ca tin trnh trong dng xp
hng sn sng v mi tin trnh c phc v n khi
kt thc hoc khi n phi chuyn sang trng thi b
ngt.
VD: Xp hng bn go
n gin, d t chc:
processor khng b phn phi li. Chi ph t chc
FCFS l thp nht v khng phi thay i th t u tin
iu v khng phn phi li processor
bn thn th t u tin iu chnh l th t xut
hin ca tin trnh trong dng xp hng sn sng.
194
Nhc im:
Nhc im c bn ca FCFS l cc tin trnh ngn
cng phi ch nh nhng tin trnh di. Ngi s dng
khng quan nim thi gian ch i trung bnh nh
nhau l bnh ng trong phc v.
Thi gian ch i trung bnh s tng v hn khi h
thng n gn gii hn kh nng phc v ca mnh.
Nu pht tn thi gian thc hin tin trnh tng
th thi gian ch i trung bnh cng tng theo.
khi gp tin trnh t b ngt th cc tin trnh khc
s b xp hng lu, nhiu thi im tin trnh phI
ch v hn.
Mi chin lc khng phn phi li processor u c
nhc im ny.
195
chin lc SJN (Shortest Job
Next)
khc phc nhc im FCFS -> xy dng cc chin
lc m bo cc tiu ha thi gian ch i trung bnh
chung.
xc nh th t u tin iu trong qu trnh thc hin
tin trnh ch khng phi l lc khi to.
tin trnh c tng thoi gian thc hien t nht c u
tin thc hin trc u tin tin trnh ngn nht.
Thng thng tin trnh ny p dng trong ch khng
phn phi li processor.
So vi FCFS, SJN gim thi gian ch i trung bnh
chung, c th l thi gian ch i trung bnh ca cc
tin trnh ngn, nh hn thi gian ch i trung bnh
ca cc tin trnh di.
196
So vi FCFS trong chin lc ny thi gian ch i trung bnh ca cc
tin trnh di ln hn, ngoi ra pht tn ca thi gian ch i
cng tng. -> kh d bo c khi no tin trnh di c phc v.
i vi nhiu ngi s dng d bo c kh nng phc v cng quan
trng khng km tc phc v.
SJN nhanh chng loi b cc tin trnh ngn, gim s tin trnh
trong dng xp hng sn sng.
C th p dng ch phn phi li processor trong SJN, ngt
tin trnh di ang thc hin phc v tin trnh ngn hn mi
xut hin trong dng xp hng sn sng.
Nhng vic phn phi li processor ny ch cng lm cho cc tin
trnh ngn c li hn.
nu tin tnh mi c tng thi gian i hi phc v nh nhng li ln
hn thi gian cn thit thc hin tin trnh ang hot ng ->
vic chuyn sang tin trnh mi l khng hp l.

197
Chin lc (Shortest Remaining
Time)
khc phc nhc im ny -> u tin tin trnh c thi
gian cn li t nht (SRT)
Chin lc ny i hi phi c tng thi gian phc v cho
mi tin trnh.
Thi gian cn li = hiu gia thi gian m ngi s dng d bo
vi thi gian phc v.
C th khng phn phi li processor nhng nu vy mi u
im c bn ca SRT s mt theo.
V vy chin lc ny thng thng c phn phi li
processor tng t nh chin lc SJN c phn phi li
processor.
Chin lc ny m bo thi gian ch i trung bnh l nh
nht.
C SRT ln SJN u da trn c s c thng bo trc v
thi gian cn thit phc v tin trnh -> khng thch hp vi
ch s dng tp th phn chia thi gian.
198
Trong SJN v SRT th t u tin iu ph thuc vo
thi gian thc hin chng trnh m ngi s dng khai
bo.
Thi gian khai bo cng t -> mc u tin nhn c cng
cao. -> ngi s dng c th nng mc u tin iu
bng cch khai thi gian t i.
chng li hin tng ny c th kt thc bt thng
tin trnh khi ht thi gian yu cu hoc p dng n
gi khc cao hn i vi thi gian ph tri.
lu : d p dng chin lc iu no th cng vn
gp nhng tin trnh b lp v hn ln -> v vy vi
mi tin trnh u phi p dng gii hn thi gian phc
v.
Gii hn ny ph thuc vo tng lp bi ton hoc xc
nh ring cho tng tin trnh. Khi tin trnh c
phc v ht thi hn ti a n s b bt buc phi kt
thc bt thng.
199
gim thi gian ch i trung bnh, h
thng c th xc nh s th t u tin
iu cao cho tin trnh c kh nng
s dng t thi gian processor
vd: cc tin trnh cn trao i vo ra
nhiu -> nhn c s u tin cao.
thng p dng trong ch nhiu dng
xp hng.
200
phng php vng trn RR (Round
Robin).
Mi tin tnh trong dng xp hng ln lt c
phn phi mt lng t thi gian thc hin.
Sau khong thi gian , nu tin trnh cha
kt thc hoc khng ri vo trng thi ch i
th n c chuyn v cui dng xp hng sn
sng.
cc tin trnh xp hng vng trn v lun lun
c m bo phc v v vyh sm hay mun
cng s kt thc.
Khi c tin trnh mi, n s c a vo dng
xp hng vng trn, t v tr s c phc
v ngay lp tc.
cc tin trnh ngn, di u c coi nh nhau.
201
c th u tin thch ng vi tin tnh di -> phn cc tin
trnh thnh m lp,
lp th i tin trnh c phc v Ki ln, mi ln vi thi gian
Ti.
Nu sau thi gian dnh tin trnh cha kt thc hoc
khng b ngt th n c chuyn sang lp (i+1), vi thi gian
phc v Ti+1 > Ti.
Lng t thi gian phc v tng dn cho n khi tin trnh ri
vo lp cui cng lp M, n s c phc v vi lng t Tm
khng i.
th t u tin iu tng dn theo thi gian xp hng ch
i.
Cch phc v ng mc theo lp nh vy -> cho php h
thng u tin ngm cc tin trnh ngn v n s kt thc sm
hn cc tin trnh di.
Tuy vy u tin ngm ny khng gy tn hi qu ln cho cc
tin trnh di.
202
Do phi thng xuyn phn phi li processor, nn thi gian ch i
trung bnh chung trong RR c th ln hn so vi phng php FCFS.
Tuy vy nu pht tn thi gian thc hin cc tin trnh ln th
thi gian ch i trung bnh chung ca RR c th nh hn so vi
FCFS.
Chin lc iu vng trn c p dng rng ri trong ch s
dng tp th phn chia thi gian.
Trong RR kch thc lng t thi gian q ng vai tr ht sc quan
trng.
Nu q ln th vic b sung thm tin trnh mi hoc tin trnh c
kch hot li t trng thi b ngt s lm dng xp hng di ra v
iu ny lm tng thi gian ch i trung bnh.
nu q qu nh th n s lm cho cc tin trnh phi lin tc chuyn
trng thi v v vy gim h s hu ch trong hot ng ca
processor. Tm li phi cho q nhn gi tr hp l.
Thng thng q c chn trong khang 200 20 mili giy.
203
Tn ti mt vi kiu phc v khc nhau da trn c s chin lc RR.
Trong ch s dng tp th phn chia thi gian ngi ta phi
khng ch thoi gian phn ng khng qu t.
Nu c n tin trnh sn sng th ly q=t/n.
Nu xut hin tin trnh mi th cho vo dng xp hng khc-> x
l xong mt lt dng xp hng ban u mi chuyn sang dng xp
hng th hai.
Nu thi gian chuyn processor t tin tnh ny sang tin trnh
khc l s th trn thc t phi chn q=t/n s.
Vi t cho trc ngi ta phi khng ch s tin trnh c q nm trong gii
hn hp l.
Nu c kh nng d bo c thi gian trao i vo ra th c th
khng ngt tin trnh khi thy php vo ra kt thc trc khi ht l-
ng thi gian phc v.
thc y cc tin trnh di mau kt thc ngi ta tng mc u tin
cho cc tin trnh tn ti lu trong dng xp hng sn sng.
Mc u tin c tnh theo hiu gia thi gian hin ti vi thi im
khi to tin trnh sn sng. Tr s cng ln th mc u tin cng
cao.
204
Ch nhiu dng xp hng.
Da vo thng tin do ngi s dng cung cp v kt qu
phn tch ca h thng -> phn lp tin trnh sn
sng v phc v mi lp theo mt cch.
Trong ch x l l cc tin trnh c s php vo ra
nhiu c xp vo lp ring v lp ny c th t u tin
iu cao.
c th khng cn t chc phn phi li processor, bn thn
vic ngt tin trnh t ng to hiu ng phn phi li.
C ba lp tin trnh phi x l ring:
- Tin trnh thi gian thc
- Tin trnh ca ch s dng tp th phn chia thi
gian
- Tin trnh x l l.
205
Tin trnh thi gian thc phi c phc v xong trc mt
thi im no , thng thng do c trng thit b
ngoi vi quyt nh.
Tin trnh ch phn chia thi gian phi c phc
v trong phm vi m ngi s dng chp nhn c, ni
chung phi to cm gic nh ngi s dng c lm vic
lin tc, khng h b gin on. Tin trnh x l l
khng i hi nhiu v thi im v khong thi gian
c phc v lin tc.
Nhiu dng xp hng cho php iu tin trnh linh
hot hn. Mi dng c th c chin lc ring hoc cc
gi tr tham s khc nhau. mt s h thng tin trnh
khng thay i lp trong sut qu trnh c phc v. C
nhng h thng cho php hcuyn tin trnh t dng
xp hng ny sang dng xp hng khc.
206
TT
u tin cao
TT
u tin thp
Thc hin
D

n
g

x

p

h

n
g

n

n
207
Mt trong s cch t chc nhiu dng xp
hng hay p dng l t chc hai dng xp
hng:
mt cho cc tin trnh trao i vo ra nhiu,
mt cho cc tin trnh tnh ton nhiu.
tin trnh dng th nht c mc u tin cao
hn trong thc hin, tuy vy do phi trao i
vo ra nhiu chng s khng s dng ht thi
gian c c.
Cc tin trnh tnh ton s c thc hin da
trn nn ca cc tin trnh dng xp hng th
nht.
Lu : tin trnh dng no th phi quay v
ng dng nu cha kt thc.
208
c. m bo kt thc ng thi hn

m bo kt thc ng thi hn l i hi mc cao hn m bo
kt thc tin trnh. Tin trnh chng nhng phi kt thc m cn
kt thc trong thi hn quy nh: Trc mt thi im no hoc
sau khong thi gian nht nh, cc tin tnh thi gian thc v tin
trnh ca ch phn chia thi gian i hi c rng buc cht
v thi gian.
C th iu phi cc tin trnh i hi cht ch v thi hn khng
kh khn lm bng cch sp xp tin trnh theo th t thi im
cn kt thc. Nhc im ca cch tip cn ny l c th c tin
trnh c phc v nhanh hn mc cn thit v nh hng ti cc tin
trnh khc cn nhiu thi gian processor. Chng hn mt tin trnh
cn 5 pht trong vng 20 pht ti. Nu tin trnh ny c th t u
tin cao, n s c thc hin ngay mc d cha tht cn thit v lm
mi tin trnh phi ch i 5 pht lin k c cc tin trnh ngn.
Gii php trong trng hp ny l phn phi processor nhiu ln cho
tin trnh, b tr ri rc trong sut qu trnh m n cn.
Gii thut ny thng p dng vi cc tin trnh thi gian thc v i
vi loi tin trnh ny quan trng l thi im kt thc.
209
Ngt v x l ngt:
nh ngha :
tin trnh c th thc hin chnh xc cn c s
phi hp nhp nhng gia hot ng ca processor vi
hot ng ca cc thit b vo ra v ngoi v. Ngt l
phng tin cc thit b khc trong h thng bo cho
processor bit vic thay i trng thi ca mnh.
Trn quan im lp trnh, t gc processor : ngt
ngng t xut vic thc hin mt tin trnh
chuyn sang thc hin tin trnh khc khi c mt s
kin no xy ra.
-> vai tr ca ngt trong h thng l cng c chuyn
iu khin ti mt tin trnh khc m tin trnh hin ti
khng bit.
210
ALU
A
d
d
r
D
e
c
o
d
e
S kin
Ngt
trong
Phn loi
Ngt c chia thnh hai loi chnh : ngt trong v ngt ngoi.
211
Ngt trong : ngt ng vi cc s kin xut hin bi hot
ng ca chnh processor.
s kin gy ra ngt trong: vic chia cho khng, trn nh...
trong ch qun l b nh theo chin lc phn on hoc
phn trang: truy nhp ti trang hoc on cha np vo b nh.
Nhiu my tnh c hai ch hot ng supervisor v
ch ngi s dng.
ch supervisor mi lnh u c th thc hin c.
ch ngi s dng th ch mt phn ca h lnh c m
bo thc hin.
lnh ch thc hin c trong ch supervisor gi l lnh h
thng.
trong ch ngi s dng processor gp lnh h thng ->
xut hin ngt trong.
212
Ngt ngai xut hin khi xy ra cc s kin
nm ngoi tin trnh ang thc hin.
VD: tn hiu t ng h, mt ngun, c s c k
thut trm trng ....
(Cc s kin gy ra bi cc thnh phn nm ngoi
h thng tnh ton).
- Ngt chn c v ngt khng chn c.
Ngt chn c : cc ngt m b x l c th b qua
khng x l. VD : nh ngt ng h (c mi xung
nhp li xut hin mt ngt).
Ngt khng chn c : cc ngt m b x l khng
th b qua c. VD : s c k thut, hng hc -> my
tnh khng hot ng tip c.
213
- Ngt cng v ngt mm.
Ngt mm : nhng li gi ngt thng c
t ngay trong chng trnh ngi s dng.
VD : Lnh intr trong Pascal. trong C..
Ngt cng : cc ngt gy ra bi processor v
cc phng tin k thut
H thng nh k sau khong thi gian
nht nh xem xt c tn hiu ngt hay
khng. Tham s k thut ny l rng buc
phi lu khi xy dng cc chng trnh h
thng.
214
T
T

b


n
g

t
T
T

x


n
g

t
S


k
i

n
(
E
v
e
n
t
)
1
2
3
4
5
Mc x l I
Mc x l II
Cc bc x l ngt
215
Ba bc u do phn k thut m
nhim.
Hai bc sau do h iu hnh thc hin.
X l s kin c m bo bng chng
trnh x l ngt.
Mi loi s kin <-> chng trnh x l
ngt ring.
chng trnh x l ngt
lu li cc thng tin v trng thi ca tin trnh
ngt
216
Phn tip theo l on chng trnh x l s kin.
Mi loi s kin i hi mt cch x l ring,
n gin nh xc lp mt vi bit trong thanh ghi c hoc khi
trng thi
phc tp nh x l sai st khi c a t.
Nu s kin khng i hi phi x l gp th h thng c th
a tin trnh x l vo dng xp hng sn sng.
Phn cui: khi phc trng thi tin trnh b ngt
m bo cho h thng tip tc hot ng bnh thng.
Nu tin trnh b ngt khng th tip tc thc hin v s
kin xy ra( chng hn v l do b trn nh trong tnh ton) ->
sau khi thng bo nguyn nhn chng trnh ngt chuyn tin
trnh b ngt sang b phn x l kt thc.
217
Mt s hnh ng trong x l ngt phi c thc hin ngay lp tc
nh trng thi tin tnh, x l li...
chng trnh thc hin cc x l ny phi thng trc trong trong b
nh v l mt b phn trong nhn ca h iu hnh.
x l t cp bch hn s do cc chng trnh khng thng trc m
nhim. -> bn thn x l ngt thng c chia thnh hai mc.
ngt lin quan ti iu khin v hot ng ca processor thng c
chuyn ti chng trnh iu ngay lp tc kch hot tin
trnh tng ng.
-> trong dng xp hng sn sng lun lun c cc tin trnh h
thng m bo iu phi, thu thp thng tin thng k, phc v x l
ngt mc hai...
Khi ch c mt processor th bn thn cc tin trnh h thng
cng phi xp hng ch x l.
218
Ngt kp
cc s kin gy ngt xut hin ng thi
hoc c cc thit b k thut ghi nhn
cng mt thi im.
ngt c th xut hin ngay trong lc
ang x l ngt.
gii php x l ngt kp : gn cho mi loi
ngt mt th t u tin.
Nu xy ra ng thi nhiu ngt th ngt
c u tin cao nht s c x l trc.
219
t chc stack-> khng cn gn u tin iu cho ch-
ng trnh x l ngt,
Ngt c th c m hay b chn.
Khi b chn, tn hiu ngt khng tc ng ln hot ng ca
processor.
Ngt b chn c th b mt hoc c xp hng x l. Nu
c xp hng th sau khi c lnh m chn h thng s x l cc
ngt xp hng.
Nu ngt mi xut hin ngay trong lc x l ngt th
n c th b chn hay c x l ngay ty quan h c
th ca hai ngt :
nu c x l th tin trnh x l ngt c tr th tin trnh b
ngt,
nu b chn ngt mi c th b mt hay c xp hng ch x
l.
Khi tin trnh x l ngt ri vo trng thi ch (b ngt)
th n c y mi cho n khi chng trnh iu
khin chuyn sang trng thi sn sng tip nhn. iu
ny xy ra khi processor gp lnh m chn.
220
h thng hot ng c bnh thng mt s loi tn
hiu phi ti c processor trong mi lc, mi ni, nu
xut hin,
chng hn tn hiu c sai st k thut trm trng
hoc tn hiu nhp ng h.
Vic chn hoc m cc chn ngt khc c th m
bo bng phng tin k thut hoc phn mm, do ch-
ng trnh x l ngt quyt nh.
Khi ang x l ngt no , nu xut hin tn hiu
ngt cng loi th tn hiu ngt phi b chn
trnh vic ghi thng tin vo cng mt b nh dnh cho
loi ngt .
Sau khi x l xong ngt mc th nht, h thng phi
m ngt x l tip ngt ang b chn.
221
Nu c thit b k thut h tr phn phi b nh khc
nhau cho tng mc u tin th vic x l ngt kp bng
phng php gn mc u tin s n gin i nhiu.
X l ngt khng nht thit phi tin hnh lin tc.
V d x l ngt vo ra c th chia thnh hai giai on
giai on mt x l kt qu cc php kim tra mc
knh v thit b. Giai on ny thc hin kh nhanh.
Giai on th hai x l kt qu chnh php vo ra
lu hn nhiu so vi giai on trc. Trong khong thi
gian gia hai giai on h thng c th i x l ngt
khc nu c.
222
Phn bit chng trnh con v ch-
ng trnh x l ngt
Ret
C
a
l
l
C
T

c
h

n
h
CT con
C ch CT con
T
T

b


n
g

t
S


k
i

n
Iret
K
h

i

p
h

c
CT x l s kin
C ch x l ngt
223
Chng trnh ngt nm thng tr trong b nh
chng trnh con th khi no c gi mi np vo
b nh
Chng trnh con c th b ngt cn chng
trnh x l ngt th khng.
Tham s ca chng trnh x l ngt th lu
thanh ghi
Trong b nh mi chng trnh x l ngt ch c
mt phin bn duy nht.
Chng trnh con c th c nhiu phin bn.
224
5) H thng ngt ca IBM PC v
x l ngt trong MS DOS
My tnh IBM PC c th phn bit c 256 tn
hiu ngt khc nhau.
Mi tn hiu ngt li c th chia thnh nhiu tr-
ng hp ring gi l cc chc nng.
Cc ngt c nh s ln lt t 0 tr i.
Mt s ngt c quy nh dnh cho ngi s
dng t nh ngha v dng vo mc ch
ring ca mnh.
V d: cc ngt phc v vit chng trnh phc
v web,gi mail...,
Mt s ngt khc cn d tr.
225
qun l ngt ngi ta xy dng bng qun l
ngt.
PC dng 1024 bytes u tin ghi cc con tr
ch ti chng trnh x l ngt,
mi con tr chim 4 bytes.
Chia lm hai phn mt phn cha du hiu
ngt (VD : 0 : li chia cho khng, 1C : ngt
ng h),
mt phn cha s hiu ngt.
Vng b nh ny -> vect ngt.
226
Ngt v phc v ngt cu DOS
Ngt 00h : Chia cho 0 :
Ngt 01h : Thc hin tng lnh
Ngt 02h : NMI (non-maskable interupt)
Ngt 3h :Thc hin n im dng
Ngt 04h :Trn nh.
Ngt 05h : Copy mn hnh

227
Ngt 06h-07h : cha c s dng.
Ngt 08h : Ngt ng h.
Ngt 09h : Ngt bn phm
Ngt 0Ah-0Ch : thay i ty theo tng
my.
Ngt 0Dh : a cng
Ngt 0Eh : a mm
Ngt 0Fh : My in
228
Ngt 10h : Ngt mn hnh
Ngt 11h : Xc nh cu hnh
Ngt 12h : Xc nh kch thc b nh
Ngt 13h : a mm
Ngt 14h Giao din ni tip
Ngt 15h Ngt Cassette c (ch dng vi
h my AT)


229
- Ngt 16h Bn phm
- Ngt 17h My in
- Ngt 18h gi BASIC trong ROM
- Ngt 19h Khi ng my
- Ngt 1Ah Ngy gi
- Ngt 1Bh Bn phm : X l phm Ctr
+Break
- Ngt 1Ch Ngt chu k (18,2 ln 1 giy)

230
- Ngt 1Dh c s dng nh mt con tr
a ch bng mn hnh
- Ngt 1Eh a ch bng a
- Ngt 1Fh a ch bng k t
- Ngt 20h Kt thc chng trnh : - Ngt
21h gi Hm ca DOS
- Ngt 22h tr ti mt th tc kt thc ch-
ng trnh

231
{$M 600,0,0}
Program Vdngat ;
Uses DOS, crt ;
Var demnhip : longint absolute 0 :$46C ;
Ngat1c :procedure ;
Procedure ngatmoi ; interupt ;
Var x,y :integer ;
Gio,phut,giay,tg :longint ;
Begin
Inline($9c) ; {cat cac tham so vao ngan xep}
Ngat1c ;{goi lai ngat 1c cu de tang bo dem}
x :=wherex ;y :=wherey ;
gio:=demnhip div 65543;
tg:=demnhip mod 65543;
.
232
phut:=tg div 1092;
tg:=tg mod 1092;
giay:=tg div 18;
gotoxy(1,1);write(gio:2,-,phut:2,-
,giay:2);
gotoxy(x,y);
end;
Begin
Getintvec($1c,@ngat1c);
Setintvec($1c,@ngatmoi);
Keep (0);
End
233
Chng 4: Qun l b nh
Bi 1. Mt s khi nim lin quan n b
nh
Bi 2. Cc khi nim lin quan n qun l
b nh
234
Bi 1. Mt s khi nim lin quan
n b nh
B nh chia thnh 3 loi
- B nh chnh (main memory): lu tr cc lnh chng trnh v d
liu ang c s dng.
+t
+dung lng thp
+ hiu nng cao
- B nh th cp/b nh ngoi (Secondary storage): Lu tr cc chng
trnh v d liu cha cn s dng.
+ R
+ dung lng cao
+ chm
- B nh tc cao (cache)
Tc truy cp cao, thng nm ngay trong b x l. Lu tr bn
sao ca cc d liu thng c s dng truy cp nhanh hn.
235
236
2 - Cc bc x l chng trnh
ng dng
Phn ln cc chng trnh c vit di dng
ngn ng thut ton.
Chng trnh ban u ny phi c dch
sang ngn ng thc hin ca h thng.
Phn h thng lp trnh m bo
chuyn i sang biu din t ngn ng
thut ton sang ngn ng my.
237
CT Tn users Tn trong
M un
ch
Hm tn
Hm a ch
.OBJ
L thuyt chng trnh dch
Ph.tch c php + ph. tch ng ngha + Sinh m + Ti u ho

238
M un
ch
M un
thc hin
CT
thc hin
Thc hin KQ
Bin tp
(Link)
Np v nh v
(Fetch)
Qun l b nh
QL
Tin trnh
QL
Processor
.COM
.EXE
239
3 - Cc loi cu trc chng
trnh:
a) Cu trc tuyn tnh
sau khi bin tp, cc m
ul c tp hp thnh mt
chng trnh hon thin,
cha y thng tin
c th thc hin (tr d
liu vo).
Mi bin tr ngoi u -
c thay bng gi tr c th.
thc hin ch cn
nh v chng trnh mt
ln vo b nh.
VD: Cc tp *.com trong
h iu hnh MS DOS
m
0
m
1
m
2
. . . . . . m
n
l




240
Cu trc tuyn tnh
u im:
n gin, d t chc bin tp v nh v.
Thi gian thc hin l nhanh nht v mi cng vic
chun b u c thc hin trc, h thng khng
cn phi bin tp thm, khng phi tm v np thm
g tr d liu vo.
Thi gian thc hin chng trnh thng c dng lm
chun nh gi, so snh cc phng php t chc
khc nhau.
Tnh linh ng cao: d dng sao chp chng trnh
mang sang cc my khc cng loi v cng h thng
m vn duy tr kh nng thc hin.
Nhc im :lng ph b nh, mc lng ph t l
vi kch thc chng trnh.
241
m
0
m
0
m
0
m
0
m
0
m
2
m
2
m
2
m
1
Cu trc ng
242
Mi m ul c bin tp ring bit. Khi c thc
hin, h thng nh v m ul gc. Cn ti
m ul no phi s dng cc lnh macro h
thng (Load, Attach, Delete) . . . np xin b
nh v np tip m ul tng ng. Khi s dng
xong c th gi cc lnh macro khc gii
phng b nh.
Trong cu trc ny trch nhim np v xa m
ul do ngi s dng m nhim. Cc lnh np
v xa phi c nu ngay trong chng trnh
ngun. iu ny i hi ngi s dng phi nm
vng chng trnh ca mnh v cc cng c
iu khin b nh ca h iu hnh.

243
Nu qun l b nh tt th cu trc ny rt tit
kim. Sai lm trong iu khin s gy lng ph
b nh v tng thi gian thc hin. Thi gian
thc hin ln: song song thc hin vi tm
kim, np v nh v.
Do phi n nh trnh t nh v m ul ngay
bc vit chng trnh ngun, chng trnh s b
ph thuc vo tng phin bn c th ca h
iu hnh. Km lu ng kh np, cp nht,
xo.
N thch hp cho cc chng trnh h thng.
244
Cu trc overlay
gii bi ton ngi ta chia chng trnh thnh cc chng trnh con
nh vy th c th m t cu trc chng trnh di dng cy.
mt thi im chng trnh hot ng theo mt nhnh cy no
v do vy thi im ny ch cn np cc m un trn mt nhnh
l .
Gi s s nt trn cy l n , s nt trn mt nhnh di nht l m
(m<<n), thay v phi np c n nt th ta np c m nt.
Cc m ul c chia thnh cc mc: mc cha m ul gc, np ch-
ng trnh.
Mc mt cha cc chng tnh c gi t chng trnh mc 0 v
khng tn ti ng thi.
Mc 2 cha cc chng trnh c cc chng trnh mc 1 gi v
khng tn ti ng thi...
B nh dnh cho chng trnh cng c chia thnh cc phn gi l
mc b nh, tng ng vi mc chng trnh.
Mi mc b nh c kch thoc bng di ln nht ca cc m ul
cng mc.
245
Chng trnh ngun c vit nh bnh thng.
Sau khi dch cc mt un ch vn c dng bnh thng, khng
cha cc lnh giao tip vi h iu hnh.
T cc m un ny ngi ta c th to cc cu trc chng trnh khc
nhau. nhn c cu trc overlay ngi s dng cn cung cp
thm thng tin cho chng trnh bin tp, cho bit c my mc, mi
mc bao gm nhng m ul no.
VD: Trong Pascal ta khai bo nh sau:
level 0
main
level 1
CT 1, CT2,CT3
level 2
.....
s overlay tm cc m ul cn thit, tp trung vo mt file ring
gi l file overlay
h iu hnh MSDOS nhng file ny c ui OVL. Trong
windows: Cc m un np trong qu trnh thc hin vo cc files
.DLL.
246
M ul mc 0 c mt file chng trnh ring.
Khi thc hin m ul gc c np v nh v nh mt chng trnh
cu trc tuyn tnh bnh thng.
Cn ti m ul no h thng s tm kim trong file overlay v np
vo b nh mc overlay tng ng.
Thnh phn h thng duy tr hot ng ca chng trnh theo s
overlay gi l supervisor overlay.
Khi np m ul mi vo mt mc dng ri, m ul c trong s
b xa. c th dng trc tip c sau ny, h thng phi ghi tm
n vo file trung gian bn ngoi. Khi cn np mt mul no
supervisor overlay s tm trc ht file trung gian bn ngoi.
V khng th d bo c, khi no cn gi m ul overlay nn
supervisor overlay phi thng tr trong b nh. -> lm gim hiu qu
ca cc bin php ti u b nh.
247
80
KB
100
KB
60
KB
110
KB
80
KB
100
KB
90
KB
80
KB
60
KB
60
KB
40
KB
110
KB
40
KB
70
KB
Moduls
mc 1
Moduls
mc 2
Moduls
mc 3
MC 0
MC 1
MC 2
MC 3
RAM


Tng cng:
270 KB 730 KB
.OVL
248
Trong cu trc overlay thng thng khng xt
cc m ul c cha ln gi l ng mc, tc l
t mt m ul gi mt m ul khc cng mc
overlay.
Li gi ng mc gy kh khn v m ul gi
c th b ph trc khi m ul c gi nh
v xong trong b nh.
s overlay hp l -> khng xut hin li
gi ng mc.
x l cc li gi ng mc chng trnh bin
tp phi to ra mt bng thng tin iu khin
na, nh supervisor overlay bit c cc m
un gi v c gi.
249
C hai cu trc va xt trn u c c trng l nh v
ng. Chnh cch nh v ng cho php ta s dng -
c nhiu b nh hn phn b nh m h thng dnh cho ch-
ng trnh.
Mc d nh v c tin hnh trong qu trnh thc hin ch-
ng trnh, nhng nhn chung cu trc chng trnh vn mang
tnh cht tnh, n khng thay i trong tt c cc ln thc
hin chng trnh.
So vi cu trc ng cu trc overlay c u im l i hi
ngi s dng cung cp nhng thng tin n gin v khng
gn c nh cu trc vo chng trnh ngun.
Nu ngi s dng xy dng c s overlay tt v cc m
ul c di hp l th cu trc overlay mang li hiu qu
khng thua km cu trc ng.
cc chng trnh ng dng h thng phn ln u c cu trc
overlay.
250
Nhc im:
+ Vn yu cu ngi s dng cung cp thm
thng tin ph.
+ Hiu qu tit kim b nh vn ph thuc cch
b tr, t chc cc m ul ca chng trnh.
Ch cn c vi m ul kch thoc ln cc mc
overlay khc nhau l hiu qu s dng b nh gim
st hn.
+ Do cu trc chng trnh l tnh -> cung cp
thm b nh t do, hiu qu s dng chng
trnh ca h thng cng khng i.
251
Cu trc phn on
m
0
m
1
m
2
m
3
m
0
m
1
RAM
252
Chng trnh ca ngi s dng oc bin tp ring tng m ul c
lp.
Kt qu tp hp chng tnh l mt bng iu khin cho bit ch-
ng trnh c th s dng nhng m ul no, di mi m un l
bao nhiu.
Bng iu khin ny c gi l bng qun l on v k hiu l
SCB (segment control block).
SCB cn cha mt s thng tin tr gip nh v chng trnh, mi
giai on thc hin h thng s da vo SCB np cc m ul
cng thit vo trong b nh.
Khc vi cc cu trc xt, trong cu trc ny chng no cn ch
th h thng cn np tip cc m ul mi.
Gp trng hp thiu b nh-> a bt ra ngoi mt s m ul c kh
nng khng s dng li na.
khng i hi ngi s dng phi khai bo thm bt k mt thng tin
ph no, mi cng vic u do h thng m nhim
khi dung lng b nh t do tng -> tc thc hin chng trnh cng
tng theo.
253
Nhc im:
hiu qu s dng b nh ph thuc nhiu vo
cch phn chia chng tnh thnh cc m ul
c lp.
Cng nh cu trc overlay, ch cn tn ti
vi m ul di ln l hiu qu s dng b
nh s gim mt cch ng k.
Cu trc ny ch p dng ch khi b nh
c qun l theo kiu phn on.
254
Cu trc phn trang
p
0
p
1
p
2
p
3
p
4
p
5
p
6
RAM
p
0
p
2
255
Chng trnh c tp hp nh trong trng hp c cu trc
tuyn tnh nhng sau c chnh chng trnh bin tp
chia thnh tng phn c di bng nhau gi l trang.
Sn phm bin tp l mt bng qun l trang, mi phn
t ca bng ng vi mt trang ca chng trnh ngi s
dng.
Khi thc hin, supervisor qun l trang s theo di, m
bo a trang cn thit vo b nh trong.
Cu trc phn trang ni chung ch pht huy c hiu
qu khi b nh c qun l theo cc chin lc kiu phn
trang.
Trong thc t, chin lc phn trang thng c p dng
vi tng mul chng trnh v vi phng php qun l d
liu kiu phn trang.

256
Bi 2. Cc chin lc qun l b nh
1)Chin lc phn chng c nh
2)Chin lc phn chng ng
3)Chin lc phn on b nh
4)Chin lc phn trang b nh
5)Chin lc phn trang kt hp phn on
257
1)Chin lc phn chng c
nh
b nh c chia thnh n phn , mi phn s dng nh mt
b nh c lp, gi l mt chng.
b nh c phn ra bao nhiu chng - > c th np vo
by nhiu chng trnh v t chc thc hin ng thi
cc chng trnh
s chng trnh ng thi c iu khin thc hin ->
h s song song ca h thng.
n chng -> h thng c h s song song khng vt qu
n.
V c thc hin nh mt b nh c lp nn mi chng
trnh c mt danh sch qun l b nh t do cha s dng
ring.
Mt chng trnh c np vo chng no s tn ti
cho n khi kt thc.
258
- u nhc im:

Chin lc ny c u im l n gin, t chc bo v, gim thi gian
tm kim. Chnh v vy n c p dng ph bin trong vic qun l cc
a t dung lng ln, trong phn phi b nh cc my SERVER phc v
mng.
Nhc im ca chin lc ny l b nh b phn on do c th c hai
kh nng xy ra: tng b nh t do cn ln nhng khng th s dng np
tip chng trnh khc. Kh nng th hai l ton b nh ang rnh nhng v
chng tnh c kch thc ln hn kch thc chng ln nht. Chng trnh
s khng bao gi thc hin c khi chng b nh khng b phn li.
Mi chng c gn vi mt s lp phc v. Chng trnh trc khi np vo h
thng cng c phn lp theo khai ba ca ngi s dng. Mi chng ch
phc v cc chng trnh thuc lp mnh qun l. Nh vy ta c th trnh c
vic np chng trnh nh vo vng b nh ln, trnh lng ph b nh.
sa i cu trc chng, ng nhin ch cn np li h iu hnh, nh-
ng nh vy, trnh mt ht thng tin, ta phi ch mi chng trnh kt
thc. Cng c sa i cu trc chng trnh cho php kt hp vi chng
k nhau thnh mt chng kch thc ln hn. Thng tin cc vng khc vn
c bo ton.
259
- Phng php qun l b nh: chin lc ny b
nh ch c mt danh sch qun l b nh t do
thng nht. Khi cn thc hin chng trnh h
thng s phn phi vng b nh thch hp, lp
phiu qun l ring, to thnh mt vng b nh
c lp. Nh vy nu cc chng trnh b th
ngi ta c th np vo b nh v ng thi thc
hin nhiu chng trnh. Ngc li khi chng trnh
c kch thc ln th ch n thun gim h s
song song ca h thng.
260
u nhc im
u:
khng phi sao chp cc m ul chng trnh iu
hin thnh nhiu phin bn v ghi vo nhiu ni
nhc im:
gy ra hin tng phn on b nh: sau mt khong
thi gian hot ng lin tc cc chng trnh phn b
ri rc khp ni v b nh t do c xu hng chia thnh
nhiu mch nh. Do h thng vn qun l y
nhng khng s dng c cc vng b nh cn
trng.
khc phc phi trang b cng c b tr li b nh
-> SWAPPING
261
Swapping
.b tr li l:
- Tm thi im thch hp ln lt dng cc ch-
ng trnh ang thc hin
- a mt s hoc tt c cc chng trnh b
nh trong cng vi trng thi ca n ra b nh
ngoi, tr b nh phn phi v cho m ul qun
l ca h thng.
- Ti nh v chng trnh t b nh ngoi vo
b nh trong, khi phc trng thi ra tip tc thc
hin.
262
2
4
6
2
4
6
B


n
h


d
o
263
y l mt cng vic ht sc phc tp v tn thi gian. Thi im
thch hp dng chng trnh l thi im cc gi tr c tnh
xong v lu vo b nh hay thanh ghi. Ch c lc ta mi lu v khi
phc trng thi cng vic c.
Do vic a chng trnh ra v nh v li cng vi vic khi phc
trng thi l phc tp nn ngi ta c th khng cn a ht mi ch-
ng trnh ra. Mi khi mt chng trnh c a ra ngoi b nh phn
phi cho n c gii phng v c kh nng mc phn on ton
b nh gim. Qu trnh a ra sau b tr li c th dng th
tm c vng b nh t do thch hp hoc gim h s phn on
xung mc an ton.
Vic ti nh v hoc l v cng phc tp, phc tp hn c chng
trnh bin tp, lin kt v chng trnh ng dng c th hot ng
theo nguyn l macro processor: to ra cc cu lnh v thc hin
trong qu trnh lm vic. h thng d dng phn bit u l
d kin ng, ph thuc vo v tr nh v cc loi my v h
thng khc nhau c nhng c ch khc nhau gip chng trnh b
tr li khng lm ln gia cc a ch v hng.
264
Chin lc phn on b nh
( Segmentation)
Nhc im ca cc chin lc trn l khng thc hin c
cc chng trnh c kch thc ln hn kch thc b nh vt
l. Ngoi ra h s tch cc ca cc byte trong b nh
cng khng u. Nhng byte c gi l tch tc nu
ni dung ca n c s dng thc hin cc cu lnh
trong thi im quan st. Nu khong thi gian quan st
khng qu b ( vi phn trm giy cho cc my tnh
hin i) mt chin lc s dng b nh tt l chin lc
cho tp cc byte tch cc ln.
Ngi s dng hon ton khng quan tm chng trnh
ca h c b tr nh th no trong b nh: lin tc hay
khng lin tc. V vy c th bin tp ring tng m ul
v xy dng bng thng tin cho bit chng trnh s
dng m ul no, di cc m ul l bao nhiu.
Bng thng tin ny c gi l SCB (Segment Control
Block bng qun l phn tin on).
265
m
0
m1
m2
m3
SCB
(Segment Control Block)
SCB = {Phn t}
D A l
Du hiu np m
un:
0 - Cha np,
1 - np.
a ch np m un
di m un
- Cp pht b
nh,
- Bo v,
266
Khi thc hin SCB c np vo trong b nh. a ch u ca n
oc a vo thanh ghi qun l on Rs.
a ch truy nhp d liu c biu din di dng cp (s,d), trong
s l s hiu segment (m ul) cn truy nhp v d- a ch t-
ng i tnh t u segment.
truy nhp ti mt d liu cn phi hai ln hng ti b nh. Ln
th nht h thng ly ni dung ca thanh ghi qun l on Rs cng
vi s v t tm c phn t th s trong Control Block. Nu du
hiu D ca phn t l 0 th h thng s lm cc th tc np m ul
vo b nh, xin cp pht b nh ( theo kch thc ghi trng L), tm
m ul tng ng b nh ngoi, c v nh v vo vng b nh
c cp pht v sau sa li ni dung trng a ch A n
ch ti m ul th s. D nhin nu khng tm c th h thng s
bo li v ngng thc hin. Du hiu D bng 1 chng t m ul
c nhp vo b nh.
Nh vy sm hay mun du hiu D cng s c xc lp bng 1, h
thng s ly ni dung trng a ch A cng vi D, truy nhp ti b
nh ln th hai theo a ch tnh oc c hay ghi d liu.
267

1
.


.


.
.

.

.
300
314
315
300
R
s
=
15 4532
s d
+
12352
12352
1000
+
16884
1
0
0
0
M un 15
RAM
RAM
268
u nhc im,

u im : khng i hi cng c t chc
c bit -> c th p dng trn my bt
k.
nhc im : hiu qu s dng b nh ph
thuc vo cu trc chng trnh do ngi s
dng vit.
269
- Phn on ngoi v b tr li
(swapping).

sau mt thi gian lm vic b nh b phn on
ngoi -> cn cng c b tr li.
Vic b tr li n gin v c s h tr ca SCB.
Phn t tng ng s b nh du 0 trng D v b
nh xc nh theo hai trng cn li tr v danh sch
qun l b nh t do.
xut hin nhu cu b tr li -> chn m ul
a ra.
- a ra m ul tn ti nht trong b nh.
- a ra m ul c ln s dng cui cng cch y lu
nht.
- a ra m ul c tn sut s dng thp nht.
270
Chin lc phn trang
Phng php qun l b nh: b nh c chia lm
nhng phn bng nhau gi l trang.
cc trang c nh s th t t 0,1,2 tr i
to thnh a ch trang.
Trang c dng lm n v phn phi b nh.
truy nhp ti d liu a ch truy nhp c
biu din di dng (p,d) trong
p- s hiu trang cn truy nhp, d- a ch tng
i tnh t u trang.
Thng thng kch thc trang l kch thc l ly
tha ca 2.
271
Page
0
Kch thc
trang:
256B
512B
1 KB
2 KB
4 KB
1
2
3
4
5
6
7
a ch trang
272
S trang b nh vt l ph thuc vo
kch thc trang,
kch thc b nh vt l.
S trang ca b nh logic th ty v khng cn a ht mt
lc cc trang vo trong b nh, nhng trang cha a vo c
th lu tr b nh ngoi, trn a t hoc cc b nh c tc
cao hn (a t vi u c c nh )
Cc trang b nh logic khi lu tr b nh ngoi chim mt
vng b nh lin tc. Nhng khi c nh x vo b nh vt
l, khng nht thit phi t lin tc nhau.
Ln lt cc trang ca mt chng trnh c np vo b
nh v c nh x vo tp khng sp xp cc trang vt l.
nh x mang tnh cht b phn v khng phi mi trang u
nht thit c np vo b nh vt l ngay c khi kch thc
cho php
273
T chc thc hin: Gi thit b nh logic
c t chc trn a t, khi np chng
trnh vo b nh h thng xy dng mt
bng qun l trang bng PCB (page
control block).
Bng qun l trang dng xc lp quan
h gia trang vt l v trang logic.
Bng qun l trang bao gm nhiu phn
t, mi phn t tng ng vi mt trang
logic v c hai trng.
274
PCB = {Phn t}
D
P
A
P
Du hiu np
trang:
0 - Cha np,
1 - np.
a ch np trang
275
a ch ca bng PCB c np vo thanh ghi qun l trang
Rp.
truy nhp ti d liu h thng cn hng ti b nh hai
ln:
ln th nht: h thng ly ni dung Rp cng vi p trong a
ch (p,d) truy nhp ti phn t th p tng ng vi
trang p cn truy nhp.
Nu du hiu D=0 th c ngha trang cn tm cha c,
h thng s to ngt np trang cn thit vo b nh.
Cng vic ny do chng trnh iu khin ca h thng
m nhim.
Nh vy sau khi truy nhp ti phn t th p trong PCB,
sm hay mun du hiu D ca n cng c xc lp
bng 1 v trng Ap s cha a ch trang trong b nh
vt l.
Tip theo, h thng ly a ch trang Ap ghp vi d to ra
a ch vt l ca d liu a vo truy nhp ti ni
dung a ch ly ra cu lnh hoc d liu cn tm.
276
1
.


.


.
.

.

.
400
480
481
400
R
p
=
81 532
p d
+
123
123000
123532
Trang 81
RAM
RAM
S truy nhp ti d liu
277
Chin lc phn trang cn c u im l
khng xy ra hin tng phn on
ngoi b nh.
Nu cn b nh t do th kch thc phi
phn trang -> bao gi cng ch a
trang mi vo
tnh trng thiu b nh ch thc s xy ra
khi tt c cc trang ca b nh vt l
thc s c s dng ht.
278
Qua cch t chc thc hin chng trnh xt
trn, ngoi bng qun l trang PCB cn cn b
nh cha cc trang d liu v chng trnh.
S trang dnh cho mt chng trnh cng nhiu
th tc thc hin chng trnh cng cao nhng
h s song song cng gim.
Khi gim s trang dnh cho mt chng trnh th
h s sng song tng nhng tc thc hin ch-
ng trnh gim v thng xuyn b ri vo tnh
trng thiu trang cn thit thc hin.
Hiu qu chung ca phng php ph thuc vo
cch np trang v cch thay th trang tch cc.
279
Chin lc np trang.
Bin php n gin nht l np tt c mi trang
ca mt chng tnh vo b nh ngay t u, nh
vy khng th xy ra tnh trng thiu trang tch
cc khi thc hin.
Chin lc phn phi gin n vi cu trc phn
trang.
Trong chin lc ny b nh ngoi khng c s
dng m rng b nh trong.
u im ca chin lc l vic np v nh v
chng trnh vo b nh trong ht sc n gin
khng pht huy c cc u im c th ca cu
trc phn trang.

280
phng php np trc
cho php gim kh nng thiu trang tch cc m khng
cn dng ti khng gian nh vt l qu ln
Phng php cho php ngi s dng to chng trnh
b nh logic vi kch thc ty , ng thi h thng c
th duy tr h s song song cn thit cho hot dng c
hiu qu ca ton h thng.
im mu cht ca np trc l d bo trang tch cc sp
cn ti trong qu trnh.
Nu xc nh c ng, c th np trc cc trang ny
vo b nh v gim trng hp thiu trang khi thc
hin chng trnh.
vic d bo trang no sp c s dng l cc k kh
khn.
Trong ch np trc chi ph np trc cc trang khng
dng n c th vt xa chi ph dnh cho vic np cc
trang thc s c s dng.
281
Thng thng phng php np theo yu cu
hay c dng :
trang ch c np khi xut hin yu cu
truy nhp ti d liu trong trang.
mi ln np trang u thc s l cn
thit.
Khi c cch phn b trang hp l, cch t
chc tm kim tt cc trang b nh ngoi
c ch phn trang s m bo hiu qu
khai thc trong ch c h s song
song cao.
282
Chin lc i trang
Nu vn cn b nh t do th khng cn thit phi thay
trang.
Khi thiu b nh, ta phi a mt trang no ra b nh
ngoi, a trang khc cn thit cho vic tip tc thc
hin chng trnh vo b nh.
Tn ti nhiu phng php chn trang a ra, gii phng
ch cho trang mi.
cc tiu ha s php x l thc hin, h thng phi
kho st thuc tnh ca trang cn gii phng. T tng ch
o l phi thay th trang m ln s dng tip theo
cng xa cng tt.
Trng hp l tng l trang khng cn cn thit cho vic
thc hin chng trnh.
283
Hai phng php n gin t chc l thay vng trn hoc thay
ngu nhin cc trang ang dng.
trong trng hp ny kh nng thay trang cn dng l kh ln.
chn trang thay th theo quy tc np trc th b thay th trc ( quy
tc FIFO).
trang c mt lu nht trong b nh ( ca chng trnh ny) s b
thay th.
Nu chng trnh xy dng theo quy tc tuyn tnh th qu
tht y l mt quy tc tt.
Vi cc chng trnh xy dng theo kiu lp trnh cu trc quy
tc c th dn n vic a ra trang quan trng.
Tuy vyphng php ny vn hp dn ngi thit k v tnh n
gin. Chng trnh phn phi b nh ch vic theo di qu trnh
np trang, xc nh trnh t u tin a ra.
S lng trang vt l c nh -> khng c kh khn trong t
chc thc hin.
284
Mt phng php ci tin l a ra trang c ln
s dng cui cng cch y lu nht (Phng
php LRU- Last Recently Used).
Phng php ny da trn gi thit chng trnh
c tnh cc b ha trong s dng, khng c cc
lnh chuyn iu khin i xa cu lnh ang
thc hin
Tuy phng php ny khng m bo ti u ha
trong mi trng hp nhng trn thc t n cng
hiu qu v vy c ng dng kh rng ri.
Nhc im chnh ca LRU l chi ph thc hin
cao. xc nh trang a ra cn nh k r
sot kim tra, thng k xem nhng trang vo
c s dng.
285
Thng thng phi xy dng cc khi qun l cho
tng trang v c bit ring nh du truy nhp.
Bit ny c xc nh bng 1 khi c truy nhp ti
trang.
Chng trnh thng k s nh du thi im
truy nhp v xa bit nh du.
Nu trong qu trnh kim tra bit nh du c gi
tr bng khng th c ngha l t ln kim tra
trc n thi im ny cha c li gi no ti
trang ang xt.
Vic xc lp gi tr 1 cho bit kim tra tc thc
hin bng cc phng tin k thut.
286
Song song vi vic xc nh thi im gi ca trang,
thng k s ln truy nhp t c th tnh tn
sut truy nhp.
Da trn thng k ny -> t chc mt chin lc thay th
khc- chin lc LFU (Last frequently Used) thay th
trang c tn sut s dng ln nht.
Cc chin lc thay trang ni trn c th p dng chung
cho ton b h thng hoc p dng cc b cho tng ch-
ng trnh ang thc hin.
Khi p dng thay th ch thay th cc b h thng
phi lu tr kch thc b nh dng cho ton chng trnh v
cn lu trnh hin tng phn phi b nh khng u
gia cc chng trnh xut hin khi khi to cc nhim v
trong ch a chng trnh.
287
Chin lc np trang theo yu cu gii phng ngi s dng
khi vic cung cp cc thng tin ph hoc t mnh
iu khin qu tnh thay i trang.
chi ph t chc c ch phn trang ln v phi u t k
thut m bo qu trnh bin i a ch khi nh
v trang.
Trong mt s trng hp, bn cnh phng tin k thut
vn phi tn thm thi gian x l bng con ng chng
trnh.
H thng cng phi tn nhiu ti nguyn nh du
qu trnh truy nhp trang v trao i vn b nh ngoi
khi thay trang truy nhp.
Tng thi gian thc hin chng trnh tng khi thng
xuyn phi np li trang.
288
C ch phn trang m bo c h s song song cao,
n cha gii quyt ht mi vn ny sinh m
bo hiu qu khai thc ti a.
Kch thc chng tnh v d liee ni chung khng hn
ch, nhng khi b nh logic dnh cho chng trnh v d
liu qu ln th bng qun l trang PCB cng ln theo v
h thng phi tn nhiu b nh vt l lu tr PCB phc v
thc hin.
Trn thc t, cng nh bn thn chng trnh, ch mt
phn bng PCB l c tc dng tch cc v trc tip
trong iu thc hin cng vic v vy c th nng
cao hiu qu c ch phn trang qua vic ci tin cch
x l PCB.

289
chn kch thc trang thch hp.
Hiu qu ca c ch phn trang ph thuc nhiu vo kch
thc trang.
Nu kch thc trang qu b bng PCB s ln v kh nng phi
thng xuyn np li trang cng ln khi h thng hot ng
gn kh nng ti hn.
Nu kch thc trang ln th nh hng ca hin tng phn
on trong- trang khng c s dng ht s tc ng ng k
n hiu qu s dng b nh.
kch thc trang phi c chn mt cch hp l v thng dao
ng trong phm vi 28 n 210 bytes.
Cc h thng ca IBM thng chn kch thc trang bng 512
byte theo truyn thng.
Cc my IBM PC hin nay c b nh trong kh ln -> kch thc
trang 4 kb trn thc t khng phi l qu ln.
290
Chin lc kt hp phn trang -
on
Phng php qun l b nh: Chin lc phn
trang m bo hiu qu s dng b nh cao,
khng ph thuc cu trc chng trnh ngi s
dng.
khc phc cc nhc im ca chin lc
phn trang s dng chin lc kt hp phn
trang- on.
Trong chin lc ny chng trnh cng c bin
tp theo ch phn on v to ra bng qun
l on SCB, nhng sau mi on (segment)
li c bin tp theo trang v to ra PCB ring ca
tng on.
291
Du hiu np
PCB:
0 - Cha np,
1 - np.
SCB = {Phn t}
D A l
a ch np PCB
di PCB
- Cp pht b
nh,
- Bo v,
292
Trng a ch A ca phn t th I trong SCB
by gi l ni cha bng PCBi nu n oc
np vo b nh, trng L chia di ca PCBi.
Khi thc hin chng trnh SCB tng ng s c
np vo b nh v a ch u ca n cng -
c a vo thanh ghi qun l on Rs. a ch
truy nhp ti d liu c biu din di dng
nhm ba (s,p,d), trong s l segment cn truy
nhp, p l s hiu trang trong segment v d l
a ch tng i tnh t u trang.
293
truy nhp ti d liu h thng cn ba ln h-
ng ti b nh:
1)Ly ni dung Rs cng vi s v truy nhp ti
phn t th s ca SCB.
2) Nu D bng 0 th lm th tc np PCB th I
vo b nh v cp nhp ni dung cc trng D.A.
Khi D bng 1 h thng cng ni dung trng A vi
p truy nhp ti phn t th p trong PCB.
3) Ch i n khi du hiu p ca phn t tm
c bng 1 th ghp ni dung ca trng a
ch Ap tng ng vi d, hng ti b nh ln th ba
ly hoc ghi d liu.
294
Trong chin lc ny b nh thng c chia lm ba phn,
tng ng cha SCB, PCB v cc trang. Bn thn b nh
dnh cho SCB v PCB cng thng c qun l theo ch
phn trang.
Chin lc ny c nhiu u im nn n thng c p
dng trong cc h thng my tnh v h thng hin i.
Cc my IBM PC t AT 386 tr ln u c ch qun
l theo chin lc ny.
Khi thiu b nh h thng c th phi b tr li cc
trang d liu hoc trang b nh phc v PCB.
m bo truy nhp hp thc trong ch dng
chung m ul chng trnh con, mi khi cn nhp vng
nh cha PCB h thng s phong ta khng cho truy
nhp ti SCB.
295
6)T chc v qun l b nh trong
IBM PC
- My PC XT : ch thc hin c mt ch
qun l gi l ch thc (real mode). Khng
gian b nh vt l v khng gian b nh lgic l
trng nhau.
- PC AT 286 thc hin c c hai ch qun l
b nh l ch thc (real mode) v ch bo
v (protect mode). Cch qun l ch protect
mode l theo kiu phn on.
- PC AT 386 c hai ch qun l b nh l ch
thc (real mode) v ch bo v (protect
mode). ch bo v, cch qun l c thc
hin theo hai kiu phn on v phn trang
296
My XT c dung lng b nh: 64K, 128K, 256K,
512K, 640K
My AT 286 : 640 K +384 K. Vng 384K c gi
l Himem.
My AT 386 : 640 K + 384 K + vng >1 M
Vng 640 K c gi l vng b nh c s (Base)
384 K gi l Himem hay Expand memory.
Vi cc my 386 th ngi ta gi l Upper Memory.
Vng b nh ln hn 1 M ngi ta gi l Extent
Memory
297
Cc mc c quyn (Privilege Level PL) ca
cc my 80386: c 4 mc t 0 n 3.
Mc 0 Ht nhn: Gm cc chng trnh c s
qun l ti nguyn ca my v b nh. ht nhn
phi gn v c kh nng vn hnh tt.
Mc 1 Qun l thit b: Cha tt c cc phn
mm lin quan n HH nh qun l cc cng
vo/ra, np cc cc thut ton trao i.
Mc 2 Qun l file: Bao ggm cc chc nng
qun l file, qun l th vin.
Mc 3 Ginh cho chng trnh ng dng.
298
Nguyn tc truy
nhp: chng trnh
ca mt mc u tin
ch c php gi v
s dng d liu ca
chng trnh bng
hoc km mc u tin
hn.
Vd: chng trnh
mc 1 ch c gi v
s dng d liu ch-
ng trnh mc 1,2,
v 3.
3
0
1
2
Nhn
Q
L
F
ile
s
T
h
i

t

b

Phc v
H


t
h

n
g
C
T

n
g
d

n
g
M

u

t
i

n
299
Ch Protect Mode: B nh c chia thnh cc
khi
Mi khi c qun l bi mt MCB (Memory
Control Block)
Mi khi MCB gm 16 byte :
Byte u cha k t M hoc Z. Nu l MCB ca
khi cui th l k t Z, cc khi cn li th l
k t M.
2 bytes tip cha a ch an m np vo khi
ny.
2 bytes tip cha di an m .
11 bytes cn li cha dng n.

300
V QUN L FILES V THIT B
NGOI VI
Qun l thit b ngoi vi: Cn m
bo h thng thch nghi vi:
S lng nhiu,
Cht lng a dng,
Thun tin cho ngi dng.
Qun l files: Cho php ngi dng:
To files cc loi b nh ngoi,
Tm kim, truy nhp files,
m bo c lp gia CT v thit b
301
1 Nguyn tc phn cp trong
qun l thit b ngoi vi
My tnh th h I v II: Processor lm
vic trc tip vi thit b ngoi vi,
Hn ch: Tc - S lng - Chng
loi,
T th h III tr ln:
Processor TB iu khin TB ngoi vi
(Control Devices)
(Controllers)
302
Procesor
Controller 1 Controller 2
Controller n
TB Vo/Ra
TB Vo/Ra
TB Vo/Ra
. . .
Knh
(Channel)
- TB iu khin v TB
vo/ra: MT chuyn dng
H lnh + Ngn ng ring
- Hot ng c lp vi
nhau v vi Processor.
303
Procesor
Controller 1 Controller 2
Controller n
TB Vo/Ra
TB Vo/Ra
TB Vo/Ra
. . .
Knh
(Channel)
CT knh
(Channel Prog)
T
h

c

h
i

n

C
T

K

n
h
Thc hin cng
vic ca mnh
X


n
g

t

v

o
/
r
a
Ngt vo/ra (I/O Interrupt)
M tr v (Return Code)
TRAO I
VO/RA
304
Nguyn tc phn cp trong qun l thit b
ngoi vi
Php trao i vo ra: thc hin theo
nguyn l Macroprocessor,
Vi my vi tnh: Thit b iu khin vo
ra I/O Card,
My Card on Board,
Khi nim knh b (Multiplex), Card
Multimedia.
305
Knh Multiplex
CONTROLLER 1
CTRL 11
I/O D
CTRL 11
I/O D
I/O D
Knh Multiplex
Knh con
306
2 - K THUT PHNG M
Khi nim phng m (Buffer) ca OS.
DISK
BUFFER
SYSTEM
a
r
e
a
d
(
f
,
a
)
;
A
M
B
U
F
F
E
R
A
M
W
r
i
t
e
(
f
2
,
a
)
;
S
Y
S
T
E
M
RAM
C
a
c
h
e
307
K THUT PHNG M
C ch phc v phng m,
Vn ng file output, FLUSH(F),
Vai tr phng m:
Song song gia trao i vo ra v x l,
m bo c lp:
Thng tin v phng tin mang,
Bn ghi l gc v vt l,
Lu tr v x l,
Gim s ln truy nhp vt l:Gi thit
mi ln truy nhp vt l: 0.01, truy
nhp kiu BYTE.
308
K THUT PHNG M








Khng c
Buffer
Buffer
512B
1B 0.01 0.01
512B ~5 0.01
5KB ~50 0.1
50KB ~8 1
309
Cc loi phng m
Phng m chung hoc gn vi file,
Cc H QTCSDL c h thng phng m
ring nng linh hot v tc x l,
Cc loi b nh Cache v phng m.
Ba kiu t chc chnh:
Phng m trung chuyn
Phng m x l,
Phng m vng trn.
310
Cc loi phng m
A) Phng m trung chuyn
a
A
M
read(f,a);
G
i


t
r


b
i

n
c im:
- Vn nng,
- H s song song cao,
- Tn b nh v thi gian
chuyn thng tin trong b nh.
311
Cc loi phng m
B) Phng m x l:

a
A
M
read(f,a);

a

c
h


b
i

n

t
r
o
n
g

p
h

n
g

m
c im:
- Km vn nng,
- H s song song thp,
- Khng tn b nh v thi
gian chuyn thng tin trong b
nh.
312
Cc loi phng m
C) Phng m vng trn: thng p
dng cho cc h QT CSDL.
X L
C GHI
313
3 - SPOOL
SPOOL Simultaneuos Peripheral
Opearations On-Line,
Khng can thip vo CT ngi dng,
Hai giai on:
Thc hin: thay th thit v ngoi vi
bng thit b trung gian (a cng),
X l kt thc:
Sau khi kt thc vic thc hin CT,
a thng tin ra thit b yu cu.
314
SPOOL
m bo song song gia x l mt
CT vi trao i vo ra ca CT khc.
Chng trnh
USERS
DISK
O
S
Thc hin
chng trnh
X l kt thc
(min ph)

315
SPOOL
Gii phng h thng khi s rng
buc v s lng thit b,
Khai thc thit b ngoi vi ti u,
K thut lp trnh hiu qu.
H thng cung cp cc phng tin
ngi dng to SPOOL,
Ai to SPOOL ngi x l kt
thc.
316


Giai on thc hin: vi mi php
trao i vo ra h thng to 2 CT
knh:
CT knh I theo thit b yu cu,
CT knh II phc v ghi CT knh I ra
thit b trung gian,
X l kt thc: c CT knh lu
v chuyn giao cho knh.
Nh vy, mi thit b s dng file
CT knh.
317
4 H THNG QUN L FILES
- CSDL qun l files,
H thng qun l files - H QT CSDL.
H thng truy
nhp vt l (truy
nhp c s)
H thng truy
nhp l gc
318
R1
R2
R3
R4
S1
S2
S3
File ca User
File lu tr
trn a
512 B
512 B
512 B
300 B
300 B
300 B
300 B
300 B
300 B
319
H QT CSDL
Qun l files
AM
Truy nhp l
gc
Truy nhp c
s
Php trao i
vo/ra
Thit b vo/ra
- Giao din tt,
- Kch thc ln
- c th tng h thng
- C cng c cho php
- C s: Lnh Macro,
- Nn cao
- Vn nng
A
P
I
USER
H
THNG
TB
K thut
320
QUN L FILE TRONG
WINDOWS
Mc ch:
Minh ho nguyn l bng tham s iu
khin,
Tnh k tha v thch nghi,
C ch bo v,
Cch th hin mt s ch qun l b
nh (chng tip theo).

321
T CHC THNG TIN TRN A T

T
r
a
c
k
1
1
1
2
2
2
3
3
3
Sector

u

t

322
CC KHI NIM C BN
Sector:
nh s t 1,
S Sector/track Constant,
H s an xen (Interleave) nguyn t
cng nhau vi s sector/track,
Kch thc 1 sector:
128B
256B
512B
1KB

323
Cylinder: 0,1,2, . . .
u t (Header): 0, 1, 2, . . .
Cluster:
Nhm sectors lin tip lgic,
n v phn phi cho ngi dng,
nh s: 2, 3, 4, . . .
Kch thc: 1, 2, 4, 8, 16, 32, 64 (S),
a ch vt l:(H, S, Cyl),
a ch tuyt i: 0, 1, 2, . . .

CC KHI NIM C BN
0 1 2 3 4 . . . . . . . . .
324
CU TRC THNG TIN TRN A T
MB BS FAT1 FAT2 ROOT FILES
Vng h thng (System Area)
FAT16
MB BS FAT1
Vng h thng (System Area)
FAT32
FAT2
ROOT
FILES
0E
0E
24 24
16
16
1C
1C
11
2C
325
BOOT SECTOR
c th
Ch k
(Signature)
Nh nhau vi mi
a cho mi loi
OS
Chng trnh
mi
(Boot Strap
Loader)
Tham s
55AA
326
BOOT SECTOR
Stt Offs
L (Byte)
Ngha
1 0 3 Lnh JMP (EB xx 90)
2 3 8 Tn h thng Format a
3 B 2 Kch thc Sector
4 D 1 Sec/Cluster
5 E 2
a ch tuyt i FAT1 trong a l gc
6 10
H
1 S lng bng FAT
7 11 2 FAT16: S phn t e ROOT
FAT32: 00 00
8 13 2 E sect/Disk (<32MB) hoc 00 00
327
BOOT SECTOR
9 15 1 Kiu a t (F8 HD, F0 1.44MB)
10 16 2 FAT16: E Sec/FAT
FAT32: 00 00
11 18 2 Sec/ Track
12 1A 2
S u t
13 1C 4
a ch tuyt BS trong a vt l
14 20 4
E Sec / Disk (>32MB) hoc 0
15 24 4 E Sec / FAT
16 28 2
Flags
17 2A 2
Version
18 2C 4
a ch ROOT (Cluster)
328

19 30 2
Inf
20 32 2
a ch lu BS
21 34 12
10
D tr (00...00)
22 40 1
a ch a ( 80 C:)
23 41 1
00
24 42 1
29 BIOS m rng
25 43 4
Serial Number
26 47 11
10
Volume Name
27 52 8
FAT32
329
Boot Sector FAT 16
15 24 1
a ch a ( 80 C:)
16 25 1
00
17 26 1
29 BIOS m rng
18 27 4
Serial Number
19 2B 11
10
Volume Name
20 36 8
FAT16
330
V d
EB 58 90 4D 53 57 49 4E 34 2E 31 00 02 08 2D 00
02 00 00 00 00 F8 00 00 3F 00 40 00 3F 00 00 00
41 0C 34 00 03 0D 00 00 00 00 00 00 02 00 00 00
01 00 06 00 00 00 00 00 00 00 00 00 00 00 00 00
80 00 29 D1 09 47 32 20 20 20 20 20 20 20 20 20
20 20 46 41 54 33 32 20 20 20 FA 33 C9 8E 41 BC
331
TH MC
ng vai tr mc lc tra cu, tm kim,
Mi h thng u phi c vi nhng tn khc
nhau (Catalog, Directory, Folder,. . .),
Bao gm: Th mc gc (ROOT) + Th mc
con,
Cc h thng ca Microsoft v OS IBM cu
trc cy,
UNIX - cu trc phn cp,
Th mc = {Phn t}, mi phn t: 32
10
B
Phn t file,
Th mc con v ROOT: File c cu trc.
332
Cu trc phn t th mc tn ngn
(Phn t 8.3)
Stt Offs L ngha
1 0 8 Tn (Name)
2 8 3 Phn m rng (Extention)
3 B 1 Thuc tnh (Attribute)
4 C 2 Thi im to file
5 E

2 Ngy to file
6 10
H
2 Ngy truy nhp gn nht
7 12 1 00 (Cho NT)
8 13 1 S 0.1 ca thi im to file
333
Phn t 8.3
9 14 2 2 bytes cao ca cluster xut
pht
10 16 2 Thi im cp nht cui cng
11 18 2 Ngy cp nht cui cng
12 1A 2 2 bytes thp ca cluster xut
pht
13 1C 4 Kch thc file (Byte)
334
Phn t 8.3
Byte s 0: Vai tr c bit.
00 Cha s dng, phn t
cha s dng u tin - du
hiu kt thc th mc,
E5 (o) b xo,
05 Tn bt u bng k t o,
2E 20 (. ) Phn t th I ca
th mc con,
2E 2E (..) Phn t th II ca
th mc con
xx
xx
xx
xx
00
335
.
..
.
..
336
5 bits
Gi
6 bits
Pht
5 bits
2 Giy
Time
x x x x x x x x x x x x x x x x
7 bits
Nm - 1980
4 bits
Thng
5 bits
Ngy
Date
x x x x x x x x x x x x x x x x
1998 18
1999 19
2000 20
2001 21
D tr thi gian:
1980 + 2
7
-1 = 2107
System
Thuc tnh
0
X
x
0
X
x
3
2
A
x
1
6
D
x

8
V
x

4
S
x

2
H
x

1
R
x
Read Only
Hidden
Vol. Name
Directory
Archive
Reserved
337
Tn di
Khng qu 255
k t,
Unicode,
H thng phn
bit theo 66 k
t u tin,
Lu tr theo
cch a vo,
Nhn dng: a
v ch hoa.
Phn t tn di n
Phn t tn di n-1
Phn t tn di 1
.

.

.
Phn t 8.3
Lu tr tn di
338
Cu trc phn t tn di
Stt Offs L ngha
1 0 1 S th t i (64+i)
2 1 10
10
5 k t C
1
C
5
3 B 1 Thuc tnh (00001111
B
)
4 C 1 00 du hiu phn t tn di
5 D 1 E
K
phn t 8.3
6 E 12
10
C
6
C
11

7 1A 2 00 00
8 1C 4 C
12
C
13

339
File Allocation Table
(FAT)
Chc nng:
Qun l b nh phn phi cho tng file,
Qun l b nh t do trn a,
Qun l b nh km cht lng.
FAT = {phn t}
Phn t:
nh s:0, 1, 2, . . .
T phn t s 2: phn t Cluster
340
Phn t
12 bits 16 bits 32 bits
FAT 16
FAT 32
Phn t 0: Fxx FFF8 FFFFFFF8
Phn t 1: FFF xx11FF8 0xx11FFFFF8
Bit Shutdown
Bit Diskerror
Bit Shutdown
Bit Diskerror
341
FAT
Bit Shutdown = 1 Ra khi h thng ng
cch
Bit Diskerror = 1 khng c li truy nhp a
ln truy nhp cui cng.

T phn t 2 tr i:
Gi tr 0 Cluster t do,
FF7 (FFF7, 0FFFFFF7) Bad cluster,
Cc gi tr khc phn phi,
Cc phn t tng ng nhng Clusters ca mt
file - to thnh mt danh sch mc ni,
EOC (End of Cluster Chain) FFF (FFFF,
FFFFFFFF).

342
FF0
FFF
FF0
5
FFF
4
9
3
7
8
0
1
2
3
4
5
6
7
8
9
6 9 8 7 3 5 4
DISK
C
C
C
I
I
I
RAM
Cluster
xut pht
FAT
343
MASTER BOOT
55AA
Chng trnh
nhn bit cu
trc
(Master Boot
Record - MBR)
c th
Ch k
(Signature)
Bng phn
vng
Nh nhau vi mi
a cho mi loi
OS
FDISK /MBR
344
Nguyn tc khai thc HD:
Chia HD thnh cc phn, mi phn
c kch thc c nh,
Mi phn s dng nh mt a t
c lp: a l gic ( Logical
Volume).
OS cho php to cc a kch thc
ng trong mi a l gic.
MASTER BOOT
345
Cu trc bng phn vng
Bng phn vng bt u t a ch 1BE
H
,
Bng phn vng = {4 phn t},
Mi phn t s dng a l gic,
Tn ti c ch cho php to nhiu hn 4 a l
gc trn mt a vt l.
a ch vt l
u
a ch vt l
cui
a ch tuyt
i u
Tng s
Sectors
4 Bytes
4 Bytes 4 Bytes 4 Bytes
346
Cu trc phn t bng phn vng
a ch vt l
u
a ch vt l
cui
a ch tuyt i
u
Tng s Sectors
4 Bytes
4 Bytes 4 Bytes 4 Bytes
SYS H S Cyl
x x x x x x x x x x x x x x x x

6 bits Sector
2
8
10 bits Cylinder
Type H S Cyl
x x x x x x x x x x x x x x x x
6 bits Sector
2
8
10 bits Cylinder
00 Cm truy nhp, 0B FAT32 ( s 2047 GB)
01 DOS12, 0C ~ 0B, ch LBA,
04 DOS16, 0E ~ 06, ch LBA,
06 BIGDOS, 0F ~ 05, ch LBA,
05 M rng (Extended)
347
Bng phn vng
80 01 01 00 0B 3F FF 4D 3F 00 00 00 41 0C 34 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
55AA

80010100 010511BF 11000000 6F4C0000
000001C0 5105511F 804C0000 40260000
00004120 510551DF C0720000 804C0000
000041E0 5105D132 40BF0000 12870000
55AA
348
Truy nhp Boot Sector
Dng cc hm API,
Chng trnh c v a ra mn hnh ni dung BS
ca a mm A: (Hexa v ASCII):
Program R_BS_A;
Uses Crt, Dos;
Const s16: string[16]=0123456789ABCDEF;
Var B: array[0..511] of char;
reg: registers;
i,j,k: integer;
c: char;
BEGIN
clrscr;
fillchar(b,sizeof(b),0);
writeln(Cho dia vao o A: va bam phim bat
ky.);
c:=readkey;
349
I := 0;
Repeat
with reg do
begin
dl := 0; { 0 -> A:, 128 -> C:}
dh := 0; {u t}
cl := 1; {Sector}
ch := 0; {Cylinder}
al := 1; {So Sectors can doc}
ah := 2; {2 -> Read; 3 -> Write;. . .}
es := seg(b);
bx := ofs(b)
end;
350
intr($13,reg);
inc(i)
Until i = 2;
for i := 0 to 511 do
begin
j := b[i] shr 4 + 1;
k := b[i] and $0F + 1;
write(s16[j]:2, s16[k]);
if (i+1) mod 16 = 0 then
begin write( :5);
for j := i-15 to i do
if (b[j] <32) or (b[j] = 255) then
write(.)
else write(chr(b[j]));
351
writeln;
if i = 255 then c:= readkey
end
end;
Repeat
Until keypressed
END.
352
winnt
1. C kh nng m rng (extensibility) : c th nng
cp d dng nh vo kin trc phn lp, thit k n th
2. D mang chuyn (portability) : c th thi hnh trn
nhiu cu hnh phn cng (bao gm nhiu h CPU khc
nhau)
3. C tin cy cao (reliability) : c kh nng phc
hi li, bo m an ton v bo mt
4. C tnh tng thch mnh (compatability) : c th
thi hnh cc ng dng MS-DOS, 16-bit Win, Win32,
OS/2 & POSIX
5. Hiu qu thc hin tt (performance) : bo m
thc hin hiu qu vi cc c ch qun l ni b, IPC,
h tr mng v h tr a x l.
353
MS DOS 2.X
MS DOS 3.3
MS DOS
4.01 6.X
WINDOWS 95
WINDOWS 98
WINDOWS NT
WINDOWS
2000
WINDOWS XP
Qun l
files
Cng c t
chc TT
song song
Qun l a
hn 32MB
a nhim 1
user
Cng c
kt ni
Internet
Qun l
mng v
htql file
mi
a nhim
nhiu
users
- Tng cng giao din,
- Phc v kt ni Per to Per,
- MultiOs
354