How to share memory between processes created by fork()?

前端 未结 3 1822
傲寒
傲寒 2020-11-22 15:30

In fork child, if we modify a global variable, it will not get changed in the main program.

Is there a way to change a global variable in child fork?



        
3条回答
  •  情歌与酒
    2020-11-22 15:48

    Here is an alternative solution.

    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    
    typedef struct
    {
      int id;
      size_t size;
    } shm_t;
    
    shm_t *shm_new(size_t size)
    {
      shm_t *shm = calloc(1, sizeof *shm);
      shm->size = size;
    
      if ((shm->id = shmget(IPC_PRIVATE, size, IPC_CREAT | IPC_EXCL | S_IRUSR | S_IWUSR)) < 0)
      {
        perror("shmget");
        free(shm);
        return NULL;
      }
    
      return shm;
    }
    
    void shm_write(shm_t *shm, void *data)
    {
      void *shm_data;
    
      if ((shm_data = shmat(shm->id, NULL, 0)) == (void *) -1)
      {
        perror("write");
        return;
      }
    
      memcpy(shm_data, data, shm->size);
      shmdt(shm_data);
    }
    
    void shm_read(void *data, shm_t *shm)
    {
      void *shm_data;
    
      if ((shm_data = shmat(shm->id, NULL, 0)) == (void *) -1)
      {
        perror("read");
        return;
      }
      memcpy(data, shm_data, shm->size);
      shmdt(shm_data);
    }
    
    void shm_del(shm_t *shm)
    {
      shmctl(shm->id, IPC_RMID, 0);
      free(shm);
    }
    
    int main()
    {
      int var = 1;
      shm_t *shm = shm_new(sizeof var);
    
      int pid;
      if ((pid = fork()) == 0)
      { /* child */
        var = 5;
        shm_write(shm, &var);
        printf("child: %d\n", var);
        return 0;
      }
      /* Wait for child to return */
      int status;
      while (wait(&status) != pid);
      /* */
      shm_read(&var, shm);
      /* Parent is updated by child */
      printf("parent: %d\n", var);
      shm_del(shm);
      return 0;
    }
    

    Build with:

    $ gcc shm.c -o shm && ./shm
    

提交回复
热议问题