Merge pull request #2303 from jwillemsen/jwi-803
[ACE_TAO.git] / ACE / examples / Synch / proc_sema.cpp
blob7c1ae32ce8f3c2fb45d50b46aec86ac1df8a7a96
1 #include "ace/OS_main.h"
2 #include "ace/Process_Semaphore.h"
3 #include "ace/Get_Opt.h"
4 #include "ace/Log_Msg.h"
5 #include "ace/OS_NS_stdlib.h"
6 #include "ace/OS_NS_unistd.h"
7 #include "ace/Synch_Traits.h"
9 int producer (ACE_SYNCH_PROCESS_SEMAPHORE &sema,
10 int iter)
12 for (int i = iter; i > 0; --i)
14 ACE_DEBUG ((LM_DEBUG,
15 "Try releasing the semaphore (%d): ",
16 i));
18 int result = sema.release ();
20 ACE_DEBUG ((LM_DEBUG,
21 "%s",
22 (result != 0 ? "fail\n" : "succeed\n")));
24 return 0;
27 int consumer (ACE_SYNCH_PROCESS_SEMAPHORE &sema,
28 int iter)
30 for (int i = iter; i > 0; --i)
32 ACE_DEBUG ((LM_DEBUG,
33 "Try acquiring the semaphore (%d): ",
34 i));
36 int result = sema.acquire ();
38 ACE_DEBUG ((LM_DEBUG,
39 "%s",
40 (result != 0 ? "fail\n" : "succeed\n")));
42 return 0;
45 int ACE_TMAIN (int argc, ACE_TCHAR *argv[])
47 //FUZZ: disable check_for_lack_ACE_OS
48 ACE_Get_Opt getopt (argc, argv, ACE_TEXT ("csn:xi:d:"));
49 //FUZZ: enable check_for_lack_ACE_OS
51 int is_consumer = 1; // By default, make us a consumer.
52 int delete_sema = 0;
53 int iteration = 0;
54 int exit_delay = 0;
55 const ACE_TCHAR *sema_name = ACE_TEXT ("Process_Semaphore_Test");
57 int opt;
59 //FUZZ: disable check_for_lack_ACE_OS
60 while ((opt = getopt ()) != -1)
62 //FUZZ: enable check_for_lack_ACE_OS
63 switch (opt)
65 case 'c': // Make us a consumer.
66 is_consumer = 1;
67 break;
68 case 's': // Make us a supplier.
69 is_consumer = 0;
70 break;
71 case 'x': // Remove the semaphore after we're done.
72 delete_sema = 1;
73 break;
74 case 'n': // Specify the name of the semaphore.
75 sema_name = getopt.opt_arg ();
76 break;
77 case 'i': // Number of acquire/release we'll perform.
78 iteration = ACE_OS::atoi (getopt.opt_arg ());
79 break;
80 case 'd':
81 exit_delay = ACE_OS::atoi (getopt.opt_arg ());
82 break;
83 default:
84 return -1;
88 ACE_SYNCH_PROCESS_SEMAPHORE sema (0, sema_name);
90 if (is_consumer != 0)
91 consumer (sema, iteration);
92 else
93 producer (sema, iteration);
95 ACE_OS::sleep (exit_delay);
97 if (delete_sema != 0)
98 sema.remove ();
99 return 0;