This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

filesystem accessed by multiple threads

Hi All,

I'm developing an application with keil RTX and STM32F4.
One thread collect events on a log file. I manage the files with fopen, fwrite, fdelete, ....

Another thread manages the network interface. With the FTP server I can download files to my PC.

I would like that one thread at a time accesses to the log file, in order to avoid read or write errors, due to simultaneous access.

Could anyone suggest me how to do?

Many thanks in advance.

Davide

Parents
  • Hi Per
    thanks for your prompt reply.

    The problem is that when I download a file via FTP, the FTP functions for managing the download are called inside a library. Here is the code that I see:

    // Open a file for reading or writing in FTP server.
    __weak void *ftp_server_fopen (const char *fname, const char *mode) {
      return (fopen (fname, mode));
    }
    
    // Close a file previously open in FTP server.
    __weak void ftp_server_fclose (void *file) {
      fclose (file);
    }
    
    // Read block of data from a file in FTP server.
    __weak uint32_t ftp_server_fread (void *file, uint8_t *buf, uint32_t len) {
      return (fread (buf, 1, len, file));
    }
    
    

    Any ideas?

    Thanks

    Davide

Reply
  • Hi Per
    thanks for your prompt reply.

    The problem is that when I download a file via FTP, the FTP functions for managing the download are called inside a library. Here is the code that I see:

    // Open a file for reading or writing in FTP server.
    __weak void *ftp_server_fopen (const char *fname, const char *mode) {
      return (fopen (fname, mode));
    }
    
    // Close a file previously open in FTP server.
    __weak void ftp_server_fclose (void *file) {
      fclose (file);
    }
    
    // Read block of data from a file in FTP server.
    __weak uint32_t ftp_server_fread (void *file, uint8_t *buf, uint32_t len) {
      return (fread (buf, 1, len, file));
    }
    
    

    Any ideas?

    Thanks

    Davide

Children
  • Note the __weak keyword before the ftp file functions - you can supply your own functions if you like, and the linker will then pick up your code instead of the weak functions in the library.

    In the end, you don't want the log file to be constantly opened/closed when you add log lines (unless you add log lines very seldomly) to avoid the wear of constantly having to not just write the new log data but also updating the information about how large the file is etc.