Question

In: Computer Science

int main(){    int i = 3;    if(fork()){    i++;    fork();    i+=3;   ...

int main(){
   int i = 3;

   if(fork()){
   i++;
   fork();
   i+=3;
   }else{
   i+=4;
   fork();
   i+=5;
   }
   printf("%d\n",i);
}

how many processes does this code create including the parent

Solutions

Expert Solution

Lets see the code step by step and understand how many process are created including the parent.

Giving each fork() function a name as shown below so that we can understand easily :

int main(){
   int i = 3;

   if(fork()){ //fork1()
   i++;
   fork(); //fork2()
   i+=3;
   }else{
   i+=4;
   fork(); //fork3()
   i+=5;
   }
   printf("%d\n",i);
}

While execution, when compiler will reach fork1(), a child process (say C1) of the parent process (say P1) will be created. The child process C1 will return 0 and the parent process P1 will return a positive integer. Hence if part will be executed by the parent process and else part by C1. First lets understand the if part by the parent process.

Parent process P1 will first increment the value of i and then while execution, when compiler will reach fork2(), again child process (say C2) of the parent process P1 will be created. Then both P1 and C2 will increment the value of i by 3.

Now lets understand the else part by the child process C1.

In else part, first C1 will increment the value of i by 4 and when compiler will reach fork3(), a child process (say C3) of the child process C1 (which is now acting as a parent) will be created. Then both C1 and C3 will increment the value of i by 5. And in this way, total 4 print() statements will execute.

So, Total number of processes created by the code including parent = 4.

If you have any doubt regarding the solution then let me know in comment. If it helps, kindly give an upVote to this answer.


Related Solutions

what is the output? int main ( ) { int a = 3, b= 2, c=...
what is the output? int main ( ) { int a = 3, b= 2, c= 1, d, e, f, g; d = a&b;    e = a | c; f = a >> 1, g = a << 1; cout << “d= “ << d << “ e = “ << e << “ f = “ << f << “g = “ << g << endl; }
#include <stdio.h> #include <math.h> int fun(int); int main(void)    {     int i = 5, x...
#include <stdio.h> #include <math.h> int fun(int); int main(void)    {     int i = 5, x = 3;     i = fun(x);     printf("%d\n", i);     return 0; } int fun(int i) {      int res = 0;      res = pow (i , 3.0);      return ( res); }
Please comment, exactly how this program works? #include <stdio.h> int main() { int i,j; int a[1000];...
Please comment, exactly how this program works? #include <stdio.h> int main() { int i,j; int a[1000]; for(i=0;i<1000;i++) a[i]=1;    for(i=2;i<1000;i++) { if(a[i]==1){ for(j=i+1;j<1000;j++) {if(j%(i)==0) a[j]=0; } } } printf("print numbers are:\n"); for(i=2;i<=1000;i++) if(a[i]==1) printf("%d, ",i); }
#include #include #include int main(void) { int feof(FILE *stdin); int i, num; int binary[10]; char input[10];...
#include #include #include int main(void) { int feof(FILE *stdin); int i, num; int binary[10]; char input[10]; printf("Starting the CPSC 1011 Decimal to Binary Converter!\n"); while(1) {    i=0;    printf("\nPlease enter a positive whole number (or EOF to quit): ");    scanf("%s", input); // user inputs value as a string for separate values    if(strcmp(input,"")==0) {        printf("\n\tThank you for using the CPSC 1011 Decimal to Binary Generator.\nGoodbye!\n\n");    return(0); } num=atoi(input); if (num<=0) {    printf("\n\tSorry, that was...
#include <stdio.h> int main() { int i, j; printf("Enter two positive integers: "); scanf("%d%d", &i, &j);...
#include <stdio.h> int main() { int i, j; printf("Enter two positive integers: "); scanf("%d%d", &i, &j); // need to validate both being positive while (i != j) { i > j ? (i -= j) : (j -= i); } printf("GCD: %d\n", i); return 0; } The above code returns a GCD from two numbers entered. Use the above program to design a C program to compute of integer coefficients a and b such that gcd(i, j) = a xi...
int get_value(int array[], unsigned int index) { ????? } int main() { int data[] = {1,...
int get_value(int array[], unsigned int index) { ????? } int main() { int data[] = {1, 2, 3, 4}; get_value(data, 2) = 100; } Write ????? that returns the value in the array at index: Question Blank type your answer...
#include <stdio.h> #include <stdlib.h> // required for atoi int main(void) {     int i=0,n,num,filenum[100],pos;     int...
#include <stdio.h> #include <stdlib.h> // required for atoi int main(void) {     int i=0,n,num,filenum[100],pos;     int c;    char line[100]; //declaring string for storing data in the line of text    FILE *fp; // declaring a FILE pointer    fp=fopen("numbers.txt","r"); // open a text file for reading    while(fgets(line, sizeof line, fp)!=NULL) {       // looping until end of the file         filenum[i]=atoi(line); //converting data in the line to integer and storing it into array        i++;    }...
#include <iostream> using namespace std; int main() { int even=0,odd=0,sum=0,sum2=0,largest,smallest; int num,i; for ( i=1; i<=10;...
#include <iostream> using namespace std; int main() { int even=0,odd=0,sum=0,sum2=0,largest,smallest; int num,i; for ( i=1; i<=10; i++){ cout << " Enter " << i << " number: "; cin >> num; if ( num%2==0){ even++; sum+=num; } else { odd++; sum2+=num; if(num>largest){ largest = num; } if(num<largest) { smallest = num; } } cout << " The sum of even number is : " << sum << endl; cout << " The total-count of even number is : " <<...
Add File I/O to the voting program below #include<iostream> using namespace std; int main() {int choice;...
Add File I/O to the voting program below #include<iostream> using namespace std; int main() {int choice; int biden = 0 , trump = 0 , bugs = 0 ; int vc = 0 ; do { cout<<"\n\n\nEVOTE\n-----" <<"\n1.Joe Biden" <<"\n2.Donald Trump" <<"\n3.Bugs Bunny" // 4. Print current tally [hidden admin option] // 5. Print audit trail [hidden admin option] // 6. mess with the vote [hidden hacker option] E.C. // 7. END THE ELECTION <<"\n\n Your selection? "; cin >>...
import java.util.*; class A { int i, j, k; public A(int i, int j, int k)...
import java.util.*; class A { int i, j, k; public A(int i, int j, int k) { this.i=i; this.j=j; this.k=k; } public String toString() { return "A("+i+","+j+","+k+")"; } } class Main { public static void main(String[] args) { ArrayList<A> aL=new ArrayList<A>(); Random rand= new Random(1000); //1000 is a seed value for (int p=0; p<10; p++) { int i = rand.nextInt(100); int j = rand.nextInt(200); int k = rand.nextInt(300); aL.add(new A(i, j, k)); } System.out.println("----- Original arraylist------"); for (A a: aL)...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT