The Gateway to Computer Science Excellence
+2 votes
646 views
#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 Veteran (117k points) | 646 views
+1
1 parent, 1 child.
+1

total no of child process =2^n-1 

here n=1 , then no of child process is 2^1-1=1

total no of parent process=1

when child process is created then it return process id to the parent process .here fork system coll create one child process and it return process id of parent

0
how do u know n=1??
+1
@sresta no. of fork() is 1 so n=1
+1
Only 1 parent and 1 child process and only child process will be able to execute the code inside the if block.

Am I correct?
+1
Yes @Ayush, fork() returns zero to the child process and process id of the child to the parent and hence non-zero.
0
@Ayush
yes, that is general concept
but here child get a value 5 more than parent, then parent also must have to be some value
right?
So, why parent wouldnot be able to execute the code?
0
@Sreshtha-Parent would be getting the processid in return from fork() and this js not 0 so parent will not execute code inside if block.

And I want to ask one thing

In case of copy on write separate page will be allocated to child process right? And virtual to physical mapping would be different for child and parent?
0
why do u think mapping would be different for parent and child?
+2
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.
0
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?

1 Answer

+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;
}
by Boss (18.3k points)
0
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
@srestha di, for point2 both parent and child receive the same set of data from the parent, so both will get a=100 as before fork(), the parent process contained the variable a initialized to 100, but after fork(),child got to execute if part so a=105 for it but parent retained the value of a=100.
0
1) only a process having PID=0 can execute, line inside if statement.

2) Because a=100 is initialized when fork system call is executed both parent and child will have a copy of variable a=100 and both can independently,

3) parent process is the program you run like factorial.c or prime.c when fork() is called entire program is copied to the child process and then suppose if the fork() was at line 6 then both child and parent will try to execute from 6 onwards , that is also reason for your 2nd question since parent and child both have exact copy of our program and from that line onward only child was allowed to execute. so in the end when we print the value of "a" parent has a=100 in its copy but the child was able to modify it as a=105.
0
then what is difference in calling main() and call fork()?
0
main() is a function and fork() is a system call

main is executed by OS when a user runs program and fork() is executed whenever it is called.

One cannot call any function or system call without main, main has multiple return type while fork() don't.

rest every difference between a function and a system call
0
@tesla memory  address would be different
0
physical memory adress would be different
0
so using pointer which  address you  find  ? virtual or physical
0
@Tesla

fork will create ptable and then create EMBRYO

these thing create a new process directory

main() will not create ptable

that is why main() will not create new process , I think

@rio

physical address cannot be pointed by pointer, only virtual address we can point
0

@Tesla

still we cannot tell parent and child value couldnot be 95 and 100

See this question

https://gateoverflow.in/765/gate2005-72

here parent and child value also could be same

like parent =100 and child=100

but in code we never take parent and child value same

0

https://gateoverflow.in/765/gate2005-72

Both parent and child are getting executed so we cannot compare it with our question

Yes main does not create a new process, OS does that( as per my knowledge)

0

@srestha,

for this question answer must be D

0
@Shaik

have u got the option?

I forgot it now.

Can u explain how u got the answer?
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

Quick search syntax
tags tag:apple
author user:martin
title title:apple
content content:apple
exclude -tag:apple
force match +apple
views views:100
score score:10
answers answers:2
is accepted isaccepted:true
is closed isclosed:true
50,647 questions
56,461 answers
195,358 comments
100,241 users