Boost logo

Boost Users :

Subject: [Boost-users] core when using managed shared memory -- file based
From: Sean K (sk92129_at_[hidden])
Date: 2014-06-27 13:54:16


I have a client and server set up where the client would write to the
shared memory via a managed memory vector. And then the server would read
that shared memory via the managed mapped file.

It works for most situations.

However, I just ran into a situation where the client -- writing to the
vector, crashes.

and I am trying to figure out what caused the crash.

Below are the client (writer) and the server (reader) source code.... as
close as to the current.

Any insights as to why the vector.push_back() crashed? (this is happening
on RH 5.9 64 bit OS.. but the code is compiled for 32 bit.)

client:
#include <iostream>
#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;
#include "fileheader.h"

int main() {
        std::string data = "Hello";
        char myinput[1024];
        printf("enter number of count \n");
        scanf("%s", &myinput);
        printf("You entered %s \n", myinput);
        data = myinput;
        int count = std::atoi(myinput);
        for (int counter = 0; counter < count; counter ++) {
                try {
                        std::string tmpPath =
"/var/tmp/log/testfile/ManagedMappedFile";;
                        printf("temp directory: %s \n", tmpPath.c_str());
                        managed_mapped_file segment(open_or_create,
tmpPath.c_str(), SHARED_MEMORY_SIZE);

                        printf("initialized managed shared memory \n");
                        BString bstrData(segment.get_segment_manager());
                        bstrData = "hello world";
                        StrAllocator
alloc_init(segment.get_segment_manager());
                        printf("ALLOCATED memory successfully\n");
                        BStrVector *myvec =
segment.find_or_construct<BStrVector> (SHARED_VECTOR_NAME)(alloc_init);
                        myvec->push_back(bstrData);
                        segment.flush();

                        printf("inserted mystring into shmvector %d\n",
counter);
                } catch (boost::interprocess::bad_alloc e) {
                        printf("we caught bad alloc\n");
                        printf("explanation: %s\n", e.what());
                        printf("error code: %d\n", e.get_error_code());
                        printf("native: %d\n", e.get_native_error());
                        break;
                } catch (...) {
                        printf("caught unknown exception\n");
                }
        }

        printf("wrote number");
        return 0;
}

contents of fileheader.h

#ifndef FILEHEADER_H_
#define FILEHEADER_H_
#include <boost/interprocess/managed_shared_memory.hpp>
#include <boost/interprocess/managed_mapped_file.hpp>
#include <boost/thread/thread.hpp>
#include <boost/interprocess/containers/vector.hpp>
#include <boost/interprocess/containers/string.hpp>
#include <boost/interprocess/allocators/allocator.hpp>
#include <boost/interprocess/detail/tmp_dir_helpers.hpp>

using namespace boost::interprocess;

typedef boost::interprocess::managed_mapped_file::segment_manager
segment_manager_t;
typedef boost::interprocess::allocator<char, segment_manager_t>
ShmemAllocator;
typedef boost::interprocess::basic_string<char, std::char_traits<char>,
ShmemAllocator> BString;
typedef boost::interprocess::allocator<BString, segment_manager_t>
StrAllocator;
typedef boost::interprocess::vector<BString, StrAllocator> BStrVector;
typedef std::pair<BString *, std::size_t> BStringPair;
typedef std::pair<BStrVector *, std::size_t> BStrVectorPair;
const char * SHARED_VECTOR_NAME = "MyVector";
const int SHARED_MEMORY_SIZE = 65536;
#endif /* FILEHEADER_H_ */

g++ -m32 -fPIC -Xlinker -zmuldefs filewriter.cpp -o writer
-I/cots/boost_1_42_0/include -L/cots/boost_1_42_0/lib -lboost_thread
-lboost_system -pthread

Server:

#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;
#include "../../filewriter/src/fileheader.h"
int main() {
        int count = 0;
        std::string tmpPath = "/var/tmp/log/testfile";
        //boost::interprocess::detail::get_tmp_base_dir(tmpPath);
        printf("temp directory: %s \n", tmpPath.c_str());

//boost::interprocess::detail::create_tmp_dir_and_get_filename("ManagedMappedFile",
tmpPath);
        printf("temp file: %s \n", tmpPath.c_str());
// file_mapping::remove(tmpPath.c_str());
        while (true) {
                try {
                        managed_mapped_file segment(open_only,
tmpPath.c_str());
                        if (segment.get_num_named_objects() > 0){
                                BStrVectorPair message =
segment.find<BStrVector> (SHARED_VECTOR_NAME);
                                printf("stuff in memory\n");
                                int size = message.first->size();
                                int retcode = 0;
                                bool success=true;
                                printf("PROCESS: Number of messages:
 %d\n", size);
                                for (int imessage=0; imessage < size ;
imessage++){
                                        const char * const buffer =
message.first->at(imessage).c_str();
                                        int len =
message.first->at(imessage).length();
                                        printf("PROCESS: going to process
this message: %s\n" , buffer);
                                }
                                printf("p.second: %d \n", message.second);

segment.destroy<BStrVectorPair>(SHARED_VECTOR_NAME);
                        }
                } catch (exception& e){
                        printf("exception %s", e.what());
                } catch (...){
                        printf("unexpected exception");
                }
            int msecs = 2000;

boost::this_thread::sleep(boost::posix_time::milliseconds(msecs));
                printf("sleeping...\n");
    }
        return 0;
}

g++ -m32 -fPIC -Xlinker -zmuldefs filereader.cpp -o reader
-I/cots/boost_1_42_0/include -L/cots/boost_1_42_0/lib -lboost_thread
-lboost_system -pthread



Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net