1 #include "ace/OS_NS_unistd.h"
2 #include "ace/Log_Msg.h"
3 #include "ace/Process.h"
4 #include "ace/Process_Mutex.h"
10 GResourceUser (ACE_Process_Mutex
&mutex
) : gmutex_(mutex
)
12 ACE_TRACE ("GResourceUser::GResourceUser");
17 ACE_TRACE ("GResourceUser::run");
22 int result
= this->gmutex_
.acquire ();
23 ACE_TEST_ASSERT (result
== 0);
26 ACE_TEXT ("(%P| %t) has the mutex\n")));
28 // Access Global resource
31 result
= this->gmutex_
.release ();
32 ACE_TEST_ASSERT (result
== 0);
33 ACE_OS::sleep (1); // Give other process a chance.
38 ACE_Process_Mutex
&gmutex_
;
42 // Listing 0 code/ch10
43 int ACE_TMAIN (int argc
, ACE_TCHAR
*argv
[])
45 if (argc
> 1) // Run as the child.
47 // Create or get the global mutex.
48 ACE_Process_Mutex
mutex ("GlobalMutex");
50 GResourceUser
acquirer (mutex
);
53 else // Run as the parent.
55 ACE_Process_Options options
;
56 options
.command_line (ACE_TEXT ("%s a"), argv
[0]);
57 ACE_Process processa
, processb
;
59 pid_t pida
= processa
.spawn (options
);
60 pid_t pidb
= processb
.spawn (options
);
63 ACE_TEXT ("Spawned processes; pids %d:%d\n"),
66 if (processa
.wait() == -1)
67 ACE_ERROR_RETURN ((LM_ERROR
, ACE_TEXT ("%p\n"),
68 ACE_TEXT ("processa wait")), -1);
70 if (processb
.wait() == -1)
71 ACE_ERROR_RETURN ((LM_ERROR
, ACE_TEXT ("%p\n"),
72 ACE_TEXT ("processb wait")), -1);