diff options
Diffstat (limited to 'ACE/examples/System_V_IPC')
17 files changed, 850 insertions, 0 deletions
diff --git a/ACE/examples/System_V_IPC/Makefile.am b/ACE/examples/System_V_IPC/Makefile.am new file mode 100644 index 00000000000..23b52ee8b29 --- /dev/null +++ b/ACE/examples/System_V_IPC/Makefile.am @@ -0,0 +1,14 @@ +## Process this file with automake to create Makefile.in +## +## $Id$ +## +## This file was generated by MPC. Any changes made directly to +## this file will be lost the next time it is generated. +## +## MPC Command: +## /acebuilds/ACE_wrappers-repository/bin/mwc.pl -include /acebuilds/MPC/config -include /acebuilds/MPC/templates -feature_file /acebuilds/ACE_wrappers-repository/local.features -noreldefs -type automake -exclude build,Kokyu + +SUBDIRS = \ + SV_Message_Queues \ + SV_Semaphores + diff --git a/ACE/examples/System_V_IPC/README b/ACE/examples/System_V_IPC/README new file mode 100644 index 00000000000..c5ebde21a60 --- /dev/null +++ b/ACE/examples/System_V_IPC/README @@ -0,0 +1,13 @@ +This directory contains a number of examples that illustrate how to +use the following ACE library components: + + . SV_Message_Queues + Illustrates the ACE wrappers for System V Message + Queues. + + . SV_Semaphores + Illustrates the ACE wrappers for System V Semaphores. + + . SV_Shared_Memory + Illustrates the ACE wrappers for System V Shared Memory. + diff --git a/ACE/examples/System_V_IPC/SV_Message_Queues/.cvsignore b/ACE/examples/System_V_IPC/SV_Message_Queues/.cvsignore new file mode 100644 index 00000000000..bcdb18d69d8 --- /dev/null +++ b/ACE/examples/System_V_IPC/SV_Message_Queues/.cvsignore @@ -0,0 +1,4 @@ +mqclient +mqserver +tmqclient +tmqserver diff --git a/ACE/examples/System_V_IPC/SV_Message_Queues/MQ_Client.cpp b/ACE/examples/System_V_IPC/SV_Message_Queues/MQ_Client.cpp new file mode 100644 index 00000000000..547e4175121 --- /dev/null +++ b/ACE/examples/System_V_IPC/SV_Message_Queues/MQ_Client.cpp @@ -0,0 +1,55 @@ +// $Id$ + +#include "ace/SV_Message_Queue.h" +#include "test.h" + +// FUZZ: disable check_for_streams_include +#include "ace/streams.h" + +#include "ace/Malloc.h" +#include "ace/OS_NS_stdio.h" +#include "ace/OS_NS_unistd.h" +#include "ace/OS_NS_stdlib.h" + +ACE_RCSID(SV_Message_Queues, MQ_Client, "$Id$") + +#if defined (ACE_HAS_SYSV_IPC) && !defined(ACE_LACKS_SYSV_SHMEM) + +int +main (int, char *[]) +{ + long pid = long (ACE_OS::getpid ()); + ACE_SV_Message_Queue msgque (SRV_KEY); + Message_Block send_msg (SRV_ID, + pid, + ACE_OS::cuserid (static_cast<char *> (0)), + "did you get this?"); + Message_Block recv_msg (pid); + + if (msgque.send (send_msg, send_msg.length ()) < 0) + ACE_OS::perror ("msgque.send"), ACE_OS::exit (1); + + if (msgque.recv (recv_msg, sizeof (Message_Data), recv_msg.type ()) < 0) + ACE_OS::perror ("msgrcv"), ACE_OS::exit (1); + + cout << "a message of length " + << recv_msg.length () + << " received from server " + << recv_msg.pid () + << " (user " + << recv_msg.user () << "): " + << recv_msg.text () << "\n"; + + return 0; +} + +#else + +int ACE_TMAIN (int, ACE_TCHAR *[]) +{ + ACE_ERROR ((LM_ERROR, + "SYSV IPC, or SYSV SHMEM is not supported on this platform\n")); + return 0; +} +#endif /* ACE_HAS_SYSV_IPC && !ACE_LACKS_SYSV_SHMEM*/ + diff --git a/ACE/examples/System_V_IPC/SV_Message_Queues/MQ_Server.cpp b/ACE/examples/System_V_IPC/SV_Message_Queues/MQ_Server.cpp new file mode 100644 index 00000000000..33761465813 --- /dev/null +++ b/ACE/examples/System_V_IPC/SV_Message_Queues/MQ_Server.cpp @@ -0,0 +1,81 @@ +// $Id$ + +#include "ace/Signal.h" +#include "ace/SV_Message_Queue.h" + +// FUZZ: disable check_for_streams_include +#include "ace/streams.h" + +#include "test.h" +#include "ace/OS_NS_stdio.h" +#include "ace/OS_NS_unistd.h" +#include "ace/OS_NS_stdlib.h" + +ACE_RCSID(SV_Message_Queues, MQ_Server, "$Id$") + +#if defined (ACE_HAS_SYSV_IPC) && !defined(ACE_LACKS_SYSV_SHMEM) + +// Must be global for signal Message... +static ACE_SV_Message_Queue ace_sv_message_queue (SRV_KEY, + ACE_SV_Message_Queue::ACE_CREATE); + +extern "C" void +handler (int) +{ + if (ace_sv_message_queue.remove () < 0) + ACE_OS::perror ("ace_sv_message_queue.close"), ACE_OS::exit (1); + ACE_OS::exit (0); +} + +int +main (int, char *[]) +{ + long pid = long (ACE_OS::getpid ()); + Message_Block recv_msg (SRV_ID); + Message_Block send_msg (0, + pid, + ACE_OS::cuserid (static_cast<char *> (0)), + "I received your message."); + + // Register a signal handler. + ACE_Sig_Action sa ((ACE_SignalHandler) handler, SIGINT); + ACE_UNUSED_ARG (sa); + + for (;;) + { + if (ace_sv_message_queue.recv (recv_msg, + sizeof (Message_Data), + recv_msg.type ()) == -1) + ::perror ("ace_sv_message_queue.recv"), ACE_OS::exit (1); + + cout << "a msg of length " + << recv_msg.length () + << " sent from client " + << recv_msg.pid () + << " (user " + << recv_msg.user () << "): " + << recv_msg.text () << "\n"; + + cout.flush (); + + send_msg.type (recv_msg.pid ()); + + if (ace_sv_message_queue.send (send_msg, + send_msg.length ()) == -1) + ACE_OS::perror ("ace_sv_message_queue.send"), ACE_OS::exit (1); + } + + ACE_NOTREACHED (return 0;) +} + +#else + +#include "ace/Log_Msg.h" + +int ACE_TMAIN (int, ACE_TCHAR *[]) +{ + ACE_ERROR ((LM_ERROR, + "SYSV IPC, or SYSV SHMEM is not supported on this platform\n")); + return 0; +} +#endif /* ACE_HAS_SYSV_IPC && !ACE_LACKS_SYSV_SHMEM */ diff --git a/ACE/examples/System_V_IPC/SV_Message_Queues/Makefile.am b/ACE/examples/System_V_IPC/SV_Message_Queues/Makefile.am new file mode 100644 index 00000000000..eadb6b8df53 --- /dev/null +++ b/ACE/examples/System_V_IPC/SV_Message_Queues/Makefile.am @@ -0,0 +1,94 @@ +## Process this file with automake to create Makefile.in +## +## $Id$ +## +## This file was generated by MPC. Any changes made directly to +## this file will be lost the next time it is generated. +## +## MPC Command: +## /acebuilds/ACE_wrappers-repository/bin/mwc.pl -include /acebuilds/MPC/config -include /acebuilds/MPC/templates -feature_file /acebuilds/ACE_wrappers-repository/local.features -noreldefs -type automake -exclude build,Kokyu + +ACE_BUILDDIR = $(top_builddir) +ACE_ROOT = $(top_srcdir) + +noinst_PROGRAMS = + +## Makefile.SV_Message_Queues_MQ_Client.am + +if !BUILD_ACE_FOR_TAO +noinst_PROGRAMS += mqclient + +mqclient_CPPFLAGS = \ + -I$(ACE_ROOT) \ + -I$(ACE_BUILDDIR) + +mqclient_SOURCES = \ + MQ_Client.cpp \ + test.h + +mqclient_LDADD = \ + $(ACE_BUILDDIR)/ace/libACE.la + +endif !BUILD_ACE_FOR_TAO + +## Makefile.SV_Message_Queues_MQ_Server.am + +if !BUILD_ACE_FOR_TAO +noinst_PROGRAMS += mqserver + +mqserver_CPPFLAGS = \ + -I$(ACE_ROOT) \ + -I$(ACE_BUILDDIR) + +mqserver_SOURCES = \ + MQ_Server.cpp \ + test.h + +mqserver_LDADD = \ + $(ACE_BUILDDIR)/ace/libACE.la + +endif !BUILD_ACE_FOR_TAO + +## Makefile.SV_Message_Queues_TMQ_Client.am + +if !BUILD_ACE_FOR_TAO +noinst_PROGRAMS += tmqclient + +tmqclient_CPPFLAGS = \ + -I$(ACE_ROOT) \ + -I$(ACE_BUILDDIR) + +tmqclient_SOURCES = \ + TMQ_Client.cpp \ + test.h + +tmqclient_LDADD = \ + $(ACE_BUILDDIR)/ace/libACE.la + +endif !BUILD_ACE_FOR_TAO + +## Makefile.SV_Message_Queues_TMQ_Server.am + +if !BUILD_ACE_FOR_TAO +noinst_PROGRAMS += tmqserver + +tmqserver_CPPFLAGS = \ + -I$(ACE_ROOT) \ + -I$(ACE_BUILDDIR) + +tmqserver_SOURCES = \ + TMQ_Server.cpp \ + test.h + +tmqserver_LDADD = \ + $(ACE_BUILDDIR)/ace/libACE.la + +endif !BUILD_ACE_FOR_TAO + +## Clean up template repositories, etc. +clean-local: + -rm -f *~ *.bak *.rpo *.sym lib*.*_pure_* core core.* + -rm -f gcctemp.c gcctemp so_locations *.ics + -rm -rf cxx_repository ptrepository ti_files + -rm -rf templateregistry ir.out + -rm -rf ptrepository SunWS_cache Templates.DB diff --git a/ACE/examples/System_V_IPC/SV_Message_Queues/SV_Message_Queues.mpc b/ACE/examples/System_V_IPC/SV_Message_Queues/SV_Message_Queues.mpc new file mode 100644 index 00000000000..62cb7a0a819 --- /dev/null +++ b/ACE/examples/System_V_IPC/SV_Message_Queues/SV_Message_Queues.mpc @@ -0,0 +1,32 @@ +// -*- MPC -*- +// $Id$ + +project(*MQ Client) : aceexe { + avoids += ace_for_tao + exename = mqclient + Source_Files { + MQ_Client.cpp + } +} +project(*MQ Server) : aceexe { + avoids += ace_for_tao + exename = mqserver + Source_Files { + MQ_Server.cpp + } +} +project(*TMQ Client) : aceexe { + avoids += ace_for_tao + exename = tmqclient + Source_Files { + TMQ_Client.cpp + } +} +project(*TMQ Server) : aceexe { + avoids += ace_for_tao + exename = tmqserver + Source_Files { + TMQ_Server.cpp + } +} + diff --git a/ACE/examples/System_V_IPC/SV_Message_Queues/TMQ_Client.cpp b/ACE/examples/System_V_IPC/SV_Message_Queues/TMQ_Client.cpp new file mode 100644 index 00000000000..d48939845d5 --- /dev/null +++ b/ACE/examples/System_V_IPC/SV_Message_Queues/TMQ_Client.cpp @@ -0,0 +1,52 @@ +// $Id$ + +#include "ace/Typed_SV_Message_Queue.h" + +// FUZZ: disable check_for_streams_include +#include "ace/streams.h" + +#include "ace/Log_Msg.h" +#include "test.h" +#include "ace/OS_NS_stdio.h" +#include "ace/OS_NS_unistd.h" + +ACE_RCSID(SV_Message_Queues, TMQ_Client, "$Id$") + +int +ACE_TMAIN (int, ACE_TCHAR *[]) +{ + long pid = long (ACE_OS::getpid ()); + + ACE_Typed_SV_Message_Queue<Message_Data> msgque (key_t (SRV_KEY)); + + Message_Data msg_data (pid, + ACE_OS::cuserid (static_cast<char *> (0)), + "did you get this?"); + + ACE_Typed_SV_Message<Message_Data> send_msg (msg_data, + SRV_ID, + msg_data.length ()), + recv_msg (pid); + + if (msgque.send (send_msg) < 0) + ACE_ERROR_RETURN ((LM_ERROR, ACE_TEXT ("%p\n"), + ACE_TEXT ("msgque.send")), 1); + + if (msgque.recv (recv_msg) < 0) + ACE_ERROR_RETURN ((LM_ERROR, ACE_TEXT ("%p\n"), + ACE_TEXT ("msgque.recv")), 1); + + Message_Data &recv_msg_data = recv_msg.data (); + + ACE_DEBUG ((LM_DEBUG, + ACE_TEXT ("a message of length %d") + ACE_TEXT (" received from server %d") + ACE_TEXT (" (user %C): %C\n"), + recv_msg_data.length (), + recv_msg_data.pid (), + recv_msg_data.user (), + recv_msg_data.text ())); + + return 0; +} + diff --git a/ACE/examples/System_V_IPC/SV_Message_Queues/TMQ_Server.cpp b/ACE/examples/System_V_IPC/SV_Message_Queues/TMQ_Server.cpp new file mode 100644 index 00000000000..0da9aeec763 --- /dev/null +++ b/ACE/examples/System_V_IPC/SV_Message_Queues/TMQ_Server.cpp @@ -0,0 +1,78 @@ +// $Id$ + +#include "ace/Signal.h" +#include "ace/Typed_SV_Message_Queue.h" + +#include "test.h" + +// FUZZ: disable check_for_streams_include +#include "ace/streams.h" + +#include "ace/Log_Msg.h" +#include "ace/OS_NS_stdio.h" +#include "ace/OS_NS_stdlib.h" +#include "ace/OS_NS_unistd.h" + +ACE_RCSID(SV_Message_Queues, TMQ_Server, "$Id$") + +#if defined (ACE_HAS_SYSV_IPC) && !defined(ACE_LACKS_SYSV_SHMEM) + +// Must be global for signal Message... +static ACE_Typed_SV_Message_Queue<Message_Data> ace_sv_message_queue + (SRV_KEY, ACE_Typed_SV_Message_Queue<Message_Data>::ACE_CREATE); + +extern "C" void +handler (int) +{ + if (ace_sv_message_queue.remove () < 0) + ACE_ERROR ((LM_ERROR, "%p\n%a", "ace_sv_message_queue.recv", 1)); + ACE_OS::exit (0); +} + +int +main (int, char *[]) +{ + char *username = ACE_OS::cuserid (static_cast<char *> (0)); + Message_Data msg_data ((int) ACE_OS::getpid (), username, "I received your message."); + ACE_Typed_SV_Message<Message_Data> send_msg (msg_data, 0, msg_data.length ()); + ACE_Typed_SV_Message<Message_Data> recv_msg (SRV_ID); + + // Register a signal handler. + ACE_Sig_Action sa ((ACE_SignalHandler) handler, SIGINT); + ACE_UNUSED_ARG (sa); + + for (;;) + { + if (ace_sv_message_queue.recv (recv_msg) == -1) + ACE_ERROR_RETURN ((LM_ERROR, "%p\n", "ace_sv_message_queue.recv"), 1); + + Message_Data &recv_msg_data = recv_msg.data (); + + cout << "a msg of length " + << recv_msg_data.length () + << " sent from client " + << recv_msg_data.pid () + << " (user " + << recv_msg_data.user () << "): " + << recv_msg_data.text () << "\n"; + cout.flush (); + + send_msg.type (recv_msg_data.pid ()); + + if (ace_sv_message_queue.send (send_msg) < 0) + ACE_ERROR_RETURN ((LM_ERROR, "%p\n", "ace_sv_message_queue.send"), 1); + } + + ACE_NOTREACHED (return 0;) +} + +#else + +int ACE_TMAIN (int, ACE_TCHAR *[]) +{ + ACE_ERROR ((LM_ERROR, + "SYSV IPC, or SYSV SHMEM is not supported on this platform\n")); + return 0; +} +#endif /* ACE_HAS_SYSV_IPC && !ACE_LACKS_SYSV_SHMEM */ + diff --git a/ACE/examples/System_V_IPC/SV_Message_Queues/test.h b/ACE/examples/System_V_IPC/SV_Message_Queues/test.h new file mode 100644 index 00000000000..f17cf11c63e --- /dev/null +++ b/ACE/examples/System_V_IPC/SV_Message_Queues/test.h @@ -0,0 +1,58 @@ +/* -*- C++ -*- */ +// $Id$ + +#include "ace/OS_NS_string.h" + +#if !defined (ACE_LACKS_PRAGMA_ONCE) +# pragma once +#endif /* ACE_LACKS_PRAGMA_ONCE */ + +#include "ace/SV_Message.h" + +#define MSGSZ 128 +#define SRV_KEY ACE_DEFAULT_SHM_KEY +#define SRV_ID 1 + +class Message_Data +{ +public: + Message_Data (long p = -1, + const char user[] = "", + const char text[] = ""): pid_ (p) + { + ACE_OS::strncpy (this->username_, user, 9); + ACE_OS::strncpy (this->mtext_, text, MSGSZ); + } + + long pid (void) { return this->pid_; } + void pid (long p) { this->pid_ = p; } + char *user (void) { return this->username_; } + void user (char user[]) { ACE_OS::strncpy (this->username_, user, 9); } + char *text (void) { return this->mtext_; } + void text (char text[]) { ACE_OS::strncpy (this->mtext_, text, MSGSZ); } + int length (void) { return sizeof *this - sizeof this->mtext_ + ACE_OS::strlen (this->mtext_) + 1; } + +protected: + long pid_; + char username_[9]; + char mtext_[MSGSZ]; +}; + +class Message_Block : public ACE_SV_Message, public Message_Data +{ + // = TITLE + // Stores message content. + // = DESCRIPTION + // This may not be 100 percent portable on all C++ compilers since + // it relies on inheritance to be "concatenation." + // +public: + Message_Block (long t, + long p = 0, + const char login[] = "", + const char message[] = "") + : ACE_SV_Message (t), + Message_Data (p, login, message) + {} +}; + diff --git a/ACE/examples/System_V_IPC/SV_Semaphores/.cvsignore b/ACE/examples/System_V_IPC/SV_Semaphores/.cvsignore new file mode 100644 index 00000000000..bb37ee6c9a7 --- /dev/null +++ b/ACE/examples/System_V_IPC/SV_Semaphores/.cvsignore @@ -0,0 +1,2 @@ +sem1 +sem2 diff --git a/ACE/examples/System_V_IPC/SV_Semaphores/Makefile.am b/ACE/examples/System_V_IPC/SV_Semaphores/Makefile.am new file mode 100644 index 00000000000..de9a88952a6 --- /dev/null +++ b/ACE/examples/System_V_IPC/SV_Semaphores/Makefile.am @@ -0,0 +1,56 @@ +## Process this file with automake to create Makefile.in +## +## $Id$ +## +## This file was generated by MPC. Any changes made directly to +## this file will be lost the next time it is generated. +## +## MPC Command: +## /acebuilds/ACE_wrappers-repository/bin/mwc.pl -include /acebuilds/MPC/config -include /acebuilds/MPC/templates -feature_file /acebuilds/ACE_wrappers-repository/local.features -noreldefs -type automake -exclude build,Kokyu + +ACE_BUILDDIR = $(top_builddir) +ACE_ROOT = $(top_srcdir) + +noinst_PROGRAMS = + +## Makefile.SV_Semaphores_1.am + +if !BUILD_ACE_FOR_TAO +noinst_PROGRAMS += sem1 + +sem1_CPPFLAGS = \ + -I$(ACE_ROOT) \ + -I$(ACE_BUILDDIR) + +sem1_SOURCES = \ + Semaphores_1.cpp + +sem1_LDADD = \ + $(ACE_BUILDDIR)/ace/libACE.la + +endif !BUILD_ACE_FOR_TAO + +## Makefile.SV_Semaphores_2.am + +if !BUILD_ACE_FOR_TAO +noinst_PROGRAMS += sem2 + +sem2_CPPFLAGS = \ + -I$(ACE_ROOT) \ + -I$(ACE_BUILDDIR) + +sem2_SOURCES = \ + Semaphores_2.cpp + +sem2_LDADD = \ + $(ACE_BUILDDIR)/ace/libACE.la + +endif !BUILD_ACE_FOR_TAO + +## Clean up template repositories, etc. +clean-local: + -rm -f *~ *.bak *.rpo *.sym lib*.*_pure_* core core.* + -rm -f gcctemp.c gcctemp so_locations *.ics + -rm -rf cxx_repository ptrepository ti_files + -rm -rf templateregistry ir.out + -rm -rf ptrepository SunWS_cache Templates.DB diff --git a/ACE/examples/System_V_IPC/SV_Semaphores/SV_Semaphores.mpc b/ACE/examples/System_V_IPC/SV_Semaphores/SV_Semaphores.mpc new file mode 100644 index 00000000000..647e4a4cf5a --- /dev/null +++ b/ACE/examples/System_V_IPC/SV_Semaphores/SV_Semaphores.mpc @@ -0,0 +1,17 @@ +// -*- MPC -*- +// $Id$ + +project(*1) : aceexe { + avoids += ace_for_tao + exename = sem1 + Source_Files { + Semaphores_1.cpp + } +} +project(*2) : aceexe { + avoids += ace_for_tao + exename = sem2 + Source_Files { + Semaphores_2.cpp + } +} diff --git a/ACE/examples/System_V_IPC/SV_Semaphores/Semaphores_1.cpp b/ACE/examples/System_V_IPC/SV_Semaphores/Semaphores_1.cpp new file mode 100644 index 00000000000..2331ea772ec --- /dev/null +++ b/ACE/examples/System_V_IPC/SV_Semaphores/Semaphores_1.cpp @@ -0,0 +1,94 @@ +// $Id$ + +#include "ace/SV_Shared_Memory.h" +#include "ace/SV_Semaphore_Simple.h" +#include "ace/SV_Semaphore_Complex.h" +#include "ace/Shared_Memory_Pool.h" +#include "ace/Malloc_T.h" +#include "ace/OS_NS_unistd.h" + + +ACE_RCSID (SV_Semaphores, + Semaphores_1, + "$Id$") + + +#if defined (ACE_HAS_SYSV_IPC) && !defined(ACE_LACKS_SYSV_SHMEM) + +// Shared memory allocator (note that this chews up the +// ACE_DEFAULT_SEM_KEY). +static ACE_Malloc<ACE_SHARED_MEMORY_POOL, ACE_SV_Semaphore_Simple> alloc; + +const int SEM_KEY = ACE_DEFAULT_SEM_KEY + 1; + +static int +parent (char *shm) +{ + char *s = shm; + + ACE_SV_Semaphore_Complex sem (SEM_KEY, ACE_SV_Semaphore_Complex::ACE_CREATE, 0, 2); + + for (char c = 'a'; c <= 'z'; c++) + *s++ = c; + + *s = '\0'; + + if (sem.release (0) == -1) + ACE_ERROR ((LM_ERROR, "%p", "parent sem.release(0)")); + else if (sem.acquire (1) == -1) + ACE_ERROR ((LM_ERROR, "%p", "parent sem.acquire(1)")); + + if (alloc.remove () == -1) + ACE_ERROR ((LM_ERROR, "%p\n", "alloc.remove")); + if (sem.remove () == -1) + ACE_ERROR ((LM_ERROR, "%p\n", "sem.remove")); + return 0; +} + +static int +child (char *shm) +{ + ACE_SV_Semaphore_Complex sem (SEM_KEY, ACE_SV_Semaphore_Complex::ACE_CREATE, 0, 2); + + while (sem.tryacquire (0) == -1) + if (errno == EAGAIN) + ACE_DEBUG ((LM_DEBUG, "spinning in client!\n")); + else + ACE_ERROR_RETURN ((LM_ERROR, "client mutex.tryacquire(0)"), 1); + + for (char *s = (char *) shm; *s != '\0'; s++) + ACE_DEBUG ((LM_DEBUG, "%c", *s)); + + ACE_DEBUG ((LM_DEBUG, "\n")); + + if (sem.release (1) < 0) + ACE_ERROR ((LM_ERROR, "client sem.release(1)")); + return 0; +} + +int +main (int, char *[]) +{ + char *shm = (char *) alloc.malloc (27); + + switch (ACE_OS::fork ()) + { + case -1: + ACE_ERROR_RETURN ((LM_ERROR, "fork failed\n"), -1); + /* NOTREACHED */ + case 0: + // Child. + return child (shm); + default: + return parent (shm); + } +} +#else +int ACE_TMAIN (int, ACE_TCHAR *[]) +{ + ACE_ERROR ((LM_ERROR, + "SYSV IPC, or SYSV SHMEM is not supported on this platform\n")); + return 0; +} +#endif /* ACE_HAS_SYSV_IPC */ + diff --git a/ACE/examples/System_V_IPC/SV_Semaphores/Semaphores_2.cpp b/ACE/examples/System_V_IPC/SV_Semaphores/Semaphores_2.cpp new file mode 100644 index 00000000000..e0ae9cd2bb5 --- /dev/null +++ b/ACE/examples/System_V_IPC/SV_Semaphores/Semaphores_2.cpp @@ -0,0 +1,110 @@ +// $Id$ + +// Illustrates the use of the ACE_SV_Semaphore_Complex class and the +// ACE_Malloc class using the ACE_Shared_Memory_Pool (which uses +// System V shared memory). Note that it doesn't matter whether the +// parent or the child creates the semaphore since Semaphore_Complex +// will correctly serialize the intialization of the mutex and synch +// objects. + +#include "ace/Malloc_T.h" +#include "ace/Shared_Memory_Pool.h" +#include "ace/SV_Semaphore_Complex.h" +#include "ace/OS_NS_unistd.h" + +ACE_RCSID(SV_Semaphores, Semaphores_2, "$Id$") + +#if defined (ACE_HAS_SYSV_IPC) && !defined(ACE_LACKS_SYSV_SHMEM) + +// Shared memory allocator (note that this chews up the +// ACE_DEFAULT_SEM_KEY). +static ACE_Malloc<ACE_SHARED_MEMORY_POOL, ACE_SV_Semaphore_Simple> my_alloc; + +const int SEM_KEY_1 = ACE_DEFAULT_SEM_KEY + 1; +const int SEM_KEY_2 = ACE_DEFAULT_SEM_KEY + 2; + +static int +parent (char *shm) +{ + char *s = shm; + + // Both semaphores are initially created with a count of 0, i.e., + // they are "locked." + ACE_SV_Semaphore_Complex mutex (SEM_KEY_1, ACE_SV_Semaphore_Complex::ACE_CREATE, 0); + ACE_SV_Semaphore_Complex synch (SEM_KEY_2, ACE_SV_Semaphore_Complex::ACE_CREATE, 0); + + // This is a critical section, which is protected by the mutex + // semaphore. + for (char c = 'a'; c <= 'z'; c++) + *s++ = c; + + *s = '\0'; + + if (mutex.release () == -1) + ACE_ERROR ((LM_ERROR, "%p", "parent mutex.release")); + else if (synch.acquire () == -1) + ACE_ERROR ((LM_ERROR, "%p", "parent synch.acquire")); + + if (my_alloc.remove () == -1) + ACE_ERROR ((LM_ERROR, "%p\n", "my_alloc.remove")); + if (mutex.remove () == -1) + ACE_ERROR ((LM_ERROR, "%p\n", "mutex.remove")); + if (synch.remove () == -1) + ACE_ERROR ((LM_ERROR, "%p\n", "synch.remove")); + return 0; +} + +static int +child (char *shm) +{ + // Both semaphores are initially created with a count of 0, i.e., + // they are "locked." + ACE_SV_Semaphore_Complex mutex (SEM_KEY_1, ACE_SV_Semaphore_Complex::ACE_CREATE, 0); + ACE_SV_Semaphore_Complex synch (SEM_KEY_2, ACE_SV_Semaphore_Complex::ACE_CREATE, 0); + + // Perform "busy waiting" here until we acquire the semaphore. This + // isn't really a good design -- it's just to illustrate that you + // can do non-blocking acquire() calls with the ACE System V + // semaphore wrappers. + while (mutex.tryacquire () == -1) + if (errno == EAGAIN) + ACE_DEBUG ((LM_DEBUG, "spinning in child!\n")); + else + ACE_ERROR_RETURN ((LM_ERROR, "child mutex.tryacquire"), 1); + + for (char *s = (char *) shm; *s != '\0'; s++) + ACE_DEBUG ((LM_DEBUG, "%c", *s)); + + ACE_DEBUG ((LM_DEBUG, "\n")); + + if (synch.release () == -1) + ACE_ERROR_RETURN ((LM_ERROR, "child synch.release"), 1); + return 0; +} + +int +main (int, char *[]) +{ + char *shm = (char *) my_alloc.malloc (27); + + switch (ACE_OS::fork ()) + { + case -1: + ACE_ERROR_RETURN ((LM_ERROR, "fork failed\n"), -1); + /* NOTREACHED */ + case 0: + // Child. + return child (shm); + default: + return parent (shm); + } +} +#else +int ACE_TMAIN (int, ACE_TCHAR *[]) +{ + ACE_ERROR ((LM_ERROR, + "SYSV IPC, or SYSV SHMEM is not supported on this platform\n")); + return 0; +} +#endif /* ACE_HAS_SYSV_IPC */ + diff --git a/ACE/examples/System_V_IPC/SV_Shared_Memory/SV_Shared_Memory_Test.cpp b/ACE/examples/System_V_IPC/SV_Shared_Memory/SV_Shared_Memory_Test.cpp new file mode 100644 index 00000000000..3a36e395d35 --- /dev/null +++ b/ACE/examples/System_V_IPC/SV_Shared_Memory/SV_Shared_Memory_Test.cpp @@ -0,0 +1,78 @@ +// $Id$ + +#include "ace/SV_Shared_Memory.h" +#include "ace/Log_Msg.h" +#include "SV_Shared_Memory_Test.h" +#include "ace/OS_NS_stdio.h" +#include "ace/OS_NS_stdlib.h" +#include "ace/OS_NS_unistd.h" + +ACE_RCSID(SV_Shared_Memory, SV_Shared_Memory_Test, "$Id$") + +#if defined (ACE_HAS_SYSV_IPC) && !defined(ACE_LACKS_SYSV_SHMEM) + +static void +client (void) +{ + ACE_SV_Shared_Memory shm_client; + + if (shm_client.open_and_attach (SHM_KEY, SHMSZ) == -1) + ACE_OS::perror ("open"), ACE_OS::exit (1); + + for (char *s = (char *) shm_client.get_segment_ptr (); *s != '\0'; s++) + putchar (*s); + + putchar ('\n'); + *(char *) shm_client.get_segment_ptr () = '*'; + ACE_OS::exit (0); +} + +static void +server (void) +{ + ACE_SV_Shared_Memory shm_server; + + if (shm_server.open_and_attach (SHM_KEY, SHMSZ, ACE_SV_Shared_Memory::ACE_CREATE) == -1) + ACE_OS::perror ("open"), ACE_OS::exit (1); + + char *s = (char *) shm_server.get_segment_ptr (); + + for (char c = 'a'; c <= 'z'; c++) + *s++ = c; + + *s = '\0'; + + for (s = (char *) shm_server.get_segment_ptr (); *s != '*'; ) + ACE_OS::sleep (1); + + if (shm_server.remove () < 0) + ACE_OS::perror ("remove"), ACE_OS::exit (1); + ACE_OS::exit (0); +} + +int +main (int, char *argv[]) +{ + switch (ACE_OS::fork ()) + { + case -1: + ACE_OS::perror (argv[0]), ACE_OS::exit (1); + case 0: + ACE_OS::sleep (1); + client (); + default: + server (); + } + return 0; +} + +#else + +int ACE_TMAIN (int, ACE_TCHAR *[]) +{ + ACE_ERROR ((LM_ERROR, + "SYSV IPC, or SYSV SHMEM is not supported on this platform\n")); + return 0; +} +#endif /* ACE_HAS_SYSV_IPC && !ACE_LACKS_SYSV_SHMEM */ + diff --git a/ACE/examples/System_V_IPC/SV_Shared_Memory/SV_Shared_Memory_Test.h b/ACE/examples/System_V_IPC/SV_Shared_Memory/SV_Shared_Memory_Test.h new file mode 100644 index 00000000000..3cfff4672b9 --- /dev/null +++ b/ACE/examples/System_V_IPC/SV_Shared_Memory/SV_Shared_Memory_Test.h @@ -0,0 +1,12 @@ +/* -*- C++ -*- */ +// $Id$ + +#include "ace/config-all.h" + +#if !defined (ACE_LACKS_PRAGMA_ONCE) +# pragma once +#endif /* ACE_LACKS_PRAGMA_ONCE */ + +#define SHMSZ 27 +#define SEM_KEY 1234 +#define SHM_KEY 5678 |