1

I have encountered an issue when trying to make a duplicate check function.

The main objective is to store data from child process (based on the check() function result that returns true or false) to parent process. I already tried global variables but didn't work.

This is my code :

...

for(j=0; j<indIP; j++)
{
    fflush(stdout);
    if (!(fork()))
    {
        char* this_ip = strdup(ip[j]);
        if(is_duplicate_check(this_ip,"file1"))
        {
            if(debugLevel >= 2) printf("Duplicate IP %s\n", this_ip);
        }
        else if( is_duplicate_check(this_ip,"file2"))
        {
            if(debugLevel >= 2) printf("Duplicate IP %s\n",this_ip);
        }
        else
        {
            if(debugLevel >= 2) printf("Checking IP [%d of %d] -> [%s]\n",current_combo,possible_combinations,ip[j]);                       
            checkauth(this_ip);
        }

        exit(0);
    }
    else
    {
        numforks++;
        current_combo += trys;                  
        if (current_combo > possible_combinations)
        {
            break;
        }

        if (numforks >= maxf)
        {
            wait(NULL);
            numforks--;
        }
    }

    indInterface++;
    if(indInterface>=countInterface) indInterface=0;
}

puts("Finalizing...");
while(numforks>0)
{
    printf("Waiting for the child processes [%d] are finished ....\n", numforks);
    wait(NULL);
    numforks--;
}
puts("Script completed!");
return 0;

...

In short the program reads ips from a text file and checks them if they are a certain GEO CODE location. The is_duplicate_check checks if the ip was not already checked, but it is working by storing ips to file1 and file2, and when it arrives on 100.000 records it is making a lot of load.

Now all i want is to store ips from child process in an array and check before calling check() function or not.

I already tried with

static int *glob_var;
glob_var = (int *) mmap(NULL, sizeof *glob_var, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS, -1, 0);


glob_var++;
printf("glob_var = [%d]\n",glob_var);

allways gives me something like (-1232557773525 ...), no use.

How can i solve this?

melpomene
  • 84,125
  • 8
  • 85
  • 148
Damian
  • 761
  • 1
  • 9
  • 26
  • 2
    With a `fork()` call the whole process environment will be duplicated, so you can't share any information between the processes using a global variable. You need IPC as with any other situations where processes communicate with each other. – πάντα ῥεῖ Aug 31 '16 at 13:28
  • Also `glob_var` is a pointer. – Margaret Bloom Aug 31 '16 at 13:29
  • Use the STDIN and STDOUT – Ben Aug 31 '16 at 13:33
  • @πάνταῥεῖ what is an IPC, also i know that `fork()` creates a copy, i just need a solution, like passing the address of the ip array, or something, i don't know – Damian Aug 31 '16 at 13:33
  • @Ben what are STDIN and STDOUT and how can i use them? – Damian Aug 31 '16 at 13:34
  • @Damian IPC is short for interprocess communication, like e.g. instantiating a ` pipe()` between parent and child process. _"... like passing the address of the ip array ..."_ Passing addresses between processes won't work, because as mentioned they don't share the same address space actually. – πάντα ῥεῖ Aug 31 '16 at 13:37
  • Try `popen` to solve your problem: http://stackoverflow.com/questions/19087772/getting-the-output-of-external-program-to-c-code/19087847#19087847 – Ben Aug 31 '16 at 16:07

1 Answers1

0

A fork duplicates the memory. So when you store the variable it is different memory from the parent process.

If you can use threads you can have the behavior where you share a global variable (because threads share memory).

Or if you have to fork you can look at

How to use shared memory with Linux in C

for shared memory usage or have a look at the boost version:

http://www.boost.org/doc/libs/1_60_0/doc/html/interprocess/sharedmemorybetweenprocesses.html#interprocess.sharedmemorybetweenprocesses.sharedmemory

Community
  • 1
  • 1
Hayt
  • 5,210
  • 30
  • 37
  • i can't use threads, sorry, only fork(), and know that forks make a copy of the program ... i just need a solution, like passing the address of the ip array, or something, i don't know – Damian Aug 31 '16 at 13:32
  • shared memory is what you want. You cannot pass the address of memory in one program to the other. Shared memory is a block of memory explicitly meant to be shared by processes. – Hayt Aug 31 '16 at 13:35
  • can i store like 10 MB there? i mean a list of 100.000 ips may take like 2MB of memory, what do you think? – Damian Aug 31 '16 at 13:37
  • The maximum shared memory size can be query'd and also changed if it is too low. see http://stackoverflow.com/questions/10991111/max-possible-shared-memory-size-in-64-bit-linux-machine – Hayt Aug 31 '16 at 13:40
  • @Damian If you need just to pass data one way, probably it's easier to use pipes instead of shared memory – alexeykuzmin0 Aug 31 '16 at 13:44
  • @Damian, the only way to share memory is with shared memory. Even if you pass a pointer, the pointer you pass means a different thing (your array copy) than in the other process (the other process' copy of the array) (2Mb is perfectly valid to be shared, even 1Gb or more) – Luis Colorado Sep 01 '16 at 08:40