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

发布于 2024-12-02 12:26:25 字数 2815 浏览 0 评论 0原文

我正在使用共享内存将数据(名称、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;
}

I am sending data(name, ph number & address) to another process using shared memory. I have to print data in the second process and store them to a file. I have tried this code but I am not receiving data in second process. Can someone help me with this.
Thank you.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

凉宸 2024-12-09 12:26:25

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

Why do you require two processes? If you want to do this you will also need a semaphone - so that the other process can be sure that all the data is in the shared memory. Perhaps a pipe (UNIX or otherwise) would be simpler.

手心的海 2024-12-09 12:26:25

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

In memcpy(&a, shared_memory, sizeof(a)); instead of passing the size of the variable a, try passing the size of structure address i.e. sizeof(addressbook).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文