What's the output from this program using fork() and why?

Help using fork() in C programming?

  • Here's two pieces of my program. The first one for normal order works fine. I need help making it so I can reverse the order of the output. In normal order the original process ...show more

  • Answer:

    i can't understand what u mean. you can't start from a child... first comes the father, then the child, and then another child of child and so on... if you want to reverse the order, you should store output using e.g. a list (beware of concurrency! you may need locking the data structure) ~ a list, simply struct anode { struct anode *next; char memstring[256]; // limited output length }; but you can also do something simpler: char *list[ MAX_NUM_OF_CHILDREN ]; MAX_NUM_OF... maybe is the same as depth. then you don't write out the output, you store it; something like list[c] = malloc(MAX_STR_LEN); // add a check snprintf(list[c], MAX_STR_LEN, "process %d .. %d\n", getpid(), getppid() ); and at the end you do a for(c=depth-1;c>=0;c--) { printf("%s", list[c]); free(list[c]); } to print the outputs in reverse order not sure it works, ...

JQPN44SNSOEXHC223JA5N3IEQI at Yahoo! Answers Visit the source

Was this solution helpful to you?

Related Q & A:

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.