this post was submitted on 17 Oct 2023
4 points (75.0% liked)

Programming

17670 readers
281 users here now

Welcome to the main community in programming.dev! Feel free to post anything relating to programming here!

Cross posting is strongly encouraged in the instance. If you feel your post or another person's post makes sense in another community cross post into it.

Hope you enjoy the instance!

Rules

Rules

  • Follow the programming.dev instance rules
  • Keep content related to programming in some way
  • If you're posting long videos try to add in some form of tldr for those who don't want to watch videos

Wormhole

Follow the wormhole through a path of communities [email protected]



founded 2 years ago
MODERATORS
 

Hello! I'm writing a Qt/c++ program that should write and read data on a file, while another process does the same. What file format should I use to prevent conflicts?

top 5 comments
sorted by: hot top controversial new old
[–] ENipo 9 points 1 year ago (1 children)

No file format will save you by itself since at the end of the day, xml, json, etc they are all plain text.

One solution would be to use some database (like sqlite if you are just doing things locally on your computer).

Another solution would be to have a single process that is responsible for writing the data, while all other processes send messages to it via a queue.

[–] [email protected] 2 points 1 year ago

I like the solution of the single process, thank you very much!

[–] [email protected] 5 points 1 year ago (2 children)

Can you describe your use case more?

I don't think format matters - if you've got multiple processes writing simultaneously, you'll have a potential for corruption. What you want is a lock file. Basically, you just create a separate file called something like my process.lock. When a process wants to write to the other file, you check if the lock file exists - if yes, wait until it doesn't; if no, create it. In the lock file, store just the process id of the file that has the lock, so you can also add logic to check if the process exists (if it doesn't, it probably died - you may have to check the file you're writing to for corruption/recover). When done writing, delete the file to release the lock.

See https://en.wikipedia.org/wiki/File_locking, and specifically the section on lock files.

This is a common enough pattern there are probably libraries to handle a lot of the logic for you, though it's also simple enough to handle yourself.

[–] [email protected] 1 points 1 year ago

Or if possible, let the process communicate with the other and let only one write to the file.

[–] [email protected] 1 points 1 year ago

Wonderful! Ill look into it, seems very promising, thanks!