1

I'm implementing pstree as an assignment,

and I would like to create test tree of processes, but I'm struggling to do so.

I tried to do something like this to create tree of background processes so I can test my script later.

recursive tree of processes

childProcess(){

        echo "mine PID is: "$$

        local -i level=$(expr $1 - 1)  # level of recursion

        if [[ $level -gt 0 ]]; then
          echo $level
          childProcess $level &           # we do binary tree
          echo "Process with  PID $! launched"
          childProcess $level &
          echo "Process with  PID $! launched"
          sleep $level
          echo "baf! "$level" pid is: "$$
        fi
}


# create binary tree of depth 4
for (( i = 0; i < 2; i++ )); do
    childProcess 4 &
    echo "running proces no. :"$!
done

But I would like to echo PID from within of process, and with this I can't.

Another not-working-solution would be this, but it creates processes in "DFS manner".

declare -i level=$1


if [[ $level -gt 0 ]]; then
  sh $0 $(expr $level - 1)  # spawn children
  sh $0 $(expr $level - 1)  # spawn children

  sleep 100;
  echo "level :"$level" process no: "$$
fi

Can anyone tell me how to construct tree of processes which I can acces by PID, and that would be there until I delete them, e.g. by killing oldest process.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.