I try to run 2 parallel processes i have a global variable x = 0 process A: increase global variable x by 1 process B: print x
I followed this example How to run two child processes simultaneously in C? but i always end up printing zeros
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
int main()
{
int x = 0;
int i;
pid_t child1, child2;
if (!(child1 = fork()))
{
// first childi
while(1)
{
x = x + 1;
}
}
else if (!(child2 = fork()))
{
// second child
while(1)
{
printf("%d", x);
}
}
else
{
// parent
wait(&child1);
wait(&child2);
}
return 0;
}