c - synchronizing 2 processes -
i'm trying make 2 processes start on task @ same time (count number, example). set 2 ready flags, 1 each process, , perform while loop check if both flags up. 2 processes start counting after check passed. here's not working code, don't know why:
int p1ready=0; int p2ready=0; int onebil = 1000000000; int main(){ int pid; int exit_code; pid=fork(); if(pid==0){ //child1 int count1=0; p1ready=1; //signal while(!(p1ready&p2ready))'//wait until 2 processes both ready while(count1!=onebil){ count1++; } exit(0); } else{ pid=fork(); if(pid==0){ //child2 int count2=0; p2ready=1; //signal while(!(p1ready&p2ready));//wait until 2 processes both ready while(count2!=onebil){ count2++; } exit(0); } else{ //parent //do stuff } return 0; }
the problem code is, in child1 , child2, own ready flag set 1. cannot see flag of other child being set. example, child1 sees p1ready=1, p2ready 0. why so? how can fix this?
thanks in advance!
the way trying synchronize processes not going work every process creating have own copy of p1ready
, p2ready
.
what seem looking kind of inter process communication. might want take @ http://en.wikipedia.org/wiki/inter-process_communication see list of possible options.
in simple case 1 mentioned in question sending both of child processes signal parent process enough, suggest best take @ that.
Comments
Post a Comment