Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win


Binary tree Process  creation in UNIX

Posted on 2009-04-10
Medium Priority
Last Modified: 2013-11-05
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?
Question by:Beebutter
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
LVL 46

Expert Comment

by:Kent Olsen
ID: 24119254
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,

Accepted Solution

xtravagan earned 2000 total points
ID: 24119490
I am slghtly uneasy to answer these kind of questions because they sound too much like school work. And as such it is a learning experience that is supposed to happen.

In unix you use

man fork

to figure stuff out.

In windows you instead search msdn for CreateProcess.

Anyhow, I intentially left a error in this code if you get what is going on it will be easy to fix. You will notice it grows more than it should.

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int run(int n);
int spawn(int n)
    int pid = fork();
    if (pid == 0)
        // Child
        printf("Child at n %d\n", n);
    else if (pid == -1)
        printf("Error spawning\n");
        // Parent
int run(int n)
    if (n > 0)
        spawn(n - 1);
        spawn(n - 1);
int main(int argc, char** argv)
    int n = 0;
    if (argc > 1)
        n = atoi(argv[1]);
    printf("Running with n %d\n", n);

Open in new window


Author Comment

ID: 24120269
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.

Author Comment

ID: 24120534
I understand your goal in maintaining Acdemic honesty. I just wanted to let you know my side of explanation. Thats all.

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.

604 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question