English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

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

5 answers

Don't use shared memory for this. There are much better uses for that and they can have I higher overhead.

You should use synchronized (that is protected with either mutexes, semaphores, or some other sort of lock) static variables of some kind. There are tons of resources on multithreaded programming all over the net.

Addition to your note: processes and threads are two different things. Are you talking about a consumer process and server process?

2006-07-11 04:08:46 · answer #1 · answered by Alex T 2 · 0 0

c++ has static variables. Although "Shared Memory" has a slightly different and more specific meaning usually, a static class variable is shared by instances of that class.

Usually "shared memory" refers to operating system managed memory that can be shared between programs. There are os functions that allow you to do this safely.

Since this sounds like inner program variable sharing, I'd suggest you look into the use of static class member variables.

2006-07-10 23:31:34 · answer #2 · answered by Gizmo L 4 · 0 0

1000's! Hiding from the Vikings even as they got here to burn down our village. Throwing previous women contained in the village pond to be sure in the adventure that they were witches or no longer. Digging elephants traps contained in the important highway, even although there have been no elephants about, and operating away even as the community police guy on his motorcycle fell in and became fatally impaled on the timber stakes on the bottom. Oh, and each and each and every summer time the Unicorns ought to come and in case you've been quiet you ought to cover up contained in the timber contained in the woods to visual show unit them bypass below............then take them out with a crossbow. Oh, such relaxing. that's all authentic......surely ;o)

2016-11-06 04:57:40 · answer #3 · answered by heyder 4 · 0 0

Use Public/Global variable..............actually i wrote my last programm in C before 1.5 years so check it out

Bye

2006-07-10 22:11:35 · answer #4 · answered by kashif_sultan2002 2 · 0 0

it is not possible in structure ...but what you want to say that control center , it is possible using class..

in class you use inheritance or friend to create that control center....

2006-07-10 22:17:20 · answer #5 · answered by Anonymous · 0 0

fedest.com, questions and answers