#!/bin/sh # This is a shell archive (produced by GNU sharutils 4.2). # To extract the files from this archive, save it to some FILE, remove # everything before the `!/bin/sh' line above, then type `sh FILE'. # # Made on 2000-03-19 15:00 EST by <jcej@chiroptera.tragus.org>. # Source directory was `/home/jcej/projects/ACE_wrappers/docs/tutorials/019'. # # Existing files will *not* be overwritten unless `-c' is specified. # # This shar contains: # length mode name # ------ ---------- ------------------------------------------ # 409 -rw-rw-r-- hdr # 71 -rw-rw-r-- bodies # 2006 -rw-rw-r-- page01.pre # 562 -rw-rw-r-- page02.pre # 217 -rw-rw-r-- page03.pre # 674 -rw-rw-r-- page04.pre # 178 -rw-rw-r-- page05.pre # 401 -rw-rw-r-- page06.pre # save_IFS="${IFS}" IFS="${IFS}:" gettext_dir=FAILED locale_dir=FAILED first_param="$1" for dir in $PATH do if test "$gettext_dir" = FAILED && test -f $dir/gettext \ && ($dir/gettext --version >/dev/null 2>&1) then set `$dir/gettext --version 2>&1` if test "$3" = GNU then gettext_dir=$dir fi fi if test "$locale_dir" = FAILED && test -f $dir/shar \ && ($dir/shar --print-text-domain-dir >/dev/null 2>&1) then locale_dir=`$dir/shar --print-text-domain-dir` fi done IFS="$save_IFS" if test "$locale_dir" = FAILED || test "$gettext_dir" = FAILED then echo=echo else TEXTDOMAINDIR=$locale_dir export TEXTDOMAINDIR TEXTDOMAIN=sharutils export TEXTDOMAIN echo="$gettext_dir/gettext -s" fi touch -am 1231235999 $$.touch >/dev/null 2>&1 if test ! -f 1231235999 && test -f $$.touch; then shar_touch=touch else shar_touch=: echo $echo 'WARNING: not restoring timestamps. Consider getting and' $echo "installing GNU \`touch', distributed in GNU File Utilities..." echo fi rm -f 1231235999 $$.touch # if mkdir _sh00389; then $echo 'x -' 'creating lock directory' else $echo 'failed to create lock directory' exit 1 fi # ============= hdr ============== if test -f 'hdr' && test "$first_param" != -c; then $echo 'x -' SKIPPING 'hdr' '(file already exists)' else $echo 'x -' extracting 'hdr' '(text)' sed 's/^X//' << 'SHAR_EOF' > 'hdr' && <HTML> <HEAD> X <META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1"> X <META NAME="Author" CONTENT="James CE Johnson"> X <TITLE>ACE Tutorial 019</TITLE> </HEAD> <BODY TEXT="#000000" BGCOLOR="#FFFFFF" LINK="#000FFF" VLINK="#FF0F0F"> X <CENTER><B><FONT SIZE=+2>ACE Tutorial 019</FONT></B></CENTER> X <CENTER><B><FONT SIZE=+2>Sharing your Memories</FONT></B></CENTER> X <P> <HR WIDTH="100%"> SHAR_EOF $shar_touch -am 03191459100 'hdr' && chmod 0664 'hdr' || $echo 'restore of' 'hdr' 'failed' if ( md5sum --help 2>&1 | grep 'sage: md5sum \[' ) >/dev/null 2>&1 \ && ( md5sum --version 2>&1 | grep -v 'textutils 1.12' ) >/dev/null; then md5sum -c << SHAR_EOF >/dev/null 2>&1 \ || $echo 'hdr:' 'MD5 check failed' 3b7d0df272c309b4b8d67d872956bae3 hdr SHAR_EOF else shar_count="`LC_ALL= LC_CTYPE= LANG= wc -c < 'hdr'`" test 409 -eq "$shar_count" || $echo 'hdr:' 'original size' '409,' 'current size' "$shar_count!" fi fi # ============= bodies ============== if test -f 'bodies' && test "$first_param" != -c; then $echo 'x -' SKIPPING 'bodies' '(file already exists)' else $echo 'x -' extracting 'bodies' '(text)' sed 's/^X//' << 'SHAR_EOF' > 'bodies' && PAGE=2 server.cpp client.cpp server2.cpp client2.cpp shmem.h shmem.cpp SHAR_EOF $shar_touch -am 1228162198 'bodies' && chmod 0664 'bodies' || $echo 'restore of' 'bodies' 'failed' if ( md5sum --help 2>&1 | grep 'sage: md5sum \[' ) >/dev/null 2>&1 \ && ( md5sum --version 2>&1 | grep -v 'textutils 1.12' ) >/dev/null; then md5sum -c << SHAR_EOF >/dev/null 2>&1 \ || $echo 'bodies:' 'MD5 check failed' 7eebfcd7b463404b494b4c97107f9d14 bodies SHAR_EOF else shar_count="`LC_ALL= LC_CTYPE= LANG= wc -c < 'bodies'`" test 71 -eq "$shar_count" || $echo 'bodies:' 'original size' '71,' 'current size' "$shar_count!" fi fi # ============= page01.pre ============== if test -f 'page01.pre' && test "$first_param" != -c; then $echo 'x -' SKIPPING 'page01.pre' '(file already exists)' else $echo 'x -' extracting 'page01.pre' '(text)' sed 's/^X//' << 'SHAR_EOF' > 'page01.pre' && X Did you ever wish you could read someone's mind? Or that they could X read yours? X <p> X Well, we won't be doing that here but we'll try to get close by X letting your code do something similar: Shared Memory. X <p> X What we're going to do is ask the operating system to set X aside a part of RAM that we can share with another process. By X doing this, we can allow our applications to swap data very X efficiently. X <p> X Along the way, we'll have to come up with some sort of X coordination betweent the processes. That is the most X difficult part of a shared memory system. In the tutorial we're X just going to take a simplistic approach (eg -- busy loop) but X real-world applications will need to take a serious look at X process-level synch mechanisms such as ACE_*_Semaphore. X <P> X Caveat: I've barely begun to use shared memory X myself. This tutorial and the next are very simple-minded and X primitive. Anyone who wants to provide more realistic X replacements is encouraged to drop me a note X (<A HREF="mailto:jcej@lads.com">jcej@lads.com</A>). <P> A tutorial wouldn't be complete without Kirthika's abstract: <UL> This tutorial shows how to use memory as a shared resource between the client and server processes. ACE_Shared_Memory_SV has been used for creating the shared memory segment. Two different approaches have been used. One uses the malloc () method to allocate memory while the other uses the placement new expression (C++) of allocating a memory chunk in advance and using it on demand. <P> The server has a string of alphabets from a-z which are converted to uppercase by the client proving that the same memory locations are being shared between the two processes. The syncronisation between the server and client is done using the ACE_OS::sleep () instead of a semaphore/mutexes with the aim of keeping our first dive into shared memory usage simple and easy to fathom. </UL> SHAR_EOF $shar_touch -am 03191459100 'page01.pre' && chmod 0664 'page01.pre' || $echo 'restore of' 'page01.pre' 'failed' if ( md5sum --help 2>&1 | grep 'sage: md5sum \[' ) >/dev/null 2>&1 \ && ( md5sum --version 2>&1 | grep -v 'textutils 1.12' ) >/dev/null; then md5sum -c << SHAR_EOF >/dev/null 2>&1 \ || $echo 'page01.pre:' 'MD5 check failed' a039d9160f8f35867c38c73ced65aaef page01.pre SHAR_EOF else shar_count="`LC_ALL= LC_CTYPE= LANG= wc -c < 'page01.pre'`" test 2006 -eq "$shar_count" || $echo 'page01.pre:' 'original size' '2006,' 'current size' "$shar_count!" fi fi # ============= page02.pre ============== if test -f 'page02.pre' && test "$first_param" != -c; then $echo 'x -' SKIPPING 'page02.pre' '(file already exists)' else $echo 'x -' extracting 'page02.pre' '(text)' sed 's/^X//' << 'SHAR_EOF' > 'page02.pre' && We'll first take a look at the server side. As usual with co-operating applications, you need the server up and running first. In the case of shared memory applications, the server will create the shared memory segment. In this example, it will also remove the segment when done. It is important to realize though that the segment can be created external to your application and can persist beyond it's lifetime. In fact, you can use shared memory to create a layer of persistence between application instances (at least, until the machine comes down.) <HR> SHAR_EOF $shar_touch -am 03191459100 'page02.pre' && chmod 0664 'page02.pre' || $echo 'restore of' 'page02.pre' 'failed' if ( md5sum --help 2>&1 | grep 'sage: md5sum \[' ) >/dev/null 2>&1 \ && ( md5sum --version 2>&1 | grep -v 'textutils 1.12' ) >/dev/null; then md5sum -c << SHAR_EOF >/dev/null 2>&1 \ || $echo 'page02.pre:' 'MD5 check failed' c879cf850b3141d673c5c56e98a80e16 page02.pre SHAR_EOF else shar_count="`LC_ALL= LC_CTYPE= LANG= wc -c < 'page02.pre'`" test 562 -eq "$shar_count" || $echo 'page02.pre:' 'original size' '562,' 'current size' "$shar_count!" fi fi # ============= page03.pre ============== if test -f 'page03.pre' && test "$first_param" != -c; then $echo 'x -' SKIPPING 'page03.pre' '(file already exists)' else $echo 'x -' extracting 'page03.pre' '(text)' sed 's/^X//' << 'SHAR_EOF' > 'page03.pre' && The client app looks much like the server. We could have included the CREATE flag with no ill effects but note the use of close() instead of remove(). Picking the correct detachment method is rather important! <HR> SHAR_EOF $shar_touch -am 03191459100 'page03.pre' && chmod 0664 'page03.pre' || $echo 'restore of' 'page03.pre' 'failed' if ( md5sum --help 2>&1 | grep 'sage: md5sum \[' ) >/dev/null 2>&1 \ && ( md5sum --version 2>&1 | grep -v 'textutils 1.12' ) >/dev/null; then md5sum -c << SHAR_EOF >/dev/null 2>&1 \ || $echo 'page03.pre:' 'MD5 check failed' a244be660bba913fe08d218180cefca5 page03.pre SHAR_EOF else shar_count="`LC_ALL= LC_CTYPE= LANG= wc -c < 'page03.pre'`" test 217 -eq "$shar_count" || $echo 'page03.pre:' 'original size' '217,' 'current size' "$shar_count!" fi fi # ============= page04.pre ============== if test -f 'page04.pre' && test "$first_param" != -c; then $echo 'x -' SKIPPING 'page04.pre' '(file already exists)' else $echo 'x -' extracting 'page04.pre' '(text)' sed 's/^X//' << 'SHAR_EOF' > 'page04.pre' && X Before we move on to shmem.h, I want to show a different approach. In X this new client/server pair, I use placement new to stuff an object X (instead of a blob of bytes) into the shared memory segment. X <P> X There are a few caveats to putting objects into shared memory. The X most important ones all deal with pointers: X <ul> X <li>Be sure your pointers point into the shared memory and not X local process memory. X <li>Only in very special cases will objects with virtual methods X work (because of the VTable pointers). X </ul> <P> That's not to say you shouldn't try... Just try carefully and test a lot! X <HR> SHAR_EOF $shar_touch -am 03191459100 'page04.pre' && chmod 0664 'page04.pre' || $echo 'restore of' 'page04.pre' 'failed' if ( md5sum --help 2>&1 | grep 'sage: md5sum \[' ) >/dev/null 2>&1 \ && ( md5sum --version 2>&1 | grep -v 'textutils 1.12' ) >/dev/null; then md5sum -c << SHAR_EOF >/dev/null 2>&1 \ || $echo 'page04.pre:' 'MD5 check failed' 721c4a100d908d9d62c9d98950ace482 page04.pre SHAR_EOF else shar_count="`LC_ALL= LC_CTYPE= LANG= wc -c < 'page04.pre'`" test 674 -eq "$shar_count" || $echo 'page04.pre:' 'original size' '674,' 'current size' "$shar_count!" fi fi # ============= page05.pre ============== if test -f 'page05.pre' && test "$first_param" != -c; then $echo 'x -' SKIPPING 'page05.pre' '(file already exists)' else $echo 'x -' extracting 'page05.pre' '(text)' sed 's/^X//' << 'SHAR_EOF' > 'page05.pre' && X Here's the mysterious shmem.h. I wanted to show it after the X placement-new client and server so that the SharedData object X would have some relevance. X <HR> SHAR_EOF $shar_touch -am 03191459100 'page05.pre' && chmod 0664 'page05.pre' || $echo 'restore of' 'page05.pre' 'failed' if ( md5sum --help 2>&1 | grep 'sage: md5sum \[' ) >/dev/null 2>&1 \ && ( md5sum --version 2>&1 | grep -v 'textutils 1.12' ) >/dev/null; then md5sum -c << SHAR_EOF >/dev/null 2>&1 \ || $echo 'page05.pre:' 'MD5 check failed' 42f4ddae6f0ce1583d1dc03d5e485cba page05.pre SHAR_EOF else shar_count="`LC_ALL= LC_CTYPE= LANG= wc -c < 'page05.pre'`" test 178 -eq "$shar_count" || $echo 'page05.pre:' 'original size' '178,' 'current size' "$shar_count!" fi fi # ============= page06.pre ============== if test -f 'page06.pre' && test "$first_param" != -c; then $echo 'x -' SKIPPING 'page06.pre' '(file already exists)' else $echo 'x -' extracting 'page06.pre' '(text)' sed 's/^X//' << 'SHAR_EOF' > 'page06.pre' && X That's it! I warned you that this one was pretty primitive. X X <ul> X <li><A HREF="server.cpp">server.cpp</A> X <li><A HREF="client.cpp">client.cpp</A> X <li><A HREF="server2.cpp">server2.cpp</A> X <li><A HREF="client2.cpp">client2.cpp</A> X <li><A HREF="shmem.h">shmem.h</A> X <li><A HREF="shmem.cpp">shmem.cpp</A> X <li><A HREF="Makefile">Makefile</A> X </ul> SHAR_EOF $shar_touch -am 03191459100 'page06.pre' && chmod 0664 'page06.pre' || $echo 'restore of' 'page06.pre' 'failed' if ( md5sum --help 2>&1 | grep 'sage: md5sum \[' ) >/dev/null 2>&1 \ && ( md5sum --version 2>&1 | grep -v 'textutils 1.12' ) >/dev/null; then md5sum -c << SHAR_EOF >/dev/null 2>&1 \ || $echo 'page06.pre:' 'MD5 check failed' 6e3002889733f5e80e7f9cc945965955 page06.pre SHAR_EOF else shar_count="`LC_ALL= LC_CTYPE= LANG= wc -c < 'page06.pre'`" test 401 -eq "$shar_count" || $echo 'page06.pre:' 'original size' '401,' 'current size' "$shar_count!" fi fi rm -fr _sh00389 exit 0