Thread: A small question about fork( )

  1. #1
    Registered User
    Join Date
    Aug 2008
    Posts
    2

    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:
    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. #2
    Registered User
    Join Date
    Sep 2007
    Posts
    1,012
    Either way is fine. Both processes are running simultaneously (or so it appears) and thus there's no guarantee which will complete first. It's essentially a roll of the die when both processes are doing the same thing. For example, on my system, I've gotten both the parent printing first and the child printing first.

  3. #3
    Registered User valaris's Avatar
    Join Date
    Jun 2008
    Location
    RING 0
    Posts
    507
    You can wait for the child to finish with waitpid().

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Like the others say When you fork a process, it is entirely arbitrary whether the current (forking) process or the new (forked) process is the one to run when the kernel call to fork is finished. The choice is made by the scheduler [that is the function in the OS kernel that selects which process/thread [[or processes/threads if the system has multiple processors]] to run at the moment], and schedulers by definition are quite complex, so trying to understand what happens in a Linux/Unix or Windows scheduler is non-trivial.

    A closely related concept is described here:
    http://www.flounder.com/badprogram.h...%20immediately
    Although that is about threads in Windows, the reasons for why Windows does/doesn't start the new thread is also in the scheduler.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    Aug 2008
    Posts
    2

    Smile

    Quote Originally Posted by matsp View Post
    Like the others say When you fork a process, it is entirely arbitrary whether the current (forking) process or the new (forked) process is the one to run when the kernel call to fork is finished. The choice is made by the scheduler [that is the function in the OS kernel that selects which process/thread [[or processes/threads if the system has multiple processors]] to run at the moment], and schedulers by definition are quite complex, so trying to understand what happens in a Linux/Unix or Windows scheduler is non-trivial.

    A closely related concept is described here:
    http://www.flounder.com/badprogram.h...%20immediately
    Although that is about threads in Windows, the reasons for why Windows does/doesn't start the new thread is also in the scheduler.

    --
    Mats
    Thanks to everyone .I got my answer

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. small question
    By burninfrost296 in forum C Programming
    Replies: 2
    Last Post: 03-06-2009, 05:27 PM
  2. Small question about gets
    By Ash1981 in forum C Programming
    Replies: 4
    Last Post: 01-03-2006, 07:36 AM
  3. New to C, small compiler question
    By Ash1981 in forum C Programming
    Replies: 14
    Last Post: 12-31-2005, 02:16 AM
  4. another exercise question
    By luigi40 in forum C# Programming
    Replies: 3
    Last Post: 11-28-2005, 03:52 PM
  5. A small Question
    By CodeJerk in forum C++ Programming
    Replies: 2
    Last Post: 11-20-2002, 09:08 AM