Link to home
Start Free TrialLog in
Avatar of Beebutter
Beebutter

asked on

Binary tree Process creation in UNIX

I have to create concurrent processes in the form of the binary tree in UNIX using C program and fork() system call. Each parent should exactly spawn two processes which in turn will spawn 2 process. Given the number of levels in the binary tree, say n, that many processes should get spawn. I am new to unix and processes, Is there anyone who could help me on this?
Avatar of Kent Olsen
Kent Olsen
Flag of United States of America image

Hi Beebutter,

Just to be clear, if each process spawns two more processes, you'll wind up with 2^n-1 processes, not n.

It's actually pretty easy.  All you need to do is work out how to get 1 process to spawn exactly 2 processes.  Once that's done, running the process will result in two more processes being created,  Each of those 2 will then create 2 more.

The trick to stopping this (and you definitely want to stop this) is to set a global variable that records the depth.  When the child inherits the parent's memory it will also inherit the current value of the depth variable.  Just check it to make sure that the task needs to spawn additional children before id does spawn them.

You might want to post your code here before you try it.  :)  No point in having thousands of tasks spawning new tasks over and over if the code's just a bit off.


Good Luck,
Kent
ASKER CERTIFIED SOLUTION
Avatar of xtravagan
xtravagan

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Beebutter
Beebutter

ASKER

First of all let me make one point clear that this is not a homework assignement. I am trying how the execution time of program is changing in a multiple proces environment by allowing each process to handle different sets of numbers and different internal sorting algorithms. Well I already tried this for chain of processes. I am left with binary tree of process structure and tree of processes.
With the response from the experts I was anyways going to post my code only, to spot the error.
I understand your goal in maintaining Acdemic honesty. I just wanted to let you know my side of explanation. Thats all.