Inter-Process Communication on Mac – Know How part2 (Shared Memory)

In  Part 1 of this blog series we discussed about various techniques available for Inter-Process Communication (IPC) on Mac OS X.

As discussed in earlier blog Shared Memory is a implementation for IPC where a memory section is shared between different processes.In other words process A writes to this memory and B can read from this memory, or vice verse. This is fast and data doesn’t have to be copied around. The downside is that it’s really difficult to coordinate changes to the shared memory area.

Here in this blog we will see the implementation  of IPC using shared memory. We will create a server and client and will create a communication channel for data transfer between two processes.

First let us look at the basic implementation of Server using Shared memory

#include <iostream>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <stdio.h>

#define SIZE      27
#define KEY       1097

void getSharedMemorySpace(char** sharedMemSpace)
{
int sharedMemId;
key_t key;

//Name shared memory segment
key = KEY;

Create the segment using shmget() which returns the identifier of the shared memory segment associated with the value of the argument key. A new shared memory segment, with size equal to the value of size rounded up to a multiple of PAGE_SIZE

if ((sharedMemId = shmget(key, SIZE, IPC_CREAT | 0666)) < 0)
{
perror(“Error in creating shared memory segment in function shmget”);
exit(1);
}

Attach the segment to data space using shmat() which attaches the shared memory segment identified by shmid to the address space of the calling process. The attaching address is specified by shmaddr with one of the following criteria:

If shmaddr is NULL, the system chooses a suitable (unused) address at which to attach the segment.

If shmaddr isn’t NULL and SHM_RND is specified in shmflg, the attach occurs at the address equal to shmaddr rounded down to the nearest multiple of SHMLBA. Otherwise shmaddr must be a page-aligned address at which the attach occurs.

if ((*sharedMemSpace = (char*)shmat(sharedMemId, NULL, 0)) == (char *)(-1))
{
perror(“Error in attaching segment to data space in function shmat”);
exit(1);
}
}

Write in to the shared memory space

int main(int argc, const char * argv[])
{
char character;
char *sharedMemSpace, *string;

getSharedMemorySpace(&sharedMemSpace);
string = sharedMemSpace;

//Create string to be shared
for (character = ‘a’; character <= ‘z’; ++character)
{
*string++ = character;
}

*string = NULL;

//wait until the other proces changes the first character of our memory to ‘*’, indicating that it has read what we put there.
while (*sharedMemSpace != ‘*’)
{
sleep(1);
}

return 0;
}

Now Let’s look at the implementation of Client Implementation.

#include <iostream>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <stdio.h>

#define SIZE      27
#define KEY       1097

void getSharedMemorySpace(char** sharedMemSpace)
{
int sharedMemId;
key_t key;

//program need to get the segment named KEY, created by the server.
key = KEY;

//Locate the segment.
if ((sharedMemId = shmget(key, SIZE, 0666)) < 0) {
perror(“Error in locating the segment in function shmget”);
exit(1);
}

//Attach the segment to data space.
if ((*sharedMemSpace = (char*)shmat(sharedMemId, NULL, 0)) == (char *) -1)
{
perror(“Error in attaching segment to data space in function shmat”);
exit(1);
}
}

Read data from shared memory

int main(int argc, const char * argv[])
{
char *sharedMemSpace;
char *string;

//Get sharedMemorySpace
getSharedMemorySpace(&sharedMemSpace);

//Now read what the server put in the memory.
for (string = sharedMemSpace; *string != NULL; ++string)
{
putchar(*string);
}
putchar(‘\n’);

//Finally, change the first character of the segment to ‘*’, indicating program has read the segment.
*sharedMemSpace = ‘*’;

return 0;
}

 

Written By: HEM DUTT, Sr. Engineer/Tech Lead (Mac OSX development), Mindfire Solutions

Advertisements

About HEM DUTT

Seasoned Mac OS X developer. Expertise in Mac OSX application development. knowledge of MFC and IOS

Posted on October 6, 2014, in Inter-Process Communication (IPC), Objective-C and tagged , , , , , . Bookmark the permalink. 2 Comments.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: