Re: Information on use of Netsys.shm_open

Gerd Stolpmann <[email protected]> Tue, 23 Mar 2010 19:35:01 +0100
Newsgroups gmane.comp.lang.ocaml.lib.net.devel
Message-ID <1269369301.7681.134.camel@thinkpad>
On Di, 2010-03-23 at 19:15 +0100, Johan Mazel wrote:
> Hi
> I'm trying this little piece of code to open a shared memory and
> modify it.
> The code looks like this :
> 
> open Unix
> open Bigarray.Array1
> 
> let _ =
>     
>     Printf.printf("Test shared memory\n");
>         
>     if (Netsys.have_posix_shm () = false) then
>         
>         Printf.printf "No POSIX SHM\n"
>     else
>         (
>             
>             Printf.printf "POSIX SHM OK\n";
>             
>             let shm_name = "/shm_test" in
>             
>             let right_list = ref [] in
>             
>             right_list := Netsys.SHM_O_CREAT :: !right_list;
>             right_list := Netsys.SHM_O_RDWR :: !right_list;
>             
>             ignore(
>                     try
>                         (
>                             Printf.printf "Netsys.shm_open call\n";
>                             
>                             let sd = Netsys.shm_open shm_name !
> right_list 1 in
>                             
>                             Printf.printf "Netsys.shm_open ok\n";
>                             
>                             let my_big_array =
> Bigarray.Array1.map_file sd Bigarray.char Bigarray.c_layout true 100
> in
>                             
>                             let big_array_size = Bigarray.Array1.dim
> my_big_array in
>                             
>                             Printf.printf "Size of BigArray: %d\n"
> big_array_size;
>                             
>                             Bigarray.Array1.set my_big_array 0 'a';
>                         )
>                     with
>                     | Unix_error (_ , invoked_function ,
> shm_name_failure) ->
>                             (
>                                 Printf.printf
>                                     "Opening failed through using
> function %s on SHM %s\n"
>                                     invoked_function
>                                     shm_name_failure
>                                 ;
>                             )
>                 );
>             
>             Printf.printf "Shared memory tested\n";
>         )
> 
> I can create the shared memory file descriptor, but only once.
> When I try to access it again (y rzlaunching my program, there is an
> error on shm_open.
> 
> I have to guess about a cause to this problem: first, I did not set
> the right in the right manner and second, I do not use unlink.
> The doc about the permissions is very light in this page
> (http://projects.camlcity.org/projects/dl/ocamlnet-2.2.9/doc/html-main/Netsys.html). I tried with with 0, 1, 777 and none of these are working.
> For the unlink function, my understanding is that it destroys the
> file. But maybe, it has the same behviour as "close" in C for a file
> descriptor.

Well, shm_open just calls the C function with the same name, so for any
detailed documentation see there.

Your guess is right, the error in your code is the bad permissions
bitfield. 0o666 instead of 1 should be just ok to give everybody
read/write access. The bitfield is interpreted as for any other file.

Note that the shared memory file may even pop up in the real file
hierarchy (depending on your OS). E.g. for Linux it
is /dev/shm/shm_test. Another popular location is somewhere in /tmp.

Gerd

> 
> Thanks in advance for your time.
> 
> Johan Mazel
> ------------------------------------------------------------------------------
> Download Intel&#174; Parallel Studio Eval
> Try the new software tools for yourself. Speed compiling, find bugs
> proactively, and fine-tune applications for parallel performance.
> See why Intel Parallel Studio got high marks during beta.
> http://p.sf.net/sfu/intel-sw-dev
> _______________________________________________ Ocamlnet-devel mailing list [email protected] https://lists.sourceforge.net/lists/listinfo/ocamlnet-devel


-- 
------------------------------------------------------------
Gerd Stolpmann, Bad Nauheimer Str.3, 64289 Darmstadt,Germany 
[email protected]          http://www.gerd-stolpmann.de
Phone: +49-6151-153855                  Fax: +49-6151-997714
------------------------------------------------------------


------------------------------------------------------------------------------
Download Intel&#174; Parallel Studio Eval
Try the new software tools for yourself. Speed compiling, find bugs
proactively, and fine-tune applications for parallel performance.
See why Intel Parallel Studio got high marks during beta.
http://p.sf.net/sfu/intel-sw-dev