Synchronization ...

This is a discussion on Synchronization ... within the C Programming forums, part of the General Programming Boards category; I have created a dll that contains a couple of methods, one is to check a zip file to see ...

  1. #1
    Deb
    Deb is offline
    Registered User
    Join Date
    Apr 2007
    Posts
    28

    Synchronization ...

    I have created a dll that contains a couple of methods, one is to check a zip file to see if the entries match criteria. Another method extracts the entries.... and so on.

    There other methods that check to see if a file is a valid file, etc... as well.

    My question is ... how do I synchronize these methods? I don't want to prevent other methods from accessing the same file...

    Thanks!

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    The term "synchronize" brings to mind the issues that arise with multiple threads, but I don't think that is what you are referring to.

    Are you just trying to lock out other programs from accessing the file in question while you are dealing with it?

    Basic idea of locking files: http://en.wikipedia.org/wiki/File_locking

    The dwShareMode parameter in CreateFile is probably what you want to check out: http://msdn2.microsoft.com/en-us/library/aa363858.aspx

  3. #3
    Deb
    Deb is offline
    Registered User
    Join Date
    Apr 2007
    Posts
    28
    Hmm.... not sure of the term to use I guess.

    What I am trying to do is to access methods in a dll from Java (using JNI).

    I can call native methods, ie: isZipFile(.... ) extractZip(.... ).

    I am sending 1 - n files to be checked, or extracted -- and it is crashing out.

    I'm calling the native method for each file... ie:

    isZipFile(String fileName);

    Would this crash out if the method is processing/executing the last file, and it's called before the method has completed?

  4. #4
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,162
    It should only crash if you don't implement proper error checking. If you check the return value of your zip file access functions (such as extractZip) then your program should be able to properly handle any access violation issues.
    If you understand what you're doing, you're not learning anything.

  5. #5
    Deb
    Deb is offline
    Registered User
    Join Date
    Apr 2007
    Posts
    28
    The c code contains a load dll method. Loads the unzip32.dll

    The method I wrote was to call the dll function to extract the zip entries from the specified zip file name.

    Takes the file name, and a few defined structures.

    Code:
    int extractZipFile(char* zipFilename)
    {
         if (!zipFileName)
         {
              return 0;
         }
    
         if (strlen(zipFileName) <= 0)
         {
              return 0;
         }
    
         return (whichever call to dll function ....);
    }
    The return from the dll call is an integer code representing the success/error code.

    It works every time when testing and calling the method one file at a time from within the C code... I call the function, it returns, I call the function, it returns....


    However, when run on more than one file at a time, calling from JNI -- "it crashes"...

    I thought it might have something to do with sending 1 - n calls to the dll function.

    From Java calling the native method, the function could get called again before it returns from the last call. Would that matter?

  6. #6
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,162
    It could if it's not a reentrant function. Functions which have some kind of internal mechanism for keeping track of things in a non-automatic type of way (global or static variable are common) then the second call could corrupt whatever data is in the variable while the first call is using it.

    A lot of functions have a reentrant-safe version. For instance, the strtok() function needs to keep track of where it is in the string between calls in order to parse it correctly. If you called strtok() from another thread while the first thread was trying to use it, it would corrupt the internal static buffer. To get around the problem, a lot of environments have a strtok_r() function which accepts a 3rd parameter.
    char *strtok_r(char *s, const char *delim, char **ptrptr);

    The strtok_r() function works the same as the strtok()
    function, but instead of using a static buffer it uses a
    pointer to a user allocated char* pointer. This pointer,
    the ptrptr parameter, must be the same while parsing the
    same string.
    I don't know if your dll has a similar workaround, but it might be worth looking into.
    If you understand what you're doing, you're not learning anything.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. using this as synchronization object
    By George2 in forum C# Programming
    Replies: 0
    Last Post: 03-22-2008, 07:49 AM
  2. synchronization object choosing
    By George2 in forum C# Programming
    Replies: 0
    Last Post: 03-22-2008, 04:33 AM
  3. time synchronization problem
    By freeindy in forum C Programming
    Replies: 1
    Last Post: 04-19-2007, 06:25 AM
  4. Thread Synchronization :: Win32 API vs. MFC
    By kuphryn in forum Windows Programming
    Replies: 2
    Last Post: 08-09-2002, 09:09 AM
  5. Thread Synchronization :: MFC
    By kuphryn in forum Windows Programming
    Replies: 0
    Last Post: 04-21-2002, 09:27 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21