views:

56

answers:

2

This is a question from PHP zend exam,

Using flock() to lock a stream is only assured to work under what circumstances?

  • When running in a Linux environment local filesystem
  • When accessing the stream of the local filesystem
  • When running in a Windows environment and accessing a share
  • When accessing a bi-directional stream
  • When accessing a read-only stream
A: 

Oh, good question.

Checking if a stream supports locking was only added in 5.3, but the streamWrapper example class has a stream_lock method that seems to "always" have existed. stream_lock suggests that it can also work on streams that can be blocked.

I don't expect you can flock a socket, so it looks like your answer is #2: You can safely know that flocking a stream will work when the stream is a (local) file.

(How flock operates on remote files (NFS, CIFS) is up to the service providing those remote files. Some older versions of various NFS daemons do not support flock at all, for example.)

Charles
The blocking mode is unrelated to file locking.
Artefacto
Indeed, I misread the reference. Misleading sentence removed.
Charles
+2  A: 

A stream has this set of operationswrite, read, close, flush (mandatory, even if they are no ops) and seek, cast, stat, set_option (optional). When you request a file lock, the set_option operation is called.

Just from here, you can see that being bi-directional or read-only has nothing to do with this. One could implement an arbitrary wrapper, make the writes and reads have some effect and yet not implement set_option, because it's optional. Likewise, one could implement a no-op write operation and yet handle file locks in my set_option implementation. Running in a Linux environment is also irrelevant, since what matters is what the stream supports.

(NOTE: I'm not sure what "running in a Linux environment local filesystem" means. I admitted it means "running PHP from the local filesystem in a Linux environment" as opposed to e.g. "running PHP from an AFS filesystem in a Linux environment". It if means "accessing a stream that backs the local filesyste in a Linux environment", this is probably the correct answer, given the manual warning described below).

The remaining questions involve the STDIO streams. Now, when checking if a stream supports blocking with stream_supports_lock, PHP doesn't actually try a flock, it passes the set_option operation a special value that queries "does this stream support file locking"? The STDIO stream operation always responds it does, so it would appear that all the two remaining answers are correct.

However, the fact the set_option operation claims it supports file locking doesn't make it true. When you actually try to acquire the lock, it may fail. So when is it guaranteed to work? Surely not Windows shares, since these can be backed by almost anything. We're left with "on the local filesystem". So the answer is, by elimination

When accessing the stream of the local filesystem

Note, however, the (admittedly outdated) warning in the manual:

flock() is not supported on antiquated filesystems like FAT and its derivates and will therefore always return FALSE under this [sic] environments (this is especially true for Windows 98 users).

Artefacto
NTFS has been the default filesystem on all versions of Windows produced since 2000. flock should work fine on NTFS. Furthermore, other UNIX-alikes such as FreeBSD and Solaris certainly would support flock, which makes the Linux-specific answer either incorrect or very poorly worded if correct.
Charles
@Cha Well, I misread the choices. It's definitely the second because the first says "running", not "accessing the stream".
Artefacto
Good point. Given the exact wording, they all seem to be incorrect.
Charles