Boost C++ Libraries: Ticket #7420: If I call managed_shared_memory() function when I create a lot of objects, it ocurrs error. https://svn.boost.org/trac10/ticket/7420 <p> I want to call managed_shared_memory(), when I create a lot of objects... however,it ocurrs error. In additional, it's too show to create shared memory and segment.. </p> <p> You can check a attached file. </p> <p> Should I call managed_shared_memory() only one time? </p> <p> please answer.. </p> en-us Boost C++ Libraries /htdocs/site/boost.png https://svn.boost.org/trac10/ticket/7420 Trac 1.4.3 mimirang@… Tue, 25 Sep 2012 07:48:21 GMT attachment set https://svn.boost.org/trac10/ticket/7420 https://svn.boost.org/trac10/ticket/7420 <ul> <li><strong>attachment</strong> → <span class="trac-field-new">boost_interprocess_errorTestCode.cpp</span> </li> </ul> Ticket Robert Luberda <robert@…> Fri, 05 Oct 2012 09:38:32 GMT <link>https://svn.boost.org/trac10/ticket/7420#comment:1 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/7420#comment:1</guid> <description> <p> Hi, </p> <p> We have observed a similar issue under Linux. We have recently switched from boost 1.42 to boost 1.48 and noticed that out application, which creates about 10,000 /dev/shm files, started to fail with "Too many open files" error. </p> <p> It turned out the for some reason the interprocess library no longer closes file descriptors after shm_open() and mmap() calls. This is most probably caused by the following `if' that was added into managed_open_or_create_impl.hpp (the value of <a class="missing wiki">StoreDevice</a> seems to be always true) </p> <pre class="wiki"> if(StoreDevice){ this-&gt;DevHolder::get_device() = boost::interprocess::move(dev); } </pre><p> The same issue exists on boost 1.50 and boost 1.51. </p> <p> <strong>Sample program</strong>: </p> <pre class="wiki">/* First set open files limit to 10 * Then try to create 20 shm files and display number of files * in /proc/self/fd after each created file */ #include &lt;boost/interprocess/managed_shared_memory.hpp&gt; #include &lt;boost/lexical_cast.hpp&gt; #include &lt;unistd.h&gt; #include &lt;vector&gt; #include &lt;stdio.h&gt; #include &lt;sys/resource.h&gt; int main(int /*argc*/, char ** /*argv*/) { // decrease limit of open files struct rlimit rlp = { 10, 10 }; setrlimit(RLIMIT_NOFILE, &amp;rlp); std::system("rm -f /dev/shm/ShmTest*"); using namespace boost::interprocess; const std::string fdCountStr = "ls -1 /proc/" + boost::lexical_cast&lt;std::string&gt;(getpid()) + "/fd | wc -l"; std::vector&lt;managed_shared_memory*&gt; v; for (int i = 0 ; i &lt; 20; ++i) { const std::string name = "ShmTest" + boost::lexical_cast&lt;std::string&gt;(i); v.push_back(new managed_shared_memory(create_only, name.c_str(), 1024)); std::system (fdCountStr.c_str()); } return 0; } </pre><p> <strong>Output when compiled with boost 1.48:</strong> </p> <pre class="wiki">&gt; ./a.out 4 5 6 7 8 9 terminate called after throwing an instance of 'boost::interprocess::interprocess_exception' what(): Too many open files Aborted (core dumped) </pre><p> <strong>Output when compiled with boost 1.42:</strong> </p> <pre class="wiki">&gt; ./a.out 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 </pre> </description> <category>Ticket</category> </item> <item> <author>kdk <kdkomsdev@…></author> <pubDate>Sat, 06 Oct 2012 16:23:01 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/7420#comment:2 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/7420#comment:2</guid> <description> <p> Hi </p> <p> I ran the attached program Robert Luerda on cygwin(win32/g++ (GCC) 4.5.3/boost 1.51.0) with 20,40,100 and 1000 as iteration values and no exception was thrown </p> <p> Administrator@VALUED-7ZSFP29H ~ $ ./a.exe 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 </p> </description> <category>Ticket</category> </item> <item> <dc:creator>mimirang</dc:creator> <pubDate>Mon, 08 Oct 2012 05:00:39 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/7420#comment:3 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/7420#comment:3</guid> <description> <p> Hi. </p> <p> Thank you for replying the bugs. I understood that It's OK on boost 1.51.0. However, When I ran my program "boost_interprocess_errorTestCode.cpp" on boost 1.51.0(win32, Visual Studio), it has still error. </p> <p> Please run "boost_interprocess_errorTestCode.cpp" with boost 1.51.0, and check the error. </p> <p> </p> </description> <category>Ticket</category> </item> <item> <dc:creator>Ion Gaztañaga</dc:creator> <pubDate>Mon, 08 Oct 2012 20:49:13 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/7420#comment:4 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/7420#comment:4</guid> <description> <p> Thanks for the report. First of all, creating so many shared memory segments it's not very likely. If you would want to emulate it with plain files, you'll need to open too many files. 10.000 managed segments at the same time, seems too high IMHO. </p> <p> However, I can't remember right now the exact reason why <a class="missing wiki">StoreDevice</a> needed to be true for shared memory managed segments. It is need for xsi_shared memory and not for windows_shared_memory. </p> <p> As a test, you could modify basic_managed_shared_memory (in managed_shared_memory.hpp) and change: </p> <blockquote> <p> , private ipcdetail::managed_open_or_create_impl&lt;shared_memory_object </p> <blockquote> <p> , AllocationAlgorithm::Alignment&gt; </p> </blockquote> </blockquote> <p> with </p> <blockquote> <p> , private ipcdetail::managed_open_or_create_impl&lt;shared_memory_object </p> <blockquote> <p> , AllocationAlgorithm::Alignment, true, false&gt; </p> </blockquote> </blockquote> <p> (that is, <a class="missing wiki">FileBased</a> = true and <a class="missing wiki">StoreDevice</a> = false) </p> <p> let me know if that fixes the problem. </p> <p> In Boost 1.52, mapped_region will hold a file descriptor in windows, to allow implementing blocking flush() in windows (as we need to call <a class="missing wiki">FlushFileBuffers</a>() with a file descriptor), so there will be another open file descriptor. If you know an alternative to avoid maintaining an open file descriptor only for this purpose, please let me know. </p> <p> In Unix, after mmap the file descriptor can be closed, as according to POSIX: </p> <p> "The mmap() function shall add an extra reference to the file associated with the file descriptor fildes which is not removed by a subsequent close() on that file descriptor. This reference shall be removed when there are no more mappings to the file." </p> <p> Please let me know if you find any problems with the proposed change and/or soon to be released Boost 1.52 </p> </description> <category>Ticket</category> </item> <item> <dc:creator>mimirang</dc:creator> <pubDate>Tue, 09 Oct 2012 00:59:23 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/7420#comment:5 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/7420#comment:5</guid> <description> <p> Hi. </p> <p> Thank you for replying. I modifed basic_managed_shared_memory following your solution. (Boost 1.51.0, Win32, Visual Studio 2008) </p> <p> change : </p> <hr /> <blockquote> <p> basic_managed_shared_memory </p> <blockquote> <p> : public ipcdetail::basic_managed_memory_impl </p> <blockquote> <p> &lt;<a class="missing wiki">CharType</a>, <a class="missing wiki">AllocationAlgorithm</a>, <a class="missing wiki">IndexType</a> ,ipcdetail::managed_open_or_create_impl&lt;shared_memory_object </p> <blockquote> <p> , AllocationAlgorithm::Alignment&gt;::<a class="missing wiki">ManagedOpenOrCreateUserOffset</a>&gt; </p> </blockquote> </blockquote> <p> , private ipcdetail::managed_open_or_create_impl&lt;shared_memory_object </p> <blockquote> <blockquote> <blockquote> <p> , AllocationAlgorithm::Alignment, true, false&gt; </p> </blockquote> </blockquote> </blockquote> </blockquote> </blockquote> <p> { </p> <blockquote> <p> <em>/ @cond typedef ipcdetail::basic_managed_memory_impl </em></p> <blockquote> <p> &lt;<a class="missing wiki">CharType</a>, <a class="missing wiki">AllocationAlgorithm</a>, <a class="missing wiki">IndexType</a>, ipcdetail::managed_open_or_create_impl </p> <blockquote> <p> &lt; shared_memory_object, AllocationAlgorithm::Alignment&gt;::<a class="missing wiki">ManagedOpenOrCreateUserOffset</a>&gt; base_t; </p> </blockquote> </blockquote> <p> typedef ipcdetail::managed_open_or_create_impl </p> <blockquote> <p> &lt;shared_memory_object, AllocationAlgorithm::Alignment, true,false&gt; base2_t; </p> </blockquote> </blockquote> <hr /> <p> However, It's not working. So, I modified my application program that calls mamaged_shared_memory() one time. </p> <p> I hope that you fix this problem, and release new Boost 1.52. </p> <p> Thank you so much to report and give me tips. </p> </description> <category>Ticket</category> </item> <item> <author>Robert Luberda <robert@…></author> <pubDate>Tue, 09 Oct 2012 21:42:00 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/7420#comment:6 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/7420#comment:6</guid> <description> <p> Hi, </p> <p> I did the exactly the same change as mimirang (i.e. added the "true, false" arguments in 3 places) and it fixes the issue for me. So it seems my issue is in fact different than the originally reported one. Sorry about not creating a new report for this. </p> <p> I agree that using over 10000 shared memory segments might seem quite unlikely, but we do this in our project - we use different kind of segments for different kind of data shared amongst several instances of application. </p> <p> Thanks, Robert </p> </description> <category>Ticket</category> </item> </channel> </rss>