Thread: SYnchronize read & write of a cache file

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    14

    Question SYnchronize read & write of a cache file

    I need to implement the following module using C programming on both Solaris and Windows.

    1. The module will be used by multiple processes;
    2. The module calls an existing batch file from time to time;
    3. The batch file provides some output information that will be parsed by its caller;

    It may take a few seconds for the batch file to complete its execution. Hence to speed up, I need to implement a caching mechanism for the outputs of the batch file. This way the first caller of the batch file writes the outputs to a cache file, the following caller can then just open the cache file to get the information. I will also use a timeout parameter to decide if the cached information is expired and if so the batch file will be re-executed to refresh the cache.

    My question is, how should I synchronize the read and write operation of the cache file? Read/write locks, semaphore, or something else?

    Thanks!

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    Read/Write lock sounds good to me. That way you can have multiple readers but only one writer.

    gg

  3. #3
    Registered User
    Join Date
    Feb 2008
    Posts
    14
    Can you please provide more details regarding to the implementation of read/write lock? For example, should I use two locks or just use one lock and access it bydifferent mode? What are the specific C function names? Please note the lock(s) is used by multiple processes instead of threads.

  4. #4
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    You could use a couple of semaphores to implement a read/write lock. In *nix, there are usually 2 API's to choose from...

    Semaphores in the SVR4 specification come from <sys/sem.h> and include the functions semctl(), semget(), and semop().

    The Single Unix Specification, aka POSIX, has their own semaphore api from <semaphore.h> and include the functions sem_init(), sem_post(), sem_wait(), sem_destroy(), etc...

    For implemented a read/write lock - the concept is covered in this thread: http://cboard.cprogramming.com/showt...php?t=95848#10

    gg

  5. #5
    Registered User
    Join Date
    Feb 2008
    Posts
    14
    Thanks!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File transfer- the file sometimes not full transferred
    By shu_fei86 in forum C# Programming
    Replies: 13
    Last Post: 03-13-2009, 12:44 PM
  2. write and read system calls
    By nacho4d in forum C Programming
    Replies: 4
    Last Post: 01-28-2008, 10:59 AM
  3. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 05:02 AM
  4. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  5. Can I write & read file in same program?
    By chad03 in forum C Programming
    Replies: 2
    Last Post: 08-04-2003, 08:39 AM