Sophie

Sophie

distrib > Mandriva > 10.0-com > i586 > by-pkgid > 21280410b6ea906d791d7a12afae2579 > files > 332

libace5-doc-5.4-2mdk.i586.rpm

<!-- page05.html,v 1.7 2000/11/27 17:56:44 othman Exp -->
<HTML>
<HEAD>
   <META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
   <META NAME="Author" CONTENT="James CE Johnson">
   <TITLE>ACE Tutorial 019</TITLE>
</HEAD>
<BODY TEXT="#000000" BGCOLOR="#FFFFFF" LINK="#000FFF" VLINK="#FF0F0F">

<CENTER><B><FONT SIZE=+2>ACE Tutorial 019</FONT></B></CENTER>

<CENTER><B><FONT SIZE=+2>Sharing your Memories</FONT></B></CENTER>

<P>
<HR WIDTH="100%">
      Here's the mysterious shmem.h.  I wanted to show it after the
      placement-new client and server so that the SharedData object
      would have some relevance.
    <HR>
<HR width=50%><P><center>shmem.h</center><HR width=50%>
<PRE>
<font color=red>// page05.html,v 1.7 2000/11/27 17:56:44 othman Exp</font>

<font color=blue>#ifndef</font> <font color=purple>SHMEM_H</font>
<font color=blue>#define</font> <font color=purple>SHMEM_H</font>

<font color=red>// This is where you'll find the ACE_Shared_Memory_SV object</font>
<font color=blue>#include</font> "<A HREF="../../../ace/Shared_Memory_SV.h">ace/Shared_Memory_SV.h</A>"

<font color=red>// SHMSZ is just enough for the alphabet and a null terminator</font>
<font color=blue>#define</font> <font color=purple>SHMSZ</font> 27

<font color=red>// Play with this, pick a value you like that isn't used by something else.</font>
<font color=blue>#define</font> <font color=purple>SHM_KEY</font> 4200

<font color=red>/*
  This is what we stuff into shared memory via placement new in the
  second client/server pair.  Notice that it is a very basic object
  with no virtual methods and only concrete data.
 */</font>
class SharedData
{
public:
  <font color=red>// Construct the object and optionally initialize buf_.</font>
  SharedData (int initialized = 1);

  <font color=red>// Put some data into buf_</font>
  void set (void);

  <font color=red>// Show the data in buf_</font>
  void show (void);

  <font color=red>// What is the value of available_</font>
  int available (void);

  <font color=red>// Set the value of available_</font>
  void available (int not_in_use);

protected:
  <font color=red>// Big enough for a simple message</font>
  char buf_[128];
  <font color=red>// A cheap mutex</font>
  int available_;
};

<font color=blue>#endif</font> <font color=red>/* SHMEM_H */</font>
</PRE>
<HR width=50%><P><center>shmem.cpp</center><HR width=50%>
<PRE>
<font color=red>// page05.html,v 1.7 2000/11/27 17:56:44 othman Exp</font>

<font color=blue>#include</font> "<font color=green>shmem.h</font>"

<font color=blue>#include</font> "<A HREF="../../../ace/Log_Msg.h">ace/Log_Msg.h</A>"

#if ! defined (ACE_LACKS_SYSV_SHMEM)

<font color=red>/*
  Set the available_ flag to zero & optionally initialize the buf_
  area.
*/</font>

<font color=#008888>SharedData::SharedData</font> (int initialize)
  : available_ (0)
{
  if (initialize)
    <font color=#008888>ACE_OS::sprintf</font> (buf_, "<font color=green>UNSET\n</font>");
}

<font color=red>/*
  Write the process ID into the buffer.  This will prove to us that
  the data really is shared between the client and server.
*/</font>
void <font color=#008888>SharedData::set</font> (void)
{
  <font color=#008888>ACE_OS::sprintf</font> (buf_,
                   "<font color=green>My PID is (%d)\n</font>",
                   <font color=#008888>ACE_OS::getpid</font> ());
}

<font color=red>/*
  Display the buffer to the user
*/</font>
void <font color=#008888>SharedData::show</font>(void)
{
  ACE_DEBUG ((LM_INFO,
              "<font color=green>(%P|%t) Shared Data text is (%s)\n</font>",
              buf_));
}

<font color=red>// Show flag</font>
int <font color=#008888>SharedData::available</font>(void)
{
  return available_;
}

<font color=red>// Set flag</font>
void <font color=#008888>SharedData::available</font>(int a)
{
  available_ = a;
}

<font color=blue>#endif</font> <font color=red>/* ACE_LACKS_SYSV_SHMEM */</font>
</PRE>
<P><HR WIDTH="100%">
<CENTER>[<A HREF="../online-tutorials.html">Tutorial Index</A>] [<A HREF="page06.html">Continue This Tutorial</A>]</CENTER>