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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T23:22:12+00:00 2026-05-27T23:22:12+00:00

This is a homework question. I have to write a program forking itself 20

  • 0

This is a homework question. I have to write a program forking itself 20 times. Each new process is adding +1 to a variable (integer) which is shared between all of them. The thing is, I have to use semaphores (IPC). This piece of code is ‘working’ – giving the value of 20 in the end.

*buf = 0;
for(i=1; i<=20; ++i) 
{
    if(fork()!=0)
    {
        *buf += 1;
        exit(0);
    }
}

EDIT:
Based on this code I am trying to get output like :
I am child 1…
I am child 2…
.
.
.
I am child 20…

It worked once (first time), and then the order became random. But I did not change any code. What am I doing wrong?

  • 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-05-27T23:22:13+00:00Added an answer on May 27, 2026 at 11:22 pm

    Well your major problem is this:

        if (fork()!=0)   //<-- this
    

    fork() will return -1 on error, the parent pid, OR ZERO for the child. So you are actually doing everything in the parent. Change to (fork() ==0) and it does what you want.

    Also you should wait on your children and detach shared memory. (I added some output of the process ids to make it a little clearer.)

    printf("I AM THE PARENT pid = %d\n", getpid());
    
    *buf = 0;
    for(i=1; i<=20; ++i)
    {
        if((pid = fork()) == -1)
        {
            perror("fork");
            exit(1);
        }
    
        if (pid == 0)
        {
            v(semid, 0);
            *buf += 1;
            p(semid, 0);
            printf("I am child %d with pid = %d\n", i, getpid());
            shmdt(buf);
            exit(0);
        }
    }
    
    
    for (i = 1; i <= 20; ++i)
    {
        pid = wait(&status);
        printf("child pid = %d reaped\n", pid);
    }
    
    printf("buf: %p\n", buf);
    printf("*buf: %d\n", *buf);
    
    shmdt(buf);
    
    return 0;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm stuck with this Java homework question: Write a new method, Refund, that simulates
This is a homework question. Frankly, I'm not sure how a C program delivers
Okay this is a homework question, and I just don't have a clue how
Here is my homework question: Write a program that asks for a film name
I have a question about my homework problem. Here is the problem: Write a
I have a homework. The question is: Write a function that takes as parameter
For starters this is a homework question. I have an idea but I am
I'm trying to solve this problem, its not a homework question, its just code
Disclaimer: This is for a homework assignment, but the question is not regarding the
This is a homework question,I'm trying to do a Depth-first search function in Scheme,Here's

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.