简体   繁体   中英

C - Shared Memory in simple fibonacci program

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);
   }
 }

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.

Inter process communication (IPC) is not achieved directly like this. You will have to use shmget() shmat() functions to implement the same using shared memory.

For more see this details about these functions shmget() , shmat()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM