Understanding the Linux Kernel [2nd Edition]


411 87 5MB

English Pages 829

Report DMCA / Copyright

DOWNLOAD PDF FILE

Recommend Papers

Understanding the Linux Kernel [2nd Edition]

  • 0 0 0
  • Like this paper and download? You can publish your own PDF file online for free in a few minutes! Sign Up
File loading please wait...
Citation preview

I l@ve Ru Bo a rd



Ta b le o f Co n t e n t s



In d e x



Re vie ws



Re a d e r Re vie ws



Erra t a

Understanding the Linux Kernel, 2nd Edition By Da n ie l P. Bo ve t , Ma rco Ce s a t i

Pu b lis h e r

: O'Re illy

Pu b Da t e

: De ce m b e r 2 0 0 2

IS BN

: 0-596-00213-0

Pa g e s

: 784

Th e n e w e d it io n o f Un d e rs t a n d in g t h e Lin u x Ke rn e l t a ke s yo u o n a g u id e d t o u r t h ro u g h t h e m o s t s ig n ifica n t d a t a s t ru ct u re s , m a n y a lg o rit h m s , a n d p ro g ra m m in g t ricks u s e d in t h e ke rn e l. Th e b o o k h a s b e e n u p d a t e d t o co ve r ve rs io n 2 . 4 o f t h e ke rn e l, wh ich is q u it e d iffe re n t fro m ve rs io n 2 . 2 : t h e virt u a l m e m o ry s ys t e m is e n t ire ly n e w, s u p p o rt fo r m u lt ip ro ce s s o r s ys t e m s is im p ro ve d , a n d wh o le n e w cla s s e s o f h a rd wa re d e vice s h a ve b e e n a d d e d . Yo u 'll le a rn wh a t co n d it io n s b rin g o u t Lin u x's b e s t p e rfo rm a n ce , a n d h o w it m e e t s t h e ch a lle n g e o f p ro vid in g g o o d s ys t e m re s p o n s e d u rin g p ro ce s s s ch e d u lin g , file a cce s s , a n d m e m o ry m a n a g e m e n t in a wid e va rie t y o f e n viro n m e n t s . I l@ve Ru Bo a rd

I l@ve Ru Bo a rd



Ta b le o f Co n t e n t s



In d e x



Re vie ws



Re a d e r Re vie ws



Erra t a

Understanding the Linux Kernel, 2nd Edition By Da n ie l P. Bo ve t , Ma rco Ce s a t i

Pu b lis h e r

: O'Re illy

Pu b Da t e

: De ce m b e r 2 0 0 2

IS BN

: 0-596-00213-0

Pa g e s

: 784

Co p yrig h t Pre fa ce Th e Au d ie n ce fo r Th is Bo o k Org a n iza t io n o f t h e Ma t e ria l Ove rvie w o f t h e Bo o k Ba ckg ro u n d In fo rm a t io n Co n ve n t io n s in Th is Bo o k Ho w t o Co n t a ct Us Ackn o wle d g m e n t s Ch a p t e r 1 . In t ro d u ct io n S e ct io n 1 . 1 . Lin u x Ve rs u s Ot h e r Un ix- Like Ke rn e ls S e ct io n 1 . 2 . Ha rd wa re De p e n d e n cy S e ct io n 1 . 3 . Lin u x Ve rs io n s S e ct io n 1 . 4 . Ba s ic Op e ra t in g S ys t e m Co n ce p t s S e ct io n 1 . 5 . An Ove rvie w o f t h e Un ix File s ys t e m S e ct io n 1 . 6 . An Ove rvie w o f Un ix Ke rn e ls Ch a p t e r 2 . Me m o ry Ad d re s s in g S e ct io n 2 . 1 . Me m o ry Ad d re s s e s S e ct io n 2 . 2 . S e g m e n t a t io n in Ha rd wa re S e ct io n 2 . 3 . S e g m e n t a t io n in Lin u x S e ct io n 2 . 4 . Pa g in g in Ha rd wa re S e ct io n 2 . 5 . Pa g in g in Lin u x Ch a p t e r 3 . Pro ce s s e s S e ct io n 3 . 1 . Pro ce s s e s , Lig h t we ig h t Pro ce s s e s , a n d Th re a d s

S e ct io n 3 . 2 . Pro ce s s De s crip t o r S e ct io n 3 . 3 . Pro ce s s S wit ch S e ct io n 3 . 4 . Cre a t in g Pro ce s s e s S e ct io n 3 . 5 . De s t ro yin g Pro ce s s e s Ch a p t e r 4 . In t e rru p t s a n d Exce p t io n s S e ct io n 4 . 1 . Th e Ro le o f In t e rru p t S ig n a ls S e ct io n 4 . 2 . In t e rru p t s a n d Exce p t io n s S e ct io n 4 . 3 . Ne s t e d Exe cu t io n o f Exce p t io n a n d In t e rru p t Ha n d le rs S e ct io n 4 . 4 . In it ia lizin g t h e In t e rru p t De s crip t o r Ta b le S e ct io n 4 . 5 . Exce p t io n Ha n d lin g S e ct io n 4 . 6 . In t e rru p t Ha n d lin g S e ct io n 4 . 7 . S o ft irq s , Ta s kle t s , a n d Bo t t o m Ha lve s S e ct io n 4 . 8 . Re t u rn in g fro m In t e rru p t s a n d Exce p t io n s Ch a p t e r 5 . Ke rn e l S yn ch ro n iza t io n S e ct io n 5 . 1 . Ke rn e l Co n t ro l Pa t h s S e ct io n 5 . 2 . Wh e n S yn ch ro n iza t io n Is No t Ne ce s s a ry S e ct io n 5 . 3 . S yn ch ro n iza t io n Prim it ive s S e ct io n 5 . 4 . S yn ch ro n izin g Acce s s e s t o Ke rn e l Da t a S t ru ct u re s S e ct io n 5 . 5 . Exa m p le s o f Ra ce Co n d it io n Pre ve n t io n Ch a p t e r 6 . Tim in g Me a s u re m e n t s S e ct io n 6 . 1 . Ha rd wa re Clo cks S e ct io n 6 . 2 . Th e Lin u x Tim e ke e p in g Arch it e ct u re S e ct io n 6 . 3 . CPU's Tim e S h a rin g S e ct io n 6 . 4 . Up d a t in g t h e Tim e a n d Da t e S e ct io n 6 . 5 . Up d a t in g S ys t e m S t a t is t ics S e ct io n 6 . 6 . S o ft wa re Tim e rs S e ct io n 6 . 7 . S ys t e m Ca lls Re la t e d t o Tim in g Me a s u re m e n t s Ch a p t e r 7 . Me m o ry Ma n a g e m e n t S e ct io n 7 . 1 . Pa g e Fra m e Ma n a g e m e n t S e ct io n 7 . 2 . Me m o ry Are a Ma n a g e m e n t S e ct io n 7 . 3 . No n co n t ig u o u s Me m o ry Are a Ma n a g e m e n t Ch a p t e r 8 . Pro ce s s Ad d re s s S p a ce S e ct io n 8 . 1 . Th e Pro ce s s 's Ad d re s s S p a ce S e ct io n 8 . 2 . Th e Me m o ry De s crip t o r S e ct io n 8 . 3 . Me m o ry Re g io n s S e ct io n 8 . 4 . Pa g e Fa u lt Exce p t io n Ha n d le r S e ct io n 8 . 5 . Cre a t in g a n d De le t in g a Pro ce s s Ad d re s s S p a ce S e ct io n 8 . 6 . Ma n a g in g t h e He a p Ch a p t e r 9 . S ys t e m Ca lls S e ct io n 9 . 1 . POS IX APIs a n d S ys t e m Ca lls S e ct io n 9 . 2 . S ys t e m Ca ll Ha n d le r a n d S e rvice Ro u t in e s S e ct io n 9 . 3 . Ke rn e l Wra p p e r Ro u t in e s Ch a p t e r 1 0 . S ig n a ls S e ct io n 1 0 . 1 . Th e Ro le o f S ig n a ls S e ct io n 1 0 . 2 . Ge n e ra t in g a S ig n a l S e ct io n 1 0 . 3 . De live rin g a S ig n a l S e ct io n 1 0 . 4 . S ys t e m Ca lls Re la t e d t o S ig n a l Ha n d lin g

Ch a p t e r 1 1 . Pro ce s s S ch e d u lin g S e ct io n 1 1 . 1 . S ch e d u lin g Po licy S e ct io n 1 1 . 2 . Th e S ch e d u lin g Alg o rit h m S e ct io n 1 1 . 3 . S ys t e m Ca lls Re la t e d t o S ch e d u lin g Ch a p t e r 1 2 . Th e Virt u a l File s ys t e m S e ct io n 1 2 . 1 . Th e Ro le o f t h e Virt u a l File s ys t e m ( VFS ) S e ct io n 1 2 . 2 . VFS Da t a S t ru ct u re s S e ct io n 1 2 . 3 . File s ys t e m Typ e s S e ct io n 1 2 . 4 . File s ys t e m Mo u n t in g S e ct io n 1 2 . 5 . Pa t h n a m e Lo o ku p S e ct io n 1 2 . 6 . Im p le m e n t a t io n s o f VFS S ys t e m Ca lls S e ct io n 1 2 . 7 . File Lo ckin g Ch a p t e r 1 3 . Ma n a g in g I/ O De vice s S e ct io n 1 3 . 1 . I/ O Arch it e ct u re S e ct io n 1 3 . 2 . De vice File s S e ct io n 1 3 . 3 . De vice Drive rs S e ct io n 1 3 . 4 . Blo ck De vice Drive rs S e ct io n 1 3 . 5 . Ch a ra ct e r De vice Drive rs Ch a p t e r 1 4 . Dis k Ca ch e s S e ct io n 1 4 . 1 . Th e Pa g e Ca ch e S e ct io n 1 4 . 2 . Th e Bu ffe r Ca ch e Ch a p t e r 1 5 . Acce s s in g File s S e ct io n 1 5 . 1 . Re a d in g a n d Writ in g a File S e ct io n 1 5 . 2 . Me m o ry Ma p p in g S e ct io n 1 5 . 3 . Dire ct I/ O Tra n s fe rs Ch a p t e r 1 6 . S wa p p in g : Me t h o d s fo r Fre e in g Me m o ry S e ct io n 1 6 . 1 . Wh a t Is S wa p p in g ? S e ct io n 1 6 . 2 . S wa p Are a S e ct io n 1 6 . 3 . Th e S wa p Ca ch e S e ct io n 1 6 . 4 . Tra n s fe rrin g S wa p Pa g e s S e ct io n 1 6 . 5 . S wa p p in g Ou t Pa g e s S e ct io n 1 6 . 6 . S wa p p in g in Pa g e s S e ct io n 1 6 . 7 . Re cla im in g Pa g e Fra m e Ch a p t e r 1 7 . Th e Ext 2 a n d Ext 3 File s ys t e m s S e ct io n 1 7 . 1 . Ge n e ra l Ch a ra ct e ris t ics o f Ext 2 S e ct io n 1 7 . 2 . Ext 2 Dis k Da t a S t ru ct u re s S e ct io n 1 7 . 3 . Ext 2 Me m o ry Da t a S t ru ct u re s S e ct io n 1 7 . 4 . Cre a t in g t h e Ext 2 File s ys t e m S e ct io n 1 7 . 5 . Ext 2 Me t h o d s S e ct io n 1 7 . 6 . Ma n a g in g Ext 2 Dis k S p a ce S e ct io n 1 7 . 7 . Th e Ext 3 File s ys t e m Ch a p t e r 1 8 . Ne t wo rkin g S e ct io n 1 8 . 1 . Ma in Ne t wo rkin g Da t a S t ru ct u re s S e ct io n 1 8 . 2 . S ys t e m Ca lls Re la t e d t o Ne t wo rkin g S e ct io n 1 8 . 3 . S e n d in g Pa cke t s t o t h e Ne t wo rk Ca rd S e ct io n 1 8 . 4 . Re ce ivin g Pa cke t s fro m t h e Ne t wo rk Ca rd Ch a p t e r 1 9 . Pro ce s s Co m m u n ica t io n

S e ct io n 1 9 . 1 . Pip e s S e ct io n 1 9 . 2 . FIFOs S e ct io n 1 9 . 3 . S ys t e m V IPC Ch a p t e r 2 0 . Pro g ra m Exe cu t io n S e ct io n 2 0 . 1 . Exe cu t a b le File s S e ct io n 2 0 . 2 . Exe cu t a b le Fo rm a t s S e ct io n 2 0 . 3 . Exe cu t io n Do m a in s S e ct io n 2 0 . 4 . Th e e xe c Fu n ct io n s Ap p e n d ix A. S ys t e m S t a rt u p S e ct io n A. 1 . Pre h is t o ric Ag e : Th e BIOS S e ct io n A. 2 . An cie n t Ag e : Th e Bo o t Lo a d e r S e ct io n A. 3 . Mid d le Ag e s : Th e s e t u p ( ) Fu n ct io n S e ct io n A. 4 . Re n a is s a n ce : Th e s t a rt u p _ 3 2 ( ) Fu n ct io n s S e ct io n A. 5 . Mo d e rn Ag e : Th e s t a rt _ ke rn e l( ) Fu n ct io n Ap p e n d ix B. Mo d u le s S e ct io n B. 1 . To Be ( a Mo d u le ) o r No t t o Be ? S e ct io n B. 2 . Mo d u le Im p le m e n t a t io n S e ct io n B. 3 . Lin kin g a n d Un lin kin g Mo d u le s S e ct io n B. 4 . Lin kin g Mo d u le s o n De m a n d Ap p e n d ix C. S o u rce Co d e S t ru ct u re Bib lio g ra p h y Bo o ks o n Un ix Ke rn e ls Bo o ks o n t h e Lin u x Ke rn e l Bo o ks o n PC Arch it e ct u re a n d Te ch n ica l Ma n u a ls o n In t e l Micro p ro ce s s o rs Ot h e r On lin e Do cu m e n t a t io n S o u rce s Co lo p h o n In d e x

I l@ve Ru Bo a rd

I l@ve Ru Bo a rd

Copyright Co p yrig h t © 2 0 0 3 O'Re illy & As s o cia t e s , In c. Prin t e d in t h e Un it e d S t a t e s o f Am e rica . Pu b lis h e d b y O'Re illy & As s o cia t e s , In c. , 1 0 0 5 Gra ve n s t e in Hig h wa y No rt h , S e b a s t o p o l, CA 95472. O'Re illy & As s o cia t e s b o o ks m a y b e p u rch a s e d fo r e d u ca t io n a l, b u s in e s s , o r s a le s p ro m o t io n a l u s e . On lin e e d it io n s a re a ls o a va ila b le fo r m o s t t it le s ( h t t p : / / s a fa ri. o re illy. co m ) . Fo r m o re in fo rm a t io n , co n t a ct o u r co rp o ra t e / in s t it u t io n a l s a le s d e p a rt m e n t : ( 8 0 0 ) 9 9 8 - 9 9 3 8 o r co rp o ra t e @o re illy. co m . Nu t s h e ll Ha n d b o o k, t h e Nu t s h e ll Ha n d b o o k lo g o , a n d t h e O'Re illy lo g o a re re g is t e re d t ra d e m a rks o f O'Re illy & As s o cia t e s , In c. Ma n y o f t h e d e s ig n a t io n s u s e d b y m a n u fa ct u re rs a n d s e lle rs t o d is t in g u is h t h e ir p ro d u ct s a re cla im e d a s t ra d e m a rks . Wh e re t h o s e d e s ig n a t io n s a p p e a r in t h is b o o k, a n d O'Re illy & As s o cia t e s , In c. wa s a wa re o f a t ra d e m a rk cla im , t h e d e s ig n a t io n s h a ve b e e n p rin t e d in ca p s o r in it ia l ca p s . Th e a s s o cia t io n b e t we e n t h e im a g e s o f t h e Am e rica n We s t a n d t h e t o p ic o f Lin u x is a t ra d e m a rk o f O'Re illy & As s o cia t e s , In c. Wh ile e ve ry p re ca u t io n h a s b e e n t a ke n in t h e p re p a ra t io n o f t h is b o o k, t h e p u b lis h e r a n d a u t h o rs a s s u m e n o re s p o n s ib ilit y fo r e rro rs o r o m is s io n s , o r fo r d a m a g e s re s u lt in g fro m t h e u s e o f t h e in fo rm a t io n co n t a in e d h e re in . I l@ve Ru Bo a rd

I l@ve Ru Bo a rd

Preface In t h e s p rin g s e m e s t e r o f 1 9 9 7 , we t a u g h t a co u rs e o n o p e ra t in g s ys t e m s b a s e d o n Lin u x 2 . 0 . Th e id e a wa s t o e n co u ra g e s t u d e n t s t o re a d t h e s o u rce co d e . To a ch ie ve t h is , we a s s ig n e d t e rm p ro je ct s co n s is t in g o f m a kin g ch a n g e s t o t h e ke rn e l a n d p e rfo rm in g t e s t s o n t h e m o d ifie d ve rs io n . We a ls o wro t e co u rs e n o t e s fo r o u r s t u d e n t s a b o u t a fe w crit ica l fe a t u re s o f Lin u x s u ch a s t a s k s wit ch in g a n d t a s k s ch e d u lin g . Ou t o f t h is wo rk — a n d wit h a lo t o f s u p p o rt fro m o u r O'Re illy e d it o r An d y Ora m — ca m e t h e firs t e d it io n o f Un d e rs t a n d in g t h e Lin u x Ke rn e l a n d t h e e n d o f 2 0 0 0 , wh ich co ve re d Lin u x 2 . 2 wit h a fe w a n t icip a t io n s o n Lin u x 2 . 4 . Th e s u cce s s e n co u n t e re d b y t h is b o o k e n co u ra g e d u s t o co n t in u e a lo n g t h is lin e , a n d in t h e fa ll o f 2 0 0 1 we s t a rt e d p la n n in g a s e co n d e d it io n co ve rin g Lin u x 2 . 4 . Ho we ve r, Lin u x 2 . 4 is q u it e d iffe re n t fro m Lin u x 2 . 2 . Ju s t t o m e n t io n a fe w e xa m p le s , t h e virt u a l m e m o ry s ys t e m is e n t ire ly n e w, s u p p o rt fo r m u lt ip ro ce s s o r s ys t e m s is m u ch b e t t e r, a n d wh o le n e w cla s s e s o f h a rd wa re d e vice s h a ve b e e n a d d e d . As a re s u lt , we h a d t o re writ e fro m s cra t ch t wo - t h ird s o f t h e b o o k, in cre a s in g it s s ize b y ro u g h ly 2 5 p e rce n t . As in o u r firs t e xp e rie n ce , we re a d t h o u s a n d s o f lin e s o f co d e , t ryin g t o m a ke s e n s e o f t h e m . Aft e r a ll t h is wo rk, we ca n s a y t h a t it wa s wo rt h t h e e ffo rt . We le a rn e d a lo t o f t h in g s yo u d o n 't fin d in b o o ks , a n d we h o p e we h a ve s u cce e d e d in co n ve yin g s o m e o f t h is in fo rm a t io n in t h e fo llo win g p a g e s .

I l@ve Ru Bo a rd

I l@ve Ru Bo a rd

The Audience for This Book All p e o p le cu rio u s a b o u t h o w Lin u x wo rks a n d wh y it is s o e fficie n t will fin d a n s we rs h e re . Aft e r re a d in g t h e b o o k, yo u will fin d yo u r wa y t h ro u g h t h e m a n y t h o u s a n d s o f lin e s o f co d e , d is t in g u is h in g b e t we e n cru cia l d a t a s t ru ct u re s a n d s e co n d a ry o n e s —in s h o rt , b e co m in g a t ru e Lin u x h a cke r. Ou r wo rk m ig h t b e co n s id e re d a g u id e d t o u r o f t h e Lin u x ke rn e l: m o s t o f t h e s ig n ifica n t d a t a s t ru ct u re s a n d m a n y a lg o rit h m s a n d p ro g ra m m in g t ricks u s e d in t h e ke rn e l a re d is cu s s e d . In m a n y ca s e s , t h e re le va n t fra g m e n t s o f co d e a re d is cu s s e d lin e b y lin e . Of co u rs e , yo u s h o u ld h a ve t h e Lin u x s o u rce co d e o n h a n d a n d s h o u ld b e willin g t o s p e n d s o m e e ffo rt d e cip h e rin g s o m e o f t h e fu n ct io n s t h a t a re n o t , fo r s a ke o f b re vit y, fu lly d e s crib e d . On a n o t h e r le ve l, t h e b o o k p ro vid e s va lu a b le in s ig h t t o p e o p le wh o wa n t t o kn o w m o re a b o u t t h e crit ica l d e s ig n is s u e s in a m o d e rn o p e ra t in g s ys t e m . It is n o t s p e cifica lly a d d re s s e d t o s ys t e m a d m in is t ra t o rs o r p ro g ra m m e rs ; it is m o s t ly fo r p e o p le wh o wa n t t o u n d e rs t a n d h o w t h in g s re a lly wo rk in s id e t h e m a ch in e ! As wit h a n y g o o d g u id e , we t ry t o g o b e yo n d s u p e rficia l fe a t u re s . We o ffe r a b a ckg ro u n d , s u ch a s t h e h is t o ry o f m a jo r fe a t u re s a n d t h e re a s o n s wh y t h e y we re u s e d . I l@ve Ru Bo a rd

I l@ve Ru Bo a rd

Organization of the Material Wh e n we b e g a n t o writ e t h is b o o k, we we re fa ce d wit h a crit ica l d e cis io n : s h o u ld we re fe r t o a s p e cific h a rd wa re p la t fo rm o r s kip t h e h a rd wa re - d e p e n d e n t d e t a ils a n d co n ce n t ra t e o n t h e p u re h a rd wa re - in d e p e n d e n t p a rt s o f t h e ke rn e l? Ot h e rs b o o ks o n Lin u x ke rn e l in t e rn a ls h a ve ch o s e n t h e la t t e r a p p ro a ch ; we d e cid e d t o a d o p t t h e fo rm e r o n e fo r t h e fo llo win g re a s o n s : ●



Efficie n t ke rn e ls t a ke a d va n t a g e o f m o s t a va ila b le h a rd wa re fe a t u re s , s u ch a s a d d re s s in g t e ch n iq u e s , ca ch e s , p ro ce s s o r e xce p t io n s , s p e cia l in s t ru ct io n s , p ro ce s s o r co n t ro l re g is t e rs , a n d s o o n . If we wa n t t o co n vin ce yo u t h a t t h e ke rn e l in d e e d d o e s q u it e a g o o d jo b in p e rfo rm in g a s p e cific t a s k, we m u s t firs t t e ll wh a t kin d o f s u p p o rt co m e s fro m t h e h a rd wa re . Eve n if a la rg e p o rt io n o f a Un ix ke rn e l s o u rce co d e is p ro ce s s o r- in d e p e n d e n t a n d co d e d in C la n g u a g e , a s m a ll a n d crit ica l p a rt is co d e d in a s s e m b ly la n g u a g e . A t h o ro u g h kn o wle d g e o f t h e ke rn e l t h e re fo re re q u ire s t h e s t u d y o f a fe w a s s e m b ly la n g u a g e fra g m e n t s t h a t in t e ra ct wit h t h e h a rd wa re .

Wh e n co ve rin g h a rd wa re fe a t u re s , o u r s t ra t e g y is q u it e s im p le : ju s t s ke t ch t h e fe a t u re s t h a t a re t o t a lly h a rd wa re - d rive n wh ile d e t a ilin g t h o s e t h a t n e e d s o m e s o ft wa re s u p p o rt . In fa ct , we a re in t e re s t e d in ke rn e l d e s ig n ra t h e r t h a n in co m p u t e r a rch it e ct u re . Ou r n e xt s t e p in ch o o s in g o u r p a t h co n s is t e d o f s e le ct in g t h e co m p u t e r s ys t e m t o d e s crib e . Alt h o u g h Lin u x is n o w ru n n in g o n s e ve ra l kin d s o f p e rs o n a l co m p u t e rs a n d wo rks t a t io n s , we d e cid e d t o co n ce n t ra t e o n t h e ve ry p o p u la r a n d ch e a p IBM- co m p a t ib le p e rs o n a l co m p u t e rs —a n d t h u s o n t h e 8 0 x 8 6 m icro p ro ce s s o rs a n d o n s o m e s u p p o rt ch ip s in clu d e d in t h e s e p e rs o n a l co m p u t e rs . Th e t e rm 8 0 x 8 6 m icro p ro ce s s o r will b e u s e d in t h e fo rt h co m in g ch a p t e rs t o d e n o t e t h e In t e l 8 0 3 8 6 , 8 0 4 8 6 , Pe n t iu m , Pe n t iu m Pro , Pe n t iu m II, Pe n t iu m III, a n d Pe n t iu m 4 m icro p ro ce s s o rs o r co m p a t ib le m o d e ls . In a fe w ca s e s , e xp licit re fe re n ce s will b e m a d e t o s p e cific m o d e ls . On e m o re ch o ice we h a d t o m a ke wa s t h e o rd e r t o fo llo w in s t u d yin g Lin u x co m p o n e n t s . We t rie d a b o t t o m - u p a p p ro a ch : s t a rt wit h t o p ics t h a t a re h a rd wa re - d e p e n d e n t a n d e n d wit h t h o s e t h a t a re t o t a lly h a rd wa re - in d e p e n d e n t . In fa ct , we 'll m a ke m a n y re fe re n ce s t o t h e 8 0 x 8 6 m icro p ro ce s s o rs in t h e firs t p a rt o f t h e b o o k, wh ile t h e re s t o f it is re la t ive ly h a rd wa re in d e p e n d e n t . On e s ig n ifica n t e xce p t io n is m a d e in Ch a p t e r 1 3 . In p ra ct ice , fo llo win g a b o t t o m - u p a p p ro a ch is n o t a s s im p le a s it lo o ks , s in ce t h e a re a s o f m e m o ry m a n a g e m e n t , p ro ce s s m a n a g e m e n t , a n d file s ys t e m s a re in t e rt win e d ; a fe w fo rwa rd re fe re n ce s —t h a t is , re fe re n ce s t o t o p ics ye t t o b e e xp la in e d —a re u n a vo id a b le . Ea ch ch a p t e r s t a rt s wit h a t h e o re t ica l o ve rvie w o f t h e t o p ics co ve re d . Th e m a t e ria l is t h e n p re s e n t e d a cco rd in g t o t h e b o t t o m - u p a p p ro a ch . We s t a rt wit h t h e d a t a s t ru ct u re s n e e d e d t o s u p p o rt t h e fu n ct io n a lit ie s d e s crib e d in t h e ch a p t e r. Th e n we u s u a lly m o ve fro m t h e lo we s t le ve l o f fu n ct io n s t o h ig h e r le ve ls , o ft e n e n d in g b y s h o win g h o w s ys t e m ca lls is s u e d b y u s e r a p p lica t io n s a re s u p p o rt e d .

Level of Description Lin u x s o u rce co d e fo r a ll s u p p o rt e d a rch it e ct u re s is co n t a in e d in m o re t h a n 8 , 0 0 0 C a n d a s s e m b ly la n g u a g e file s s t o re d in a b o u t 5 3 0 s u b d ire ct o rie s ; it co n s is t s o f ro u g h ly 4 m illio n lin e s o f co d e , wh ich o ccu p y o ve r 1 4 4 m e g a b yt e s o f d is k s p a ce . Of co u rs e , t h is b o o k ca n

co ve r o n ly a ve ry s m a ll p o rt io n o f t h a t co d e . Ju s t t o fig u re o u t h o w b ig t h e Lin u x s o u rce is , co n s id e r t h a t t h e wh o le s o u rce co d e o f t h e b o o k yo u a re re a d in g o ccu p ie s le s s t h a n 3 m e g a b yt e s o f d is k s p a ce . Th e re fo re , we wo u ld n e e d m o re t h a n 4 0 b o o ks like t h is t o lis t a ll co d e , wit h o u t e ve n co m m e n t in g o n it ! S o we h a d t o m a ke s o m e ch o ice s a b o u t t h e p a rt s t o d e s crib e . Th is is a ro u g h a s s e s s m e n t o f o u r d e cis io n s : ● ●





We d e s crib e p ro ce s s a n d m e m o ry m a n a g e m e n t fa irly t h o ro u g h ly. We co ve r t h e Virt u a l File s ys t e m a n d t h e Ext 2 a n d Ext 3 file s ys t e m s , a lt h o u g h m a n y fu n ct io n s a re ju s t m e n t io n e d wit h o u t d e t a ilin g t h e co d e ; we d o n o t d is cu s s o t h e r file s ys t e m s s u p p o rt e d b y Lin u x. We d e s crib e d e vice d rive rs , wh ich a cco u n t fo r a g o o d p a rt o f t h e ke rn e l, a s fa r a s t h e ke rn e l in t e rfa ce is co n ce rn e d , b u t d o n o t a t t e m p t a n a lys is o f e a ch s p e cific d rive r, in clu d in g t h e t e rm in a l d rive rs . We co ve r t h e in n e r la ye rs o f n e t wo rkin g in a ra t h e r s ke t ch y wa y, s in ce t h is a re a d e s e rve s a wh o le n e w b o o k b y it s e lf.

Th e b o o k d e s crib e s t h e o fficia l 2 . 4 . 1 8 ve rs io n o f t h e Lin u x ke rn e l, wh ich ca n b e d o wn lo a d e d fro m t h e we b s it e , h t t p : / / www. ke rn e l. o rg . Be a wa re t h a t m o s t d is t rib u t io n s o f GNU/ Lin u x m o d ify t h e o fficia l ke rn e l t o im p le m e n t n e w fe a t u re s o r t o im p ro ve it s e fficie n cy. In a fe w ca s e s , t h e s o u rce co d e p ro vid e d b y yo u r fa vo rit e d is t rib u t io n m ig h t d iffe r s ig n ifica n t ly fro m t h e o n e d e s crib e d in t h is b o o k. In m a n y ca s e s , t h e o rig in a l co d e h a s b e e n re writ t e n in a n e a s ie r- t o - re a d b u t le s s e fficie n t wa y. Th is o ccu rs a t t im e - crit ica l p o in t s a t wh ich s e ct io n s o f p ro g ra m s a re o ft e n writ t e n in a m ixt u re o f h a n d - o p t im ize d C a n d As s e m b ly co d e . On ce a g a in , o u r a im is t o p ro vid e s o m e h e lp in s t u d yin g t h e o rig in a l Lin u x co d e . Wh ile d is cu s s in g ke rn e l co d e , we o ft e n e n d u p d e s crib in g t h e u n d e rp in n in g s o f m a n y fa m ilia r fe a t u re s t h a t Un ix p ro g ra m m e rs h a ve h e a rd o f a n d a b o u t wh ich t h e y m a y b e cu rio u s ( s h a re d a n d m a p p e d m e m o ry, s ig n a ls , p ip e s , s ym b o lic lin ks , e t c. ) . I l@ve Ru Bo a rd

I l@ve Ru Bo a rd

Overview of the Book To m a ke life e a s ie r, Ch a p t e r 1 p re s e n t s a g e n e ra l p ict u re o f wh a t is in s id e a Un ix ke rn e l a n d h o w Lin u x co m p e t e s a g a in s t o t h e r we ll- kn o wn Un ix s ys t e m s . Th e h e a rt o f a n y Un ix ke rn e l is m e m o ry m a n a g e m e n t . Ch a p t e r 2 e xp la in s h o w 8 0 x 8 6 p ro ce s s o rs in clu d e s p e cia l circu it s t o a d d re s s d a t a in m e m o ry a n d h o w Lin u x e xp lo it s t h e m . Pro ce s s e s a re a fu n d a m e n t a l a b s t ra ct io n o ffe re d b y Lin u x a n d a re in t ro d u ce d in Ch a p t e r 3 . He re we a ls o e xp la in h o w e a ch p ro ce s s ru n s e it h e r in a n u n p rivile g e d Us e r Mo d e o r in a p rivile g e d Ke rn e l Mo d e . Tra n s it io n s b e t we e n Us e r Mo d e a n d Ke rn e l Mo d e h a p p e n o n ly t h ro u g h we ll- e s t a b lis h e d h a rd wa re m e ch a n is m s ca lle d in t e rru p t s a n d e x ce p t io n s . Th e s e a re in t ro d u ce d in Ch a p t e r 4 . In m a n y o cca s io n s , t h e ke rn e l h a s t o d e a l wit h b u rs t s o f in t e rru p t s co m in g fro m d iffe re n t d e vice s . S yn ch ro n iza t io n m e ch a n is m s a re n e e d e d s o t h a t a ll t h e s e re q u e s t s ca n b e s e rvice d in a n in t e rle a ve d wa y b y t h e ke rn e l: t h e y a re d is cu s s e d in Ch a p t e r 5 fo r b o t h u n ip ro ce s s o r a n d m u lt ip ro ce s s o r s ys t e m s . On e t yp e o f in t e rru p t is cru cia l fo r a llo win g Lin u x t o t a ke ca re o f e la p s e d t im e ; fu rt h e r d e t a ils ca n b e fo u n d in Ch a p t e r 6 . Ne xt we fo cu s a g a in o n m e m o ry: Ch a p t e r 7 d e s crib e s t h e s o p h is t ica t e d t e ch n iq u e s re q u ire d t o h a n d le t h e m o s t p re cio u s re s o u rce in t h e s ys t e m ( b e s id e s t h e p ro ce s s o rs , o f co u rs e ) , a va ila b le m e m o ry. Th is re s o u rce m u s t b e g ra n t e d b o t h t o t h e Lin u x ke rn e l a n d t o t h e u s e r a p p lica t io n s . Ch a p t e r 8 s h o ws h o w t h e ke rn e l co p e s wit h t h e re q u e s t s fo r m e m o ry is s u e d b y g re e d y a p p lica t io n p ro g ra m s . Ch a p t e r 9 e xp la in s h o w a p ro ce s s ru n n in g in Us e r Mo d e m a ke s re q u e s t s t o t h e ke rn e l, wh ile Ch a p t e r 1 0 d e s crib e s h o w a p ro ce s s m a y s e n d s yn ch ro n iza t io n s ig n a ls t o o t h e r p ro ce s s e s . Ch a p t e r 1 1 e xp la in s h o w Lin u x e xe cu t e s , in t u rn , e ve ry a ct ive p ro ce s s in t h e s ys t e m s o t h a t a ll o f t h e m ca n p ro g re s s t o wa rd t h e ir co m p le t io n s . No w we a re re a d y t o m o ve o n t o a n o t h e r e s s e n t ia l t o p ic, h o w Lin u x im p le m e n t s t h e file s ys t e m . A s e rie s o f ch a p t e rs co ve r t h is t o p ic. Ch a p t e r 1 2 in t ro d u ce s a g e n e ra l la ye r t h a t s u p p o rt s m a n y d iffe re n t file s ys t e m s . S o m e Lin u x file s a re s p e cia l b e ca u s e t h e y p ro vid e t ra p d o o rs t o re a ch h a rd wa re d e vice s ; Ch a p t e r 1 3 o ffe rs in s ig h t s o n t h e s e s p e cia l file s a n d o n t h e co rre s p o n d in g h a rd wa re d e vice d rive rs . An o t h e r is s u e t o co n s id e r is d is k a cce s s t im e ; Ch a p t e r 1 4 s h o ws h o w a cle ve r u s e o f RAM re d u ce s d is k a cce s s e s , t h e re fo re im p ro vin g s ys t e m p e rfo rm a n ce s ig n ifica n t ly. Bu ild in g o n t h e m a t e ria l co ve re d in t h e s e la s t ch a p t e rs , we ca n n o w e xp la in in Ch a p t e r 1 5 h o w u s e r a p p lica t io n s a cce s s n o rm a l file s . Ch a p t e r 1 6 co m p le t e s o u r d is cu s s io n o f Lin u x m e m o ry m a n a g e m e n t a n d e xp la in s t h e t e ch n iq u e s u s e d b y Lin u x t o e n s u re t h a t e n o u g h m e m o ry is a lwa ys a va ila b le . Th e la s t ch a p t e r d e a lin g wit h file s is Ch a p t e r 1 7 wh ich illu s t ra t e s t h e m o s t fre q u e n t ly u s e d Lin u x file s ys t e m , n a m e ly Ext 2 a n d it s re ce n t e vo lu t io n , Ext 3 . Ch a p t e r 1 8 d e a ls wit h t h e lo we r la ye rs o f n e t wo rkin g . Th e la s t t wo ch a p t e rs e n d o u r d e t a ile d t o u r o f t h e Lin u x ke rn e l: Ch a p t e r 1 9 in t ro d u ce s co m m u n ica t io n m e ch a n is m s o t h e r t h a n s ig n a ls a va ila b le t o Us e r Mo d e p ro ce s s e s ; Ch a p t e r

2 0 e xp la in s h o w u s e r a p p lica t io n s a re s t a rt e d . La s t , b u t n o t le a s t , a re t h e a p p e n d ixe s : Ap p e n d ix A s ke t ch e s o u t h o w Lin u x is b o o t e d , wh ile Ap p e n d ix B d e s crib e s h o w t o d yn a m ica lly re co n fig u re t h e ru n n in g ke rn e l, a d d in g a n d re m o vin g fu n ct io n a lit ie s a s n e e d e d . Ap p e n d ix C is ju s t a lis t o f t h e d ire ct o rie s t h a t co n t a in t h e Lin u x s o u rce co d e . I l@ve Ru Bo a rd

I l@ve Ru Bo a rd

Background Information No p re re q u is it e s a re re q u ire d , e xce p t s o m e s kill in C p ro g ra m m in g la n g u a g e a n d p e rh a p s s o m e kn o wle d g e o f As s e m b ly la n g u a g e . I l@ve Ru Bo a rd

I l@ve Ru Bo a rd

Conventions in This Book Th e fo llo win g is a lis t o f t yp o g ra p h ica l co n ve n t io n s u s e d in t h is b o o k:

Constant Width Is u s e d t o s h o w t h e co n t e n t s o f co d e file s o r t h e o u t p u t fro m co m m a n d s , a n d t o in d ica t e s o u rce co d e ke ywo rd s t h a t a p p e a r in co d e . It a lic Is u s e d fo r file a n d d ire ct o ry n a m e s , p ro g ra m a n d co m m a n d n a m e s , co m m a n d - lin e o p t io n s , URLs , a n d fo r e m p h a s izin g n e w t e rm s .

I l@ve Ru Bo a rd

I l@ve Ru Bo a rd

How to Contact Us Ple a s e a d d re s s co m m e n t s a n d q u e s t io n s co n ce rn in g t h is b o o k t o t h e p u b lis h e r: O'Re illy & As s o cia t e s , In c. 1 0 0 5 Gra ve n s t e in Hig h wa y No rt h S e b a s t o p o l, CA 9 5 4 7 2 ( 8 0 0 ) 9 9 8 - 9 9 3 8 ( in t h e Un it e d S t a t e s o r Ca n a d a ) ( 7 0 7 ) 8 2 9 - 0 5 1 5 ( in t e rn a t io n a l o r lo ca l) ( 7 0 7 ) 8 2 9 - 0 1 0 4 ( fa x) We h a ve a we b p a g e fo r t h is b o o k, wh e re we lis t e rra t a , e xa m p le s , o r a n y a d d it io n a l in fo rm a t io n . Yo u ca n a cce s s t h is p a g e a t : h t t p : / / www. o re illy. co m / ca t a lo g / lin u xke rn e l2 / To co m m e n t o r a s k t e ch n ica l q u e s t io n s a b o u t t h is b o o k, s e n d e m a il t o : b o o kq u e s t io n s @o re illy. co m Fo r m o re in fo rm a t io n a b o u t o u r b o o ks , co n fe re n ce s , Re s o u rce Ce n t e rs , a n d t h e O'Re illy Ne t wo rk, s e e o u r we b s it e a t : h t t p : / / www. o re illy. co m

I l@ve Ru Bo a rd

I l@ve Ru Bo a rd

Acknowledgments Th is b o o k wo u ld n o t h a ve b e e n writ t e n wit h o u t t h e p re cio u s h e lp o f t h e m a n y s t u d e n t s o f t h e Un ive rs it y o f Ro m e s ch o o l o f e n g in e e rin g "To r Ve rg a t a " wh o t o o k o u r co u rs e a n d t rie d t o d e cip h e r le ct u re n o t e s a b o u t t h e Lin u x ke rn e l. Th e ir s t re n u o u s e ffo rt s t o g ra s p t h e m e a n in g o f t h e s o u rce co d e le d u s t o im p ro ve o u r p re s e n t a t io n a n d co rre ct m a n y m is t a ke s . An d y Ora m , o u r wo n d e rfu l e d it o r a t O'Re illy & As s o cia t e s , d e s e rve s a lo t o f cre d it . He wa s t h e firs t a t O'Re illy t o b e lie ve in t h is p ro je ct , a n d h e s p e n t a lo t o f t im e a n d e n e rg y d e cip h e rin g o u r p re lim in a ry d ra ft s . He a ls o s u g g e s t e d m a n y wa ys t o m a ke t h e b o o k m o re re a d a b le , a n d h e wro t e s e ve ra l e xce lle n t in t ro d u ct o ry p a ra g ra p h s . Ma n y t h a n ks a ls o t o t h e O'Re illy s t a ff, e s p e cia lly Ro b Ro m a n o , t h e t e ch n ica l illu s t ra t o r, a n d Le n n y Mu e lln e r, fo r t o o ls s u p p o rt . We h a d s o m e p re s t ig io u s re vie we rs wh o re a d o u r t e xt q u it e ca re fu lly. Th e firs t e d it io n wa s ch e cke d b y ( in a lp h a b e t ica l o rd e r b y firs t n a m e ) Ala n Co x, Mich a e l Ke rris k, Pa u l Kin ze lm a n , Ra p h Le vie n , a n d Rik va n Rie l. Ere z Za d o k, Je rry Co o p e rs t e in , Jo h n Go e rze n , Mich a e l Ke rris k, Pa u l Kin ze lm a n , Rik va n Rie l, a n d Wa lt S m it h re vie we d t h is s e co n d e d it io n . Th e ir co m m e n t s , t o g e t h e r wit h t h o s e o f m a n y re a d e rs fro m a ll o ve r t h e wo rld , h e lp e d u s t o re m o ve s e ve ra l e rro rs a n d in a ccu ra cie s a n d h a ve m a d e t h is b o o k s t ro n g e r. —Da n ie l P. Bo ve t Ma rco Ce s a t i S e pte m be r 2002 I l@ve Ru Bo a rd

I l@ve Ru Bo a rd

Chapter 1. Introduction Lin u x is a m e m b e r o f t h e la rg e fa m ily o f Un ix- like o p e ra t in g s ys t e m s . A re la t ive n e wco m e r e xp e rie n cin g s u d d e n s p e ct a cu la r p o p u la rit y s t a rt in g in t h e la t e 1 9 9 0 s , Lin u x jo in s s u ch we llkn o wn co m m e rcia l Un ix o p e ra t in g s ys t e m s a s S ys t e m V Re le a s e 4 ( S VR4 ) , d e ve lo p e d b y AT&T ( n o w o wn e d b y t h e S CO Gro u p ) ; t h e 4 . 4 BS D re le a s e fro m t h e Un ive rs it y o f Ca lifo rn ia a t Be rke le y ( 4 . 4 BS D) ; Dig it a l Un ix fro m Dig it a l Eq u ip m e n t Co rp o ra t io n ( n o w He wle t t Pa cka rd ) ; AIX fro m IBM; HP- UX fro m He wle t t - Pa cka rd ; S o la ris fro m S u n Micro s ys t e m s ; a n d Ma c OS X fro m Ap p le Co m p u t e r, In c. Lin u x wa s in it ia lly d e ve lo p e d b y Lin u s To rva ld s in 1 9 9 1 a s a n o p e ra t in g s ys t e m fo r IBMco m p a t ib le p e rs o n a l co m p u t e rs b a s e d o n t h e In t e l 8 0 3 8 6 m icro p ro ce s s o r. Lin u s re m a in s d e e p ly in vo lve d wit h im p ro vin g Lin u x, ke e p in g it u p t o d a t e wit h va rio u s h a rd wa re d e ve lo p m e n t s a n d co o rd in a t in g t h e a ct ivit y o f h u n d re d s o f Lin u x d e ve lo p e rs a ro u n d t h e wo rld . Ove r t h e ye a rs , d e ve lo p e rs h a ve wo rke d t o m a ke Lin u x a va ila b le o n o t h e r a rch it e ct u re s , in clu d in g He wle t t - Pa cka rd 's Alp h a , It a n iu m ( t h e re ce n t In t e l's 6 4 - b it p ro ce s s o r) , MIPS , S PARC, Mo t o ro la MC6 8 0 x0 , Po we rPC, a n d IBM's zS e rie s . On e o f t h e m o re a p p e a lin g b e n e fit s t o Lin u x is t h a t it is n 't a co m m e rcia l o p e ra t in g s ys t e m : it s s o u rce co d e u n d e r t h e GNU Pu b lic Lice n s e [ 1 ] is o p e n a n d a va ila b le t o a n yo n e t o s t u d y ( a s we will in t h is b o o k) ; if yo u d o wn lo a d t h e co d e ( t h e o fficia l s it e is h t t p : / / www. ke rn e l. o rg ) o r ch e ck t h e s o u rce s o n a Lin u x CD, yo u will b e a b le t o e xp lo re , fro m t o p t o b o t t o m , o n e o f t h e m o s t s u cce s s fu l, m o d e rn o p e ra t in g s ys t e m s . Th is b o o k, in fa ct , a s s u m e s yo u h a ve t h e s o u rce co d e o n h a n d a n d ca n a p p ly wh a t we s a y t o yo u r o wn e xp lo ra t io n s . [1]

Th e GNU p ro je ct is co o rd in a t e d b y t h e Fre e S o ft wa re Fo u n d a t io n , In c. ( h t t p : / / www. g n u . o rg ) ; it s a im is t o im p le m e n t a wh o le o p e ra t in g s ys t e m fre e ly u s a b le b y e ve ryo n e . Th e a va ila b ilit y o f a GNU C co m p ile r h a s b e e n e s s e n t ia l fo r t h e s u cce s s o f t h e Lin u x p ro je ct .

Te ch n ica lly s p e a kin g , Lin u x is a t ru e Un ix ke rn e l, a lt h o u g h it is n o t a fu ll Un ix o p e ra t in g s ys t e m b e ca u s e it d o e s n o t in clu d e a ll t h e Un ix a p p lica t io n s , s u ch a s file s ys t e m u t ilit ie s , win d o win g s ys t e m s a n d g ra p h ica l d e s kt o p s , s ys t e m a d m in is t ra t o r co m m a n d s , t e xt e d it o rs , co m p ile rs , a n d s o o n . Ho we ve r, s in ce m o s t o f t h e s e p ro g ra m s a re fre e ly a va ila b le u n d e r t h e GNU Ge n e ra l Pu b lic Lice n s e , t h e y ca n b e in s t a lle d o n t o o n e o f t h e file s ys t e m s s u p p o rt e d b y Lin u x. S in ce t h e Lin u x ke rn e l re q u ire s s o m u ch a d d it io n a l s o ft wa re t o p ro vid e a u s e fu l e n viro n m e n t , m a n y Lin u x u s e rs p re fe r t o re ly o n co m m e rcia l d is t rib u t io n s , a va ila b le o n CD- ROM, t o g e t t h e co d e in clu d e d in a s t a n d a rd Un ix s ys t e m . Alt e rn a t ive ly, t h e co d e m a y b e o b t a in e d fro m s e ve ra l d iffe re n t FTP s it e s . Th e Lin u x s o u rce co d e is u s u a lly in s t a lle d in t h e / u s r/ s rc/ lin u x d ire ct o ry. In t h e re s t o f t h is b o o k, a ll file p a t h n a m e s will re fe r im p licit ly t o t h a t d ire ct o ry.

I l@ve Ru Bo a rd

I l@ve Ru Bo a rd

1.1 Linux Versus Other Unix-Like Kernels Th e va rio u s Un ix- like s ys t e m s o n t h e m a rke t , s o m e o f wh ich h a ve a lo n g h is t o ry a n d s h o w s ig n s o f a rch a ic p ra ct ice s , d iffe r in m a n y im p o rt a n t re s p e ct s . All co m m e rcia l va ria n t s we re d e rive d fro m e it h e r S VR4 o r 4 . 4 BS D, a n d a ll t e n d t o a g re e o n s o m e co m m o n s t a n d a rd s like IEEE's Po rt a b le Op e ra t in g S ys t e m s b a s e d o n Un ix ( POS IX) a n d X/ Op e n 's Co m m o n Ap p lica t io n s En viro n m e n t ( CAE) . Th e cu rre n t s t a n d a rd s s p e cify o n ly a n a p p lica t io n p ro g ra m m in g in t e rfa ce ( API) —t h a t is , a we ll- d e fin e d e n viro n m e n t in wh ich u s e r p ro g ra m s s h o u ld ru n . Th e re fo re , t h e s t a n d a rd s d o n o t im p o s e a n y re s t rict io n o n in t e rn a l d e s ig n ch o ice s o f a co m p lia n t ke rn e l. [ 2 ] [2]

As a m a t t e r o f fa ct , s e ve ra l n o n - Un ix o p e ra t in g s ys t e m s , s u ch a s Win d o ws NT, a re POS IX- co m p lia n t .

To d e fin e a co m m o n u s e r in t e rfa ce , Un ix- like ke rn e ls o ft e n s h a re fu n d a m e n t a l d e s ig n id e a s a n d fe a t u re s . In t h is re s p e ct , Lin u x is co m p a ra b le wit h t h e o t h e r Un ix- like o p e ra t in g s ys t e m s . Re a d in g t h is b o o k a n d s t u d yin g t h e Lin u x ke rn e l, t h e re fo re , m a y h e lp yo u u n d e rs t a n d t h e o t h e r Un ix va ria n t s t o o . Th e 2 . 4 ve rs io n o f t h e Lin u x ke rn e l a im s t o b e co m p lia n t wit h t h e IEEE POS IX s t a n d a rd . Th is , o f co u rs e , m e a n s t h a t m o s t e xis t in g Un ix p ro g ra m s ca n b e co m p ile d a n d e xe cu t e d o n a Lin u x s ys t e m wit h ve ry lit t le e ffo rt o r e ve n wit h o u t t h e n e e d fo r p a t ch e s t o t h e s o u rce co d e . Mo re o ve r, Lin u x in clu d e s a ll t h e fe a t u re s o f a m o d e rn Un ix o p e ra t in g s ys t e m , s u ch a s virt u a l m e m o ry, a virt u a l file s ys t e m , lig h t we ig h t p ro ce s s e s , re lia b le s ig n a ls , S VR4 in t e rp ro ce s s co m m u n ica t io n s , s u p p o rt fo r S ym m e t ric Mu lt ip ro ce s s o r ( S MP) s ys t e m s , a n d s o o n . By it s e lf, t h e Lin u x ke rn e l is n o t ve ry in n o va t ive . Wh e n Lin u s To rva ld s wro t e t h e firs t ke rn e l, h e re fe rre d t o s o m e cla s s ica l b o o ks o n Un ix in t e rn a ls , like Ma u rice Ba ch 's Th e De s ig n o f t h e Un ix Op e ra t in g S y s t e m ( Pre n t ice Ha ll, 1 9 8 6 ) . Act u a lly, Lin u x s t ill h a s s o m e b ia s t o wa rd t h e Un ix b a s e lin e d e s crib e d in Ba ch 's b o o k ( i. e . , S VR4 ) . Ho we ve r, Lin u x d o e s n 't s t ick t o a n y p a rt icu la r va ria n t . In s t e a d , it t rie s t o a d o p t t h e b e s t fe a t u re s a n d d e s ig n ch o ice s o f s e ve ra l d iffe re n t Un ix ke rn e ls . Th e fo llo win g lis t d e s crib e s h o w Lin u x co m p e t e s a g a in s t s o m e we ll- kn o wn co m m e rcia l Un ix ke rn e ls : Mo n o lit h ic k e rn e l It is a la rg e , co m p le x d o - it - yo u rs e lf p ro g ra m , co m p o s e d o f s e ve ra l lo g ica lly d iffe re n t co m p o n e n t s . In t h is , it is q u it e co n ve n t io n a l; m o s t co m m e rcia l Un ix va ria n t s a re m o n o lit h ic. ( A n o t a b le e xce p t io n is Ca rn e g ie - Me llo n 's Ma ch 3 . 0 , wh ich fo llo ws a m icro ke rn e l a p p ro a ch . ) Co m p ile d a n d s t a t ica lly lin k e d t ra d it io n a l Un ix k e rn e ls Mo s t m o d e rn ke rn e ls ca n d yn a m ica lly lo a d a n d u n lo a d s o m e p o rt io n s o f t h e ke rn e l co d e ( t yp ica lly, d e vice d rive rs ) , wh ich a re u s u a lly ca lle d m o d u le s . Lin u x's s u p p o rt fo r m o d u le s is ve ry g o o d , s in ce it is a b le t o a u t o m a t ica lly lo a d a n d u n lo a d m o d u le s o n d e m a n d . Am o n g t h e m a in co m m e rcia l Un ix va ria n t s , o n ly t h e S VR4 . 2 a n d S o la ris ke rn e ls h a ve a s im ila r fe a t u re .

Ke rn e l t h re a d in g S o m e m o d e rn Un ix ke rn e ls , s u ch a s S o la ris 2 . x a n d S VR4 . 2 / MP, a re o rg a n ize d a s a s e t o f ke rn e l t h re a d s . A ke rn e l t h re a d is a n e xe cu t io n co n t e xt t h a t ca n b e in d e p e n d e n t ly s ch e d u le d ; it m a y b e a s s o cia t e d wit h a u s e r p ro g ra m , o r it m a y ru n o n ly s o m e ke rn e l fu n ct io n s . Co n t e xt s wit ch e s b e t we e n ke rn e l t h re a d s a re u s u a lly m u ch le s s e xp e n s ive t h a n co n t e xt s wit ch e s b e t we e n o rd in a ry p ro ce s s e s , s in ce t h e fo rm e r u s u a lly o p e ra t e o n a co m m o n a d d re s s s p a ce . Lin u x u s e s ke rn e l t h re a d s in a ve ry lim it e d wa y t o e xe cu t e a fe w ke rn e l fu n ct io n s p e rio d ica lly; s in ce Lin u x ke rn e l t h re a d s ca n n o t e xe cu t e u s e r p ro g ra m s , t h e y d o n o t re p re s e n t t h e b a s ic e xe cu t io n co n t e xt a b s t ra ct io n . ( Th a t 's t h e t o p ic o f t h e n e xt it e m . ) Mu lt it h re a d e d a p p lica t io n s u p p o rt Mo s t m o d e rn o p e ra t in g s ys t e m s h a ve s o m e kin d o f s u p p o rt fo r m u lt it h re a d e d a p p lica t io n s — t h a t is , u s e r p ro g ra m s t h a t a re we ll d e s ig n e d in t e rm s o f m a n y re la t ive ly in d e p e n d e n t e xe cu t io n flo ws t h a t s h a re a la rg e p o rt io n o f t h e a p p lica t io n d a t a s t ru ct u re s . A m u lt it h re a d e d u s e r a p p lica t io n co u ld b e co m p o s e d o f m a n y lig h t w e ig h t p ro ce s s e s ( LWP) , wh ich a re p ro ce s s e s t h a t ca n o p e ra t e o n a co m m o n a d d re s s s p a ce , co m m o n p h ys ica l m e m o ry p a g e s , co m m o n o p e n e d file s , a n d s o o n . Lin u x d e fin e s it s o wn ve rs io n o f lig h t we ig h t p ro ce s s e s , wh ich is d iffe re n t fro m t h e t yp e s u s e d o n o t h e r s ys t e m s s u ch a s S VR4 a n d S o la ris . Wh ile a ll t h e co m m e rcia l Un ix va ria n t s o f LWP a re b a s e d o n ke rn e l t h re a d s , Lin u x re g a rd s lig h t we ig h t p ro ce s s e s a s t h e b a s ic e xe cu t io n co n t e xt a n d h a n d le s t h e m via t h e n o n s t a n d a rd clone( ) s ys t e m ca ll.

No n p re e m p t iv e k e rn e l Lin u x 2 . 4 ca n n o t a rb it ra rily in t e rle a ve e xe cu t io n flo ws wh ile t h e y a re in p rivile g e d m o d e . [ 3 ] S e ve ra l s e ct io n s o f ke rn e l co d e a s s u m e t h e y ca n ru n a n d m o d ify d a t a s t ru ct u re s wit h o u t fe a r o f b e in g in t e rru p t e d a n d h a vin g a n o t h e r t h re a d a lt e r t h o s e d a t a s t ru ct u re s . Us u a lly, fu lly p re e m p t ive ke rn e ls a re a s s o cia t e d wit h s p e cia l re a lt im e o p e ra t in g s ys t e m s . Cu rre n t ly, a m o n g co n ve n t io n a l, g e n e ra l- p u rp o s e Un ix s ys t e m s , o n ly S o la ris 2 . x a n d Ma ch 3 . 0 a re fu lly p re e m p t ive ke rn e ls . S VR4 . 2 / MP in t ro d u ce s s o m e fix e d p re e m p t io n p o in t s a s a m e t h o d t o g e t lim it e d p re e m p t io n ca p a b ilit y. [3]

Th is re s t rict io n h a s b e e n re m o ve d in t h e Lin u x 2 . 5 d e ve lo p m e n t ve rs io n .

Mu lt ip ro ce s s o r s u p p o rt S e ve ra l Un ix ke rn e l va ria n t s t a ke a d va n t a g e o f m u lt ip ro ce s s o r s ys t e m s . Lin u x 2 . 4 s u p p o rt s s ym m e t ric m u lt ip ro ce s s in g ( S MP) : t h e s ys t e m ca n u s e m u lt ip le p ro ce s s o rs a n d e a ch p ro ce s s o r ca n h a n d le a n y t a s k — t h e re is n o d is crim in a t io n a m o n g t h e m . Alt h o u g h a fe w p a rt s o f t h e ke rn e l co d e a re s t ill s e ria lize d b y m e a n s o f a s in g le "b ig ke rn e l lo ck, " it is fa ir t o s a y t h a t Lin u x 2 . 4 m a ke s a n e a r o p t im a l u s e o f S MP. File s y s t e m Lin u x's s t a n d a rd file s ys t e m s co m e in m a n y fla vo rs , Yo u ca n u s e t h e p la in o ld Ext 2 file s ys t e m if yo u d o n 't h a ve s p e cific n e e d s . Yo u m ig h t s wit ch t o Ext 3 if yo u wa n t t o a vo id le n g t h y file s ys t e m ch e cks a ft e r a s ys t e m cra s h . If yo u 'll h a ve t o d e a l wit h

m a n y s m a ll file s , t h e Re is e rFS file s ys t e m is like ly t o b e t h e b e s t ch o ice . Be s id e s Ext 3 a n d Re is e rFS , s e ve ra l o t h e r jo u rn a lin g file s ys t e m s ca n b e u s e d in Lin u x, e ve n if t h e y a re n o t in clu d e d in t h e va n illa Lin u x t re e ; t h e y in clu d e IBM AIX's Jo u rn a lin g File S ys t e m ( JFS ) a n d S ilico n Gra p h ics Irix's XFS file s ys t e m . Th a n ks t o a p o we rfu l o b je ct o rie n t e d Virt u a l File S ys t e m t e ch n o lo g y ( in s p ire d b y S o la ris a n d S VR4 ) , p o rt in g a fo re ig n file s ys t e m t o Lin u x is a re la t ive ly e a s y t a s k. S TREAMS Lin u x h a s n o a n a lo g t o t h e S TREAMS I/ O s u b s ys t e m in t ro d u ce d in S VR4 , a lt h o u g h it is in clu d e d n o w in m o s t Un ix ke rn e ls a n d h a s b e co m e t h e p re fe rre d in t e rfa ce fo r writ in g d e vice d rive rs , t e rm in a l d rive rs , a n d n e t wo rk p ro t o co ls . Th is s o m e wh a t m o d e s t a s s e s s m e n t d o e s n o t d e p ict , h o we ve r, t h e wh o le t ru t h . S e ve ra l fe a t u re s m a ke Lin u x a wo n d e rfu lly u n iq u e o p e ra t in g s ys t e m . Co m m e rcia l Un ix ke rn e ls o ft e n in t ro d u ce n e w fe a t u re s t o g a in a la rg e r s lice o f t h e m a rke t , b u t t h e s e fe a t u re s a re n o t n e ce s s a rily u s e fu l, s t a b le , o r p ro d u ct ive . As a m a t t e r o f fa ct , m o d e rn Un ix ke rn e ls t e n d t o b e q u it e b lo a t e d . By co n t ra s t , Lin u x d o e s n 't s u ffe r fro m t h e re s t rict io n s a n d t h e co n d it io n in g im p o s e d b y t h e m a rke t , h e n ce it ca n fre e ly e vo lve a cco rd in g t o t h e id e a s o f it s d e s ig n e rs ( m a in ly Lin u s To rva ld s ) . S p e cifica lly, Lin u x o ffe rs t h e fo llo win g a d va n t a g e s o ve r it s co m m e rcia l co m p e t it o rs : ●



Lin u x is fr e e . Yo u ca n in s t a ll a co m p le t e Un ix s ys t e m a t n o e xp e n s e o t h e r t h a n t h e h a rd wa re ( o f co u rs e ) . Lin u x is fu lly c u s t o m iz a b le in a ll it s c o m p o n e n t s . Th a n ks t o t h e Ge n e ra l Pu b lic Lice n s e ( GPL) , yo u a re a llo we d t o fre e ly re a d a n d m o d ify t h e s o u rce co d e o f t h e ke rn e l a n d o f a ll s ys t e m p ro g ra m s . [ 4 ] [4]

S e ve ra l co m m e rcia l co m p a n ie s h a ve s t a rt e d t o s u p p o rt t h e ir p ro d u ct s u n d e r Lin u x. Ho we ve r, m o s t o f t h e m a re n 't d is t rib u t e d u n d e r a n o p e n s o u rce lice n s e , s o yo u m ig h t n o t b e a llo we d t o re a d o r m o d ify t h e ir s o u rce co d e .











Lin u x r u n s o n lo w - e n d , c h e a p h a r d w a r e p la t fo r m s . Yo u ca n e ve n b u ild a n e t wo rk s e rve r u s in g a n o ld In t e l 8 0 3 8 6 s ys t e m wit h 4 MB o f RAM. Lin u x is p o w e r fu l. Lin u x s ys t e m s a re ve ry fa s t , s in ce t h e y fu lly e xp lo it t h e fe a t u re s o f t h e h a rd wa re co m p o n e n t s . Th e m a in Lin u x g o a l is e fficie n cy, a n d in d e e d m a n y d e s ig n ch o ice s o f co m m e rcia l va ria n t s , like t h e S TREAMS I/ O s u b s ys t e m , h a ve b e e n re je ct e d b y Lin u s b e ca u s e o f t h e ir im p lie d p e rfo rm a n ce p e n a lt y. Lin u x h a s a h ig h s t a n d a r d fo r s o u r c e c o d e q u a lit y . Lin u x s ys t e m s a re u s u a lly ve ry s t a b le ; t h e y h a ve a ve ry lo w fa ilu re ra t e a n d s ys t e m m a in t e n a n ce t im e . Th e Lin u x k e r n e l c a n b e v e r y s m a ll a n d c o m p a c t . It is p o s s ib le t o fit b o t h a ke rn e l im a g e a n d fu ll ro o t file s ys t e m , in clu d in g a ll fu n d a m e n t a l s ys t e m p ro g ra m s , o n ju s t o n e 1 . 4 MB flo p p y d is k. As fa r a s we kn o w, n o n e o f t h e co m m e rcia l Un ix va ria n t s is a b le t o b o o t fro m a s in g le flo p p y d is k. Lin u x is h ig h ly c o m p a t ib le w it h m a n y c o m m o n o p e r a t in g s y s t e m s . It le t s yo u d ire ct ly m o u n t file s ys t e m s fo r a ll ve rs io n s o f MS - DOS a n d MS Win d o ws , S VR4 , OS / 2 , Ma c OS , S o la ris , S u n OS , Ne XTS TEP, m a n y BS D va ria n t s , a n d s o o n . Lin u x is a ls o a b le t o o p e ra t e wit h m a n y n e t wo rk la ye rs , s u ch a s Et h e rn e t ( a s we ll a s Fa s t Et h e rn e t a n d Gig a b it Et h e rn e t ) , Fib e r Dis t rib u t e d Da t a In t e rfa ce ( FDDI) , Hig h Pe rfo rm a n ce Pa ra lle l In t e rfa ce ( HIPPI) , IBM's To ke n Rin g , AT&T Wa ve LAN, a n d DEC Ro a m Ab o u t DS . By u s in g s u it a b le lib ra rie s , Lin u x s ys t e m s a re e ve n a b le t o d ire ct ly ru n p ro g ra m s writ t e n fo r o t h e r o p e ra t in g s ys t e m s . Fo r e xa m p le , Lin u x is a b le t o e xe cu t e a p p lica t io n s writ t e n fo r MS - DOS , MS Win d o ws , S VR3 a n d R4 , 4 . 4 BS D, S CO Un ix, XENIX, a n d o t h e rs o n t h e 8 0 x 8 6 p la t fo rm .



Lin u x is w e ll s u p p o r t e d . Be lie ve it o r n o t , it m a y b e a lo t e a s ie r t o g e t p a t ch e s a n d u p d a t e s fo r Lin u x t h a n fo r a n y o t h e r p ro p rie t a ry o p e ra t in g s ys t e m . Th e a n s we r t o a p ro b le m o ft e n co m e s b a ck wit h in a fe w h o u rs a ft e r s e n d in g a m e s s a g e t o s o m e n e ws g ro u p o r m a ilin g lis t . Mo re o ve r, d rive rs fo r Lin u x a re u s u a lly a va ila b le a fe w we e ks a ft e r n e w h a rd wa re p ro d u ct s h a ve b e e n in t ro d u ce d o n t h e m a rke t . By co n t ra s t , h a rd wa re m a n u fa ct u re rs re le a s e d e vice d rive rs fo r o n ly a fe w co m m e rcia l o p e ra t in g s ys t e m s — u s u a lly Micro s o ft 's . Th e re fo re , a ll co m m e rcia l Un ix va ria n t s ru n o n a re s t rict e d s u b s e t o f h a rd wa re co m p o n e n t s .

Wit h a n e s t im a t e d in s t a lle d b a s e o f s e ve ra l t e n s o f m illio n s , p e o p le wh o a re u s e d t o ce rt a in fe a t u re s t h a t a re s t a n d a rd u n d e r o t h e r o p e ra t in g s ys t e m s a re s t a rt in g t o e xp e ct t h e s a m e fro m Lin u x. In t h a t re g a rd , t h e d e m a n d o n Lin u x d e ve lo p e rs is a ls o in cre a s in g . Lu ckily, t h o u g h , Lin u x h a s e vo lve d u n d e r t h e clo s e d ire ct io n o f Lin u s t o a cco m m o d a t e t h e n e e d s o f the m a sse s. I l@ve Ru Bo a rd

I l@ve Ru Bo a rd

1.2 Hardware Dependency Lin u x t rie s t o m a in t a in a n e a t d is t in ct io n b e t we e n h a rd wa re - d e p e n d e n t a n d h a rd wa re in d e p e n d e n t s o u rce co d e . To t h a t e n d , b o t h t h e a rch a n d t h e in clu d e d ire ct o rie s in clu d e n in e s u b d ire ct o rie s t h a t co rre s p o n d t o t h e n in e h a rd wa re p la t fo rm s s u p p o rt e d . Th e s t a n d a rd n a m e s o f t h e p la t fo rm s a re : a lp h a He wle t t - Pa cka rd 's Alp h a wo rks t a t io n s a rm ARM p ro ce s s o r- b a s e d co m p u t e rs a n d e m b e d d e d d e vice s cris "Co d e Re d u ce d In s t ru ct io n S e t " CPUs u s e d b y Axis in it s t h in - s e rve rs , s u ch a s we b ca m e ra s o r d e ve lo p m e n t b o a rd s i3 8 6 IBM- co m p a t ib le p e rs o n a l co m p u t e rs b a s e d o n 8 0 x 8 6 m icro p ro ce s s o rs ia 6 4 Wo rks t a t io n s b a s e d o n In t e l 6 4 - b it It a n iu m m icro p ro ce s s o r m 68k Pe rs o n a l co m p u t e rs b a s e d o n Mo t o ro la MC6 8 0 x 0 m icro p ro ce s s o rs m ip s Wo rks t a t io n s b a s e d o n MIPS m icro p ro ce s s o rs m ip s 6 4 Wo rks t a t io n s b a s e d o n 6 4 - b it MIPS m icro p ro ce s s o rs p a ris c Wo rks t a t io n s b a s e d o n He wle t t Pa cka rd HP 9 0 0 0 PA- RIS C m icro p ro ce s s o rs ppc Wo rks t a t io n s b a s e d o n Mo t o ro la - IBM Po we rPC m icro p ro ce s s o rs

s390 3 2 - b it IBM ES A/ 3 9 0 a n d zS e rie s m a in fra m e s s390 x IBM 6 4 - b it zS e rie s s e rve rs sh S u p e rH e m b e d d e d co m p u t e rs d e ve lo p e d jo in t ly b y Hit a ch i a n d S TMicro e le ct ro n ics s p a rc Wo rks t a t io n s b a s e d o n S u n Micro s ys t e m s S PARC m icro p ro ce s s o rs s p a rc6 4 Wo rks t a t io n s b a s e d o n S u n Micro s ys t e m s 6 4 - b it Ult ra S PARC m icro p ro ce s s o rs

I l@ve Ru Bo a rd

I l@ve Ru Bo a rd

1.3 Linux Versions Lin u x d is t in g u is h e s s t a b le ke rn e ls fro m d e ve lo p m e n t ke rn e ls t h ro u g h a s im p le n u m b e rin g s ch e m e . Ea ch ve rs io n is ch a ra ct e rize d b y t h re e n u m b e rs , s e p a ra t e d b y p e rio d s . Th e firs t t wo n u m b e rs a re u s e d t o id e n t ify t h e ve rs io n ; t h e t h ird n u m b e r id e n t ifie s t h e re le a s e . As s h o wn in Fig u re 1 - 1 , if t h e s e co n d n u m b e r is e ve n , it d e n o t e s a s t a b le ke rn e l; o t h e rwis e , it d e n o t e s a d e ve lo p m e n t ke rn e l. At t h e t im e o f t h is writ in g , t h e cu rre n t s t a b le ve rs io n o f t h e Lin u x ke rn e l is 2 . 4 . 1 8 , a n d t h e cu rre n t d e ve lo p m e n t ve rs io n is 2 . 5 . 2 2 . Th e 2 . 4 ke rn e l — wh ich is t h e b a s is fo r t h is b o o k — wa s firs t re le a s e d in Ja n u a ry 2 0 0 1 a n d d iffe rs co n s id e ra b ly fro m t h e 2 . 2 ke rn e l, p a rt icu la rly wit h re s p e ct t o m e m o ry m a n a g e m e n t . Wo rk o n t h e 2 . 5 d e ve lo p m e n t ve rs io n s t a rt e d in No ve m b e r 2 0 0 1 . Fig u re 1 - 1 . N u m b e rin g Lin u x v e rs io n s

Ne w re le a s e s o f a s t a b le ve rs io n co m e o u t m o s t ly t o fix b u g s re p o rt e d b y u s e rs . Th e m a in a lg o rit h m s a n d d a t a s t ru ct u re s u s e d t o im p le m e n t t h e ke rn e l a re le ft u n ch a n g e d . [ 5 ] [5]

Th e p ra ct ice d o e s n o t a lwa ys fo llo w t h e t h e o ry. Fo r in s t a n ce , t h e virt u a l m e m o ry s ys t e m h a s b e e n s ig n ifica n t ly ch a n g e d , s t a rt in g wit h t h e 2 . 4 . 1 0 re le a s e .

De ve lo p m e n t ve rs io n s , o n t h e o t h e r h a n d , m a y d iffe r q u it e s ig n ifica n t ly fro m o n e a n o t h e r; ke rn e l d e ve lo p e rs a re fre e t o e xp e rim e n t wit h d iffe re n t s o lu t io n s t h a t o cca s io n a lly le a d t o d ra s t ic ke rn e l ch a n g e s . Us e rs wh o re ly o n d e ve lo p m e n t ve rs io n s fo r ru n n in g a p p l