Fork - unpredictable?

This is a discussion on Fork - unpredictable? within the Linux Programming forums, part of the Platform Specific Boards category; Hi, I am trying to learn the concept of fork, thus I wrote a program to test my knowledge, I ...

  1. #1
    Registered User
    Join Date
    Mar 2006
    Posts
    1

    Fork - unpredictable?

    Hi,

    I am trying to learn the concept of fork, thus I wrote a program to test my knowledge, I will appreicate if anyone can point out what am I doing wrong with this codes. Thank you !

    Freddie
    Code:
    #include <stdio.h>
    #include <errno.h>
    #include <sys/types.h>
    #include <unistd.h>
    
    
    // fork() -1 no process is created, original goes on running
    // fork(), The child returns 0
    // fork(), parent returns positive PID of the child
    
    int main(int argc,char **argv) {
    
    pid_t child, parent, old_ppid, new_ppid;
        
     printf(" Current PID is %d\n", parent = getpid());
     printf(" Current process PPID %d\n ", old_ppid = getppid()); 
    
     if ((  child =  fork() ) < 0 ) 
         /* handle error */
         fprintf(stderr, " %s Fork failed! %s", argv[0], strerror(errno));
         exit(1);
    
       else if (child == 0 ) {  //fork succesful
         {
               sleep(3); //if it is a process , sleep for one second
               printf("This is the new process\n");
               printf("It PID as %d \n",getpid());
               printf("Parent PID as %d\n", getppid() );
               exit(0);
     } else{
    
               sleep(1); //if it is a parent , sleep for 2 seconds
               printf("This is the parent process\n");
               printf("Parent PID as %d \n", getppid( ));
               exit(0);}
     return 0;
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,824
    Seems OK to me, apart from your comments

    sleep(3); //if it is a process , sleep for one second
    sleep(1); //if it is a parent , sleep for 2 seconds

    "Fork - unpredictable?"
    But what is your question?
    What are you seeing, and what is it that you don't understand about what you're seeing?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Sep 2004
    Posts
    197
    You also don't seem to be waiting for the child, look up the wait and wait_pid manpages.
    I don't exactly know what your having trouble understanding either, since you haven't told us what you mean by unpredictable.
    If any part of my post is incorrect, please correct me.

    This post is not guarantied to be correct, and is not to be taken as a matter of fact, but of opinion or a guess, unless otherwise noted.

  4. #4
    C/C++Newbie Antigloss's Avatar
    Join Date
    May 2005
    Posts
    212
    If I don't get it wrong, you're asking about whether the child or the parent will execute first, right?

    Yes, it is unpredictable whether the child or the parent execute first. It all depends on the os to determine who runs first. Even you call some functions such as sleep, also it isn't guaranteed who will run first. But you can call the wait funtion family to wait for the child to execute.

  5. #5
    Registered User
    Join Date
    Oct 2004
    Posts
    15
    Code:
    if ((  child =  fork() ) < 0 ) 
         /* handle error */
         fprintf(stderr, " %s Fork failed! %s", argv[0], strerror(errno));
         exit(1);
    you are missing { and } for that if.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Understanding fork()
    By NuNn in forum C Programming
    Replies: 8
    Last Post: 02-27-2009, 12:09 PM
  2. Fork(), pause(), and storing PID's in a linked list
    By vital101 in forum C Programming
    Replies: 10
    Last Post: 09-28-2007, 03:16 AM
  3. Fork() not working.
    By BENCHMARKMAN in forum C++ Programming
    Replies: 3
    Last Post: 08-01-2007, 01:28 AM
  4. fork(), exit() - few questions!
    By s3t3c in forum C Programming
    Replies: 10
    Last Post: 11-30-2004, 06:58 AM
  5. Daemon programming: allocated memory vs. fork()
    By twisgabak in forum Linux Programming
    Replies: 2
    Last Post: 09-25-2003, 03:53 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21