zahid
08-22-2001, 11:14 PM
Hello,
Anyone worked with shared memory in linux? I want to share a B-TREE of structures.
Does anyone have any idea?
Root or one user can give access to his/her data in memory for others.
Can you provide me a sample of few lines? Just two node of a simple structure.
What will be the client program, and what change need in server program.
Server:
#include<stdio.h>
int main()
{
struct NAME {
char name[30];
int id;
struct NAME *top;
struct NAME *down;
} *tm, *hd;
tm=(struct NAME *)malloc(sizeof(struct NAME));
tm->top=NULL;
strcpy(tm->name,"Zahid Hossain");
tm->id=1999;
hd=tm;
tm=(struct NAME *)malloc(sizeof(struct NAME));
tm->top=hd;
tm->down=NULL;
strcpy(tm->name,"Glynn Clements");
tm->id=2001;
hd->down=tm;
while(1)
{
printf("%s %d\n",hd->name, hd->id);
printf("zahid@allbd.com %s %d\n",hd->down->name, hd->down->id);
sleep(12);
}
}
Note: Once I have tried for string but did not work for structure or BTREE. Or I don't know how to implement it. Last time xmdvp helped me on the problem.
zahid
01-05-2002, 04:40 AM
Is there any alternative of shared memory?
rook_5150
01-22-2002, 12:40 PM
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
#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
#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
zahid
01-26-2002, 11:15 PM
Your code is working..
I can do this .. it's okay. But I have an application which uses B-Tree And I want to make abailable the data on the memory for all other of read or if necessary for write.
Now, for the implementation of the B-Tree where the required memory space is not predefined seems little complex to me.
I was asking for the help or any idea of implementing.
Powered by vBulletin® Version 4.2.5 Copyright © 2019 vBulletin Solutions Inc. All rights reserved.