summaryrefslogtreecommitdiff
path: root/docs/tutorials/Chap_3/ex02.html
blob: ed5f0caf497f97d8a30654870925de295108edb9 (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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
<HTML>
<!-- $Id$ -->
<HEAD>
   <META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
   <META NAME="Author" CONTENT="Ambreen Ilyas">
   <META NAME="GENERATOR" CONTENT="Mozilla/4.05 [en] (X11; I; SunOS 5.5.1 sun4u) [Netscape]">
   <TITLE>Example 2</TITLE>
</HEAD>
<BODY>
<FONT COLOR="#CC0000">/////////////////////////////////////////////////////////////////////////////////////////////////////////////////</FONT>
<BR><FONT COLOR="#CC0000">//// This example is from the ACE Programmers
Guide.</FONT>
<BR><FONT COLOR="#CC0000">////&nbsp; Chapter:&nbsp; "Memory Management"</FONT>
<BR><FONT COLOR="#CC0000">//// For details please see the guide at</FONT>
<BR><FONT COLOR="#CC0000">//// http://www.cs.wustl.edu/~schmidt/ACE.html</FONT>
<BR><FONT COLOR="#CC0000">////&nbsp; AUTHOR: Umar Syyid (usyyid@hns.com)</FONT>
<BR><FONT COLOR="#CC0000">//// and Ambreen Ilyas (ambreen@bitsmart.com)</FONT>
<BR><FONT COLOR="#CC0000">/////////////////////////////////////////////////////////////////////////////////////////////////////////////</FONT>

<P><FONT COLOR="#CC0000">//Example 2</FONT>
<BR><FONT COLOR="#000099">#include</FONT> <FONT COLOR="#006600">"ace/Shared_Memory_MM.h"</FONT>
<BR><FONT COLOR="#000099">#include</FONT> <FONT COLOR="#006600">"ace/Malloc.h"</FONT>
<BR><FONT COLOR="#000099">#include</FONT> <FONT COLOR="#006600">"ace/Malloc_T.h"</FONT>
<BR><FONT COLOR="#000099">#define </FONT><FONT COLOR="#663366">DATA_SIZE
100</FONT>
<BR><FONT COLOR="#000099">#define</FONT><FONT COLOR="#663366"> MESSAGE1
"Hiya over there client process"</FONT>
<BR><FONT COLOR="#000099">#define </FONT><FONT COLOR="#663366">MESSAGE2&nbsp;
"Did you hear me the first time?"</FONT>
<BR>LPCTSTR poolname="My_Pool";

<P><FONT COLOR="#000000">typedef ACE_Malloc&lt;ACE_SHARED_MEMORY_POOL,ACE_Null_Mutex>
Malloc_Allocator;</FONT>

<P>static void
<BR>server (void){
<BR>&nbsp;<FONT COLOR="#FF0000">//Create the memory allocator passing it
the shared memory</FONT>
<BR>&nbsp;<FONT COLOR="#FF0000">//pool that you want to use</FONT>
<BR>&nbsp;Malloc_Allocator shm_allocator(poolname);

<P>&nbsp;<FONT COLOR="#FF0000">//Create a message, allocate memory for
it and bind it with</FONT>
<BR><FONT COLOR="#FF0000">&nbsp;//a name so that the client can the find
it in the memory</FONT>
<BR><FONT COLOR="#FF0000">&nbsp;//pool</FONT>
<BR>&nbsp;char* Message1=(char*)shm_allocator.malloc(strlen(MESSAGE1));
<BR>&nbsp;ACE_OS::strcpy(Message1,MESSAGE1);
<BR>&nbsp;shm_allocator.bind("FirstMessage",Message1);
<BR>&nbsp;ACE_DEBUG((LM_DEBUG,"&lt;&lt;%s\n",Message1));
<BR>&nbsp;
<BR><FONT COLOR="#FF0000">&nbsp;//How about a second message</FONT>
<BR>&nbsp;char* Message2=(char*)shm_allocator.malloc(strlen(MESSAGE2));
<BR>&nbsp;ACE_OS::strcpy(Message2,MESSAGE2);
<BR>&nbsp;shm_allocator.bind("SecondMessage",Message2);
<BR>&nbsp;ACE_DEBUG((LM_DEBUG,"&lt;&lt;%s\n",Message2));
<BR>&nbsp;
<BR>&nbsp;<FONT COLOR="#FF0000">//Set the Server to go to sleep for a while
so that the client has</FONT>
<BR><FONT COLOR="#FF0000">&nbsp;//a chance to do its stuff</FONT>
<BR>&nbsp;ACE_DEBUG((LM_DEBUG,
<BR>&nbsp;&nbsp;&nbsp; "Server done writing.. going to sleep zzz..\n\n\n"));
<BR>&nbsp;ACE_OS::sleep(2);
<BR>&nbsp;
<BR><FONT COLOR="#FF0000">&nbsp;//Get rid of all resources allocated by
the server. In other</FONT>
<BR><FONT COLOR="#FF0000">&nbsp;//words get rid of the shared memory pool
that had been</FONT>
<BR><FONT COLOR="#FF0000">&nbsp;//previously allocated</FONT>
<BR>&nbsp;shm_allocator.remove();
<BR>&nbsp;
<BR>}
<BR>&nbsp;

<P>static void
<BR>client(void){
<BR>&nbsp;<FONT COLOR="#FF0000">//Create a memory allocator. Be sure that
the client passes</FONT>
<BR><FONT COLOR="#FF0000">&nbsp;// in the "right" name here so that both
the client and the</FONT>
<BR><FONT COLOR="#FF0000">&nbsp;//server use the same memory pool. We wouldnt
want them to</FONT>
<BR><FONT COLOR="#FF0000">&nbsp;// BOTH create different underlying pools.</FONT>
<BR>&nbsp;Malloc_Allocator shm_allocator(poolname);

<P><FONT COLOR="#FF0000">&nbsp;//Lets get that first message. Notice that
the find is looking up the</FONT>
<BR><FONT COLOR="#FF0000">&nbsp;//memory based on the "name" that was bound
to it by the server.</FONT>
<BR>&nbsp;void *Message1;
<BR>&nbsp;if(shm_allocator.find("FirstMessage",Message1)==-1){
<BR>&nbsp; ACE_ERROR((LM_ERROR,
<BR>&nbsp;&nbsp;&nbsp; "Client: Problem cant find data that server has
sent\n"));
<BR>&nbsp; ACE_OS::exit(1);
<BR>&nbsp; }
<BR>&nbsp;ACE_OS::printf(">>%s\n",(char*) Message1);
<BR>&nbsp;ACE_OS::fflush(stdout);

<P>&nbsp;<FONT COLOR="#FF0000">//Lets get that second message now.</FONT>
<BR>&nbsp;void *Message2;
<BR>&nbsp;if(shm_allocator.find("SecondMessage",Message2)==-1){
<BR>&nbsp; ACE_ERROR((LM_ERROR,"Client: Problem cant find data that server
has sent\n"));
<BR>&nbsp; ACE_OS::exit(1);
<BR>&nbsp; }
<BR>&nbsp;ACE_OS::printf(">>%s\n",(char*)Message2);
<BR>&nbsp;ACE_OS::fflush(stdout);

<P>&nbsp;ACE_DEBUG((LM_DEBUG,"Client done reading! BYE NOW\n"));
<BR>&nbsp;ACE_OS::fflush(stdout);
<BR>}
<BR>&nbsp;

<P>int main (int, char *[]){
<BR>switch (ACE_OS::fork ())
<BR>&nbsp;&nbsp;&nbsp; {
<BR>&nbsp;&nbsp;&nbsp; case -1:
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ACE_ERROR_RETURN ((LM_ERROR, "%p\n",
"fork"), 1);
<BR>&nbsp;&nbsp;&nbsp; case 0:
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<FONT COLOR="#FF0000"> // Make sure the
server starts up first.</FONT>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ACE_OS::sleep (1);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; client ();
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; break;
<BR>&nbsp;&nbsp;&nbsp; default:
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; server ();
<BR>&nbsp;&nbsp;&nbsp; break;
<BR>&nbsp;}
<BR>&nbsp; return 0;
<BR>}
<BR>&nbsp;
<BR>&nbsp;<A HREF="../Chap_4/ex01.html">Next Example</A>
</BODY>
</HTML>