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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T07:22:32+00:00 2026-05-18T07:22:32+00:00

i wrote the code below but the last printf return 0 for sumofall how

  • 0

i wrote the code below but the last printf return 0 for sumofall how can i get the values of sumoro,sumort and sumorth from child

the code :

#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/wait.h>
#include <stdlib.h>
int main()
{
    int array[3][3];
    int sumoro = 0,sumort = 0,sumorth = 0;

    pid_t pid = fork();

if (pid < 0) {
    printf("fork faild");
    exit(1);
}
else {

    if (pid == 0)
    {
        for (int i = 0; i < 3 ; i++) {
            for (int j = 0; j < 3; j++) {
                array[i][j] = rand()%9;
                if (i == 0)
                sumoro += array[0][j];
                if (i == 1) 
                sumort += array[1][j];
                if (i == 2)
                sumorth += array[2][j];
            }
        }
    }
    else {
        waitpid(pid, NULL, 0);
        int sumofall = sumoro + sumort + sumorth;
        printf("sum of all equal : %d ", sumofall);
    }


return 0;

    }
}

Note : not necessarily but if you can help me, how can i make the rand() gives everytime new numbers because i notice every time same values

  • 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-18T07:22:32+00:00Added an answer on May 18, 2026 at 7:22 am

    When you fork, each process resides in its own space afterwards. There is no easy way to move data back and forth – at least nothing as simple as reading a variable. You need to use some kind of Inter-Process Communication (IPC) method, such as anonymous pipes (see the pipe(2) manual page).

    As for rand(), you need to seed the random number generator with a relatively random value. A simple solution with adequate randomness for pretty much anything but cryptography is issuing this statement once at the beginning of your program:

    srand(time(NULL));

    This uses the clock to seed the RNG with different values each time that you run your program, unless you manage to run it twice withing the same second.

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

Sidebar

Related Questions

i am suppose to solve this question but i am stuck. Write a program
I'll ask this with a Scala example, but it may well be that this
I'm trying to do something i've done numerous times. I can't figure out why
I have created a basic Client Server that will send image files in a
I have an array whose structure is like $data = array{0=>'abc',1=>xyz,2=>tqs} Now I need
I'm writing a form validation script for a form with multiple ratings, and I'd
I'm trying to write a program to create a chart in excel by C#.
Hey guys I'm new to rails, There's a lot of eye-opener for me, and
I find that my HTTPWebRequest Upload is failing at the very end of the

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.