diff options
Diffstat (limited to 'examples/System_V_IPC')
16 files changed, 778 insertions, 0 deletions
diff --git a/examples/System_V_IPC/Makefile b/examples/System_V_IPC/Makefile new file mode 100644 index 00000000000..f799c16b5dc --- /dev/null +++ b/examples/System_V_IPC/Makefile @@ -0,0 +1,26 @@ +#---------------------------------------------------------------------------- +# @(#)Makefile 1.1 10/18/96 +# +# Makefile for the System V IPC directory +#---------------------------------------------------------------------------- + +#---------------------------------------------------------------------------- +# Local macros +#---------------------------------------------------------------------------- + +INFO = README + +DIRS = SV_Message_Queues \ + SV_Semaphores \ + SV_Shared_Memory + +#---------------------------------------------------------------------------- +# Include macros and targets +#---------------------------------------------------------------------------- + +include $(WRAPPER_ROOT)/include/makeinclude/wrapper_macros.GNU +include $(WRAPPER_ROOT)/include/makeinclude/macros.GNU +include $(WRAPPER_ROOT)/include/makeinclude/rules.common.GNU +include $(WRAPPER_ROOT)/include/makeinclude/rules.nested.GNU +include $(WRAPPER_ROOT)/include/makeinclude/rules.nolocal.GNU + diff --git a/examples/System_V_IPC/README b/examples/System_V_IPC/README new file mode 100644 index 00000000000..c5ebde21a60 --- /dev/null +++ b/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/examples/System_V_IPC/SV_Message_Queues/MQ_Client.cpp b/examples/System_V_IPC/SV_Message_Queues/MQ_Client.cpp new file mode 100644 index 00000000000..31d56c23e6a --- /dev/null +++ b/examples/System_V_IPC/SV_Message_Queues/MQ_Client.cpp @@ -0,0 +1,29 @@ +#include "ace/SV_Message_Queue.h" +// @(#)MQ_Client.cpp 1.1 10/18/96 + +#include "test.h" + +int +main (void) +{ + long pid = long (ACE_OS::getpid ()); + ACE_SV_Message_Queue msgque (SRV_KEY); + ACE_Message_Block send_msg (SRV_ID, pid, ACE_OS::cuserid (0), "did you get this?"); + ACE_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; +} diff --git a/examples/System_V_IPC/SV_Message_Queues/MQ_Server.cpp b/examples/System_V_IPC/SV_Message_Queues/MQ_Server.cpp new file mode 100644 index 00000000000..1f7729ad4e1 --- /dev/null +++ b/examples/System_V_IPC/SV_Message_Queues/MQ_Server.cpp @@ -0,0 +1,51 @@ +#include "ace/Signal.h" +// @(#)MQ_Server.cpp 1.1 10/18/96 + +#include "ace/SV_Message_Queue.h" +#include "test.h" + +/* Must be global for signal Message... */ +ACE_SV_Message_Queue msgque (SRV_KEY, ACE_SV_Message_Queue::ACE_CREATE); + +void +SIGNAL_handler (int) +{ + if (msgque.remove () < 0) + ACE_OS::perror ("msgque.close"), ACE_OS::exit (1); + ACE_OS::exit (0); +} + +int +main (void) +{ + long pid = long (ACE_OS::getpid ()); + ACE_Message_Block recv_msg (SRV_ID); + ACE_Message_Block send_msg (0, pid, ACE_OS::cuserid (0), + "I received your message."); + + ACE_Sig_Action sig ((ACE_SignalHandler) SIGNAL_handler, SIGINT); + + for (;;) + { + if (msgque.recv (recv_msg, sizeof (Message_Data), recv_msg.type ()) == -1) + ::perror ("msgque.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 (msgque.send (send_msg, send_msg.length ()) < 0) + ACE_OS::perror ("msgque.send"), ACE_OS::exit (1); + } + + /* NOTREACHED */ + return 0; +} + diff --git a/examples/System_V_IPC/SV_Message_Queues/Makefile b/examples/System_V_IPC/SV_Message_Queues/Makefile new file mode 100644 index 00000000000..cf5b28482e9 --- /dev/null +++ b/examples/System_V_IPC/SV_Message_Queues/Makefile @@ -0,0 +1,101 @@ +#---------------------------------------------------------------------------- +# @(#)Makefile 1.1 10/18/96 +# +# Makefile for testing the typed and untyped Message Queue abstraction +#---------------------------------------------------------------------------- + +#---------------------------------------------------------------------------- +# Local macros +#---------------------------------------------------------------------------- + +BIN = MQ_Server \ + MQ_Client \ + TMQ_Server \ + TMQ_Client + +LSRC = $(addsuffix .cpp,$(BIN)) + +LDLIBS = + +VLDLIBS = $(LDLIBS:%=%$(VAR)) + +BUILD = $(VBIN) + +#---------------------------------------------------------------------------- +# Include macros and targets +#---------------------------------------------------------------------------- + +include $(WRAPPER_ROOT)/include/makeinclude/wrapper_macros.GNU +include $(WRAPPER_ROOT)/include/makeinclude/macros.GNU +include $(WRAPPER_ROOT)/include/makeinclude/rules.common.GNU +include $(WRAPPER_ROOT)/include/makeinclude/rules.nonested.GNU +include $(WRAPPER_ROOT)/include/makeinclude/rules.bin.GNU +include $(WRAPPER_ROOT)/include/makeinclude/rules.local.GNU + +#---------------------------------------------------------------------------- +# Local targets +#---------------------------------------------------------------------------- + +#---------------------------------------------------------------------------- +# Dependencies +#---------------------------------------------------------------------------- + +# DO NOT DELETE THIS LINE -- g++dep uses it. +# DO NOT PUT ANYTHING AFTER THIS LINE, IT WILL GO AWAY. + +.obj/MQ_Server.o .shobj/MQ_Server.so: MQ_Server.cpp \ + $(WRAPPER_ROOT)/ace/SV_Message_Queue.h \ + $(WRAPPER_ROOT)/ace/ACE.h \ + $(WRAPPER_ROOT)/ace/OS.h \ + $(WRAPPER_ROOT)/ace/Time_Value.h \ + $(WRAPPER_ROOT)/ace/config.h \ + $(WRAPPER_ROOT)/ace/Trace.h \ + $(WRAPPER_ROOT)/ace/SV_Message.h \ + $(WRAPPER_ROOT)/ace/SV_Message_Queue.i \ + test.h +.obj/MQ_Client.o .shobj/MQ_Client.so: MQ_Client.cpp \ + $(WRAPPER_ROOT)/ace/SV_Message_Queue.h \ + $(WRAPPER_ROOT)/ace/ACE.h \ + $(WRAPPER_ROOT)/ace/OS.h \ + $(WRAPPER_ROOT)/ace/Time_Value.h \ + $(WRAPPER_ROOT)/ace/config.h \ + $(WRAPPER_ROOT)/ace/Trace.h \ + $(WRAPPER_ROOT)/ace/SV_Message.h \ + $(WRAPPER_ROOT)/ace/SV_Message_Queue.i \ + test.h +.obj/TMQ_Server.o .shobj/TMQ_Server.so: TMQ_Server.cpp \ + $(WRAPPER_ROOT)/ace/Typed_SV_Message_Queue.h \ + $(WRAPPER_ROOT)/ace/SV_Message_Queue.h \ + $(WRAPPER_ROOT)/ace/ACE.h \ + $(WRAPPER_ROOT)/ace/OS.h \ + $(WRAPPER_ROOT)/ace/Time_Value.h \ + $(WRAPPER_ROOT)/ace/config.h \ + $(WRAPPER_ROOT)/ace/Trace.h \ + $(WRAPPER_ROOT)/ace/SV_Message.h \ + $(WRAPPER_ROOT)/ace/SV_Message_Queue.i \ + $(WRAPPER_ROOT)/ace/Typed_SV_Message.h \ + $(WRAPPER_ROOT)/ace/Log_Msg.h \ + $(WRAPPER_ROOT)/ace/Log_Record.h \ + $(WRAPPER_ROOT)/ace/Log_Priority.h \ + $(WRAPPER_ROOT)/ace/Log_Record.i \ + $(WRAPPER_ROOT)/ace/Synch.h \ + $(WRAPPER_ROOT)/ace/Synch_T.h test.h +.obj/TMQ_Client.o .shobj/TMQ_Client.so: TMQ_Client.cpp \ + $(WRAPPER_ROOT)/ace/Typed_SV_Message_Queue.h \ + $(WRAPPER_ROOT)/ace/SV_Message_Queue.h \ + $(WRAPPER_ROOT)/ace/ACE.h \ + $(WRAPPER_ROOT)/ace/OS.h \ + $(WRAPPER_ROOT)/ace/Time_Value.h \ + $(WRAPPER_ROOT)/ace/config.h \ + $(WRAPPER_ROOT)/ace/Trace.h \ + $(WRAPPER_ROOT)/ace/SV_Message.h \ + $(WRAPPER_ROOT)/ace/SV_Message_Queue.i \ + $(WRAPPER_ROOT)/ace/Typed_SV_Message.h \ + $(WRAPPER_ROOT)/ace/Log_Msg.h \ + $(WRAPPER_ROOT)/ace/Log_Record.h \ + $(WRAPPER_ROOT)/ace/Log_Priority.h \ + $(WRAPPER_ROOT)/ace/Log_Record.i \ + $(WRAPPER_ROOT)/ace/Synch.h \ + $(WRAPPER_ROOT)/ace/Synch_T.h test.h + +# IF YOU PUT ANYTHING HERE IT WILL GO AWAY diff --git a/examples/System_V_IPC/SV_Message_Queues/TMQ_Client.cpp b/examples/System_V_IPC/SV_Message_Queues/TMQ_Client.cpp new file mode 100644 index 00000000000..0961f92da6a --- /dev/null +++ b/examples/System_V_IPC/SV_Message_Queues/TMQ_Client.cpp @@ -0,0 +1,43 @@ +#include "ace/Typed_SV_Message_Queue.h" +// @(#)TMQ_Client.cpp 1.1 10/18/96 + +#include "ace/Log_Msg.h" +#include "test.h" + +int +main (void) +{ + 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 (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, "%p\n", "msgque.send"), 1); + + if (msgque.recv (recv_msg) < 0) + ACE_ERROR_RETURN ((LM_ERROR, "%p\n", "msgque.recv"), 1); + + Message_Data &recv_msg_data = recv_msg.data (); + + cout << "a message of length " + << recv_msg_data.length () + << " received from server " + << recv_msg_data.pid () + << " (user " + << recv_msg_data.user () << "): " + << recv_msg_data.text () << "\n"; + + return 0; +} + +#if defined (ACE_TEMPLATES_REQUIRE_SPECIALIZATION) +template class ACE_Typed_SV_Message_Queue<Message_Data>; +template class ACE_Typed_SV_Message<Message_Data>; +#endif /* ACE_TEMPLATES_REQUIRE_SPECIALIZATION */ diff --git a/examples/System_V_IPC/SV_Message_Queues/TMQ_Server.cpp b/examples/System_V_IPC/SV_Message_Queues/TMQ_Server.cpp new file mode 100644 index 00000000000..ab843c830d6 --- /dev/null +++ b/examples/System_V_IPC/SV_Message_Queues/TMQ_Server.cpp @@ -0,0 +1,59 @@ +#include "ace/Signal.h" +// @(#)TMQ_Server.cpp 1.1 10/18/96 + +#include "ace/Typed_SV_Message_Queue.h" +#include "ace/Log_Msg.h" +#include "test.h" + +// Must be global for signal Message... +ACE_Typed_SV_Message_Queue<Message_Data> msgque + (SRV_KEY, ACE_Typed_SV_Message_Queue<Message_Data>::ACE_CREATE); + +void +SIGNAL_handler (int) +{ + if (msgque.remove () < 0) + ACE_ERROR ((LM_ERROR, "%p\n%a", "msgque.recv", 1)); + ACE_OS::exit (0); +} + +int +main (void) +{ + char *username = ACE_OS::cuserid (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); + + ACE_Sig_Action sig2 ((ACE_SignalHandler) SIGNAL_handler, SIGINT); + + for (;;) + { + if (msgque.recv (recv_msg) == -1) + ACE_ERROR_RETURN ((LM_ERROR, "%p\n", "msgque.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 (msgque.send (send_msg) < 0) + ACE_ERROR_RETURN ((LM_ERROR, "%p\n", "msgque.send"), 1); + } + + /* NOTREACHED */ + return 0; +} + +#if defined (ACE_TEMPLATES_REQUIRE_SPECIALIZATION) +template class ACE_Typed_SV_Message_Queue<Message_Data>; +template class ACE_Typed_SV_Message<Message_Data>; +#endif /* ACE_TEMPLATES_REQUIRE_SPECIALIZATION */ diff --git a/examples/System_V_IPC/SV_Message_Queues/test.h b/examples/System_V_IPC/SV_Message_Queues/test.h new file mode 100644 index 00000000000..ebfbe3448ac --- /dev/null +++ b/examples/System_V_IPC/SV_Message_Queues/test.h @@ -0,0 +1,42 @@ +/* -*- C++ -*- */ +// @(#)test.h 1.1 10/18/96 + +#include "ace/OS.h" +#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[] = "", char text[] = ""): pid_ (p) + { + ::strncpy (this->username_, user, 9); + ::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[]) { ::strncpy (this->username_, user, 9); } + char *text (void) { return this->mtext_; } + void text (char text[]) { ::strncpy (this->mtext_, text, MSGSZ); } + int length (void) { return sizeof *this - sizeof this->mtext_ + ::strlen (this->mtext_) + 1; } + +protected: + long pid_; + char username_[9]; + char mtext_[MSGSZ]; +}; + +/* Note, this may not be 100 percent portable on all C++ compilers... */ +class ACE_Message_Block : public ACE_SV_Message, public Message_Data +{ +public: + ACE_Message_Block (long t, long p = 0, char login[] = "", char message[] = "") + : ACE_SV_Message (t), Message_Data (p, login, message) + {} +}; + diff --git a/examples/System_V_IPC/SV_Semaphores/Makefile b/examples/System_V_IPC/SV_Semaphores/Makefile new file mode 100644 index 00000000000..09ab2ef1359 --- /dev/null +++ b/examples/System_V_IPC/SV_Semaphores/Makefile @@ -0,0 +1,116 @@ +#---------------------------------------------------------------------------- +# @(#)Makefile 1.1 10/18/96 +# +# Makefile for Semaphore wrapper class +#---------------------------------------------------------------------------- + +#---------------------------------------------------------------------------- +# Local macros +#---------------------------------------------------------------------------- + +BIN = Semaphore_Client \ + Semaphore_Server \ + Semaphores + +LSRC = $(addsuffix .cpp,$(BIN)) + +VLDLIBS = $(LDLIBS:%=%$(VAR)) + +BUILD = $(VBIN) + +#---------------------------------------------------------------------------- +# Include macros and targets +#---------------------------------------------------------------------------- + +include $(WRAPPER_ROOT)/include/makeinclude/wrapper_macros.GNU +include $(WRAPPER_ROOT)/include/makeinclude/macros.GNU +include $(WRAPPER_ROOT)/include/makeinclude/rules.common.GNU +include $(WRAPPER_ROOT)/include/makeinclude/rules.nonested.GNU +include $(WRAPPER_ROOT)/include/makeinclude/rules.bin.GNU +include $(WRAPPER_ROOT)/include/makeinclude/rules.local.GNU + +#---------------------------------------------------------------------------- +# Local targets +#---------------------------------------------------------------------------- + +#---------------------------------------------------------------------------- +# Dependencies +#---------------------------------------------------------------------------- + +# DO NOT DELETE THIS LINE -- g++dep uses it. +# DO NOT PUT ANYTHING AFTER THIS LINE, IT WILL GO AWAY. + +.obj/Semaphore_Client.o .shobj/Semaphore_Client.so: Semaphore_Client.cpp Semaphore_Test.h \ + $(WRAPPER_ROOT)/ace/Log_Msg.h \ + $(WRAPPER_ROOT)/ace/Log_Record.h \ + $(WRAPPER_ROOT)/ace/ACE.h \ + $(WRAPPER_ROOT)/ace/OS.h \ + $(WRAPPER_ROOT)/ace/Time_Value.h \ + $(WRAPPER_ROOT)/ace/config.h \ + $(WRAPPER_ROOT)/ace/Trace.h \ + $(WRAPPER_ROOT)/ace/Log_Priority.h \ + $(WRAPPER_ROOT)/ace/Log_Record.i \ + $(WRAPPER_ROOT)/ace/Synch.h \ + $(WRAPPER_ROOT)/ace/Synch_T.h \ + $(WRAPPER_ROOT)/ace/Synch_T.cpp \ + $(WRAPPER_ROOT)/ace/Thread.h \ + $(WRAPPER_ROOT)/ace/Synch_T.i \ + $(WRAPPER_ROOT)/ace/SV_Shared_Memory.h \ + $(WRAPPER_ROOT)/ace/SV_Semaphore_Simple.h \ + $(WRAPPER_ROOT)/ace/SV_Semaphore_Simple.i +.obj/Semaphore_Server.o .shobj/Semaphore_Server.so: Semaphore_Server.cpp \ + $(WRAPPER_ROOT)/ace/Log_Msg.h \ + $(WRAPPER_ROOT)/ace/Log_Record.h \ + $(WRAPPER_ROOT)/ace/ACE.h \ + $(WRAPPER_ROOT)/ace/OS.h \ + $(WRAPPER_ROOT)/ace/Time_Value.h \ + $(WRAPPER_ROOT)/ace/config.h \ + $(WRAPPER_ROOT)/ace/Trace.h \ + $(WRAPPER_ROOT)/ace/Log_Priority.h \ + $(WRAPPER_ROOT)/ace/Log_Record.i \ + $(WRAPPER_ROOT)/ace/Synch.h \ + $(WRAPPER_ROOT)/ace/Synch_T.h \ + $(WRAPPER_ROOT)/ace/Synch_T.cpp \ + $(WRAPPER_ROOT)/ace/Thread.h \ + $(WRAPPER_ROOT)/ace/Synch_T.i \ + $(WRAPPER_ROOT)/ace/SV_Shared_Memory.h \ + $(WRAPPER_ROOT)/ace/SV_Semaphore_Simple.h \ + $(WRAPPER_ROOT)/ace/SV_Semaphore_Simple.i \ + $(WRAPPER_ROOT)/ace/Signal.h \ + $(WRAPPER_ROOT)/ace/Event_Handler.h \ + $(WRAPPER_ROOT)/ace/Set.h \ + $(WRAPPER_ROOT)/ace/Set.cpp \ + $(WRAPPER_ROOT)/ace/Set.i \ + Semaphore_Test.h +.obj/Semaphores.o .shobj/Semaphores.so: Semaphores.cpp \ + $(WRAPPER_ROOT)/ace/Malloc.h \ + $(WRAPPER_ROOT)/ace/ACE.h \ + $(WRAPPER_ROOT)/ace/OS.h \ + $(WRAPPER_ROOT)/ace/Time_Value.h \ + $(WRAPPER_ROOT)/ace/config.h \ + $(WRAPPER_ROOT)/ace/Trace.h \ + $(WRAPPER_ROOT)/ace/Malloc_T.h \ + $(WRAPPER_ROOT)/ace/Log_Msg.h \ + $(WRAPPER_ROOT)/ace/Log_Record.h \ + $(WRAPPER_ROOT)/ace/Log_Priority.h \ + $(WRAPPER_ROOT)/ace/Log_Record.i \ + $(WRAPPER_ROOT)/ace/Synch.h \ + $(WRAPPER_ROOT)/ace/Synch_T.h \ + $(WRAPPER_ROOT)/ace/Synch_T.cpp \ + $(WRAPPER_ROOT)/ace/Thread.h \ + $(WRAPPER_ROOT)/ace/Synch_T.i \ + $(WRAPPER_ROOT)/ace/Malloc_T.cpp \ + $(WRAPPER_ROOT)/ace/Malloc_T.i \ + $(WRAPPER_ROOT)/ace/Memory_Pool.h \ + $(WRAPPER_ROOT)/ace/Event_Handler.h \ + $(WRAPPER_ROOT)/ace/Signal.h \ + $(WRAPPER_ROOT)/ace/Set.h \ + $(WRAPPER_ROOT)/ace/Set.cpp \ + $(WRAPPER_ROOT)/ace/Set.i \ + $(WRAPPER_ROOT)/ace/SV_Semaphore_Complex.h \ + $(WRAPPER_ROOT)/ace/SV_Semaphore_Simple.h \ + $(WRAPPER_ROOT)/ace/SV_Semaphore_Simple.i \ + $(WRAPPER_ROOT)/ace/SV_Semaphore_Complex.i \ + Semaphore_Test.h + +# IF YOU PUT ANYTHING HERE IT WILL GO AWAY diff --git a/examples/System_V_IPC/SV_Semaphores/Semaphore_Client.cpp b/examples/System_V_IPC/SV_Semaphores/Semaphore_Client.cpp new file mode 100644 index 00000000000..69b414cef0c --- /dev/null +++ b/examples/System_V_IPC/SV_Semaphores/Semaphore_Client.cpp @@ -0,0 +1,31 @@ +#include "Semaphore_Test.h" +// @(#)Semaphore_Client.cpp 1.1 10/18/96 + +#include "ace/SV_Shared_Memory.h" +#include "ace/SV_Semaphore_Simple.h" + +int +main (void) +{ + ACE_SV_Shared_Memory shm_client (SHM_KEY, + SHMSZ, + ACE_SV_Shared_Memory::ACE_OPEN); + ACE_SV_Semaphore_Simple sem (SEM_KEY_1, + ACE_SV_Semaphore_Simple::ACE_OPEN, 0, 2); + + char *s = (char *) shm_client.get_segment_ptr (); + + if (sem.acquire (0) < 0) + ACE_OS::perror ("client sem.acquire"), ACE_OS::exit (1); + + while (*s != '\0') + putchar (*s++); + + putchar ('\n'); + + if (sem.release (1) < 0) + ACE_OS::perror ("client sem.release"), ACE_OS::exit (1); + + return 0; +} + diff --git a/examples/System_V_IPC/SV_Semaphores/Semaphore_Server.cpp b/examples/System_V_IPC/SV_Semaphores/Semaphore_Server.cpp new file mode 100644 index 00000000000..721bb07171e --- /dev/null +++ b/examples/System_V_IPC/SV_Semaphores/Semaphore_Server.cpp @@ -0,0 +1,41 @@ +#include "ace/Log_Msg.h" +// @(#)Semaphore_Server.cpp 1.1 10/18/96 + +#include "ace/SV_Shared_Memory.h" +#include "ace/SV_Semaphore_Simple.h" +#include "ace/Signal.h" +#include "Semaphore_Test.h" + +ACE_SV_Shared_Memory shm_server (SHM_KEY, SHMSZ, ACE_SV_Shared_Memory::ACE_CREATE); +ACE_SV_Semaphore_Simple sem (SEM_KEY_1, ACE_SV_Semaphore_Simple::ACE_CREATE, 0, 2); + +static void +cleanup (int = 0) +{ + if (shm_server.remove () < 0 || sem.remove () < 0) + ACE_ERROR ((LM_ERROR, "%p\n%a", "remove", 1)); + ACE_OS::exit (0); +} + +int +main (void) +{ + ACE_Sig_Action sig ((ACE_SignalHandler) cleanup, SIGINT); + + char *s = (char *) shm_server.get_segment_ptr (); + + for (char c = 'a'; c <= 'z'; c++) + *s++ = c; + + *s = '\0'; + + if (sem.release (0) < 0) + ESD ("server sem.release", done); + + if (sem.acquire (1) < 0) + ESD ("server sem.acquire", done); + +done: + cleanup (); + return 0; +} diff --git a/examples/System_V_IPC/SV_Semaphores/Semaphore_Test.h b/examples/System_V_IPC/SV_Semaphores/Semaphore_Test.h new file mode 100644 index 00000000000..07bc3e9ecec --- /dev/null +++ b/examples/System_V_IPC/SV_Semaphores/Semaphore_Test.h @@ -0,0 +1,11 @@ +/* -*- C++ -*- */ +// @(#)Semaphore_Test.h 1.1 10/18/96 + +#include "ace/Log_Msg.h" + +#define SHMSZ 27 +#define SEM_KEY_1 2345 +#define SEM_KEY_2 4321 +#define SHM_KEY 5678 + +#define ESD(MSG,LABEL) do { ACE_ERROR ((LM_ERROR, MSG)); goto LABEL; } while (0) diff --git a/examples/System_V_IPC/SV_Semaphores/Semaphores.cpp b/examples/System_V_IPC/SV_Semaphores/Semaphores.cpp new file mode 100644 index 00000000000..923bd142cc3 --- /dev/null +++ b/examples/System_V_IPC/SV_Semaphores/Semaphores.cpp @@ -0,0 +1,94 @@ +// Illustrates the use of the Semaphore_Complex class. Note that it +// @(#)Semaphores.cpp 1.1 10/18/96 + +// 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.h" +#include "ace/SV_Semaphore_Complex.h" +#include "Semaphore_Test.h" + +ACE_Malloc<ACE_Shared_Memory_Pool, ACE_SV_Semaphore_Simple> allocator; +ACE_SV_Semaphore_Complex *mutex = 0; +ACE_SV_Semaphore_Complex *synch = 0; + +/* Pointer to memory shared by both the client and server. */ +static char *shm; + +static int +do_parent (void) +{ + char *s = shm; + + mutex = new ACE_SV_Semaphore_Complex (SEM_KEY_1, ACE_SV_Semaphore_Complex::ACE_CREATE, 0); + synch = new ACE_SV_Semaphore_Complex (SEM_KEY_2, ACE_SV_Semaphore_Complex::ACE_CREATE, 0); + + for (char c = 'a'; c <= 'z'; c++) + *s++ = c; + + *s = '\0'; + + if (mutex->release () == -1) + ACE_ERROR_RETURN ((LM_ERROR, "%p", "server mutex.release"), 1); + + if (synch->acquire () == -1) + ACE_ERROR_RETURN ((LM_ERROR, "%p", "server synch.acquire"), 1); + return 0; +} + +static int +do_child (void) +{ + mutex = new ACE_SV_Semaphore_Complex (SEM_KEY_1, ACE_SV_Semaphore_Complex::ACE_CREATE, 0); + synch = new ACE_SV_Semaphore_Complex (SEM_KEY_2, ACE_SV_Semaphore_Complex::ACE_CREATE, 0); + + while (mutex->tryacquire () == -1) + if (errno == EAGAIN) + ACE_DEBUG ((LM_DEBUG, "spinning in client!\n")); + else + ACE_ERROR_RETURN ((LM_ERROR, "client mutex.tryacquire"), 1); + + for (char *s = (char *) shm; *s != '\0'; s++) + putchar (*s); + + putchar ('\n'); + + if (synch->release () == -1) + ACE_ERROR_RETURN ((LM_ERROR, "client synch.release"), 1); + return 0; +} + +int +main (void) +{ + shm = (char *) allocator.malloc (27); + + switch (ACE_OS::fork ()) + { + case -1: + ACE_ERROR_RETURN ((LM_ERROR, "fork failed\n"), -1); + /* NOTREACHED */ + case 0: + return do_child (); + default: + { + int result = do_parent (); + + if (wait (0) == -1) + ACE_ERROR_RETURN ((LM_ERROR, "wait"), -1); + + allocator.remove (); + + if (mutex->remove () == -1) + ACE_ERROR_RETURN ((LM_ERROR, "%p\n", "mutex.remove"), -1); + else if (synch->remove () == -1) + ACE_ERROR_RETURN ((LM_ERROR, "%p\n", "synch.remove"), -1); + return result; + } + } +} + +#if defined (ACE_TEMPLATES_REQUIRE_SPECIALIZATION) +template class ACE_Malloc<ACE_Shared_Memory_Pool, ACE_SV_Semaphore_Simple>; +#endif /* ACE_TEMPLATES_REQUIRE_SPECIALIZATION */ + diff --git a/examples/System_V_IPC/SV_Shared_Memory/Makefile b/examples/System_V_IPC/SV_Shared_Memory/Makefile new file mode 100644 index 00000000000..a950c681250 --- /dev/null +++ b/examples/System_V_IPC/SV_Shared_Memory/Makefile @@ -0,0 +1,54 @@ +#---------------------------------------------------------------------------- +# @(#)Makefile 1.1 10/18/96 +# +# Makefile for tests of the SV_Shared_Memory wrapper +#---------------------------------------------------------------------------- + +#---------------------------------------------------------------------------- +# Local macros +#---------------------------------------------------------------------------- + +BIN = SV_Shared_Memory_Test + +LSRC = $(addsuffix .cpp,$(BIN)) + +LDLIBS = + +VLDLIBS = $(LDLIBS:%=%$(VAR)) + +BUILD = $(VBIN) + +INSTALL = + +#---------------------------------------------------------------------------- +# Include macros and targets +#---------------------------------------------------------------------------- + +include $(WRAPPER_ROOT)/include/makeinclude/wrapper_macros.GNU +include $(WRAPPER_ROOT)/include/makeinclude/macros.GNU +include $(WRAPPER_ROOT)/include/makeinclude/rules.common.GNU +include $(WRAPPER_ROOT)/include/makeinclude/rules.nonested.GNU +include $(WRAPPER_ROOT)/include/makeinclude/rules.bin.GNU +include $(WRAPPER_ROOT)/include/makeinclude/rules.local.GNU + +#---------------------------------------------------------------------------- +# Local targets +#---------------------------------------------------------------------------- + +#---------------------------------------------------------------------------- +# Dependencies +#---------------------------------------------------------------------------- + +# DO NOT DELETE THIS LINE -- g++dep uses it. +# DO NOT PUT ANYTHING AFTER THIS LINE, IT WILL GO AWAY. + +.obj/SV_Shared_Memory_Test.o .shobj/SV_Shared_Memory_Test.so: SV_Shared_Memory_Test.cpp \ + $(WRAPPER_ROOT)/ace/SV_Shared_Memory.h \ + $(WRAPPER_ROOT)/ace/ACE.h \ + $(WRAPPER_ROOT)/ace/OS.h \ + $(WRAPPER_ROOT)/ace/Time_Value.h \ + $(WRAPPER_ROOT)/ace/config.h \ + $(WRAPPER_ROOT)/ace/Trace.h \ + SV_Shared_Memory_Test.h + +# IF YOU PUT ANYTHING HERE IT WILL GO AWAY diff --git a/examples/System_V_IPC/SV_Shared_Memory/SV_Shared_Memory_Test.cpp b/examples/System_V_IPC/SV_Shared_Memory/SV_Shared_Memory_Test.cpp new file mode 100644 index 00000000000..3b1fa48ceb5 --- /dev/null +++ b/examples/System_V_IPC/SV_Shared_Memory/SV_Shared_Memory_Test.cpp @@ -0,0 +1,60 @@ +#include "ace/SV_Shared_Memory.h" +// @(#)SV_Shared_Memory_Test.cpp 1.1 10/18/96 + +#include "SV_Shared_Memory_Test.h" + +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; +} + diff --git a/examples/System_V_IPC/SV_Shared_Memory/SV_Shared_Memory_Test.h b/examples/System_V_IPC/SV_Shared_Memory/SV_Shared_Memory_Test.h new file mode 100644 index 00000000000..795e9180f65 --- /dev/null +++ b/examples/System_V_IPC/SV_Shared_Memory/SV_Shared_Memory_Test.h @@ -0,0 +1,7 @@ +/* -*- C++ -*- */ +// @(#)SV_Shared_Memory_Test.h 1.1 10/18/96 + +#include "ace/OS.h" +#define SHMSZ 27 +#define SEM_KEY 1234 +#define SHM_KEY 5678 |