Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9155017
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T12:31:21+00:00 2026-06-17T12:31:21+00:00

Here’s a program with fork and waitpid . #!/usr/bin/perl use strict; use warnings; my

  • 0

Here’s a program with fork and waitpid.

#!/usr/bin/perl
use strict;
use warnings;
my ($childProcessID, $i);

print "I AM THE ONLY PROCESS.\n";

$childProcessID = fork ();

if ($childProcessID){
        print "I am the parent process.\n";
        print "I spawned a new process with ID $childProcessID\n";
        waitpid ($childProcessID, 0);
        print "The child process is terminated with status $?\n";
    }
else{
    for ($i = 0; $i <= 10; $i++){
         print "I am the child process: Counting $i\n";
    }
}

The output may look like the following.

I AM THE ONLY PROCESS.
I am the parent process.
I spawned a new process with ID 7610
I am the child process: Counting 0
I am the child process: Counting 1
I am the child process: Counting 2
I am the child process: Counting 3
I am the child process: Counting 4
I am the child process: Counting 5
I am the child process: Counting 6
I am the child process: Counting 7
I am the child process: Counting 8
I am the child process: Counting 9
I am the child process: Counting 10
The child process is terminated with status 0

Now with many similar programs regarding fork on web and in books, that says

Code in if block is executed by parent process, while that in else is executed by child process. And waitpid is used to wait for childs to finish.

My question is

How and why is else block executed for child process? I get that fork has created new child process. But how execution for child (that is else block) happens after fork statement? Can somebody explain me this step by step for child process or maybe more insight into something that I am missing like why child doesn’t execute statement below?

print "I AM THE ONLY PROCESS.\n";
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T12:31:22+00:00Added an answer on June 17, 2026 at 12:31 pm

    Fork splits the current process into two when it is executed. Both processes continue executing right after the fork call.

    The only* difference between the two resulting processes is that in one (the parent), fork() returns the child’s PID, while in the other (the child), fork() returns zero.

    So in the parent, the $childProcessID is non-zero and the if branch is taken, while in the child that variable is zero and the else branch is executed.

    * Probable not really pedantically true.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

here is the site : http://www.notrepanorama.com at the bottom left, i use a jquery
Here is what I want to do. Use this HTML line and have the
Here's a newbie question, but how do I use dates for the x-axis in
Here's the code I have. It works. The only problem is that the first
Here's my code: x = 1.0 y = 100000.0 print x/y My quotient displays
Here is an object that I'd like to use with ng-repeat, but it's not
Here a simple question : What do you think of code which use try
Here is what I am getting when I use single line comment (using --
Here is the syntax I am trying to use to replace PHP5.3 problematic if
Here's the view: @if (stream.StreamSourceId == 1) { <img class=source src=@Url.Content(~/Public/assets/images/own3dlogo.png) alt= /> }

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.