Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to get an exclusive lock on a OSS file opened by fopen() call

107 views
Skip to first unread message

p.b. pathi

unread,
Jun 19, 2010, 3:12:31 AM6/19/10
to
Hi All,

I opened one OSS file programatically with fopen(), now I want to
acquire a exclusive lock on this file.
I though of using flock() with O_EXLOCK flag, however, I have not
found flock() call in "Open System Services
Library Calls Reference Manual". Any idea, which OSS system call
should I use to achieve the lock?

I thought of using open() call where we can specify type of lock while
opening the file, but I am not sure whether I can use open descriptor
given by open() call in subsequent file operations calls like fgets().
Beacuse, in the manual its said the file descripto given by open()
will be used in write() and read() calls.

I dont think I can use LOCKFILE() gpc as it expects file num in
short type, where as I have the fd of FILE *.
Could any one advice me how to get an exclusive lock on OSS file opend
by fopen() call.

Thanks,
Bikku

Keith Dick

unread,
Jun 19, 2010, 10:23:43 AM6/19/10
to

I think you could open the file with fopen(), use fileno() to get a file number, and use fcntl() to obtain and release locks.

I imagine that you understand that those are advisory locks, not mandatory locks. If you need mandatory locks, I think you might have to use Guardian I/O for all operations on the file.

You might try using gfileno() to get the Guardian file number from your stream, cast it to short, and call LOCKFILE using that, but that may not do what you want, given that the stream I/O functions buffer the file I/O. I don't know why gfileno() returns int rather than short, but the example using it in the OSS Programmer's Guide shows casting the result of gfileno() to short to use it in a Guardian call, so that seems to be what you are expected to do. I think you should not mix stream I/O with LOCKFILE on the same file, but if you want to experiment with it, that is how you can do so.

0 new messages