Thread: Question about fork()

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    14

    Question about fork()

    Does the CPU freeze the parent and start executing the child process right away when this gets called? Or does it just create a child process image, then resume the parent process where the child is ready for execution? Thanks

  2. #2
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    That is more or less implementation defined. The scheduler may first put either the parent or the child process on the cpu.

  3. #3
    Registered User
    Join Date
    Oct 2010
    Posts
    107
    Quote Originally Posted by stevenswj View Post
    Does the CPU freeze the parent and start executing the child process right away when this gets called? Or does it just create a child process image, then resume the parent process where the child is ready for execution? Thanks
    We have no idea what happens. The fork system call requests the operating system to make another process and execution continues from there. In my opinion, the way fork should be called is:

    Code:
    int pid = fork();
    if(pid == -1)
    {
    	// Some kind of error handling for fork() failure
    }
    // In the child process, the value of pid == 0. So pretend
    // like we're running the same code in a parallel universe
    // in which pid == 0
    else if(pid == 0)
    {
    	// Stuff to do in child process. Maybe something like execvp()?
    }
    
    // In the parent process, the value of pid is the process ID of
    // the child.
    else
    {
    	// Business as usual
    }

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > Does the CPU freeze the parent and start executing the child process right away when this gets called?
    What happens if your machine has only one CPU?

    What happens if your machine has more than one CPU?

    The latter could be running both at the same time.
    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.

  5. #5
    Registered User
    Join Date
    Oct 2010
    Posts
    14
    What if we're talking just 1 CPU where only a single process can be running at a time?

    For my assignment we are writing a unix variant operating system based on something like this where the child appears to only get created then execution resumes inside the parent. The only way child gets executed after fork is if we explicitly context switch to it by calling a switch.

    I'm not sure if this is what is actually done on Red Had or Ubuntu linux flavors? Or this only happens because it's a single CPU 1 running proc at a time environment and for a multiple running process environment, both would be running even if that environment has only 1 CPU?

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Read the manual pages for your system.
    In the absence of any positive statement like "child always runs first", then you should assume anything can happen.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. fork() background execution hang
    By Diavolche in forum C Programming
    Replies: 1
    Last Post: 04-08-2010, 01:00 PM
  2. A small question about fork( )
    By mlhazan in forum C Programming
    Replies: 4
    Last Post: 08-13-2008, 08:18 PM
  3. fork() question
    By cstudent in forum C Programming
    Replies: 3
    Last Post: 04-25-2008, 06:48 AM
  4. Question sorting character arrays: C programming
    By NeoSigma in forum C Programming
    Replies: 3
    Last Post: 05-23-2003, 09:28 PM
  5. Fork() child process question
    By CrackDown in forum Linux Programming
    Replies: 0
    Last Post: 04-17-2003, 11:58 AM