使用共享内存将数据结构传递到另一个进程并保存到文件

时间:2011-08-31 21:59:22

标签: c shared-memory

我使用共享内存将数据(名称,ph号和地址)发送到另一个进程。我必须在第二个进程中打印数据并将它们存储到一个文件中。我尝试过这段代码但是我没有在第二个过程中接收数据。有人可以帮我弄这个吗。 谢谢。

address.c

        typedef struct 
{
  char lname[25];
  char fname[20];
  char address[20];
  char phonenumber[20];
}addressbook;

addressbook a;

char *shared_memory;
int main()
{
  int select;
  int segment_id;
  char* shared_memory;
  int segment_size;
  key_t shm_key;
  const int shared_segment_size = 0x6500;
  shm_key = ftok("/home/madan/programs/shm_tok",'C');
  if(shm_key < 0) {
    printf("failed to create the key %s\n",strerror(errno));
  }
  /* Allocate a shared memory segment. */
   segment_id = shmget (shm_key, shared_segment_size,
            IPC_CREAT | IPC_EXCL | S_IRUSR | S_IWUSR);
    if(segment_id < 0) {
      printf("error geting the segment id %s\n",strerror(errno));
    }
    printf("segment ID:%d\n", segment_id);
   /* Attach the shared memory segment. */
   shared_memory = (char*) shmat (segment_id, 0, 0);
   printf ("shared memory attached at address %p\n", shared_memory);


   printf("enter lastname:\n");
   gets(a.lname);
   printf("enter firstname:\n");
   gets(a.fname);
   printf("enter address:\n");
   gets(a.address);
   printf("enter phone number:\n");
   gets(a.phonenumber);


   memcpy(shared_memory, &a, sizeof(a));

   printf("data:%s\n", shared_memory);

   system("./address-insert");
      /* Detach the shared memory segment. */
  shmdt (shared_memory);
   /

    * Deallocate the shared memory segment.*/
           shmctl (segment_id, IPC_RMID, 0);
}

address-insert.c

typedef struct 
{
  char lname[20];
  char fname[20];
  char address[20];
  char phonenumber[20];
}addressbook;
addressbook a;
int main ()
{
  int segment_id;
  char* shared_memory;
  FILE *fp;
  char *name;
  int segment_size;
   key_t shm_key;
  shm_key = ftok("/home/madan/programs/shm_tok",'D');
  const int shared_segment_size = 0x6500;
  /* Allocate a shared memory segment. */
  segment_id = shmget (shm_key, shared_segment_size,
              S_IRUSR | S_IWUSR);
  if(segment_id < 0) {
    printf("error:[%s]",strerror(errno));
  }
  printf("segment id %d\n",segment_id);
  /* Attach the shared memory segment. */
  shared_memory = (char*) shmat (segment_id, 0, 0);
  if(shared_memory == NULL) {
    printf("failed to attach the shared memory %s",strerror(errno));
  }
  printf ("shared memory2 attached at address %p\n", shared_memory);

  printf ("name=%s\n", shared_memory);

  memcpy(&a, shared_memory, sizeof(a));
  printf("name: %s\n", a.fname);
  printf("address:%s\n", a.address);
  printf("phone number=%s\n", a.phonenumber);

  fp = fopen("filename","a+");
  fwrite(a, 1, strlen(a),fp);
  fclose(fp);


  /* Detach the shared memory segment. */
  shmdt (shared_memory);
  return 0;
}

2 个答案:

答案 0 :(得分:0)

为什么需要两个流程?如果你想这样做,你还需要一个semaphone - 这样其他进程就可以确保所有数据都在共享内存中。也许管道(UNIX或其他)会更简单。

答案 1 :(得分:0)

memcpy(&a, shared_memory, sizeof(a));中,而不是传递变量a的大小,请尝试传递结构地址的大小,即sizeof(addressbook)