zapis do jedneho suboru z viacerych procesov

Pavel Kankovsky peak na argo.troja.mff.cuni.cz
Středa Srpen 2 12:08:44 CEST 2000


On Mon, 31 Jul 2000, Petr Novotny wrote:

> Zapisy kratsi nez nejaka magicka konstanta (512 bytu? 1024 bytu? 
> tak neco) se provedou atomicky - na to je nejaka garance.

Ta magicka konstanta se jmenuje PIPE_BUF a atomicita je zarucena pouze
pouze pro zapisy do rour (pojmenovanych i nepojmenovanych):

<quote source="Single Unix Specification v2">

     Write requests to a pipe or FIFO will be handled the same as a
     regular file with the following exceptions: 

            Write requests of {PIPE_BUF} bytes or less will not be
            interleaved with data from other processes doing writes on the
            same pipe. Writes of greater than {PIPE_BUF} bytes may have
            data interleaved, on arbitrary boundaries, with writes
            by other processes, whether or not the O_NONBLOCK flag of the
            file status flags is set. 

</quote>

U zapisu do souboru to v praxi take funguje tak, ze se male zapisy
provadeji (vetsinou) atomicky, ale je to spis vedlejsi vlastnost
implementace, cili za atomicke lze principialne povazovat pouze zapisy
jednoho bajtu.

Trochu zajimavejsi situace je u souboru otevrenych s O_APPEND, jehoz efekt
je popisovan takto:

<quote source="Single Unix Specification v2">

     If the O_APPEND flag of the file status flags is set, the file offset
     will be set to the end of the file prior to each write and no
     intervening file modification operation will occur between changing
     the file offset and the write operation. 

</quote>

protoze z toho uplne jasne nevyplyva, co by se melo stat, kdyz by se
write() neprovedl najednou, ale mezitim doslo k provedeni jineho zapisu
(napr. z toho duvodu, ze se prvni proces na chvili zastavil kvuli
vypadku stranky), ktery by delku souboru zmenil. To skutecne neni
hypoteticky problem, protoze ve starsich linuxovych jadrech mohlo dojit
ke kolizi dvou ci vice zapisu, ktere se pak vzajemne prepisovaly, a to i
v pripade, ze byl nastaven O_APPEND.

Nicmene dobra zprava je, ze to konecne nekdo opravil a nova linuxova jadra
(2.4) se ve funkci generic_file_write() synchronizuji pres semafor, a
tudiz jsou pod Linuxem (pod fs, ktere write() implementuji uvedenou
funkci) operace write() nad jednim souborem striktne serializovany.

> Protoze "yes" si /tmp/log otevrel pro zapis nejspis exkluzivne. 

"Exkluzivni" otevreni lze provest akorat pres mandatory locking (nebo
tim, ze se to spusti pod Woknama :> ). V kazdem pripade je takove chovani 
kandidat na strace.

--Pavel Kankovsky aka Peak  [ Boycott Microsoft--http://www.vcnet.com/bms ]
"Resistance is futile. Open your source code and prepare for assimilation."



Další informace o konferenci Linux