//============================================================================= /** * @file Process_Semaphore_Test.cpp * * Tests an ACE Semaphore shared between multiple child processes. * * @author Martin Corino */ //============================================================================= #include "test_config.h" #include "ace/Mutex.h" #include "ace/Process.h" #if defined (ACE_WIN32) || \ defined (ACE_USES_FIFO_SEM) || \ (defined (ACE_HAS_POSIX_SEM) && !defined (ACE_LACKS_NAMED_POSIX_SEM)) # include "ace/Time_Value.h" # include "ace/OS_NS_sys_time.h" # include "ace/Semaphore.h" #else # include "ace/Process_Semaphore.h" #endif #include "ace/Get_Opt.h" #include "ace/ACE.h" #include "ace/OS_NS_stdio.h" #include "ace/OS_NS_string.h" #include "ace/os_include/os_dirent.h" #include "ace/OS_NS_stdlib.h" #include "ace/SString.h" #if !defined (ACE_LACKS_FORK) static int iterations = 10; static int child_process = 0; static const char *sema_ping_name = "ACE_Ping_Semaphore"; static const char *sema_pong_name = "ACE_Pong_Semaphore"; // Explain usage and exit. static void print_usage_and_die () { ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("usage: %n [-i #iterations] [-c (child process)]\n"))); ACE_OS::exit (1); } // Parse the command-line arguments and set options. static void parse_args (int argc, ACE_TCHAR *argv[]) { ACE_Get_Opt get_opt (argc, argv, ACE_TEXT ("i:c")); int c; while ((c = get_opt ()) != -1) switch (c) { case 'i': iterations = ACE_OS::atoi (get_opt.opt_arg ()); break; case 'c': child_process = 1; break; default: print_usage_and_die (); break; } } static void acquire_release () { #if defined (ACE_WIN32) || \ defined (ACE_USES_FIFO_SEM) || \ (defined (ACE_HAS_POSIX_SEM) && !defined (ACE_LACKS_NAMED_POSIX_SEM)) ACE_Semaphore sema_ping (0, USYNC_PROCESS, ACE_TEXT_CHAR_TO_TCHAR (sema_ping_name)); ACE_Semaphore sema_pong (0, USYNC_PROCESS, ACE_TEXT_CHAR_TO_TCHAR (sema_pong_name)); #else ACE_Process_Semaphore sema_ping (0, ACE_TEXT_CHAR_TO_TCHAR (sema_ping_name)); ACE_Process_Semaphore sema_pong (0, ACE_TEXT_CHAR_TO_TCHAR (sema_pong_name)); #endif // Make sure the constructor succeeded ACE_TEST_ASSERT (ACE_LOG_MSG->op_status () == 0); ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("(%P) Begin ping-pong\n"))); if (child_process) { for (int i=0; i