summaryrefslogtreecommitdiff
path: root/docs/tutorials/019/page03.html
blob: 5b35f19c8ddfadefea942aec50a1b60f167b2cd3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
<!-- $Id$ -->
<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%">
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>
<PRE>
<font color=red>// $Id$</font>

<font color=red>// Again, the common stuff</font>
<font color=blue>#include</font> "<font color=green>shmem.h</font>"

<font color=blue>#if defined</font>(<font color=purple>ACE_LACKS_SYSV_SHMEM</font>)
int
main (int, char *[])
{
  ACE_ERROR_RETURN ((LM_ERROR,
                     "<font color=green>System V Shared Memory not available on this platform\n</font>"),
                    100);
}
#else <font color=red>// ACE_LACKS_SYSV_SHMEM</font>
int
main (int, char *[])
{
  <font color=red>/*
    Attach ourselves to the shared memory segment.
    */</font>
  ACE_Shared_Memory_SV shm_client (SHM_KEY, SHMSZ);

  <font color=red>/*
    Get our reference to the segment...
    */</font>
  char *shm = (char *) shm_client.malloc ();

  <font color=red>/*
    If the segment identified by SHM_KEY didn't exist then we'll
    get back a 0 from malloc().  You should do this check even
    if you include the CREATE flag 'cause you never know when it
    might fail.
    */</font>
  if (shm == 0)
    ACE_ERROR_RETURN ((LM_ERROR,
                       "<font color=green>(%P|%t) Could not get the segment!\n</font>"),
                      100);

  <font color=red>/*
    Does this match what your server said?
    */</font>
  ACE_DEBUG ((LM_INFO,
              "<font color=green>(%P|%t) Shared Memory is at 0x%x\n</font>",
              shm ));

  <font color=red>/*
    Show the shared data to the user and convert it all to
    uppper-case along the way.
    */</font>
  for (char *s = shm; *s != '\0'; s++)
    {
      putchar (*s);
      *s = toupper(*s);
    }

  putchar ('\n');

  <font color=red>/*
    Flag the server that we're done.
    */</font>
  *shm = '*';

  <font color=red>/*
    Here, we use close() instead of remove().  Remember, that
    will just remove our attachment to the segment.  Look
    closely at the 'nattch' column of the ipcs output & you'll
    see that this decrements it by one.
    */</font>
  shm_client.close();

  return 0;
}

<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="page04.html">Continue This Tutorial</A>]</CENTER>