Sei sulla pagina 1di 31

http://msdn.microsoft.com/en-us/library/ms859638.

aspx

UNI -!II

Unix"Internals-3 #ata"$ana%ement
Chapter Outline
Data Management Managing Memory Simple Memory Allocation Allocating Lots of Memory The Null Pointer Creating Lock Files Locking Regions se of rea! an! "rite "ith Locking Competing Locks Other Lock Comman!s Dea!locks Data#ase The !m# Data#ase The !#m Routines !#m Access Functions A!!itional !#m Functions Summary

#ata"$ana%ement

Managing Memory
On all computer systems memory is a scarce resource$ N%& applications are ne'er permitte! to access physical memory !irectly$ N%& pro'i!es memory protection "hich guarantees that incorrect (or malicious) programs !on*t o'er"rite the memory of other processes or the operating system$ %n general+ the memory allocate! to one process can #e neither rea! nor "ritten to #y any other process$ Almost all 'ersions of memory N%& use har!"are facilities to enforce this pri'ate use of

&imple"$emory"'llocation
,e allocate memory using the malloc call in the stan!ar! C li#rary$

ry"It"(ut"-"&imple"$emory"'llocation
,e can allocate a great !eal of memory on most N%& systems$ Let*s try$ Type in the follo"ing program+ memory).c

,hen "e run this program+ it outputs.memory/ 0ello ,orl!

*o+"It",or-s
The program asks malloc for a pointer ot one mega#yte of memory$ %t checks that the memory "as assigne! an! uses the memory to hol! the "or!s 10ello ,orl!1+ an! then prints out the "or!s$

'llocatin%".ots"of"$emory
No" "e "ill ask the machine for all of its memory2

ry"It"(ut"-"'s-in%"for"all"/hysical"$emory
,ith memory0.c+ "e*re going to ask for all the machine*s memory-

The output+ some"hat a##re'iate!+ is-

*o+"It",or-s
The program loops+ asking for more an! more memory$ %f "e time ho" long it took to run using the comman!+ . time memory3 on this system+ "e see an elapse! time of 4ust 5$67 secon!s$

ry"It"(ut"-"'1ailable"$emory
Let*s see 4ust ho" much memory "e can allocate on this machine "ith memory3.c$ %t is e8tremely system9unfrien!ly an! coul! affect a multiuser machine :uite seriously$

This time+ the output+ again a##re'iate!+ is+

an! then the program en!s$

*o+"It",or-s
The N%& kernel allocates memory upon re:uest$ ,hen it runs out of physical memory+ it uses s+ap"space$ ,hen the ma8imum stack si;e is e8cee!e!+ the ni8 kernel finally refuses the re:uest for further memory$

'busin%"$emory
Suppose "e try an! !o *#a!* things "ith memory$

ry"It"(ut"-"'buse"2our"$emory
Let*s allocate some memory an! then attempt to "rite past the en!+ in memory3.c$

The output is simply.memory7 Segmentation fault

*o+"It",or-s
The N%& memory management system has protecte! the rest of the system from this a#use of memory$

he"Null"/ointer
N%& systems are 'ery protecti'e a#out "riting or rea!ing from a null pointer$

ry"It"(ut"-"'ccessin%"a"Null"/ointer
Let*s fin! out "hat happens "hen "e access a null pointer in memory5.c$

The output is.memory6 A rea! from null (null) Segmentation fault

*o+"It",or-s
The first printf attempts to print out a string o#taine! from a null pointer+ then the sprintf attempts to "rite to a null pointer$ The program terminates$

4reein%"$emory
,e ha'e #een using memory an! hoping that N%& "oul! reco'er it$ %t can "hen the progrm terminates$ Programs that use memory on a !ynamic #asis shoul! al"ays release the unuse! memory #y to the malloc memory manager using the free call$

A call to free shoul! only #e ma!e "ith a pointer to memory allocate! #y a call to malloc$

ry"It"(ut"-"4reein%"$emory
This program*s calle! memory6.c-

*o+"It",or-s
This program simply sho"s ho" to call free "ith a pointer to some pre'iously allocate! memory$

(ther"$emory"'llocation"4unctions
There are to" otherr memory allocation functions that are not use! as often as malloc" an! free$ These are calloc an! realloc$ The prototypes are-

Another pro#lem is that realloc returns a null pointer if it "as una#le to resi;e the memory$ This means that+ in some applications+ shoul! a'oi! "riting co!e like this-

4ile".oc-in%
File locking is use! on a multiuser + multitasking system+ to esta#lish control of a file so it can #e up!ate! in a safe fashion$

5reatin%".oc-"4iles
Linu8 creates a lock file in the /usr/spool/uucp !irectory$ This !irectory may also #e use! to in!icate the a'aila#ility of serial ports.ls <usr<spool<uucp LC=$$ttyS/ Lock files act only as in!icators$ They are ad1isory+ not man!atory$ To create a file to use as a lock in!icator+ "e use the open system call !efinie! in fcntl.h+ "ith the O>CR?AT? an! O>?&CL flags set$

ry"It"(ut"-"5reatin%"a".oc-"4ile
Let*s see this in action "ith loc-).c

The first time "e run the program+ the output is+

.lock/ Open succee!e! #ut the ne8t time "e try+ "e get.lock/ Open faile! "ith error /@

*o+"It",or-s
The program calls open to create a file calle! /tmp/.56.test+ using the (7589' an! (79:5."fla%s$ The first time it "orks+ the secon! time it fails$ ?rror /@ refers to 99:I& + an error that is use! to in!icate that a file alrea!y e8ists$ ?rror num#ers are !efine! inthe hea!er file errno.h or files inclu!e! #y it$ %n this case+ the !efinition rea!s-

ry"It"(ut"-"5ooperati1e".oc-"4iles
/$ 0ere*s the source of our test program+ loc-0.c-

3$ The critical section starts here$$$

$$an! en!s here-

To run it+ "e shoul! first ensure that the lock file !oesn*t e8ist+ "ith . rm 9f <tmp<LC=$test3 thenrun t"o copies of the program+ #y using the comman!. lock3 A lock3 This is the output that "e get-

*o+"It",or-s
The program loops ten times trying to access the critical resource #y creating a uni:ue lock file$

.oc-in%"8e%ions
,e can create locking regons of the file+ so that a particular section of hte file is locke!+ #ut other programs may access other parts of the file$ %t can #e !one using calls to either fcntl an! loc-f$ The !efiniton of fcntl is-

fcntl operates on open file !escriptors$ The three parameters "e are intereste! in for file locking are-

,hen "e use these+ the thir! argument must #e a pointer to a struct " floc-+ so the prototype is effecti'ely-

The floc- (file lock) structure is implementation9!epen!ent+ #ut "ill contain at least the follo"ing mem#ers-

The l7type mem#er takes one of ser'er 'alues+ also !efine! in fcntl.h Theres are-

he"47;9 .6"5ommand
The 47;9 .6 gets locking information a#out the file that fildes (the first parameter) has open$ fcntl use! "ith the 47;9 .6 comman! returns any information that "oul! pre'ent the lock occurring$ The 'alues use! in the floc- structure are-

he"47&9 .6"5ommand
This comman! attempts to lock or unlock part of the file reference! #y fildes$ The 'alues use! in the floc- structure (an! !ifferent from those use! #y 47;9 .6) are-

he"47&9 .6,"5ommand
This is the same as the 47&9 .6 comman! a#o'e+ e8cept that if it can*t o#tain the lock+ the call "ill "ait until it can$

Use"of"read"and"+rite"+ith".oc-in%
,hen using locking on regions of a file+ it*s important to use the lo"er le'el read an! +rite calls to access the !ata in the file$ They !on*t use #uffering of !ata$

ry"It"(ut"-".oc-in%"a"4ile"+ith"fcntl
To try out locking+ "e nee! t"o program+ one to !o the locking an! one to test$ The first program !oes the locking$ /$ Start "ith the includes an! 'aria#le !eclarations-

3$ Open a file !escriptor-

B$ Put some !ata in the file-

7$ Set up region / "ith a share! lock+ from #ytes /5 to B5-

6$ Set up region 3 "ith an e8clusi'e lock+ from #ytes 75 to 65-

C$ No" lock the file$$$

@$ $$$an! "ait for a "hile$

*o+"It",or-s
The program creates a file+ opens it for rea!ing an! "riting+ an! fills it "ith !ata$ %t sets up a share! rea! lock on #ytes /5 to B5+ an! an e8clusi'e "rite lock on #ytes 75 to 65$ Then it locks the t"o regions$ ,hen the program starts to "ait+ this is the situation "ith locks-

ry"It"(ut"-" estin%".oc-s"on"a"4ile
The secon! program for testing the locks is loc-3.c$ /$ As usual+ #egin "ith the includes an! !eclarations-

3$ Open a file !escriptor-

B$ Set up the region "e "ish to test-

7$ No" test the lock on the file-

6$ No" repeat the test "ith a share! (read) lock$ Set up the region "e "ish to test again-

C$ Test the lock on the file again-

To test out locking+ "e first nee! to run the loc-3 program+ then the loc-3 program$ ,e !o this #y e8ecuting the loc-3 program in the #ackgroun!+ "ith the comman!.lockB A . process /6B7 locking file The comman! prompt returns+ since loc-3 is running in the #ackgroun! an! "e then imme!iately run the loc-3 program "ith the comman!lock7 The output "e get+ "ith some omissions for #re'ity+ is-

*o+"It",or-s
For each group of fi'e #ytes in the file+ loc-3 sets up region structure to test for locks on the file$

5ompetin%".oc-s
"hat happens "hen t"o programs compete for locks on the same section of the fileD ,e use loc-3 to lock the file an! a ne" program+ loc-5.c to try to lock it again$

ry"It"(ut"-"5ompetin%".oc-s
/$ After the <include an! !eclarations+ open a file !escriptor-

E3$ The remain!er of the program is spent specifying !ifferent regions of the file an! trying !ifferent locking operations on them-<PF

%f "e first run our loc-3 program in the #ackgroun!+ then imme!iately run the ne" program+ the output "e get is-

*o+"It",or-s
First+ the program attempts to lock a region from #ytes /5 to /6 "ith a share! lock$ Then it unlocks its op"n share! lock on the region an! attempts to unlock the first 65 #ytes of the file$ The program attempts a share! lock on the region from #ytes 75 to 65$ Finally the program again attempts to o#tain an e8clusi'e lock on the region from #ytes /C to 3/$

(ther".oc-"5ommands
Another metho! of locking files uses the loc-f function$ This also operates using file !escriptors$ %t has the prototype-

%t can take the follo"ing function 'alues-

The si=e7to7loc- parameter is the num#er of #ytes to operate on+ from the current offset in the file$

#eadloc-s
A deadloc- occurs "hen t"o programs can*t procee! #ecause each has a lock on something that the other programs nee!s$

Databases
Data#ases can store recor!s that 'ary in si;e+ an! store an! retrie'e !ata using an in!e8$

he"dbm"#atabase
&<Open compliant 'ersions of N%& come "ith a !ata#ase calle! dbm$

he"dbm"8outines
Like curses+ the dbm facility consists of a hea!er file an! a li#rary that must #e linke! "hen the program is compile!$

dbm"5oncepts
The dbm !ata#ase element is a #lock of !ata to store+ couple! "ith a companion #lock of !ata that acts as a key for retrie'ing that !ata$ ?ach #lock must ha'e a uni:ue key$

To manipulate these #locks as !ata+ the ndbm.h inclu!e file !efines a ne" type+ calle! datum$ The e8act contents of this type is implementation9!epen!ent+ #ut it must ha'e at least the follo"ing mem#ers-

dbm"'ccess"4unctions
The prototypes for the main dbm functions are-

dbm7open
This function is use! to open e8isting !ata#ases an! can to #e use! to create ne" !ata#ases$

dbm7store
,e use this function for entering !ata into the !ata#ase$

dbm7fetch
This routine is use! for retrie'ing !ata from the !ata#ase$

dbm7close
This routine clases a !ata#ase opene! "ith dbm7open$

ry"It"(ut"-"'"&imple"dbm"#atabase
/$ First of all+ its incluu!e files+ <defines+ the main function an! the !eclaration of the test7data structure-

3$ ,ithin main+ "e set up the items7to7store an! items7recei1ed structures+ the key string an! datum types-

B$ 0a'ing !eclare! a pointer to a dbm type structure+ "e no" open our test !ata#ase for rea!ing an! "riting+ creating it if necessary-

7$ No" "e a!! some !ata to the items7to7store structure-

6$ For each entry+ "e nee! to #uil! a key for future referencing$

C$ Then "e see if "e can retrie'e this ne" !ata an!+ finally+ "e must close the !ata#ase-

,hen "e compile an! run the program+ the output is simply-

*o+"It",or-s
First "e open the !ata#ase an! then fill in the test !ata an! keys$ Then "e setup the t"o !atum structures for the !ata an! keys$ Finally "e print out the retrie'e! !ata$

'dditional"dbm"4unctions
No" that "e*'e seen the principle dbm$ functions+ "e can co'er the fe" remaining functions that are use! "ith dbm These are-

dbm7delete
The !#m>!elete function is use! to !elete entries from the !ata#ase$

dbm7error
The dbm7error function simply tests "hether an error has occurre! in the !ata#ase+ returning 5 if there is none$

dbm7clearerr
The dbm7clearerr clears any error con!ition flag that may #e set in the !ata#ase$

dbm7first-ey"and"dbm7next-ey
These routines are normally use! as a pair to scan through all the keys of all the items in a !ata#ase$ The loop structure re:uire! is-

ry"It"(ut"-"8etrie1in%"and"#eletin%
No" amen! dbm).c "ith some of these ne" functions$ %t is calle! dbm0.c$ /$ Make a copy of dbm).c an! open it for e!iting$ ?!it the <define" 9& 7#>74I.9" line-

3$ Then+ the only change that "e nee! to make is in the retrie'al section-

The output is-

*o+"It",or-s
The program simply stores some !ata in the !ata#ase+ #uil!s a key+ an! !eletes the item from a !ata#ase$

Potrebbero piacerti anche