如何在Linux中的子和父(在fork()之后)之间共享数据?

时间:2015-05-01 12:21:14

标签: c fork multiprocess

如何在Linux(而非Windows)中在子级和父级之间共享数据?

源文件:

1.H

extern int a;
extern int b;

2.H

#include "1.h"

void adder();

2.C

#include "2.h"

void adder()
{
    a++;
    b++;
}

1.C

#include "1.h"
#include "2.h"

int a = 0;
int b = 0;

int main()
{
    pid_t pid;

    pid = fork();
    while (1) {
        if (pid == 0) {
            adder();
        }
        printf("a: %d , b: %d\n", a, b);
    }

    return 0;
}

结果

a: 0  b: 0 

如何分享ab的值?我想得到ab正在增加的结果。

0 个答案:

没有答案