c - Different processes writing out simultaneously -


We have NUM_PLAYERS of child processes, and each outputs its ID < / P>

  player 5: I scored 0 (PID = 411160)  

But the problem is, they are all writing together, so this output The total mess of is done. Player player player 1: I am in this game (PID = 11049) Player 01: I scored 3 runs: I am in this game (PID = 11051: I play this game (PID = 1048)

How can I wait and write for each other? This is my real code

  Int main (int argc, char * argv [] {} (for (i = 0; i <+ NUM_PLAYERS; i ++) {/ * TODO: generate players emulation / / switch ( Pd = fork ()) {Case -1: printf ("panic \ n"); Exit (EXIT_FAILURE); break; case 0: // printf ("% s <% d>% s <% d> N "," baby ", getpid ()," ppid: ", miller ppd ()); // sleep (1); du 2 (seed) [i] [0], STDINIFLNO); near (seedArray [i] 0]); Dupta 2 (scoring [i] [1], STDOUTIEFLNO); close score (scoreArray [i] [1]); sprintf (ARG1, "% d", I); Execv (". / Shooter", Args ); // Shooter (I, Beed Arrere [I] [0], scorer [i] [1]); // exhaust (EXIT_SUCCESS); breakdown; default: // pid = wait (NULL); Pdare [ii] = pid; }} // some intimate code here; For (i = 0; i  

You need some explicit locking mechanism (but limited for lock file) No) limited, or shared mute x

For lock file: Ideas for using OS File Locking Semantics

Link:

Shared mutics for: Share shared memory and location process sharing Use in Mute X

link:

link:


Comments