I basically want a central controller object in C++, where all the control information is stored, and classes creating an object of that class will get the current data in the shared memory. Creating a structure or class in shared memory would be great, rather that serializing it and storing...
2006-07-10
21:47:12
·
5 answers
·
asked by
iamxsj
1
in
Computers & Internet
➔ Programming & Design
it is in the same program, but there is a producer and consumer thread which have to be synchronized, and have to share some variables with them
2006-07-11
00:11:01 ·
update #1
#include
#include
#include
struct emp {
char name[22];
int age;
};
struct emp *emp1;
int main(int argc, char *argv[])
{
key_t key;
int shmid;
char *data;
int mode;
/* make the key: */
if ((key = ftok("shmdemo.c", 'R')) == -1) {
perror("ftok");
exit(1);
}
/* connect to (and possibly create) the segment: */
if ((shmid = shmget(key, sizeof(emp), 0644 | IPC_CREAT)) == -1) {
perror("shmget");
exit(1);
}
/* attach to the segment to get a pointer to it: */
emp1 = (emp *)shmat(shmid, (void *)0, 0);
if (data == (char *)(-1)) {
perror("shmat");
exit(1);
}
strncpy(emp1->name, "Suraj", 6);
emp1->age = 66;
printf("segment contains: \"%s\"\n", emp1);
/* detach from the segment: */
if (shmdt(emp1) == -1) {
2006-07-11
00:53:05 ·
update #2
perror("shmdt");
exit(1);
}
return 0;
}
2006-07-11
00:53:31 ·
update #3
figured out...just had to typecast for a structure...no idea about class though...ll do with structure
2006-07-11
00:54:25 ·
update #4
but how can u use static variables between 2 processes? they get a copy each, dont they? I tried to do that at first, and there are only 2 processes anyways...so it's not worth going through the complexities of threads...i guess
2006-07-11
17:07:19 ·
update #5