fork() function in unix

hi there
I have a homework that has a class like this
#include csapp.h
void end(void){
printf("2");
}
int main(){
if(Fork()==0)
atexit(end);
}
if(Fork==0)
printf("0");
else
printf("1");
exit(0);
}


the question is to determine which of the input below are possible. The atexit funtiontak apointer to a function and add it to a list of functions(initial empty) that will be called when the exit() is called

A. 112002
B. 211020
C. 102120
D. 122001
E. 100212

In my opinion
D is the correct answer since 1 can be called then 2 2 since fork can be at 0 0 then 00 1 but I am not very sure and wanted to ask you guys

Thanks
dminh01Asked:
Who is Participating?
 
ozoConnect With a Mentor Commented:
How many different processes are involved?
What is the output from each process?
What are possible outputs of
void end(void){
  printf("(%d)2",getpid());
}
int main(){
  if(fork()==0)
    atexit(end);

if(fork()==0)
     printf("(%d)0",getpid());
     else
     printf("(%d)1",getpid());
exit(0);
}
0
 
ozoCommented:
I disagree
0
 
dminh01Author Commented:
can you guide me a bit? as I understand whenever we call Fork() we will get 2 return: 1 in the parent and one in child. Since atexit(end) will add the print "2" everytime exit() is called so the possible is if FOrk==0 then it will add 22 firstly or 00 else 11. Since the only choice I see is D which has 2200 together it will make sense to me

thanks for your explanation
0
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

 
ozoCommented:
Assuming you meant
void end(void){
  printf("2");
}
int main(){
  if(fork()==0)
    atexit(end);
if(fork()==0)
     printf("0");
     else
     printf("1");
exit(0);
}
there are more than one correct answers
0
 
dminh01Author Commented:
well but can u explain how to find that value. I am new to this and still not quite get it how it is working. does fork() return twice as I mentioned?

thanks
0
 
ozoCommented:
In each process, some things are always called before exit,
but something in one process can run before or after something in a different processes
0
 
dminh01Author Commented:
thanks but so does the fork has to return twice when called?
0
 
dminh01Author Commented:
also in process can something be called twice?
thanks
0
 
ozoCommented:
fork returns twice, once in the parent and once in the child, unless it fails
0
 
ozoCommented:
What would the possible outputs be if fork did not return twice?
for example if you had had
#define fork() (0)
or
#define fork() (1)
or
#define fork() (rand())
0
 
dminh01Author Commented:
look like C will be a good choice
102120
0
 
dminh01Author Commented:
I think I get it C and E
Thanks for your help. So fork alwasy return value of parent first then child
0
 
ozoCommented:
It is true that 102120 is a possible output
0
 
ozoCommented:
either the child or the parent can run first.  they can run at the same time or go back and forth
0
 
Infinity08Commented:
>> I think I get it C and E

Are you sure that's all ?
0
 
ozoCommented:
That's not all
0
 
dminh01Author Commented:
also B
0
 
ozoCommented:
How to you get 2 first?
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.