Search code examples
g-wan

How to read, write file over gwan


I am new to gwan, and currently coding a read/write static file over gwan, however, I found that when I try to open file (corresponding to some parameter pass to gwan), it run the main() twice (or infinite looping), could any one help ? Thanks!

Here is the getanddelivery.c

int main(int argc, char *argv[])
{
    global_count = 1;
    printf("Global count : %d\n", global_count);

    xbuf_t *reply = get_reply(argv);
    char *name = 0;

    while(global_count<argc){
            get_arg("zoneid=", &name, global_count, argv);

          if(hadCache(name)){
                    printf("have file\n");
          }else{
                    printf("No file found!\n");
          }
            global_count++;
    }

    xbuf_xcat(reply, "Work!");

    return 200;

}

Function hadCache is to check whether the static file exist or not! Thanks again!!


Thank you Gil for answering my question! I had modified the code and it work now! However, I try to use my own header file and function file on gwan under gwan/include/myownfunction.h & hadCache.c, I found that the getanddelivery.c cannot normally call the hadCache() function

#include "myownfunction.h"

int main(){ ... }

myownfunction.h

#include <stdio.h>
#include <stdbool.h>

bool hadCache(char* zoneid);

hadCache.c

#include "myownfunction.h"

bool hadCache(char* zoneid){...}

However, it work if I replace this on my getanddelivery file:

Replace this #include "myownfunction.h"
By           #include "hadCache.c"

Could you please help me ? Thank you v much !


Solution

  • Your problem comes from the thread-unsafe global_count++; variable increment which will cause havock under concurrency.

    You should rather read about atomicity and use an atomic incrementation. See this other question: feature request: an atomicAdd() function included in gwan.h.