dylan@ibmpcug.co.uk (Matthew Farwell) (05/14/91)
This is a generic Unix question (ie the version of unix isn't really important because I'm trying to devise a fairly portable way to do this) I have an application which requires a set of structures (indexed by tty name) to be maintained in a file. The structures are simple and 68 bytes long. This is assuming that two processes will not be running this application from the same tty. I've settled on a utmp like structure to the file ie a process searches through the file looking for its tty name. If it finds it, it uses that. If it doesn't find it, then it closes the file, reopens it in append mode and writes the structure onto the end of the file. My question is this. Is the write guaranteed to be atomic? The main concern is that the write will half-finish and the second process will seek to the wrong place, something like this Process 1 Process 2 starts writes 25 bytes switches to process 2 starts seeks to the end of the file (ie 25) writes 68 bytes switches to process 1 restarts writes the rest of the struct, corrupting the file. Are my fears well founded or complete dog-poo? Marc Rochkind basically says that this way of write should be atomic, but not to trust the semantics of write in append mode because its not actually documented anywhere (pp. 28). I realise this is good advice but I want to avoid actually implmenting some sort of locking if I can. How many unices is write broken in this way? Does anyone have a reliable way to test for this behaviour? To make things more complex, I'm actually using stdio in this case ie fwrite + fflush instead of write. Does POSIX fix this? Comments welcome. Also comments on any different way to implement this type of thing would also be welcome. Thanks. Dylan. -- Matthew J Farwell: dylan@ibmpcug.co.uk || ...!uunet!ukc!ibmpcug!dylan You can't kill me, I'm a genius!!!!