简体   繁体   English

fork()子进程中的全局变量

[英]Global variable in fork() child process

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. 主要目标是将子进程(基于返回true或false的check()函数结果)的数据存储到父进程。 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. 简而言之,程序从文本文件中读取ip,并检查它们是否在某个GEO CODE位置。 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. is_duplicate_check检查ip是否尚未检查,但是通过将ips存储到file1file2 ,并且到达100.000条记录时,ip负载很大。

Now all i want is to store ips from child process in an array and check before calling check() function or not. 现在,我想要的只是将子进程中的ips存储在数组中,并在调用check()函数之前进行check()

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. 总是给我类似(-1232557773525 ...)的东西,没用。

How can i solve this? 我该如何解决?

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 如何在C语言中的Linux中使用共享内存

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 http://www.boost.org/doc/libs/1_60_0/doc/html/interprocess/sharedmemorybetweenprocesses.html#interprocess.sharedmemorybetweenprocesses.sharedmemory

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM