user140053
user140053

Reputation:

File synchronization with mutex

In C (Linux AND Windows) if I want to manage a "perfect" concurrent file access like :

  1. A opens a file and begins writing something
  2. B opens, at the same time, the same file and waits that A ends its work
  3. A closes the file.
  4. B can write now.
  5. ...and so on...

Do you think I have to use mutex ? Should O_EXCL work too ? Better options ?

Upvotes: 1

Views: 1061

Answers (2)

codestriker
codestriker

Reputation: 61

if A and B are different threads, mutex or symaphores can be used to achieve synchronization between them. you can find more information on mutext at http://www.yolinux.com/TUTORIALS/LinuxTutorialPosixThreads.html

Upvotes: 1

Martin Beckett
Martin Beckett

Reputation: 96109

Are A and B threads or programs?

If they are separate programs it's probably better to only have one have the file open for writing at once, even if you manage to synchronise access between two apps you have to be very careful about flushing buffers to ensure that they really are in the state you expect.

Upvotes: 1

Related Questions