Unix Technical Forum

IPC shared memory with a struct

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 ...


Go Back   Unix Technical Forum > Unix Operating Systems > Solaris Operating System > comp.unix.solaris

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 01-12-2008, 05:54 AM
pedro
 
Posts: n/a
Default IPC shared memory with a struct

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;
}

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 01-12-2008, 05:54 AM
Stefan Naewe
 
Posts: n/a
Default Re: IPC shared memory with a struct

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
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 10:04 AM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0
www.UnixAdminTalk.com