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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T00:28:21+00:00 2026-06-18T00:28:21+00:00

I dont understand why in the parent process my data is not set to

  • 0

I dont understand why in the parent process my data is not set to what its set to in my child process. I create the shared_data struct variable before I fork my program so it should be shared memory, correct?

Here is the code:

#include <sys/types.h>
#include <stdio.h>
#include <unistd.h>

#define MAX_SEQUENCE 20

typedef struct
{
  long fib_sequence[MAX_SEQUENCE];
  int sequence_size;
} shared_data;

void fibonacci(shared_data* sdata);

int main ( int argc, char *argv[])
{

   pid_t pid, pid1;
  shared_data sdata;

 /* check for parameter values to program */
  int i;
  for(i = 0; i < argc; i++)
  {
     if(strcmp(argv[i], "-ms") == 0)
     {
         int j = ++i;
         if(argv[j])
         {
            /* set passed in value to the sequence_size */
            int paramValue = atoi(argv[j]);
            if(paramValue <= MAX_SEQUENCE)
            {
                sdata.sequence_size = paramValue;
            }
            else
            {
                sdata.sequence_size = MAX_SEQUENCE;
            }
        }
        break;
    }
}

printf("sequence size: %i\n", sdata.sequence_size);

/* fork a child process */
pid = fork();

if (pid < 0)
{
    /* error occurred */
    fprintf(stderr, "Fork Failed");
    return 1;
}
else if (pid == 0)
{
    /* child process */
    pid1 = getpid();
    printf("current child process id = %d\n",pid);
    printf("child's parent process id = %d\n",pid1);
    fibonacci(&sdata);
    printf("child: [%d] Fibonacci Result: %i\n", getpid(), sdata.fib_sequence[sdata.sequence_size - 1]);
}
else
{
    /* parent process */
    pid1 = getpid();
    printf("current parent process id = %d\n",pid); /* C */
    printf("current parent process parent id = %d\n",pid1); /* D */
    wait(NULL);
    int i =0;
    for(i = 0; i < sdata.sequence_size; i++)
    {
        printf("parent: [%d] Fibonacci Result: %i\n", getpid(), sdata.fib_sequence[i]);
       }
     }
   }
void fibonacci(shared_data* sdata)
{
 int first = 0;
 int second = 1;
 int next;

int i;
for(i = 0; i < sdata->sequence_size; i++)
{
    if(i <= 1)
    {
        next = i;
    }
    else
    {
        next = first + second;
        sdata->fib_sequence[i] = next;

        first = second;
        second = next;
    }
     printf("child: [%d] %i\n", getpid(), next);
   }
 }
  • 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-18T00:28:23+00:00Added an answer on June 18, 2026 at 12:28 am

    During fork, all your memory (even preallocated one) becomes unshared. On the implementation level it’s likely to become copy-on-write, but visible effect is the same.

    You can use POSIX shm_open (and friends) to allocate shared memory.

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

Sidebar

Related Questions

Have a Parent process which spawns multipe child process via fork. I want the
I dont understand why I am not able to update a field on the
There is something I dont understand in core data. I have created a NSManagedDocument,
I'm attempting to understand how to group / link child meshes to a parent.
I have a question actually I didn't understand something about child and parent processes.When
CREATE TABLE parent (id INT NOT NULL, PRIMARY KEY (id) ) ENGINE=INNODB; CREATE TABLE
I don't understand why but .parent() is not working on dynamically created elements. Here
i dont understand the problem with returning multiple rows: here is my table BBC:
I dont understand what would be the problem with the following code. It needs
according to this code i dont understand about NSClassFromString how did i know viewControllerName

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.