By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
431,745 Members | 1,873 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 431,745 IT Pros & Developers. It's quick & easy.

fork function working procedure

100+
P: 1,059
Expand|Select|Wrap|Line Numbers
  1. /*name of the built program is dmns*/
  2. int main()
  3. {
  4.  int i, j;
  5.  i=fork();
  6.  if(i<0)
  7.   return 1;
  8.  printf("created pid=%d",i);
  9.  
  10.  while(1) {i=i;} //unlimited loop
  11.  
  12. }
  13.  
This is a useless program for test only. You see in this program after successfully generated fork i didn't exit from main only for test purpose.

after running the program on another console I check the process running.
Expand|Select|Wrap|Line Numbers
  1. #ps -ef|grep dmns
  2. root  20117 20065 44 23:44 pts/1 00:00:03 ./dmns
  3. root  20118 20117 44 23:44 pts/1 00:00:03 ./dmns
  4.  
Now what happen if I kill process:
If I kill the process 20118 then the ps -ef result is
Expand|Select|Wrap|Line Numbers
  1. #ps -ef|grep dmns
  2. root  20117 20065 44 23:44 pts/1 00:00:03 ./dmns
  3. root  20118 20117 44 23:44 pts/1 00:00:03 [dmns] <defunct>
  4.  
But if I kill 20117 instead 20118 ps result is different.
Expand|Select|Wrap|Line Numbers
  1. #ps -ef|grep dmns
  2. root  20118 20117 44 23:44 pts/1 00:00:03 ./dmns
  3.  
Now My question is how the whole process is going on. What happens after we create fork? how after creating fork only main program get the exit function under condition why the sub function don't get it? I mean take a look below program:
Expand|Select|Wrap|Line Numbers
  1. /*name of the built program is dmns*/
  2. int main()
  3. {
  4.  int i, j;
  5.  i=fork();
  6.  if(i<0)
  7.   exit(1);
  8.  
  9.  if(i>0)
  10.   exit(0);
  11.  ...
  12. }
  13.  
Where fork start to work and how?
Sep 21 '10 #1

✓ answered by ashitpro

I guess you are making me confuse.
earlier your code in question is:
Expand|Select|Wrap|Line Numbers
  1. if(i<0)
  2.   exit(1);
  3.  
Now you are saying its:
Expand|Select|Wrap|Line Numbers
  1. if(i<1)
  2.   exit(1);
  3.  
I am considering first case.
Lets consider whole code again:
Expand|Select|Wrap|Line Numbers
  1. /*name of the built program is dmns*/
  2. int main()
  3. {
  4.  int i, j;
  5.  i=fork();
  6.  if(i<0)
  7.   exit(1);
  8.  
  9.  if(i>0)
  10.   exit(0);
  11.  ...
  12. }
  13.  
At line 5 fork() gets executed.
Here, it will create the new process in memory i.e. brand new address space. All code will be shared by both the processes.

It will return some positive integer as pid (assigned by system to new process). It will get stored in i.
Remember i will receive positive number in parent process.

Now with respect to further code flow.
Condition at line 6 will get fail, as i is positive integer and greater than 0.

Condition at line will return true and exit the process.
Here, parent process terminates.

At very simultaneously (since fork executed) child process will be executing. It will start executing instruction from line 5. So it will again execute fork. Here fork will return 0. Conditions at line 6 and 9 both will get failed.

You should have written a condition for i==0 and put code which will be executed by child.

In this case as fork is executed successfully, the condition at line 6 will never come true in parent as well as child.

Share this Question
Share on Google+
4 Replies


ashitpro
Expert 100+
P: 542
your basic structure is partially right.

fork returns twice.
it will return 0 in case of child process and new pid in case of parent process.

your code should be:
Expand|Select|Wrap|Line Numbers
  1.  /*name of the built program is dmns*/
  2.  int main()
  3.  {
  4.   int i, j;
  5.   i=fork();
  6.  
  7.   if(i>0)
  8.    //parent code
  9.   else if(i == 0)
  10.    //Child code
  11.   else
  12.    //error while fork
  13.    ...
  14.  }
  15.  
Other important thing is, you should always query the exit status of child process using any of the wait family system call.

If parent dies or exits without waiting for child to exit, child becomes defunct process that is zombie and the resources allocated to child process won't be taken back until next restart.

This is what happened when you kill the parent.
Sep 21 '10 #2

100+
P: 1,059
@ashitpro
My confusion lies somewhere else.

My Question is where does fork() start to work?

why it dont get the line
Expand|Select|Wrap|Line Numbers
  1.  if(i<1)
  2.    exit(1);
  3.  
how come fork miss that line?
Sep 22 '10 #3

ashitpro
Expert 100+
P: 542
I guess you are making me confuse.
earlier your code in question is:
Expand|Select|Wrap|Line Numbers
  1. if(i<0)
  2.   exit(1);
  3.  
Now you are saying its:
Expand|Select|Wrap|Line Numbers
  1. if(i<1)
  2.   exit(1);
  3.  
I am considering first case.
Lets consider whole code again:
Expand|Select|Wrap|Line Numbers
  1. /*name of the built program is dmns*/
  2. int main()
  3. {
  4.  int i, j;
  5.  i=fork();
  6.  if(i<0)
  7.   exit(1);
  8.  
  9.  if(i>0)
  10.   exit(0);
  11.  ...
  12. }
  13.  
At line 5 fork() gets executed.
Here, it will create the new process in memory i.e. brand new address space. All code will be shared by both the processes.

It will return some positive integer as pid (assigned by system to new process). It will get stored in i.
Remember i will receive positive number in parent process.

Now with respect to further code flow.
Condition at line 6 will get fail, as i is positive integer and greater than 0.

Condition at line will return true and exit the process.
Here, parent process terminates.

At very simultaneously (since fork executed) child process will be executing. It will start executing instruction from line 5. So it will again execute fork. Here fork will return 0. Conditions at line 6 and 9 both will get failed.

You should have written a condition for i==0 and put code which will be executed by child.

In this case as fork is executed successfully, the condition at line 6 will never come true in parent as well as child.
Sep 22 '10 #4

100+
P: 1,059
Expand|Select|Wrap|Line Numbers
  1. if(i>1)
was a typing mistake.

Your explanation is clear
Sep 22 '10 #5

Post your reply

Sign in to post your reply or Sign up for a free account.