从Linux的子进程修改全局变量 [英] Modify global variable from child process in Linux

查看:568
本文介绍了从Linux的子进程修改全局变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我来自一个C#的背景,我有一个有点艰难的时间与并发在C.我不会骗你......这是一个项目,我对学校做的一部分。虽然专业我与高级语言的工作,我的毕业论文的教授通过强制C中的整个类code(我们大多数人都很少用... :(没有经验)把我们下了车。

无论哪种情况下,返回到这个问题。我有一个全局整数变量(从0开始),需要直到它达到100至5进程同时增加(100%相当)。

基本上发生的事情是,而不是分别修改completionCounter每个子进程正在它的一个副本...

有一个锁(对象)或诸如此类的东西,因为它是在C#中可用?我试图用一个二进制信号,但我无法得到它的工作,我没有使用共享内存还没有玩过。

  const int的THREADCOUNT = 5;
INT completionCounter = 0;INT主(INT ARGC,字符** argv的)
{
    诠释计数= 0;
    将为pid_t PID;    为(计数= 0; COUNT< THREADCOUNT;计数++)
    {
        如果((PID =叉())℃,)
        {
            //错误...
            返回-1;
        }
        否则,如果(PID == 0)
        {
            //儿童;
            而(completionCounter!= 100)
            {
                的printf([%I]过程下载数据块......,GETPID());                //在这里做的东西                completionCounter + = 5;            }
        }
        其他
        {
            //父。
        }
    }
}


解决方案

子进程不共享与父母的变量。你可以用派生的子进程做到这一点的唯一方法是通过专门创建和共享映射内存来保存的价值。

一个更合理的方式做这将是使用线程,而不是子进程,但似乎你坚持你给予的任务。在这种情况下,我建议是这样的:

 为int * X = MMAP(0,PAGE_SIZE,PROT_READ | PROT_WRITE,MAP_SHARED | MAP_ANONYMOUS,-1,0);

分叉,那么增量前 * X 中的子进程...

但是!

现在,你必须处理同步。由于-是,你会很容易得到丢失或在输出重复的数字。查找POSIX进程共享互斥体,让你在这个方向开始...

I come from a C# background and I have a bit of a tough time with concurrency in C. I am not going to lie to you... This is part of a project I have to do for school. Although professionally I have worked with high level languages, my senior thesis professor threw us under the bus by forcing the entire class to code in C (which most of us have little to no experience with... :( ).

Either case, back to the problem. I have a global integer variable (starting from 0) which needs to be simultaneously incremented by 5 processes until it reaches 100 (100% rather).

Basically what's happening is that instead of individually modifying the completionCounter each child process is making a copy of it...

Is there a lock(object) or something of the sort as it is available in C#? I tried using a binary semaphore but I couldn't get it to work, i haven't played with shared memory yet.

const int THREADCOUNT = 5;
int completionCounter = 0;

int main(int argc, char **argv)
{
    int count = 0;
    pid_t pid;    

    for(count = 0; count<THREADCOUNT; count++)
    {
        if( (pid = fork()) < 0 )
        {
            //error...
            return -1;
        }
        else if( pid == 0 )
        {
            //child;
            while(completionCounter != 100 )
            {
                printf("[%i] process downloading data chunk...", getpid());

                //do stuff here

                completionCounter += 5;

            }
        }
        else
        {
            //parent.
        }
    }    
}

解决方案

Child processes do not share variables with their parents. The only way you can do this with forked child processes is by specifically creating and mapping shared memory to hold the value.

A more reasonable way to do this would be to use threads rather than child processes, but it seems like you're stuck with the assignment you were given. In that case, I would recommend something like:

int *x = mmap(0, PAGE_SIZE, PROT_READ|PROT_WRITE, MAP_SHARED|MAP_ANONYMOUS, -1, 0);

before forking, then incrementing *x in the child processes...

BUT!

Now you have to deal with synchronization. As-is, you'll easily get missing or duplicate numbers in your output. Lookup POSIX process-shared mutexes to get you started in that direction...

这篇关于从Linux的子进程修改全局变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆