dminh01
asked on
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
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
I disagree
ASKER
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
thanks for your explanation
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
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
ASKER
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
thanks
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
but something in one process can run before or after something in a different processes
ASKER
thanks but so does the fork has to return twice when called?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
also in process can something be called twice?
thanks
thanks
fork returns twice, once in the parent and once in the child, unless it fails
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())
for example if you had had
#define fork() (0)
or
#define fork() (1)
or
#define fork() (rand())
ASKER
look like C will be a good choice
102120
102120
ASKER
I think I get it C and E
Thanks for your help. So fork alwasy return value of parent first then child
Thanks for your help. So fork alwasy return value of parent first then child
It is true that 102120 is a possible output
either the child or the parent can run first. they can run at the same time or go back and forth
>> I think I get it C and E
Are you sure that's all ?
Are you sure that's all ?
That's not all
ASKER
also B
How to you get 2 first?