matlab and c# working with same file
7 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Hi. So I have C# application that constantly edits data.txt like this:
while(true){
open data.txt;
overwrite old data with new data;
close data.txt;
wait 1 second;
}
The file always contains one number. While that is happening I'm using matlab to read data.txt like this:
sum = 0;
i = 0;
while(i < 1000){
i = i+1;
open data.txt;
number = read data.txt;
close data.txt;
sum = sum + number;
}
At the end, I always get that sum = []. What should I do to fix this problem? Thank you.
0 commentaires
Réponse acceptée
Walter Roberson
le 27 Nov 2011
The code you show to read the file is not MATLAB code.
Exactly how you do the reading in MATLAB can be important for this question, as some of the file reading mechanisms return the empty array when they are unable to read data. Adding an empty array to a scalar value (sum) returns the empty array, and thus once you get the empty array once the sum would continue to be the empty array.
Your code appears to assume that from the time that the C# part opens the file until it closes the file, that MATLAB will pause and wait for the file to be available. That behavior is not implemented by default on any operating system I can think of at the moment (though perhaps one of the Orange Book A1 certified operating systems might do that.)
Most operating systems do not implement mandatory file locking (it leads to an amazingly long list of problems in practice); MS Windows appears to do locking of files that are open for writing (a behavior that I have not been able to trace back in Microsoft documentation), but the default behavior in MS Windows is that if a process attempts to open a file being written to, then the open fails rather than the second process queuing until the file is available.
This is a situation where the details of your code matter. You should be building a semaphore. Unfortunately MATLAB does not offer any built-in semaphore systems, and they are difficult to implement correctly in networked file systems.
1 commentaire
Walter Roberson
le 27 Nov 2011
Those kinds of files are more typically known as "semaphore files". They can be quite useful when properly programmed. Unfortunately they are unreliable over most networked file systems (such as NFS). [It has been a good 25 years since I last used a system that had a networked file system that handled this properly.]
If you check for the existence of a file in MATLAB and then create the file if it did not exist, then you have a race condition, as the other process might do all of its checks in-between those two statements. In order to avoid the race condition, you must use an atomic system call that *in one step* returns an error if the file exists but grabs it and creates it if it did not exist. MATLAB does not provide direct access to those system calls: you would need to drop in to C and use open() with (O_CREAT | O_EXCL) -- the flags needed are not available with fopen()
Plus de réponses (1)
Voir également
Catégories
En savoir plus sur Communications Toolbox dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!