I've never worked with a B-tree before, but I have worked with shared memory. On linux, pretty much you only option is SYSV shared memory (any one know of a distro supporting posix shared memory?) Any way here is code for a shared memory server and client. The server prints out a shared memory id to the screen, which you have to use as the first argument to the client. The server then asks for a message in a loop, and writes your message to shared memory. You can than use the client to access what message was written to shared memory.
I did not remove the shared memory when the server exits, so you have to use ipcrm -m <shmid> to remove the shared memory.
see the man pages for shmop, and shmget for more info.
shmserver.c
Code:
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <errno.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
int main (int argc, char ** argv) {
int shmid;
void * mem;
char buffer [1024];
/* get the shared memory (handle) */
if ((shmid = shmget(IPC_PRIVATE, sizeof(buffer), IPC_CREAT|0666)) == -1) {
fprintf(stderr, "Error [%d]: shmget failed - %s.\n", errno, strerror(errno));
exit(EXIT_FAILURE);
}
printf("shmid: [%d]\n", shmid);
/* attach the memory to an address */
if ((mem = shmat(shmid, NULL, SHM_W)) == (void*) -1) {
fprintf(stderr, "Error [%d]: shmat failed - %s.\n", errno, strerror(errno));
exit(EXIT_FAILURE);
}
while (1) {
printf("Enter Message: ");
if (!fgets(buffer, sizeof(buffer), stdin) || !strncmp(buffer, "exit", 4)) {
break;
}
strncpy(mem, buffer, sizeof(buffer));
}
if (shmdt(mem) == -1) {
fprintf(stderr, "Error [%d]: shmdt failed - %s.\n", errno, strerror(errno));
exit(EXIT_FAILURE);
}
return EXIT_SUCCESS;
}
shmclient.c
Code:
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <errno.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
int main (int argc, char ** argv) {
int shmid;
void * mem;
if (argc != 2) {
fprintf(stderr, "Usage: %s <shmid>\n", argv[0]);
exit(EXIT_FAILURE);
}
shmid = atoi(argv[1]);
/* attach the memory to an address */
if ((mem = shmat(shmid, NULL, SHM_R)) == (void*) -1) {
fprintf(stderr, "Error [%d]: shmat failed - %s.\n", errno, strerror(errno));
exit(EXIT_FAILURE);
}
printf("Read from shared memory:\n[%s]\n", mem);
if (shmdt(mem) == -1) {
fprintf(stderr, "Error [%d]: shmdt failed - %s.\n", errno, strerror(errno));
exit(EXIT_FAILURE);
}
return EXIT_SUCCESS;
}
Running ther server
$ shmserver
shmid: [792]
Enter Message: hello shared memory!
Enter Message: maybey i should finish fixing the olstores table and send a report to my boss
Enter Message: or work on my robocode tank
Enter Message: exit
Running the client
$ shmclient 792
Read from shared memory:
[hello shared memory!
]
[jabba@/users/dp/bryanc/shm]
$ shmclient 792
Read from shared memory:
[maybey i should finish fixing the olstores table and send a report to my boss
]
[jabba@/users/dp/bryanc/shm]
$ shmclient 792
Read from shared memory:
[or work on my robocode tank
]
Listing and removing the shared memory (by the way im running on Solaris 8 sparc, should be similar though.
[jabba@/users/dp/bryanc/shm]
$ ipcs -m | grep bryanc
m 792 0 --rw-rw-rw- bryanc dp
[jabba@/users/dp/bryanc/shm]
$ ipcrm -m 792