Original Post
Hi I am trying to make a binary tree of processes w/ fork() in c/c++.
i.e. 1 parent 2 child 2 childs(now parents) have 4 childern(now parents)... 8 childern etc.
I got the first layer down but I am having trouble figuring out what to do next. Any help would be appreciated.
[Edited by - Zekk on September 29, 2010 2:31:14 AM]
i.e. 1 parent 2 child 2 childs(now parents) have 4 childern(now parents)... 8 childern etc.
I got the first layer down but I am having trouble figuring out what to do next. Any help would be appreciated.
void Child(int, int, int);void Parent(int, int&);int main(){ int child = 0; int i; int r = 1; i = 1; int num = 0; int parentid; int child01 = 0; int child02 = 0; int pid; for(i=1; i < 2; i++) { cout << "Level\t Procs\t Parent\t Child1\t Child2\n"; cout << "No.\t ID\t ID\t ID\t ID\n" << endl; } // start main loop for(child = 0; child < 2; child++) { pid = fork (); //Parent Scope if (pid > 0) { Parent(num, parentid); } //Child Scope else if (pid == 0) { Child(r, child01, child02); } else if (pid < 0) { perror ("Can not create fork."); exit (1); } } cout << num << "\t" << getppid(); cout << "\t" << parentid; }void Child(int r, int child01, int child02){ sleep(1); child02 = getpid(); cout << "\t" << child02; exit(1);}void Parent(int num, int& parentid){ parentid= getpid();}[Edited by - Zekk on September 29, 2010 2:31:14 AM]