To start,
blakem is completly right. But, i thought i would add to this the concept of how to make sure the printing is done in order.
One way to have the print statements sorted would be to have one process (the parent), that each other process had a
pipe() opened to. Then have the parent read from each child and print it's output. this would hust performace alot
One other way would be to create a named pipe on the file system (
mknod in unix), and have each child direct it's STDOUT and STDERR into the pipe. Then, have another child reading from that, ordering the messages, and printing them to screen.
Last i can think of is to use
shmread() and
shmwrite() to pass the information around, and have one process read the messages , sort them, and print.
While all of these work (as would using UDP or TCP for the message passing), it does seem a bit much. I usually feel that this is not how
fork() is meant to work, so it is better to understand that they will simply be out-of-order, and design with that in mind.
i had a memory leak once, and it ruined my favorite shirt.