A small question about fork()


 
Thread Tools Search this Thread
Top Forums Programming A small question about fork()
# 1  
Old 08-13-2008
A small question about fork()

Hello experts,
I am using fork() in my code but I am confused which output comes first child or parent?
I did the following code .My book shows parent first but my linux shows child first.Can anyone tell me why?
Code:
#include <stdio.h>
int main(){
int pid;
printf("I am original process with pid is %d ,My parent (Terminal) pid is %d\n",getpid(),getppid());
pid=fork();
if(pid>0)//parent starts
    {
    printf("I am parent,my pid is %d and my parent pid is %d\n",getpid(),getppid());
    }
else 
    {
    printf("I am child,my pid is %d and my parent pid is %d\n",getpid(),getppid());
    }
//both parent and child executes the next printf
printf("PID %d terminates \n",getpid());    
}

output:
Code:
mlhazan@dEBx:~/Desktop$ gcc myFork.c 
mlhazan@dEBx:~/Desktop$ ./a.out 
I am original process with pid is 13426 ,My parent (Terminal) pid is 28555
I am child,my pid is 13427 and my parent pid is 13426
PID 13427 terminates 
I am parent,my pid is 13426 and my parent pid is 28555
PID 13426 terminates 
mlhazan@dEBx:~/Desktop$

# 2  
Old 08-13-2008
By definition, you cannot know whether the child or the parent prints first. Your book should explain this.
# 3  
Old 08-15-2008
With just fork, its not guaranteed

But if you want to control the order of execution.

Try vfork - where child is guaranteed to execute first.

else with fork - try using synchronization mechanism to schedule parent or child to run first
# 4  
Old 08-15-2008
Don't try vfork. Expecting it to sequence the processes is a famous bug. See The Single UNIX ® Specification definition of vfork which says:
Quote:
The vfork() function has the same effect as fork(), except that the behaviour is undefined if the process created by vfork() either modifies any data other than a variable of type pid_t used to store the return value from vfork(), or returns from the function in which vfork() was called, or calls any other function before successfully calling _exit() or one of the exec family of functions.
and...
Quote:
On some systems, vfork() is the same as fork().
# 5  
Old 08-17-2008
Ahh, I didn't know about that. This is interesting. Smilie

Thank you very much Per.
# 6  
Old 08-17-2008
If you design your application correctly it shouldn't matter which comes first. When you want something to come before child's code just put it before calling fork(). If you want the parent to stop while child is executing use wait(). Finally if you want to do synchronized stuff with child and parent use IPC.

Anyway this totally depends on the implementation of fork().
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

Ok i have a small assembly question

I have this small program that runs with the flat assembler. My problem is that at the receive line function it receives the line and if there isn't a $ typed at the end of the user input the program displays a lot of strange stuff, sometimes beeps and then it seems to terminate without causing any... (13 Replies)
Discussion started by: Errigour
13 Replies

2. Shell Programming and Scripting

Small fast question

just to confirm du from sh show sizes as multiples of 512 byte right? (4 Replies)
Discussion started by: Nick1097
4 Replies

3. Programming

question about fork

i'm experimenting fork function and i found this code #include <stdio.h> #include <sys/stat.h> #include <sys/types.h> #include <wait.h> #include <fcntl.h> #include <unistd.h> int main(void) { int fd; pid_t p; p = fork(); fork(); if (p>0) { fork();} fork(); fork();... (6 Replies)
Discussion started by: blob84
6 Replies

4. Programming

multiple fork() question

I writing a program that forks three times but only on the parent process. The three children processes then produces output in order. 1, 2, 3. I am confused on how to do this. I have tried multiple if and else if statements but the output does not come out right. How should I go about doing this? (1 Reply)
Discussion started by: TWhitt24
1 Replies

5. Programming

Beginners question about fork

Hi everyone: I'm developing a dynamic library for notifications, this library is used for a daemon that i've programmed, when something goes wrong the library should send an email to an administrator, but since sending an email is a non-vital process then it can fail (it should work as an... (4 Replies)
Discussion started by: edgarvm
4 Replies

6. UNIX for Dummies Questions & Answers

Small question regarding SSH

I am looking for some model like this: My Computer ------------- Intermediate Server (IS) ------------- Own Server I must be able to ssh into the Intermediate Internet Server which is generally an online version of SSH service through which I will connect to Own Server. I was the IS to... (2 Replies)
Discussion started by: Legend986
2 Replies

7. Shell Programming and Scripting

A small minix question

First af all hi. i want to create a batch script which inform when users log in last time on system or if they are online when they logged in. I want ot use a file .users which has the usernames of users. i want to print for example peter is ONLINE: Logged in on Wed Feb 11 07:47 alex... (2 Replies)
Discussion started by: sasa
2 Replies

8. Shell Programming and Scripting

small question regarding substr()

Hello.. I am doing some awk-ing and among all I use substr inside it.. I have: ....substr($0,60,37) meaning as U all know take from 37 char. from point 60.. can I put it like this substr($0,60,end of line) meaning take it from point 60 and take all characketrs in that line until line... (2 Replies)
Discussion started by: amon
2 Replies

9. Shell Programming and Scripting

small question

Hi there, I found the following script on the net, i like to use it as a standard template for new scripts. But i do not understand the meaning of the last line, can anybody explain what going on on the last line vflag=off filename= while getopts vf: opt do case "$opt" in v)... (9 Replies)
Discussion started by: janr
9 Replies

10. Programming

simple fork question

When executing this simple program: #include <unistd.h> void main() { int f; printf("\n Parent procces ID=%d\n",getpid()); f=fork(); if(f==0) { printf("\n Child process ID=%d father=%d\n",getpid(),getppid()); } ... (2 Replies)
Discussion started by: bb666
2 Replies
Login or Register to Ask a Question