This code is part of my OS project, the project asks to do something where parallel processes are applied, I decided to create a client server poker project with two players, where I use the processes of sons and grandchildren to determine the value of the hand.
The scheme used in the code is as follows:

The problem with the code is that in the same game only the first hand is correctly evaluated, and in the second - incorrectly, and in the third game an error occurs, and the program ends, this happens for each new game
Here is the code:
void check_hand(int suits[5],int ranks[5],int *point){ pid_t son[2]; int i,j; for (i = 0; i < 2; i++){ son[i]=fork(); /*********************************************** straight flush son ************************************************/ if(son[i]==0 && i==0){ pid_t grandson[3]; int k; for(k=0;k<3;k++){ grandson[k]=fork(); if(grandson[k]==0 && k==0){ exit(F_highcard(ranks)); } else if(grandson[k]==0 && k==1){ exit(F_flush(suits)); } else if(grandson[k]==0 && k==2){ exit(F_straight(ranks)); } else if(grandson[k]<0){ puts("fork failed"); exit(-1); } } int exit_status_straight,exit_status_flush,exit_status_highcard; //waiting his sons waitpid(grandson[0],&exit_status_highcard,0); waitpid(grandson[1],&exit_status_flush,0); waitpid(grandson[2],&exit_status_straight,0); /**checkpoint A****/ //elaborate the exit statuses and exit with a value } /*********************************************** full house son ************************************************/ if(son[i]==0 && i==1){ pid_t grandson[2]; int k; for(k=0;k<2;k++){ grandson[k]=fork(); if(grandson[k]==0 && k==0){ exit(F_n_pairs(ranks)); } else if(grandson[k]==0 && k==1){ exit(F_tris_poker(ranks)); } else if(grandson[k]<0){ exit(-1); } } int exit_status_pair,exit_status_tris_or_poker; waitpid(grandson[0],&exit_status_pair,0); waitpid(grandson[1],&exit_status_tris_or_poker,0); /**checkpoint B****/ //elaborate the exit statuses and exit with a value } } if(son[i]<0){ puts("fork failed"); exit(-1); } } /*********************************************** analysis exit status of his 2 sons ************************************************/ pid_t pid; int status; int values[10]; //initialization for(j=0;j<10;j++)values[j]=0; for(j=0;j<2;j++ ){ pid = wait(&status); if(pid==son[0]){ values[WEXITSTATUS(status)]=1; } else if(pid==son[1]){ values[WEXITSTATUS(status)]=1; } else if(pid==-1){ puts("error"); exit(1); } } for(j=9;j>=0;j--){ if(values[j]==1)break; } *point=j; printf("point=%d\n",*point); }
In the following code, I set some control point to find an error, the result at runtime for each new game is the same for player 1 and player 2:
1 hand (always correct)
checkpoint A checkpoint B point=value
2 hands
checkpoint A point=value checkpoint B
why did this happen? A father must wait for his son, in which case he does not wait
3 hands
point=-1 and exit
Thanks in advance.
abc
source share