[Boost-users] Information about create_only and open_or_create

676 views
Skip to first unread message

manish4gupta

unread,
Oct 14, 2009, 10:14:01 PM10/14/09
to boost...@lists.boost.org

Hi,

I am working on shared memory. I am successful in using it but I am trying
to optimize my code. I just want to know the differences between create_only
and open_or_create. As far as i know, create_only creates the shared memory
with given size, while open_or_create can open and create both. But If I use
open_or_create instead of create-only, i am getting interprocess exception.
Why?
Please help. Thanks in advance.
--
View this message in context: http://www.nabble.com/Information-about-create_only-and-open_or_create-tp25902079p25902079.html
Sent from the Boost - Users mailing list archive at Nabble.com.

_______________________________________________
Boost-users mailing list
Boost...@lists.boost.org
http://lists.boost.org/mailman/listinfo.cgi/boost-users

manish4gupta

unread,
Oct 21, 2009, 11:36:10 PM10/21/09
to boost...@lists.boost.org

Hi,

I am working on shared memory. I am successful in using it but I am trying
to optimize my code. I just want to know the differences between create_only
and open_or_create. As far as i know, create_only creates the shared memory
with given size, while open_or_create can open and create both. But If I use
open_or_create instead of create-only, i am getting interprocess exception.
Why?
Please help. Thanks in advance.

manish4gupta wrote:
>
> Hi,
>
> I am working on shared memory. I am successful in using it but I am trying
> to optimize my code. I just want to know the differences between
> create_only and open_or_create. As far as i know, create_only creates the
> shared memory with given size, while open_or_create can open and create
> both. But If I use open_or_create instead of create-only, i am getting
> interprocess exception. Why?
> Please help. Thanks in advance.
>

--
View this message in context: http://www.nabble.com/Information-about-create_only-and-open_or_create-tp25902079p26003540.html

Christoph Gysin

unread,
Oct 22, 2009, 6:57:35 AM10/22/09
to boost...@lists.boost.org
2009/10/22 manish4gupta <man...@synamatix.com>:

> I am working on shared memory. I am successful in using it but I am trying

Please provide more detailed information about your problem. Add a
short (compilable) example demonstrating the problem, including the
complete error message. Also provide information about your operating
system, compiler, etc...

Don't just post the same question again. Thanks.

Chris
--
echo mailto: NOSPAM !#$.'<*>'|sed 's. ..'|tr "<*> !#:2" org@fr33z3

manish4gupta

unread,
Oct 22, 2009, 10:46:07 PM10/22/09
to boost...@lists.boost.org

Process1.cpp

#include <boost/interprocess/managed_shared_memory.hpp>
#include <boost/interprocess/allocators/allocator.hpp>
#include <boost/interprocess/containers/map.hpp>
#include <boost/interprocess/containers/string.hpp>
#include <iostream>
#include <vector>
#include <fstream>
using namespace boost::interprocess;
typedef managed_shared_memory::segment_manager segment_manager_t;
typedef allocator<void, segment_manager_t> void_allocator;
typedef allocator<char, segment_manager_t> char_allocator;
typedef basic_string<char, std::char_traits<char>, char_allocator>
char_string;
typedef std::pair<const char_string, char_string> map_value_type;
typedef allocator<map_value_type, segment_manager_t>
map_value_type_allocator;
typedef multimap< char_string, char_string, std::less<char_string>,
map_value_type_allocator> complex_map_type;

std::string trim(const char *str)
{
std::string ptr;
int i,j=0;
for(i=0;str[i]!='\0';i++)
{
if (str[i] != ' ' && str[i] != '\t')
ptr += str[i];
}
return ptr;
}

int main (int argc, char* argv[])
{
using namespace std;
const char* MySharedMemory= "MySharedMemory";
shared_memory_object::remove(MySharedMemory); // It works
fine but when put on comment then

managed_shared_memory segment(open_or_create,MySharedMemory, 999999999);
void_allocator alloc_inst (segment.get_segment_manager());
complex_map_type *mymap =
segment.construct<complex_map_type>("MyMap")(std::less<char_string>(),
alloc_inst);
std::cout << mymap->size() << std::endl;
ifstream in(argv[1]);
char line[1000];
char recline[100];
int index = 0;
while(in)
{
in.getline(line,1000);
int len = strlen(line);
strcpy(recline,line);
if(in)
{
std::stringstream ss;
//std::cout << index << endl;
ss<< index;
char name[1000];
strcpy(name, "1:");
std::cout << name << endl;
char* pch;
pch = strtok(line,"\t");
vector <const char*> vec;
while(pch != NULL)
{
vec.push_back(pch);
pch = strtok(NULL,"\t");
}
std::string y = trim(vec[vec.size()-1]);
std::cout << y << endl;
char_string cs(y.c_str(), alloc_inst);
strcat(name, (ss.str()).c_str());
char_string indexvalue(name, alloc_inst);
std::cout << cs << "\t" << indexvalue << std::endl;
mymap->insert(std::pair<char_string, char_string>(cs,indexvalue));
index += len;
index += 1;
}
}
return 0;
}

It works fine if i create a new sharedmemroy and use it but when i run this
code again by putting comment on
shared_memory_object::remove(MySharedMemory) to use the existing
Sharedmemory and map in it.
Exception
terminate called after throwing an instance of
'boost::interprocess::interprocess_exception'
what(): boost::interprocess_exception::library_error
Aborted

What is the reason for this? And how can i overcome such pblm? Thanks in
advance.

--
View this message in context: http://www.nabble.com/Information-about-create_only-and-open_or_create-tp25902079p26020156.html


Sent from the Boost - Users mailing list archive at Nabble.com.

_______________________________________________

Christoph Gysin

unread,
Oct 24, 2009, 5:38:18 AM10/24/09
to boost...@lists.boost.org
>   managed_shared_memory segment(open_or_create,MySharedMemory, 999999999);

This is not a valid mode you pass to managed_shared_memory. Read man 2 open.

And btw, there is a trim() function already in boost string_algo:
http://www.boost.org/doc/libs/1_40_0/doc/html/boost/algorithm/trim.html

But it doesn't behave exactly like your implementation. If you really
intended that behaviour you could use:
http://www.boost.org/doc/libs/1_40_0/doc/html/boost/algorithm/replace_all.html

manish4gupta

unread,
Oct 26, 2009, 4:06:02 AM10/26/09
to boost...@lists.boost.org

But i am getting the following defintion for the function.

basic_managed_shared_memory (open_or_create_t open_or_create,
const char *name, std::size_t size,
const void *addr = 0)
: base_t()
, base2_t(open_or_create, name, size, read_write, addr,
create_open_func_t(get_this_pointer(),
detail::DoOpenOrCreate))
{}
I think i am using in same way. Can u suggest how it can be used in
defferent way.Thanks in advance.

--
View this message in context: http://www.nabble.com/Information-about-create_only-and-open_or_create-tp25902079p26055704.html


Sent from the Boost - Users mailing list archive at Nabble.com.

_______________________________________________

Christoph Gysin

unread,
Oct 26, 2009, 1:58:09 PM10/26/09
to boost...@lists.boost.org
> But i am getting the following defintion for the function.

Sorry, I wasn't reading your post correctly. I was referring to
open_shared_object.

For me, the following stripped down example throws already:

#include <boost/interprocess/managed_shared_memory.hpp>
using namespace boost::interprocess;


int main (int argc, char *argv[])
{

managed_shared_memory segment(open_or_create, "MySharedMemory", 999999999);
}

$ ./a.out


terminate called after throwing an instance of
'boost::interprocess::interprocess_exception'
what():

Aborted

This is due to an illegal size for the shared memory area on my
plattform (linux):

$ cat /proc/sys/kernel/shmmax
33554432

Shouldn't the exception report "invalid argument" if size > SHMMAX?

manish4gupta, what system are you running the example on?

Manish Kumar

unread,
Oct 26, 2009, 6:15:49 PM10/26/09
to boost...@lists.boost.org
I am running on Itanium IA64. Still if i keep the size 9999999, still there will be error.


--
Manish Gupta
Bioinformatics Software Developer
Synamatix Sdn Bhd.
Kulalumpur, Malaysia
Reply all
Reply to author
Forward
0 new messages