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);
}

what is the output of the code

Solutions

Expert Solution

OUTPUT:

7 7 12 12

NOTE: The order of the numbers in output changes randomly after every execution, because the compiler is executing all the processes in the same execution space.

EXPLANATION:

When the function starts there's 1 process, this process has a int i= 3.

Now, when fork() executes, we have two processes. They both have the same execution space, they both are going to run the same code (to a point), but the child will get its own copy of i.

Now, when fork() will return a 0 to the child process. the rest of the function according to child's prospective is:

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

Now the value of i is updated to 7.

Again the fork() function spilts the child process into two further subprocesses.Both processes recieves the updated value of i(i.e 7). Now both the process runs seperately in the same execution space. After i+=5 statement, both the processes prints the value( i.e 12 12)

The parent process on the other hand, will get a valid value back from the fork() command, thus it executes the line of if statement.

if(fork()){
   i++;
   fork();
   i+=3;
   }

printf("%d\n",i);

Now the value of i is updated to 4.

Again the fork() function spilts the child process into two further subprocesses.Both processes recieves the updated value of i(i.e 4). Now both the process runs seperately in the same execution space. After i+=3 statement, both the processes prints the value( i.e 7 7)


Related Solutions

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
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...
Implement a generic queue.  Use the code below for main(). main() { int i, x; float y;...
Implement a generic queue.  Use the code below for main(). main() { int i, x; float y; char z; Queue<int> A; Queue<float> B; Queue<char> C; ifstream in; in.open("int.txt"); for (i = 0; i < 100; i++){         in >> x;         A.enqueue(x);     } cout << A.dequeue() << endl;; for (i = 0; i < 12; i++)         A.enqueue(i); cout << A.dequeue() << endl; cout << A.dequeue() << endl; cout << "Dequeueing: "<< A.dequeue()<< endl; while (!A.isEmpty())      cout  << A.dequeue() << "  "; if (A.isEmpty())       cout <<...
#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 : " <<...
#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++;    }...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT