in Operating System
1,917 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?

in Operating System
by
1.9k views

4 Comments

why do u think mapping would be different for parent and child?
0
0
Because the child had tried to modify the data. And so, in the process, it will be allocated a new physical page frame to hold the newly modified data which will be different from the parent's value.
3
3
There are still some question need to answer----------

1)Only nonzero value could executes next line of codes, right?

where fork is 0, next line couldnot execute

2)Moreover, how do u say parent has value 100 and child has value 105?

the difference between them is 5

means parent could be 95 too and child could be 100 then. right?

3)main() is one system call , and fork() is a system call inside it

So, is there no parent and child process will create for main() too?
0
0

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

4 Comments

@srestha,

for this question answer must be D

0
0
@Shaik

have u got the option?

I forgot it now.

Can u explain how u got the answer?
0
0

there is copy on write, therefore child process values stored at somewhere and parent process values stored at somewhere

therefore v!=y.

 

let the a value initially some c

fork()==0 ===> child process.a = c+5

else ===> parent process.a = c-5

we can conclude that parent.a + 10 = child.a

they given that parent.a represent by u and child.a represent by x

u+10=x

0
0

Related questions