Skip to content

Instantly share code, notes, and snippets.

@ox1111
Last active February 27, 2019 06:07
Show Gist options
  • Save ox1111/7e41320603a2372e582c7762851320b8 to your computer and use it in GitHub Desktop.
Save ox1111/7e41320603a2372e582c7762851320b8 to your computer and use it in GitHub Desktop.
#include "ace/Shared_Memory_SV.h"
#include "ace/Log_Msg.h"
#include "ace/OS_NS_unistd.h"
#include <string>
#include <iostream>
#define SHMSZ 27
#define SHM_KEY (key_t) 5678
static void
client (void)
{
ACE_Shared_Memory_SV shm_client (SHM_KEY, SHMSZ);
char *shm = (char *) shm_client.malloc ();
for (char *s = shm; *s != '\0'; s++)
putchar (*s);
putchar ('\n');
*shm = '*';
}
static void
server (void)
{
ACE_Shared_Memory_SV shm_server (SHM_KEY,
SHMSZ,
ACE_Shared_Memory_SV::ACE_CREATE);
char *shm = (char *) shm_server.malloc ();
char *s = shm;
for (char c = 'a'; c <= 'z'; c++)
*s++ = c;
*s = '\0';
while (*shm != '*')
ACE_OS::sleep (1);
if (shm_server.remove () < 0)
ACE_ERROR ((LM_ERROR, "%p\n", "remove"));
}
int
main( int argc , char ** argv )
{
std::cout << " man " << std::endl;
switch (ACE_OS::fork ())
{
case -1:
ACE_ERROR_RETURN ((LM_ERROR, "%p\n", "fork"), 1);
case 0:
// Make sure the server starts up first.
ACE_OS::sleep (1);
client ();
break;
default:
server ();
break;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment