This is a discussion on IPC shared memory with a struct within the comp.unix.solaris forums, part of the Solaris Operating System category; --> i am trying to set a memory segment with a structure but its not working , i am missing ...
| |||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| ||||
| i am trying to set a memory segment with a structure but its not working , i am missing something : typedef struct dataestrutura { char * usr; char * passwd; char * osid; } dataestrutura; int main( int argc, char *argv[] ) { key_t key; int shmid; int mode; ///struct em vez do char */// dataestrutura *data_ptr; cout <<"/* make the key: */"<<endl; if ((key = ftok("/home/pedro/SHARED_MEMORY/teste.txt", 'LP')) == -1) { perror("ftok"); exit(1); } cout << "/* connect to (and possibly create) the segment: */" << endl; if ((shmid = shmget(key,SHM_SIZE, 0644 | IPC_CREAT)) == -1) { perror("shmget"); exit(1); } cout << "/* attach to the segment to get a pointer to it: */" << endl; data_ptr = (dataestrutura *)shmat(shmid,0, 0); if (data_ptr == (dataestrutura *)(-1)) { perror("shmat"); exit(1); } cout << "/* read or modify the segment, based on the command line: */" << endl; /*strcpy(data_ptr->usr, "bdsn"); strcpy(data_ptr->passwd, "bdsn"); strcpy(data_ptr->osid, "oesis");*/ data_ptr->usr="bdsn"; data_ptr->passwd="bdsn"; data_ptr->osid="osid"; cout <<"/* detach from the segment: */" << endl; if (shmdt(data_ptr) == -1) { perror("shmdt"); exit(1); } cout << "/* Finish up */" << endl; return 0; } |
| ||||
| On 6/26/2007 4:55 PM, pedro wrote: > i am trying to set a memory segment with a structure but its not > working , i am missing something : > > typedef struct dataestrutura { > char * usr; > char * passwd; > char * osid; > } dataestrutura; I'd try this instead: typedef struct dataestrutura { char usr[MAX_USR_SIZE]; char passwd[MAX_PASSWD_SIZE]; char osid[MAX_OSID_SIZE]; } dataestrutura; and #define the MAX_... values somewhere. > int main( int argc, char *argv[] ) > { > key_t key; > int shmid; > int mode; > ///struct em vez do char */// > > dataestrutura *data_ptr; > > > cout <<"/* make the key: */"<<endl; > if ((key = ftok("/home/pedro/SHARED_MEMORY/teste.txt", 'LP')) == > -1) > { > perror("ftok"); > exit(1); > } > > > cout << "/* connect to (and possibly create) the segment: */" << > endl; > if ((shmid = shmget(key,SHM_SIZE, 0644 | IPC_CREAT)) == -1) > { > perror("shmget"); > exit(1); > } > > cout << "/* attach to the segment to get a pointer to it: */" > << endl; > data_ptr = (dataestrutura *)shmat(shmid,0, 0); > if (data_ptr == (dataestrutura *)(-1)) > { > perror("shmat"); > exit(1); > } > > cout << "/* read or modify the segment, based on the command > line: */" << endl; > > > /*strcpy(data_ptr->usr, "bdsn"); > strcpy(data_ptr->passwd, "bdsn"); > strcpy(data_ptr->osid, "oesis");*/ > > data_ptr->usr="bdsn"; > data_ptr->passwd="bdsn"; > data_ptr->osid="osid"; > > > > > cout <<"/* detach from the segment: */" << endl; > if (shmdt(data_ptr) == -1) > { > perror("shmdt"); > exit(1); > } > > > > > > cout << "/* Finish up */" << endl; > > return 0; > } > Regards, Stefan -- Stefan Naewe stefan dot naewe at atlas-elektronik dot com Don't top-post http://www.catb.org/~esr/jargon/html/T/top-post.html Plain text mails only, please http://www.expita.com/nomime.html |