Merge pull request #2309 from mitza-oci/warnings
[ACE_TAO.git] / ACE / examples / Shared_Memory / test_MM.cpp
blob03be6b7363a6e2809dd5ffd634f96873e829ae81
1 #include "ace/Shared_Memory_MM.h"
2 #include "ace/Log_Msg.h"
3 #include "ace/OS_NS_errno.h"
4 #include "ace/OS_NS_unistd.h"
5 #include "ace/OS_NS_stdlib.h"
7 #define SHMSZ 27
8 ACE_TCHAR shm_key[] = ACE_TEXT ("/tmp/fooXXXXXX");
10 static void
11 client ()
13 ACE_Shared_Memory *shm_client = new ACE_Shared_Memory_MM (shm_key);
14 char *shm = (char *) shm_client->malloc ();
16 for (char *s = shm; *s != '\0'; s++)
17 putchar (*s);
19 putchar ('\n');
20 *shm = '*';
23 static void
24 server ()
26 ACE_Shared_Memory *shm_server = new ACE_Shared_Memory_MM (shm_key, SHMSZ);
27 char *shm = (char *) shm_server->malloc ();
28 char *s = shm;
30 for (char c = 'a'; c <= 'z'; c++)
31 *s++ = c;
33 *s = '\0';
35 // Perform a busy wait (ugh)
36 while (*shm != '*')
37 ACE_OS::sleep (1);
39 if (shm_server->remove () < 0)
40 ACE_ERROR ((LM_ERROR, ACE_TEXT ("%p\n"), ACE_TEXT ("remove")));
41 ACE_OS::unlink (shm_key);
44 int
45 ACE_TMAIN (int, ACE_TCHAR *[])
47 if (ACE_OS::mkstemp (shm_key) == 0
48 || (ACE_OS::unlink (shm_key) == -1 && errno == EPERM))
49 ACE_ERROR_RETURN ((LM_ERROR, ACE_TEXT ("%p\n"), shm_key), 1);
51 switch (ACE_OS::fork ())
53 case -1:
54 ACE_ERROR_RETURN ((LM_ERROR, ACE_TEXT ("%p\n"), ACE_TEXT ("fork")), 1);
55 case 0:
56 // Make sure the server starts up first.
57 ACE_OS::sleep (1);
58 client ();
59 break;
60 default:
61 server ();
62 break;
64 return 0;