Write a program using C to generate a binary tree of processes. The input to the
ID: 3672703 • Letter: W
Question
Write a program using C to generate a binary tree of processes. The input to the program includes the number of levels in the tree. The maximum number of levels is 5, but the program should work for a general case of any number. Use command line arguments.
Example of a program run is as follows:
ecs416lin213.cecs.csuld.edu:1>p1 4
Please show output listing using N = 3. Draw a tree diagram showing the process IDs in circles. My previous lab is shown as reference, please complete
Starting point:
/**************************************************************************/
/* PROGRAM: lab2.c */
/* DESCRIPTION: This program generates a chain of processes */
/* using fork(). The number of processes n is a command line argument. */ /* Each process sleeps for a random time less than 10 seconds then prints out */
/* process ID, parent ID, and child ID */
/**************************************************************************/
# include <stdio.h>
# include <stdlib.h>
# include <sys/types.h>
# include <unistd.h>
int main(int argc, char *argv[])
{
int i, m, n, sleeptime, seed;
pid_t childpid;
if (argc !=2) {
printf(" Usage: %s processes ", argv[0]);
exit(1);
}
n = atoi(argv[1]); m = 10;
childpid = 0;
for (i=0; i<n;i++)
if (childpid = fork()) break;
if (childpid == -1) {
perror (" The fork failed ");
exit(1);
}
printf(" %d: process ID:%6ld parent ID:%6ld child
ID:%6ld",i, (long)getpid(), (long)getppid(), (long)childpid);
seed = (int)(getpid() + childpid);
8
srand(seed);
/* since each process has a different childpid, using the childpid
as the seed number will restart the random function.
Therefore, each process will have a different sleeptime
*/
sleeptime = rand()%m;
printf (" sleep = %d ", sleeptime);
sleep(sleeptime);
exit(0);
}
Explanation / Answer
#include <stdio.h>
void buildTree(int depth)
{
int i = 0;
int pid, status;
int total = 2; //total number of processes that need to be created
int data = 0;
if(depth > 1)
{
//performs pow with ints
//2^depth to get the total number of processes in the tree
for(i = 0; i < depth; i++ )
{
total = total*2;
}
total--; //subtract 1 from the total because the top level only has 1 process
for(i = 0; i < total; i++)
{
pid = fork();
if(pid < 0)
{
//Error
printf("Fork failed ");
exit(1);
}
else if(pid == 0)
{
//Child
srand(getpid());
data = 1 + rand()%5; //get a random value to store in process
//print the child's information
printf("My pid is %d, my parent pid is %d, my data value is %d ", getpid(), getppid(), data);
if(depth > 0) //another level of the tree needs to be created
buildTree(--depth);
else //no more levels need to be created so prepare to exit
printf("My pid is %d and my exit value is %d. ", getpid(), data);
exit(data); //exit and return the data value currently stored
return;
}
else
{
//Parent
wait(&status); //wait for child and store returned value in status
data = data + status; //add child's data value to parent's
printf("My pid is %d and my exit value is %d. ", getpid(), data); //parent is exiting, so print its information
}
}
}
else
total = 1; //there is only 1 process
}
int main(int argc, char *argv[])
{
if(argc != 2)
printf("Usage: <program> <integer> ");
else
{
int depth = atoi(argv[1]);
buildTree(depth);
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.