locking the end of the file for appending

hi, I'm using fcntl for file locking but if say I want to lock just the end of the file for appending, what values do I put into the flock structure?
I know that for l_whence I can set it to SEEK_END but what about l_start and l_len?
Unless I'm misunderstanding you, I don't think that's how file locking works. You lock a file, not parts of it.
Unless I'm misunderstanding you, I don't think that's how file locking works. You lock a file, not parts of it.

fcntl is used for file region locking
Last edited on
If you want to guarantee appending from different threads/processes you need to open it as ios_base::app. That guarantee's correct appending in all cases as the append is done as an atomic operation in the file system.

You don't need to lock regions just to append.

See: http://stdcxx.apache.org/doc/stdlibug/30-3.html

There's also a discussion of it in Steven's Unix Programming book.
Last edited on
Q1) @kbw, the link you gave didn't say that appending using ios::app is atomic...
Q2) Nonetheless, how do I go about programmatically locking the end of file?
Q3) what about reading from a file? I do need to lock it with fcntl or lockf when I'm reading right?
Last edited on
the link you gave didn't say that appending using ios::app is atomic
The Stevens books describes the atomic behaviour.

what about reading from a file? I do need to lock it with fcntl or lockf when I'm reading right?
You only need to lock it if someone else is going to write to that region. Otherwise just need to make sure it's shared, no locking involved.

Nonetheless, how do I go about programmatically locking the end of file?
Read the Stevens book http://www.kohala.com/start/apue.html Why do you want to lock a file anyway?
say I have these values,
l_whence = SEEK_END;
l_start = 0;
l_len = 1024;

Will the lock start at the end and go backwards or forwards in the file?
Last edited on
Topic archived. No new replies allowed.