Binary Tree Generation Using fork()

Discussion in 'Programmer's Corner' started by learneros, Nov 2, 2007.

  1. learneros

    Thread Starter Member

    Nov 2, 2007
    I am working on a program and kind of a stuck,nt getting it done.
    "The program should take one command line arguments: number of hierarchy level. The

    hierarchy of your program should of that level and each node have two child processes."
    Can anyone give me the C code using fork() of this program.Thanks.
  2. Papabravo


    Feb 24, 2006
    Sounds like a homework assignment. Do we get your grade if we do it for you? What exactly is fork(), bsides a function that takes no arguments and returns no values. Do you think we are mind readers?
  3. learneros

    Thread Starter Member

    Nov 2, 2007
    Alright it seems ppl r in fighting mood.Fork() is a system call which creates a child process in unix.Its not a homework its a small part of assignment.I m finding it difficult to attempt this part thts y i wanted the help.
  4. Pootworm


    May 18, 2007
    Can you restate the question with a little more detail as per what exactly you're trying to do? Do you need to store the tree? Are you just using the tree idea to pop a bunch of child processes...?
  5. Luke

    New Member

    Nov 2, 2007
  6. Papabravo


    Feb 24, 2006
    So your not just trying to create the tree as a data structure to store data. What you want is to spawn two child processes. Each of those child processes will spawn two child processes. This process will continue until a child process recognizes that is is on the target level and is not allowed to spawn more processes. If a parent can pass its level to a child and the child can increment that to get its own level and check the result against a limit that should do the job.

    That's what I know. What I don't know is the mechanics of passing values and parameters among processes.
  7. learneros

    Thread Starter Member

    Nov 2, 2007
    sorry 4 bit delayed reply...thought hard on the problem.Let me clearify it..
    Suppose user enter 2 as command line argurment,it means i need to create a tree of level 2 with the help of fork() call.Now wat happen in the program is :
    As program start executing,in main i will call fork().It will create a child process.
    In the parent block i will call another fork so by doin so i will have a root node and two child processes.Let me call process a node.So wat i have now a 1 level tree with root node having two childrens.As command line arguement was 2,i neet to extend this tree,where
    each of the two child process will have further two nodes makin it 2 level tree where every
    node will have 2 child processes.With command line argument of 2 or or mayb 4,i can develop
    a tree bt for larger values i need to use a loop.And this is a problem part.There is nothin to b done in child processes,all of them will have one print statement.Basic demand is to
    make a tree of level n where n is command lind arguement and every node having two nodes.
    Hope this clearifies things.
    With hardcoded 2,3 level tree,it is not a problem.But doin it in a loop so tht it can
    extent to any level is creating all the problem.

    Code for 1 level tree will b like

    int p_id,p_id2;
    p_id = fork();
    if(p_id == -1)
    printf("Fork failed,No child process created");
    if(p_id==0) // Child block
    else // Parent Block
    p_id2= fork();
  8. hgmjr


    Jan 28, 2005
    Greetings learneros,

    Here is your code using the CODE html tags. The "#" symbol in the tool set at the top of the post form is the method of invoking this feature. Since readability is enhanced when you use this feature you may want to consider its use when posting your source code snippets in the future.

    Code ( (Unknown Language)):
    1. Code for 1 level tree will b like
    3. int p_id,p_id2;
    5. p_id = fork();
    7. if(p_id == -1)
    8. {
    9.     printf("Fork failed,No child process created");
    10. }
    12. if(p_id==0) // Child block
    13. {
    14. }
    15. else // Parent Block
    16. {
    17.     p_id2= fork();
    18. }