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
137
138
139
140
141
142
143
|
// $Id$
// ============================================================================
//
// = LIBRARY
// tests
//
// = FILENAME
// Process_Mutex_Test.cpp
//
// = DESCRIPTION
// Tests a Process Mutex shared between multiple processes
//
// = AUTHOR
// Irfan Pyarali
//
// ============================================================================
#include "test_config.h"
#include "ace/Synch.h"
#include "ace/Process.h"
#include "ace/Get_Opt.h"
ACE_RCSID(tests, Process_Mutex_Test, "$Id$")
#if defined(__BORLANDC__) && __BORLANDC__ >= 0x0530
USELIB("..\ace\aced.lib");
//---------------------------------------------------------------------------
#endif /* defined(__BORLANDC__) && __BORLANDC__ >= 0x0530 */
static int release_mutex = 1;
static int child_process = 0;
static char *mutex_name = ACE_DEFAULT_MUTEX_A;
// Explain usage and exit.
static void
print_usage_and_die (void)
{
ACE_DEBUG ((LM_DEBUG,
"usage: %n [-d (don't release mutex)] [-c (child process)] [-n mutex name] \n"));
ACE_OS::exit (1);
}
// Parse the command-line arguments and set options.
static void
parse_args (int argc, char *argv[])
{
ACE_Get_Opt get_opt (argc, argv, "dcn:");
int c;
while ((c = get_opt ()) != -1)
switch (c)
{
case 'd':
release_mutex = 0;
break;
case 'c':
child_process = 1;
break;
case 'n':
mutex_name = get_opt.optarg;
break;
default:
print_usage_and_die ();
break;
}
}
static void
acquire_release (void)
{
ACE_Process_Mutex mutex (ACE_WIDE_STRING (mutex_name));
// Make sure the constructor succeeded
ACE_ASSERT (ACE_LOG_MSG->op_status () == 0);
// Grab the lock
ACE_ASSERT (mutex.acquire () == 0);
ACE_DEBUG ((LM_DEBUG, "(%P) Mutex acquired %s\n", mutex_name));
ACE_DEBUG ((LM_DEBUG, "(%P) Working....\n"));
// work
ACE_OS::sleep (2);
// Check if we need to release the mutex
if (release_mutex == 1)
{
ACE_DEBUG ((LM_DEBUG, "(%P) Releasing the mutex %s\n", mutex_name));
ACE_ASSERT (mutex.release () == 0);
}
}
int
main (int argc, char *argv[])
{
parse_args (argc, argv);
// Child process code
if (child_process)
{
ACE_APPEND_LOG ("Process_Mutex_Test-children");
acquire_release ();
ACE_END_LOG;
}
else
{
ACE_START_TEST ("Process_Mutex_Test");
ACE_INIT_LOG ("Process_Mutex_Test-children");
ACE_Process_Options options;
if (release_mutex == 0)
options.command_line (__TEXT (".") ACE_DIRECTORY_SEPARATOR_STR
__TEXT ("Process_Mutex_Test") ACE_PLATFORM_EXE_SUFFIX
__TEXT (" -c -n %s -d"), ACE_WIDE_STRING (mutex_name));
else
options.command_line (__TEXT (".") ACE_DIRECTORY_SEPARATOR_STR
__TEXT ("Process_Mutex_Test") ACE_PLATFORM_EXE_SUFFIX
__TEXT (" -c -n %s"), ACE_WIDE_STRING (mutex_name));
// Spawn ACE_MAX_PROCESSES processes that will contend for the
// lock.
ACE_Process servers[ACE_MAX_PROCESSES];
size_t i;
for (i = 0; i < ACE_MAX_PROCESSES; i++)
{
ACE_ASSERT (servers[i].spawn (options) != -1);
ACE_DEBUG ((LM_DEBUG,
"Server forked with pid = %d.\n",
servers[i].getpid ()));
}
for (i = 0; i < ACE_MAX_PROCESSES; i++)
{
// Wait for the process we created to exit.
ACE_ASSERT (servers[i].wait () != -1);
ACE_DEBUG ((LM_DEBUG,
"Server %d finished\n",
servers[i].getpid ()));
}
ACE_END_TEST;
}
return 0;
}
|