ِAmin
ِAmin

Reputation: 184

what happen when flock processed?

I need help in this scenario,

I need to write in the file for some reason during the handling of request, So I used flock to be sure there is just one request is writing in the file in the same time.

My questions is what happen when a request call flock during locked by other request. is it wait until released or just return false and do not open the file?

Upvotes: 1

Views: 155

Answers (1)

Déjà vu
Déjà vu

Reputation: 28850

According to the documentation

By default, this function will block until the requested lock is acquired

Just be sure to use the right lock (see doc)

  • LOCK_SH to acquire a shared lock (reader).
  • LOCK_EX to acquire an exclusive lock (writer).

basically a writer waits for an exclusive lock, meaning it waits for all readers or the current writer to release the(ir) lock(s). While readers may be more than one to read the file.

and, to release the lock

  • LOCK_UN to release a lock (shared or exclusive).

Upvotes: 3

Related Questions