What is the exact output of the following code Solutioninclu
Solution
#include<bits/stdc++.h>
#include<unistd.h>
using namespace std;
int main(int argc, char const *argv[])
{
int i,j;
i=fork();
for (j = 0; j <3; ++j)
{
if(i==0&& j==0)
{
sleep(3);
cout<<\"Cats\ \";
}
else if(i==0)
{
sleep(2);
cout<<\"Dogs\ \";
}
else
{
sleep(2);
cout<<\"Raining\ \";
}
}
return 0;
}
Output:
Raining
Cats
Raining
Dogs
Raining
=================================================================
Explanation:when we call fork there are 2 proceess runing child and parent.In for loop,parent process will first run and print \"Raining\" at the same time child process in background will run at j==0 and i==0 because fork returns 0 for child process hence prints \"Cats\".In next iteration,parent will run and print \"Raining\" but at this time j!=0 but i==0 in background child process hence it will print \"Dogs\",In last iteration,it will print \"Raining \" only becuase it is only condition that will be satisfied
