open and lock the input file open lock the output file read the input file write the output file rename the input file rename the output file close both files (which unlocks them) delete old renamed input file