如何在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
如何分享a
和b
的值?我想得到a
和b
正在增加的结果。