[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

fork() function in unix

Posted on 2007-12-04
18
Medium Priority
?
2,657 Views
Last Modified: 2008-02-01
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
0
Comment
Question by:dminh01
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 10
  • 7
18 Comments
 
LVL 84

Expert Comment

by:ozo
ID: 20409225
I disagree
0
 

Author Comment

by:dminh01
ID: 20409249
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
 
LVL 84

Expert Comment

by:ozo
ID: 20409251
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Author Comment

by:dminh01
ID: 20409258
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
 
LVL 84

Expert Comment

by:ozo
ID: 20409264
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
 

Author Comment

by:dminh01
ID: 20409287
thanks but so does the fork has to return twice when called?
0
 
LVL 84

Accepted Solution

by:
ozo earned 2000 total points
ID: 20409290
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
 

Author Comment

by:dminh01
ID: 20409292
also in process can something be called twice?
thanks
0
 
LVL 84

Expert Comment

by:ozo
ID: 20409293
fork returns twice, once in the parent and once in the child, unless it fails
0
 
LVL 84

Expert Comment

by:ozo
ID: 20409321
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
 

Author Comment

by:dminh01
ID: 20409335
look like C will be a good choice
102120
0
 

Author Comment

by:dminh01
ID: 20409344
I think I get it C and E
Thanks for your help. So fork alwasy return value of parent first then child
0
 
LVL 84

Expert Comment

by:ozo
ID: 20409351
It is true that 102120 is a possible output
0
 
LVL 84

Expert Comment

by:ozo
ID: 20409357
either the child or the parent can run first.  they can run at the same time or go back and forth
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 20409940
>> I think I get it C and E

Are you sure that's all ?
0
 
LVL 84

Expert Comment

by:ozo
ID: 20413217
That's not all
0
 

Author Comment

by:dminh01
ID: 20413334
also B
0
 
LVL 84

Expert Comment

by:ozo
ID: 20413439
How to you get 2 first?
0

Featured Post

What’s Wrong with Your Cloud Strategy ?

Even as many CIOs are embracing a cloud-first strategy, the reality is that moving to the cloud is a lengthy process and the end-state is likely to be a blend of multiple clouds—public and private. Learn why multicloud solutions matter in this webinar by Nimble Storage.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.
The viewer will learn how to implement Singleton Design Pattern in Java.
Suggested Courses

650 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question