Question
#include #include #include #include #include void leaf(){ char filename[20]; sprintf(filename,%d.txt,/**/); FILE *fp=fopen(filename,w); srand(getpid()); fprintf(fp, %d ,(rand()%100) ); fclose(fp); } void parent(int pid1,int pid2){ //some code
#include
void leaf(){ char filename[20]; sprintf(filename,"%d.txt",/**/); FILE *fp=fopen(filename,"w"); srand(getpid()); fprintf(fp, "%d ",(rand()%100) ); fclose(fp); } void parent(int pid1,int pid2){ //some code will add... char filename1[20]; sprintf(filename1,"%d.txt",/**/); char filename2[20]; sprintf(filename2,"%d.txt",/**/); char filename3[20]; sprintf(filename3,"%d.txt",/**/); FILE *fp1,*fp2,*fp3; fp1=fopen(filename1,"r"); fp2=fopen(filename2,"r"); int left,right; fscanf(fp1,"%d",&left); fscanf(fp2,"%d",&right); fclose(fp1); fclose(fp2); fp3=fopen(filename3,"w"); fprintf(fp3, "%d",left+right); fclose(fp3); } void tree_creation(int height){
// }
int main(){ tree_creation(3); }
1) Briefly read the file operations in C from any tutorial
2) Read the random number generation in C from any tutorial
3) Create a full binary process tree with height = 3.
4) Each of the processes in the tree will create a text file with their own process id.
5) Each of the leaf processes will write a random number into the text file.
6) Each parent will sum the random number from children and write it into its text file.
7) Finally, the root process will print out the sum of all random numbers generated by the leaf nodes.
Hint: You can get the pid of running process via getpid function. You can read the man page of function in linux.
So, you have 3 types of processes: Leaf Process: creates a random number and writes it into a file named with its own process id
Root Process: Sums the values written by its child processes and writes summation to the screen. All other processes: sums the values written by child process and write the summation into a file named with its own p
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started