<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: IPC shared memory with a struct? in Operating System - Linux</title>
    <link>https://community.hpe.com/t5/operating-system-linux/ipc-shared-memory-with-a-struct/m-p/4027081#M93848</link>
    <description>typedef struct dataestrutura {&lt;BR /&gt;char * usr;&lt;BR /&gt;char * passwd;&lt;BR /&gt;char * osid;&lt;BR /&gt;} dataestrutura&lt;BR /&gt;&lt;BR /&gt;The fundamental problem is that you have allocated space for the pointers but not for the data pointed to.&lt;BR /&gt;&lt;BR /&gt;If you change your struct to something like this:&lt;BR /&gt;&lt;BR /&gt;#define BIG_ENOUGH 256&lt;BR /&gt;&lt;BR /&gt;typedef struct dataestrutura {&lt;BR /&gt;char usr[BIG_ENOUGH];&lt;BR /&gt;char passwd[BIG_ENOUGH];&lt;BR /&gt;char osid[BIG_ENOUGH];&lt;BR /&gt;} dataestrutura&lt;BR /&gt;&lt;BR /&gt;then you will actually allocate storage space. &lt;BR /&gt;&lt;BR /&gt;</description>
    <pubDate>Tue, 26 Jun 2007 10:10:03 GMT</pubDate>
    <dc:creator>A. Clay Stephenson</dc:creator>
    <dc:date>2007-06-26T10:10:03Z</dc:date>
    <item>
      <title>IPC shared memory with a struct?</title>
      <link>https://community.hpe.com/t5/operating-system-linux/ipc-shared-memory-with-a-struct/m-p/4027080#M93847</link>
      <description>&lt;BR /&gt;i am trying to set a memory segment with this struct:&lt;BR /&gt;&lt;BR /&gt;SERVER&lt;BR /&gt;&lt;BR /&gt;typedef struct dataestrutura {&lt;BR /&gt;    char *  usr;&lt;BR /&gt;    char  * passwd;&lt;BR /&gt;    char  * osid;&lt;BR /&gt;} dataestrutura;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;int main( int argc, char *argv[] ) &lt;BR /&gt;{&lt;BR /&gt;   key_t key;&lt;BR /&gt;   int shmid;&lt;BR /&gt;   int mode;&lt;BR /&gt;   ///struct em vez do char *///&lt;BR /&gt;  &lt;BR /&gt;   dataestrutura  *data_ptr;&lt;BR /&gt;        &lt;BR /&gt;    /* Initialize the logging interface */&lt;BR /&gt;    openlog( DAEMON_NAME, LOG_PID, LOG_LOCAL5 );&lt;BR /&gt;    syslog( LOG_INFO, "starting" );&lt;BR /&gt;&lt;BR /&gt;    /* One may wish to process command line arguments here */&lt;BR /&gt;    cout &amp;lt;&amp;lt; "\n daemon started \n" &amp;lt;&amp;lt; endl;&lt;BR /&gt;    &lt;BR /&gt;    /* Daemonize */&lt;BR /&gt;    daemonize( DAEMON_NAME );&lt;BR /&gt;    &lt;BR /&gt;&lt;BR /&gt;    &lt;BR /&gt;     cout &amp;lt;&amp;lt;"/* make the key: */"&amp;lt;&lt;ENDL&gt;&lt;/ENDL&gt;     if ((key = ftok("/home/pedro/SHARED_MEMORY/teste.txt", 'LP')) == -1) &lt;BR /&gt;     {&lt;BR /&gt;       perror("ftok");&lt;BR /&gt;       exit(1);&lt;BR /&gt;     }&lt;BR /&gt;     &lt;BR /&gt;&lt;BR /&gt;     &lt;BR /&gt;      cout &amp;lt;&amp;lt; "/* connect to (and possibly create) the segment: */" &amp;lt;&amp;lt; endl;&lt;BR /&gt;      if ((shmid = shmget(key,SHM_SIZE, 0644 | IPC_CREAT)) == -1) &lt;BR /&gt;      {&lt;BR /&gt;        perror("shmget");&lt;BR /&gt;        exit(1);&lt;BR /&gt;      }&lt;BR /&gt;      &lt;BR /&gt;       cout &amp;lt;&amp;lt; "/* attach to the segment to get a pointer to it: */" &amp;lt;&amp;lt; endl;&lt;BR /&gt;      data_ptr = (dataestrutura *)shmat(shmid,0, 0);&lt;BR /&gt;      if (data_ptr  == (dataestrutura *)(-1)) &lt;BR /&gt;      {&lt;BR /&gt;        perror("shmat");&lt;BR /&gt;        exit(1);&lt;BR /&gt;      }&lt;BR /&gt;&lt;BR /&gt;       cout &amp;lt;&amp;lt; "/* read or modify the segment, based on the command line: */" &amp;lt;&amp;lt; endl;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;       /*strcpy(data_ptr-&amp;gt;usr, "bdsn");&lt;BR /&gt;       strcpy(data_ptr-&amp;gt;passwd, "bdsn");&lt;BR /&gt;       strcpy(data_ptr-&amp;gt;osid, "oesis");*/&lt;BR /&gt;      &lt;BR /&gt;         data_ptr-&amp;gt;usr="bdsn";&lt;BR /&gt;         data_ptr-&amp;gt;passwd="bdsn";&lt;BR /&gt;         data_ptr-&amp;gt;osid="osid"; &lt;BR /&gt;                 &lt;BR /&gt;&lt;BR /&gt;      &lt;BR /&gt;&lt;BR /&gt;     cout &amp;lt;&amp;lt;"/* detach from the segment: */" &amp;lt;&amp;lt; endl;&lt;BR /&gt;    if (shmdt(data_ptr) == -1) &lt;BR /&gt;    {&lt;BR /&gt;      perror("shmdt");&lt;BR /&gt;      exit(1);&lt;BR /&gt;    }&lt;BR /&gt;    &lt;BR /&gt;&lt;BR /&gt;    &lt;BR /&gt;&lt;BR /&gt;    &lt;BR /&gt;    &lt;BR /&gt;&lt;BR /&gt;    cout &amp;lt;&amp;lt; "/* Finish up */" &amp;lt;&amp;lt; endl;&lt;BR /&gt;    syslog( LOG_NOTICE, "terminated" );&lt;BR /&gt;    closelog();&lt;BR /&gt;    return 0;    &lt;BR /&gt;}&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;CLIENT&lt;BR /&gt;&lt;BR /&gt;its not getting the data :&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;int main( int argc, char *argv[] ) &lt;BR /&gt;{  &lt;BR /&gt;&lt;BR /&gt;  int shmid ;  /* identificateur de la memoire commune */&lt;BR /&gt;  int size = 1000 ;&lt;BR /&gt;  char *path="/home/pedro/SHARED_MEMORY/teste.txt" ;&lt;BR /&gt;&lt;BR /&gt;  int flag = 0 ;&lt;BR /&gt;  dataestrutura  *mem;&lt;BR /&gt;    &lt;BR /&gt;   /*&lt;BR /&gt;   * recuperacao do shmid &lt;BR /&gt;   */&lt;BR /&gt;   &lt;BR /&gt;   if (( shmid = shmget(ftok(path,(key_t)KEY), size,0)) == -1) &lt;BR /&gt;   {&lt;BR /&gt;          perror("Erro no shmget") ;&lt;BR /&gt;          exit(1) ;&lt;BR /&gt;   }&lt;BR /&gt;   &lt;BR /&gt;    printf("Sou o processo com pid: %d \n",getpid()) ;&lt;BR /&gt;    printf("Identificador do segmento recuperado: %d \n",shmid) ;&lt;BR /&gt;    printf("Este segmento e associado a chave unica: %d\n",ftok(path,(key_t)KEY)) ;&lt;BR /&gt;    /*&lt;BR /&gt;     * acoplamento do processo a zona de memoria&lt;BR /&gt;     * recuperacao do pornteiro sobre a area de memoria comum&lt;BR /&gt;     */&lt;BR /&gt;     if ((mem = (dataestrutura*)shmat (shmid, 0, flag)) == (dataestrutura*)-1){&lt;BR /&gt;          perror("acoplamento impossivel") ;&lt;BR /&gt;          exit (1) ;&lt;BR /&gt;     }&lt;BR /&gt;    /*&lt;BR /&gt;     * tratamento do conteudo do segmento&lt;BR /&gt;     */&lt;BR /&gt;    &lt;BR /&gt;     printf("leitura do segmento de memoria compartilhada:\n");&lt;BR /&gt;     printf("\t==&amp;gt;endereço:%d\n",&amp;amp;mem) ;&lt;BR /&gt;     printf("\t==&amp;gt;mem:%d\n",sizeof(mem)) ;     &lt;BR /&gt;     printf("\t==&amp;gt;mem:%s\n",mem) ; &lt;BR /&gt;       printf("\t==&amp;gt;mem:%s\n",mem-&amp;gt;usr) ; &lt;BR /&gt;      &lt;BR /&gt;           cout &amp;lt;&amp;lt;"/* detach from the segment: */" &amp;lt;&amp;lt; endl;&lt;BR /&gt;    if (shmdt(mem) == -1) &lt;BR /&gt;    {&lt;BR /&gt;      perror("shmdt");&lt;BR /&gt;      exit(1);&lt;BR /&gt;    }&lt;BR /&gt;  &lt;BR /&gt;   &lt;BR /&gt;}</description>
      <pubDate>Tue, 26 Jun 2007 10:01:18 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/ipc-shared-memory-with-a-struct/m-p/4027080#M93847</guid>
      <dc:creator>Pedro Dinis</dc:creator>
      <dc:date>2007-06-26T10:01:18Z</dc:date>
    </item>
    <item>
      <title>Re: IPC shared memory with a struct?</title>
      <link>https://community.hpe.com/t5/operating-system-linux/ipc-shared-memory-with-a-struct/m-p/4027081#M93848</link>
      <description>typedef struct dataestrutura {&lt;BR /&gt;char * usr;&lt;BR /&gt;char * passwd;&lt;BR /&gt;char * osid;&lt;BR /&gt;} dataestrutura&lt;BR /&gt;&lt;BR /&gt;The fundamental problem is that you have allocated space for the pointers but not for the data pointed to.&lt;BR /&gt;&lt;BR /&gt;If you change your struct to something like this:&lt;BR /&gt;&lt;BR /&gt;#define BIG_ENOUGH 256&lt;BR /&gt;&lt;BR /&gt;typedef struct dataestrutura {&lt;BR /&gt;char usr[BIG_ENOUGH];&lt;BR /&gt;char passwd[BIG_ENOUGH];&lt;BR /&gt;char osid[BIG_ENOUGH];&lt;BR /&gt;} dataestrutura&lt;BR /&gt;&lt;BR /&gt;then you will actually allocate storage space. &lt;BR /&gt;&lt;BR /&gt;</description>
      <pubDate>Tue, 26 Jun 2007 10:10:03 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/ipc-shared-memory-with-a-struct/m-p/4027081#M93848</guid>
      <dc:creator>A. Clay Stephenson</dc:creator>
      <dc:date>2007-06-26T10:10:03Z</dc:date>
    </item>
    <item>
      <title>Re: IPC shared memory with a struct?</title>
      <link>https://community.hpe.com/t5/operating-system-linux/ipc-shared-memory-with-a-struct/m-p/4027082#M93849</link>
      <description>now its working &lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;thanks&lt;BR /&gt;&lt;BR /&gt;Muchas Gracias</description>
      <pubDate>Tue, 26 Jun 2007 10:26:21 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/ipc-shared-memory-with-a-struct/m-p/4027082#M93849</guid>
      <dc:creator>Pedro Dinis</dc:creator>
      <dc:date>2007-06-26T10:26:21Z</dc:date>
    </item>
    <item>
      <title>Re: IPC shared memory with a struct?</title>
      <link>https://community.hpe.com/t5/operating-system-linux/ipc-shared-memory-with-a-struct/m-p/4027083#M93850</link>
      <description>&lt;P&gt;Basically what you are doing is taking addresses from one process and expect them to be valid in another.&lt;BR /&gt;&lt;BR /&gt;Instead of Clay's suggestion of arrays, you can take addresses of other parts of shared memory and then initialize your struct.&lt;BR /&gt;&lt;BR /&gt;shmid = shmget(key,SHM_SIZE+slop, 0644 | IPC_CREAT)&lt;BR /&gt;&lt;BR /&gt;data_ptr = (dataestrutura*)shmat(shmid,0, 0);&lt;BR /&gt;&lt;BR /&gt;// Copy strings into slop area.&lt;BR /&gt;&lt;BR /&gt;char *string_area = (char*)&amp;amp;dataestrutura[1];&lt;BR /&gt;char *string_ptr = string_area;&lt;BR /&gt;strcpy(string_ptr, "bdsn");&lt;BR /&gt;data_ptr-&amp;gt;usr = string_ptr;&lt;BR /&gt;string_ptr += strlen("bdsn")+1;&lt;BR /&gt;...&lt;BR /&gt;&lt;BR /&gt;And of course you need to make sure that you don't run off the end of the "slop" area.&lt;BR /&gt;&lt;BR /&gt;Since you are using C++, you can just use an STL allocator to allocate space in your shared memory. In fact, you can create a basic_string with that allocator, so when you construct a string, it just goes there.&lt;BR /&gt;&lt;BR /&gt;(Of course if you want to free space, you would have to know how to reclaim the space.)&lt;/P&gt;</description>
      <pubDate>Sun, 25 Sep 2011 22:49:21 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/ipc-shared-memory-with-a-struct/m-p/4027083#M93850</guid>
      <dc:creator>Dennis Handly</dc:creator>
      <dc:date>2011-09-25T22:49:21Z</dc:date>
    </item>
    <item>
      <title>Re: IPC shared memory with a struct?</title>
      <link>https://community.hpe.com/t5/operating-system-linux/ipc-shared-memory-with-a-struct/m-p/4027084#M93851</link>
      <description>Oops, that should have been:&lt;BR /&gt;char *string_area = (char*)&amp;amp;dataestrutura[1];&lt;BR /&gt;&lt;BR /&gt;(When you have gotten the answers in a thread, you should close them so people know you don't need more help.  You have several that aren't closed.)</description>
      <pubDate>Sat, 07 Jul 2007 03:01:46 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/ipc-shared-memory-with-a-struct/m-p/4027084#M93851</guid>
      <dc:creator>Dennis Handly</dc:creator>
      <dc:date>2007-07-07T03:01:46Z</dc:date>
    </item>
  </channel>
</rss>

