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
|
// $Id: proc_sema.cpp 80826 2008-03-04 14:51:23Z wotte $
#include "ace/OS_main.h"
#include "ace/Process_Semaphore.h"
#include "ace/Get_Opt.h"
#include "ace/Log_Msg.h"
#include "ace/OS_NS_stdlib.h"
#include "ace/OS_NS_unistd.h"
#include "ace/Synch_Traits.h"
int producer (ACE_SYNCH_PROCESS_SEMAPHORE &sema,
int iter)
{
for (int i = iter; i > 0; --i)
{
ACE_DEBUG ((LM_DEBUG,
"Try releasing the semaphore (%d): ",
i));
int result = sema.release ();
ACE_DEBUG ((LM_DEBUG,
"%s",
(result != 0 ? "fail\n" : "succeed\n")));
}
return 0;
}
int consumer (ACE_SYNCH_PROCESS_SEMAPHORE &sema,
int iter)
{
for (int i = iter; i > 0; --i)
{
ACE_DEBUG ((LM_DEBUG,
"Try acquiring the semaphore (%d): ",
i));
int result = sema.acquire ();
ACE_DEBUG ((LM_DEBUG,
"%s",
(result != 0 ? "fail\n" : "succeed\n")));
}
return 0;
}
int ACE_TMAIN (int argc, ACE_TCHAR *argv[])
{
//FUZZ: disable check_for_lack_ACE_OS
ACE_Get_Opt getopt (argc, argv, ACE_TEXT ("csn:xi:d:"));
//FUZZ: enable check_for_lack_ACE_OS
int is_consumer = 1; // By default, make us a consumer.
int delete_sema = 0;
int iteration = 0;
int exit_delay = 0;
const ACE_TCHAR *sema_name = ACE_TEXT ("Process_Semaphore_Test");
int opt;
//FUZZ: disable check_for_lack_ACE_OS
while ((opt = getopt ()) != -1)
{
//FUZZ: enable check_for_lack_ACE_OS
switch (opt)
{
case 'c': // Make us a consumer.
is_consumer = 1;
break;
case 's': // Make us a supplier.
is_consumer = 0;
break;
case 'x': // Remove the semaphore after we're done.
delete_sema = 1;
break;
case 'n': // Specify the name of the semaphore.
sema_name = getopt.opt_arg ();
break;
case 'i': // Number of acquire/release we'll perform.
iteration = ACE_OS::atoi (getopt.opt_arg ());
break;
case 'd':
exit_delay = ACE_OS::atoi (getopt.opt_arg ());
break;
default:
return -1;
}
};
ACE_SYNCH_PROCESS_SEMAPHORE sema (0, sema_name);
if (is_consumer != 0)
consumer (sema, iteration);
else
producer (sema, iteration);
ACE_OS::sleep (exit_delay);
if (delete_sema != 0)
sema.remove ();
return 0;
}
|