We have NUM_PLAYERS
of child processes, and each of them outputs its id like
player 5: I scored 0 (PID = 411160)
but the problem is, all of them writing out simultaneously so it gets total mess of outputs.
player player player 1: I'm in this game (PID = 11049)
player 01: I scored 3: I'm in this game (PID = 11051: I'm in this game (PID = 1048)
How can i make them wait for each other and write? Here is my actual code
int main(int argc, char *argv[])
{
for (i = 0; i < NUM_PLAYERS; i++) {
/* TODO: spawn the processes that simulate the players */
switch(pid = fork()) {
case -1:
printf("Perror\n");
exit(EXIT_FAILURE);
break;
case 0:
//printf("%s<%d>%s<%d>\n", "CHILD ", getpid(), " ppid: ", getppid());
//sleep(1);
dup2(seedArray[i][0], STDIN_FILENO);
close(seedArray[i][0]);
dup2(scoreArray[i][1], STDOUT_FILENO);
close(scoreArray[i][1]);
sprintf(arg1,"%d",i);
execv("./shooter", args);
//shooter(i, seedArray[i][0], scoreArray[i][1]);
//exit(EXIT_SUCCESS);
break;
default:
//pid = wait(NULL);
pidArray[i] = pid;
}
}
// SOME IRRELEVANT CODE HERE
int status;
for(i = 0;i < NUM_PLAYERS; i++)
{
wait(&status);
}
return 0;
}