# flock in windows?

This topic is 1661 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Im cross compiling with mingw and currently using flock for some serverside sessions.

I get error: ‘LOCK_EX’ was not declared in this scope but its not saying anything about flock itself?

Does flock work in windows or do I have to change it to use the LockFileEx? If it does work in windows what would I include to get LOCK_EX defined?

##### Share on other sites

It's a missing symbol error like any other.

In which file do you think "LOCK_EX" is defined? Are you really including that file? Is it really defined there? Does some #if or #ifdef remove the definition? Are you doing something unusual with namespaces and/or macros? The questions are the usual generic ones, and the techniques to answer them, like inspecting preprocessor output, are platform independent.

##### Share on other sites

For linux I believe its in sys/fcntl.h or sys/file.h and those are included in the mingw build as well nothing any different. Im coming to the conclusion that flock simply doesnt work in windows because if I use say 2 for LOCK_EX which I found it to be defined as it doesnt compile because it can find the flock function. Can someone confirm or deny that flock works or doesnt on windows?

##### Share on other sites

flock is not C, or Win32 API, and does not even appear to be POSIX. So the question would be if your compiler/gcc port includes that function in its libraries.

##### Share on other sites

I see I am finding contradictory posts about flock being posix though. So now I guess the question is if mingw does flock?

##### Share on other sites

A quick google search would seem to indicate that Mingw/Windows does not support flock().

##### Share on other sites

Yes it does but when I search "mingw flock" the two posts I see that have a striaght to the point answer one is from 2001 and another from 2007 infact the newest post on the first page is from 2012 so im hoping to get a more informed answer or an alternative rather than just look it up on google.

##### Share on other sites

Also, if I recall Linux and Window's do not have the same functionality anyway. As far as I know on Window's it is not possible to ignore a lock (via LockFile or the CreateFile sharing modes), but on Linux a file lock is only an advice thing (and from memory I am not even sure if fopen follows it by default). So you may need to think carefully about what you are trying to do.

Regarding that wiki page. It seems to just say most unix like systems have it, not that it is part of the POSIX standard, and searching for POSIX related things, the closest I just found was fcntl http://pubs.opengroup.org/onlinepubs/9699919799/

Edited by SyncViews

##### Share on other sites

Yea in linux you can still open a file that is locked but you cant obtain a lock on it so you have to build your program to handle it other programs that dont check will ignore the lock.

Yea I see that now I was taken there from the posix wiki and didnt think about it.

Im just trying to get something locking it seems odd this would be so hard to find information about someone has to be doing it.

Hmm ill look more at the flock file and see if thats implemented

##### Share on other sites

What exactly is it you are trying to do anyway? At most on Window's I used the CreateFile share modes, and on Linux currently I just resort to trusting other processes not to mess with my files. Things like LockFileEx seem to be for locking segments of files, which only seems useful if you are writing a DB like SQLite or something and want multiple independent processes to safely read and write.

##### Share on other sites

Im trying to setup a server with sessions where theres multiple processes where they can be reading and writing at the same time. The current setup uses file descriptors ie ints for the files works great in linux! My thoughts are either to get some version of flock working or rewrite the file handling to use the windows api for files ie createfile lockfile and such.

If I use lockfileex to lock a segmant I cant get another lock on that segmant so it would effectively block other processes of the same type of course. So I guess maybe a third option is to convert a file descriptor to a winapi file handle? Ill do some looking up on that and see what comes out of it.

##### Share on other sites

The conversion seems to have worked well it atleast compiled I need to throughly test it!

LockFile( _get_osfhandle(fd), 0, 0, 1024, 0 )

Thanks SyncViews! Ill post if it works the way I need it to or not!

##### Share on other sites

Works perfectly took me a bit to test because I forgot it doesnt lock in the same process lol. Hopfully this helps the next person racking there brain and saves some time vs rewriting to use createfile and the windows file handles.