2,457 views
2 votes
2 votes
#include<stdio.h>
#include<stdlib.h>
void main()
{
    int a =100;
    if(fork()==0)
    {
     a=a+5;
     printf("%d %d \n",a,&a );
    }
}

How many parent and child process will be created?

1 Answer

4 votes
4 votes

if(fork()==0)

before execution of if there was only one process P1 in the system as soon as if statement is executed a new separate process will be created and it will be a child of p1.

As per fork system call definition on the successful execution of fork system call, 2 process Id is returned, for parent return code is non zero positive number for a child it is zero.

so for parent positive code is returned thus it will not execute code inside if

for a child, it will return zero allowing the child process to get inside if statement.

Now regarding virtual to physical mapping both

child and parent will have the same virtual address of the variable a, but their physical address will be different.

so after execution of if statement parent will have a value of "a" as 100 while the child will have 105 and if you print the address of memory location then both parent and child will print same memory location.

Just try and run this code 

#include<stdio.h>
#include<stdlib.h>
int main(){
	int a=100;
	if(fork()==0)
		a=a+5;
	printf("%d %x\n",a,&a);
	return 0;
}

Related questions

2 votes
2 votes
3 answers
1
Philosophical_Virus asked Dec 10, 2023
766 views
Int main (){fork();printf("a");fork();printf("b");return 0;}How many distinct outputs are possible of above code? And also give outputs
0 votes
0 votes
1 answer
2
Erwin Smith asked Apr 11, 2023
755 views
void main() { int n = 1; if(fork()==0) { n = n<<1; printf(“%d, “, n); n = n <<1; } if(fork()==0) n=n+700; printf(“%d, “,n); }Which of the following output is not ...
0 votes
0 votes
2 answers
4
dd asked Sep 15, 2018
1,029 views
The fork system call creates new entries in the open file table for the newly created child process. [True / False][ what is open file table ? ]