一个简单的C ++共享内存程序写在linux:分段故障 [英] A simple C++ shared memory program written on linux: segmentation fault

查看:369
本文介绍了一个简单的C ++共享内存程序写在linux:分段故障的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  #include< stdio.h> 
#include< sys / shm.h>
#include< sys / stat.h>
#include< string>
#include< vector>
#include< iostream>

using namespace std;

struct LOCK {
string name;
string type;
vector< string> pids;
};

int main()

{

int segment_id;

LOCK * shared_memory;

struct shmid_ds shmbuffer;

int segment_size;

const int shared_segment_size = 0x6400;



/ *分配共享内存段。 * /

segment_id = shmget(IPC_PRIVATE,shared_segment_size,

IPC_CREAT | IPC_EXCL | S_IRUSR | S_IWUSR);

/ *附加共享内存段。 * /

shared_memory =(LOCK *)shmat(segment_id,0,0);

printf(shared memory attached at address%p\\\
,shared_memory);

/ *确定段的大小。 * /

shmctl(segment_id,IPC_STAT,& shmbuffer);

segment_size = shmbuffer.shm_segsz;

printf(segment size:%d\\\
,segment_size);

/ *向共享内存段写入一个字符串。 * /

// sprintf(shared_memory,Hello,world。);
shared_memory - > name =task 1;
shared_memory - > type =read;
(shared_memory - > pids).push_back(12345);
(shared_memory - > pids).push_back(67890);

/ *分离共享内存段。 * /

shmdt(shared_memory);



/ *重新连接共享内存段,位于不同的地址。 * /

shared_memory =(LOCK *)shmat(segment_id,(void *)0x5000000,0);

printf(shared memory reattached at address%p\\\
,shared_memory);

/ *从共享内存中打印出字符串。 * /

// printf(%s\\\
,shared_memory - > name);
cout<< 共享内存的名称:+ shared_memory - >名称<< endl;

/ *分离共享内存段。 * /

shmdt(shared_memory);



/ *取消分配共享内存段。 * /

shmctl(segment_id,IPC_RMID,0);



return 0;

}



我从共享内存教程中获得了代码。它工作,直到我定义了struct LOCK,并试图写入LOCKs而不是char *到共享内存。



有人可以帮我找出导致分割的问题 c> 和<$ c $

$

c> string
进入共享内存。这两个类都分配了自己的内存,这些内存将在任何进程生成分配的地址空间内分配,并且当从其他进程访问时会产生segfault。你可以尝试指定分配器来使用共享内存,但是由于在C ++ 03中分配器被假定为无状态的,我不知道是否可能。



考虑检查Boost.Interprocess是如何做到的。


#include <stdio.h> 
#include <sys/shm.h> 
#include <sys/stat.h> 
#include <string>
#include <vector>
#include <iostream>

using namespace std;

struct LOCK {
  string name;
  string type;
  vector <string> pids;
};

int main () 

{

  int segment_id; 

  LOCK* shared_memory; 

  struct shmid_ds shmbuffer; 

  int segment_size; 

  const int shared_segment_size = 0x6400; 



  /* Allocate a shared memory segment.  */ 

  segment_id = shmget (IPC_PRIVATE, shared_segment_size, 

                     IPC_CREAT | IPC_EXCL | S_IRUSR | S_IWUSR); 

  /* Attach the shared memory segment.  */ 

  shared_memory = (LOCK*) shmat (segment_id, 0, 0); 

  printf ("shared memory attached at address %p\n", shared_memory); 

  /* Determine the segment's size. */ 

  shmctl (segment_id, IPC_STAT, &shmbuffer); 

  segment_size  =               shmbuffer.shm_segsz; 

  printf ("segment size: %d\n", segment_size); 

  /* Write a string to the shared memory segment.  */ 

  //sprintf (shared_memory, "Hello, world."); 
  shared_memory -> name = "task 1";
  shared_memory -> type = "read";
  (shared_memory -> pids).push_back("12345");
  (shared_memory -> pids).push_back("67890");

  /* Detach the shared memory segment.  */ 

  shmdt (shared_memory); 



  /* Reattach the shared memory segment, at a different address.  */ 

  shared_memory = (LOCK*) shmat (segment_id, (void*) 0x5000000, 0); 

  printf ("shared memory reattached at address %p\n", shared_memory); 

  /* Print out the string from shared memory.  */ 

  //printf ("%s\n", shared_memory -> name); 
  cout << "Name of the shared memory: " + shared_memory -> name << endl;

  /* Detach the shared memory segment.  */ 

  shmdt (shared_memory); 



  /* Deallocate the shared memory segment.  */ 

  shmctl (segment_id, IPC_RMID, 0); 



  return 0; 

} 

I got the code from a tutorial on shared memory. It worked until I defined struct LOCK and tried to write LOCKs instead of char* into the shared memory.

Could someone please help me figure out the problem here that causes the segmentation fault?

解决方案

You are placing vectors and strings into shared memory. Both those classes allocate memory of their own, which will be allocated within the address space of whatever process generates the allocation, and will produce a segfault when accessed from the other process. You could try specifying allocators to use that shared memory, but since in C++03 allocators are assumed to be stateless I'm not sure if it will be possible.

Consider checking out how Boost.Interprocess does it.

这篇关于一个简单的C ++共享内存程序写在linux:分段故障的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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