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 8378685
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T15:59:02+00:00 2026-06-09T15:59:02+00:00

I am new to UNIX programming and i was reading about zombie processes and

  • 0

I am new to UNIX programming and i was reading about zombie processes and how to avoid them using fork() twice. I read the code from a book and tried to run it on my system.. I am using ubuntu 12.04. I ran the following code:

#include<stdio.h>   
#include<sys/wait.h>
#include<stdlib.h>

int main()
{
    pid_t pid;
    if(pid = fork() < 0)
        printf("Fork Error.!!!\n");
    else 
    if(pid == 0)
    {
        if((pid = fork()) < 0)
             printf("Fork2 Error.!!!\n");
        else 
             if(pid > 0)
                   exit(0);

        sleep(2);
        printf("Second Child, parent id: %d\n", getppid());
        exit(0);
    }
    if(waitpid(pid, NULL, 0) != pid)
        printf("Waitpid Error.!!!\n");

    exit(0);
}

The output that i get is as follows:

Second Child, parent id: 1
Second Child, parent id: 1

The book says this should be printed only once and that is also what i feel should happen when i see what’s happening in the code. I dont understand why its getting printed twice. I found this code at many places on the net but could not get something that explains this. Any help is welcome. Thanks.!!

  • 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-09T15:59:03+00:00Added an answer on June 9, 2026 at 3:59 pm

    second line of the code in main – missing brackets around pid = fork()

    if( ( pid = fork() ) < 0){
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm new to UNIX. I am using VI editor to code for C programming.
I am new to UNIX and when I was reading a book about UNIX,
I am just new to programming in Unix and have a small issue that
I am new to Unix and am using sun solaris (v10 I think). I
New to PHP and MySQL, have heard amazing things about this website from Leo
I am very new to C and UNIX systems programming. I know what to
I'm new to Windows programming, and I'm looking for an equivalent of the Unix
I'm super new to programming and I've been using appengine to help me learn
On Unix, we can create a new process with fork(); execvp(argv[0], argv); (with a
im using json in my new app. im getting time in UNIX timestamps format.

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.