Process tree using fork()

7,405

OK I understand your question now. Your answer is correct. All I will say is that you can just do

fork()

for the final forks instead of

if (fork()) {}
else {}
Share:
7,405

Related videos on Youtube

Altair64
Author by

Altair64

Updated on September 18, 2022

Comments

  • Altair64
    Altair64 over 1 year

    I am given the task of creating this process tree in C using fork, if and else:

    |_____1___
    |___2__   |
    |_4_   |  |_3_
    |   |  |  |   |
    

    Or as a pstree like drawing:

    p(0)─┬─p(1)───p(3)
         ├─p(2)
         └─p(4)
    

    Is this code correct?

    #include <stdio.h>
    #include <stdlib.h>
    #include <unistd.h>
    
    int main() {
    
        if(fork()){
            // 1
            if(fork()){
                // 2
                if(fork()){
                    // 4
                }
                else{}
            }
            else{}
        }
        else{
            if(fork()){
                // 3}
            else{}
        }
    
        pause();
        return 0;
    }
    
    • gardenhead
      gardenhead over 8 years
      The question is unclear. This is clearly a homework problem: what were you give, and what do you have to find?
    • Altair64
      Altair64 over 8 years
      Hmm - i need to programme this process tree using only fork()
    • joepd
      joepd over 8 years
      Hi. It does not help if you use a non standard way to make a graph. Have a look at the output of pstree -ap to see how PIDs and relations are normaslly displayed. Also: Can't you just print out the pid that is returned by fork? You could use pstree to check your assumptions.
    • Altair64
      Altair64 over 8 years
      Ok thank you. I think that our lecturer need to specify what he wants from us :) I have to create a process tree using fork() and if, else in C. The proc tree have to look like is shown above.
    • Altair64
      Altair64 over 8 years
      Here is similar problem but different process tree. stackoverflow.com/questions/24082775/…
    • Runium
      Runium over 8 years
      Added a pstree like drawing. Is it correct? If not edit question and remove it.
  • gardenhead
    gardenhead over 8 years
    Could you elaborate The order of what?
  • gardenhead
    gardenhead over 8 years
    No it can't. Besides the numbers don't matter -- only the structure of the tree.
  • Runium
    Runium over 8 years
    Guess we're talking past each other. The point is that there is no guarantee 3 is forked before 4.
  • gardenhead
    gardenhead over 8 years
    I don't think that diagram is meant to have a timeline to it. Whether 3 or 4 is forked first, the tree structure will be the same.
  • Runium
    Runium over 8 years
    Yes, guess not. But then there is those dangling lines. Are those the forks and the pstree drawing wrong? (I added that one but perhaps I should not have. Did it as comments does not support that kind of drawings ...)
  • gardenhead
    gardenhead over 8 years
    The only way to know what exactly the original drawing was intended to represent is to ask the professor :D I think the lines are confusing and your pstree drawing is better. The best way to visualize a process tree is as, well, a tree!
  • Runium
    Runium over 8 years
    Ach. The update seems to never arrive. Was looking forward to this ...