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

  • Home
  • SEARCH
  • 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 8682715
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T21:50:58+00:00 2026-06-12T21:50:58+00:00

I wrote a program that creates 3 children for a process. Then, each children

  • 0

I wrote a program that creates 3 children for a process.
Then, each children create 3 processes as well.

This is the code:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <time.h>
#include <stdbool.h>
#define GENERACIONES 2

void crearHijo(int, int);

int main()
{
    int i;

    for (i = 1; i <= 3; i++)
        crearHijo(i, 1);

    return 0;
}

void crearHijo(int hijoNum, int gen)
{
    pid_t pid = fork();
    int i, hijos = 3;

    if (pid < 0)
    {
        fprintf(stderr, "Fork Failed");
        exit(-1);                       
    }
    else if (pid == 0)
    {
        printf("Soy de la generación %d, hijo # %d, PID: %d, PPID: %d\n",
                 gen, hijoNum, getpid(), getppid());

        if (gen < GENERACIONES)
        {
            gen++;
            /*srand(time(NULL));
            hijos = rand() % 3 + 1;*/

            for (i = 1; i <= hijos; i++)
                crearHijo(i, gen);
        }

        exit(0);
    }
    else
        waitpid(pid, NULL, NULL);
}

I’m expecting 3 sons and 9 grandsons.
But I get the following output:

Soy de la generación 1, hijo # 1, PID: 2533, PPID: 2532
Soy de la generación 2, hijo # 1, PID: 2534, PPID: 2533
Soy de la generación 1, hijo # 1, PID: 2533, PPID: 2532
Soy de la generación 2, hijo # 2, PID: 2535, PPID: 2533
Soy de la generación 1, hijo # 1, PID: 2533, PPID: 2532
Soy de la generación 2, hijo # 3, PID: 2536, PPID: 2533
Soy de la generación 1, hijo # 1, PID: 2533, PPID: 2532
Soy de la generación 1, hijo # 2, PID: 2537, PPID: 2532
Soy de la generación 2, hijo # 1, PID: 2538, PPID: 2537
Soy de la generación 1, hijo # 2, PID: 2537, PPID: 2532
Soy de la generación 2, hijo # 2, PID: 2539, PPID: 2537
Soy de la generación 1, hijo # 2, PID: 2537, PPID: 2532
Soy de la generación 2, hijo # 3, PID: 2540, PPID: 2537
Soy de la generación 1, hijo # 2, PID: 2537, PPID: 2532
Soy de la generación 1, hijo # 3, PID: 2541, PPID: 2532
Soy de la generación 2, hijo # 1, PID: 2542, PPID: 2541
Soy de la generación 1, hijo # 3, PID: 2541, PPID: 2532
Soy de la generación 2, hijo # 2, PID: 2543, PPID: 2541
Soy de la generación 1, hijo # 3, PID: 2541, PPID: 2532
Soy de la generación 2, hijo # 3, PID: 2544, PPID: 2541
Soy de la generación 1, hijo # 3, PID: 2541, PPID: 2532

Some entries are duplicated and I don’ understand why.
There are not more than 12 processes but some of them print the output several times.
How can I avoid this behavior?

TIA

  • 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-12T21:51:00+00:00Added an answer on June 12, 2026 at 9:51 pm

    When your:

    waitpid(pid, NULL, NULL);
    

    is done on the child process, the function returns and the main’s

    for (i = 1; i <= 3; i++)
    

    is continued

    Try something like this:

        int main()
    {
        int i;
        bool j = true;
    
        for (i = 1; i <= 3; i++)
        {
           if (j == true)
            j = crearHijo(i, 1);
        }
    
        return 0;
    }
    
    bool crearHijo(int hijoNum, int gen)
    {
        pid_t pid = fork();
        int i, hijos = 3;
    
        if (pid < 0)
        {
            fprintf(stderr, "Fork Failed");
            exit(-1);                       
        }
        else if (pid == 0)
        {
            printf("Soy de la generación %d, hijo # %d, PID: %d, PPID: %d\n",
                     gen, hijoNum, getpid(), getppid());
    
            if (gen < GENERACIONES)
            {
                gen++;
                /*srand(time(NULL));
                hijos = rand() % 3 + 1;*/
    
                for (i = 1; i <= hijos; i++)
                    crearHijo(i, gen);
            }
    
            exit(0);
        }
        else
        {
            waitpid(pid, NULL, NULL);
            return (false);
        }
     return (true);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I wrote this program: #include <stdio.h> /*Part B Write a program that: defines an
I wrote a small program, that creates files at an interval of 1 minute.
I wrote a program that forks some processes with fork(). I want to kill
I've wrote a program that gets a string, and then calculate a total value
I need to create a program that creates another program but not a compiler
i wrote a program that get data from user and store it in database.this
I've wrote an adequate little program in C that creates a window. However, I'm
I wrote a Python program that acts on a large input file to create
Here's what my program is supposed to do: create 4 child processes: process 0
I wrote a program that creates a puzzle based on user's inputs. There is

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.