summaryrefslogtreecommitdiff
path: root/tests/SV_Shared_Memory_Test.cpp
blob: d05bce465d930d4c2d9a2822e14e46c6bbcbd388 (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
// $Id$

// ============================================================================
//
// = LIBRARY
//    tests
// 
// = FILENAME
//    SV_Shared_Memory_Test.cpp
//
// = DESCRIPTION
//     This is a simple test of ACE_SV_Shared_Memory
//
// = AUTHOR
//    Prashant Jain and Doug Schmidt
// 
// ============================================================================

#include "ace/OS.h"
#include "ace/SV_Shared_Memory.h"
#include "test_config.h"

const int SHMSZ = 27;
const int SEM_KEY = 1234;
const int SHM_KEY = 5678;

static void
client (void)
{
  ACE_SV_Shared_Memory shm_client;
  char t = 'a';

  ACE_ASSERT (shm_client.open_and_attach (SHM_KEY, SHMSZ, ACE_SV_Shared_Memory::ACE_CREATE) != -1);

  for (char *s = (char *) shm_client.get_segment_ptr (); *s != '\0'; s++)
    {
      ACE_ASSERT (t == s[0]);
      t++;
    }

  *(char *) shm_client.get_segment_ptr () = '*';
  ACE_OS::exit (0);
}

static void
server (void)
{
  ACE_SV_Shared_Memory shm_server;

  ACE_ASSERT (shm_server.open_and_attach (SHM_KEY, SHMSZ, ACE_SV_Shared_Memory::ACE_CREATE) != -1);

  char *s = (char *) shm_server.get_segment_ptr ();

  for (char c = 'a'; c <= 'z'; c++)
    *s++ = c;

  *s = '\0';

  for (s = (char *) shm_server.get_segment_ptr (); *s != '*'; )
    ACE_OS::sleep (1);

  if (shm_server.remove () < 0)
    ACE_OS::perror ("remove"), ACE_OS::exit (1);
}

int
main (int, char *argv[])
{
  ACE_START_TEST ("SV_Shared_Memory_Test.cpp");

  switch (ACE_OS::fork ())
    {
    case -1:
      ACE_ERROR ((LM_ERROR, "%p%a", "main", 1));
    case 0: 
      client ();
    default:
      server ();
    }

  ACE_END_TEST;
  return 0;
}