summaryrefslogtreecommitdiff
path: root/examples/System_V_IPC/SV_Semaphores/Semaphores.cpp
blob: b98c664883bc303311c0d3c84bbdc241f001c23a (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
// Illustrates the use of the Semaphore_Complex class.  Note that it
// $Id$

// doesn't matter whether the parent or the child creates the
// semaphore since Semaphore_Complex will correctly serialize the
// intialization of the mutex and synch objects.
#include "ace/Malloc.h"
#include "ace/SV_Semaphore_Complex.h"
#include "Semaphore_Test.h"

ACE_Malloc<ACE_Shared_Memory_Pool, ACE_SV_Semaphore_Simple> allocator;
ACE_SV_Semaphore_Complex *mutex = 0; 
ACE_SV_Semaphore_Complex *synch = 0; 

/* Pointer to memory shared by both the client and server. */
static char *shm;

static int
do_parent (void)
{
  char *s = shm;

  mutex = new ACE_SV_Semaphore_Complex (SEM_KEY_1, ACE_SV_Semaphore_Complex::ACE_CREATE, 0);
  synch = new ACE_SV_Semaphore_Complex (SEM_KEY_2, ACE_SV_Semaphore_Complex::ACE_CREATE, 0);

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

  *s = '\0';

  if (mutex->release () == -1)
    ACE_ERROR_RETURN ((LM_ERROR, "%p", "server mutex.release"), 1);

  if (synch->acquire () == -1)
    ACE_ERROR_RETURN ((LM_ERROR, "%p", "server synch.acquire"), 1);
  return 0;
}

static int
do_child (void)
{
  mutex = new ACE_SV_Semaphore_Complex (SEM_KEY_1, ACE_SV_Semaphore_Complex::ACE_CREATE, 0);
  synch = new ACE_SV_Semaphore_Complex (SEM_KEY_2, ACE_SV_Semaphore_Complex::ACE_CREATE, 0);

  while (mutex->tryacquire () == -1)
    if (errno == EAGAIN)
      ACE_DEBUG ((LM_DEBUG, "spinning in client!\n"));
    else
      ACE_ERROR_RETURN ((LM_ERROR, "client mutex.tryacquire"), 1);

  for (char *s = (char *) shm; *s != '\0'; s++)
    putchar (*s);

  putchar ('\n');

  if (synch->release () == -1)
    ACE_ERROR_RETURN ((LM_ERROR, "client synch.release"), 1);
  return 0;
}

int
main (void)
{
  shm = (char *) allocator.malloc (27);

  switch (ACE_OS::fork ())
    {
    case -1:
      ACE_ERROR_RETURN ((LM_ERROR, "fork failed\n"), -1);
      /* NOTREACHED */
    case 0:
      return do_child ();
    default:
      {
	int result = do_parent ();

	if (wait (0) == -1)
	  ACE_ERROR_RETURN ((LM_ERROR, "wait"), -1);

	allocator.remove ();

	if (mutex->remove () == -1)
	  ACE_ERROR_RETURN ((LM_ERROR, "%p\n", "mutex.remove"), -1);
	else if (synch->remove () == -1)
	  ACE_ERROR_RETURN ((LM_ERROR, "%p\n", "synch.remove"), -1);
	return result;
      }
    }
}

#if defined (ACE_TEMPLATES_REQUIRE_SPECIALIZATION)
template class ACE_Malloc<ACE_Shared_Memory_Pool, ACE_SV_Semaphore_Simple>;
#endif /* ACE_TEMPLATES_REQUIRE_SPECIALIZATION */