如何使用共享内存两个进程之间的通信 [英] how to use shared memory to communicate between two processes

查看:209
本文介绍了如何使用共享内存两个进程之间的通信的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想两个进程之间的通信。我想数据(如姓名,电话号码,地址)共享内存中保存一个进程并试图打印通过其他处理数据。

process1.c

 的#include<&stdio.h中GT;
#包括LT&; SYS / shm.h>
#包括LT&; SYS / stat.h>
诠释的main()
{
  INT SEGMENT_ID;
  字符* shared_memory [3];
  INT segment_size;
  的key_t shm_key;
  INT I = 0;
  const int的shared_segment_size = 0x6400;
  / *分配共享内存段。 * /
  SEGMENT_ID = shmget的(shm_key,shared_segment_size,
            IPC_CREAT | IPC_EXCL | S_IRUSR | S_IWUSR);
  / *附加共享内存段。 * /
  shared_memory [3] =(字符*)的shmat(SEGMENT_ID,0,0);
  的printf(,shared_memory附加地址%P \\ n共享内存);
  / *写一个字符串到共享内存段。 * /
   sprintf的(shared_memory [I],马迪\\ n);
   sprintf的(shared_memory [I + 1],73453916 \\ n);
   sprintf的(shared_memory第[i + 2],美国\\ n);  / *调用其他进程* /
  系统(./过程2);  / *分离共享内存段。 * /
  shmdt(shared_memory);
  / *解除分配共享内存段。* /
  了shmctl(SEGMENT_ID,IPC_RMID,0);  返回0;
}


process2.c

 的#include<&stdio.h中GT;
#包括LT&; SYS / shm.h>
#包括LT&; SYS / stat.h>
诠释的main()
{
  INT SEGMENT_ID;
  字符* shared_memory [3];
  INT segment_size;
  INT I = 0;
  的key_t shm_key;
  const int的shared_segment_size = 0x6400;
  / *分配共享内存段。 * /
  SEGMENT_ID = shmget的(shm_key,shared_segment_size,
              S_IRUSR | S_IWUSR);
  / *附加共享内存段。 * /
  shared_memory [3] =(字符*)的shmat(SEGMENT_ID,0,0);
  输出(附加地址%P \\ n共享memory22,shared_memory);
   的printf(NAME =%S \\ n,shared_memory [I]);
   的printf(%S \\ n,shared_memory [I + 1]);
   的printf(%S \\ n,shared_memory第[i + 2]);
  / *分离共享内存段。 * /
  shmdt(shared_memory);
   返回0;
}

但我没有得到所需的输出。
这是我得到的输出是:

 连接地址0x7fff0fd2d460共享内存
分段故障

任何人都可以请帮助我。这是初始化的正确方法 shared_memory [3]

感谢您。


解决方案

 的char * shared_memory [3];
...
shared_memory [3] =(字符*)的shmat(SEGMENT_ID,0,0);

您声明 shared_memory 为可容纳三个指针为char数组,但你实际做的是写一个指针的一个落后结束的地方阵列的的。由于没有有效说明内存有其它用途,接下来发生的事情一般是联合国predictable。

东西去决定性坏之后,当您尝试使用指针在 shared_memory [0] shared_memory [2] ,因为这些指针从来没有被初始化。他们充满了从堆栈无意义的垃圾 - 因此分段错误

看来,在一般情况下,你失败的阵列和它的元素之间进行区分。你应该去,让自己的很多的更舒适,在连续code数组和指针亲自尝试一下在共享内存之前IPC。

请注意,共享内存的IPC做在那里更容易得到,错误的方法之一。除非你有刚性效率的制约和将要交换的很多的数据,它更容易与管道,命名管道,或插座工作。

I am trying to communicate between two processes. I am trying to save data(like name, phone number, address) to shared memory in one process and trying to print that data through other process.

process1.c

#include <stdio.h>
#include <sys/shm.h>
#include <sys/stat.h>
int main ()
{
  int segment_id;
  char* shared_memory[3];
  int segment_size;
  key_t shm_key;
  int i=0;
  const int shared_segment_size = 0x6400;
  /* Allocate a shared memory segment. */
  segment_id = shmget (shm_key, shared_segment_size,
            IPC_CREAT | IPC_EXCL | S_IRUSR | S_IWUSR);
  /* Attach the shared memory segment. */
  shared_memory[3] = (char*) shmat (segment_id, 0, 0);
  printf ("shared memory attached at address %p\n", shared_memory);
  /* Write a string to the shared memory segment. */
   sprintf(shared_memory[i], "maddy \n");
   sprintf(shared_memory[i+1], "73453916\n");
   sprintf(shared_memory[i+2], "america\n");

  /*calling the other process*/
  system("./process2");

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

  return 0;
}


process2.c

#include <stdio.h>
#include <sys/shm.h>
#include <sys/stat.h>
int main ()
{
  int segment_id;
  char* shared_memory[3];
  int segment_size;
  int i=0;
  key_t shm_key;
  const int shared_segment_size = 0x6400;
  /* Allocate a shared memory segment. */
  segment_id = shmget (shm_key, shared_segment_size,
              S_IRUSR | S_IWUSR);
  /* Attach the shared memory segment. */
  shared_memory[3] = (char*) shmat (segment_id, 0, 0);
  printf ("shared memory22 attached at address %p\n", shared_memory);
   printf ("name=%s\n", shared_memory[i]);
   printf ("%s\n", shared_memory[i+1]);
   printf ("%s\n", shared_memory[i+2]);
  /* Detach the shared memory segment. */
  shmdt (shared_memory);
   return 0;
}

But I am not getting the desired output. the output which i got is:

shared memory attached at address 0x7fff0fd2d460
Segmentation fault

Anyone can please help me with this. Is this the correct way of initializing shared_memory[3].

Thank you.

解决方案

char* shared_memory[3];
...
shared_memory[3] = (char*) shmat (segment_id, 0, 0);

You declare shared_memory as an array capable of holding three pointers to char, but what you actually do with it is to write a pointer one place behind the end of the array. Since there is no telling what the memory there is otherwise used for, what happens next is generally unpredictable.

Things go conclusively bad afterwards when you try to make use of the pointers in shared_memory[0] through shared_memory[2], because those pointers have never been initialized. They are filled with meaningless garbage from the stack -- thus the segmentation fault.

It seems, in general, that you're failing to distinguish between the array and its elements. You should go and make yourself a lot more comfortable with arrays and pointers in sequential code before you try your hand at shared-memory IPC.

Note that shared memory is one of the more easy-to-get-wrong ways of doing IPC out there. Unless you have rigid efficiency constraints and are going to exchange a lot of data, it's much easier to work with pipes, named pipes, or sockets.

这篇关于如何使用共享内存两个进程之间的通信的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆