<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: shm code</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Sun, 27 Jul 2008 04:42:16 GMT</pubDate>
    <description>DZone Snippets: shm code</description>
    <item>
      <title>Unix Shared Memory</title>
      <link>http://snippets.dzone.com/posts/show/2585</link>
      <description>If you want to use multiple blocks of shared memory in a single process&lt;br /&gt;you might need to pass a different id to ftok() for each one.  In any case,&lt;br /&gt;the main entry point is open_shm().  It returns a block of shared memory&lt;br /&gt;of the specified size.  If justreading is 1, then you access what has been&lt;br /&gt;written to that block of shared mem.  If it's 0, then you create a new one&lt;br /&gt;with undefined contents.&lt;br /&gt;&lt;br /&gt;PDIE is left as an exercise to the reader...&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;/* This code written by Nick Welch &lt;mack@incise.org&gt;, 2006.&lt;br /&gt; *&lt;br /&gt; * This code is in the public domain&lt;br /&gt; * and is provided AS IS, with NO WARRANTY. */&lt;br /&gt;&lt;br /&gt;static key_t get_shm_key(void)&lt;br /&gt;{&lt;br /&gt;    /* both arbitrary */&lt;br /&gt;    const char * KEY_PATH = "/dev/null";&lt;br /&gt;    const char KEY_ID = 0xc4; &lt;br /&gt;&lt;br /&gt;    key_t key = ftok(KEY_PATH, KEY_ID);&lt;br /&gt;    if (key == (key_t)-1)&lt;br /&gt;        PDIE("ftok");&lt;br /&gt;&lt;br /&gt;    return key;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;static int get_shm_min(void)&lt;br /&gt;{&lt;br /&gt;    struct shminfo info;&lt;br /&gt;    if((shmctl(0, IPC_INFO, (struct shmid_ds *)(void *)&amp;info)) == -1)&lt;br /&gt;        PDIE("shmctl (shminfo)");&lt;br /&gt;    return info.shmmin;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;static void * open_shm(size_t bytes, int justreading)&lt;br /&gt;{&lt;br /&gt;    void * shm;&lt;br /&gt;    key_t key = get_shm_key();&lt;br /&gt;&lt;br /&gt;    int id; &lt;br /&gt;&lt;br /&gt;    if(!justreading)&lt;br /&gt;    {&lt;br /&gt;        /* we need to delete it in case we need to allocate more memory this&lt;br /&gt;         * time than last time.  if we try that without first deleting it, we&lt;br /&gt;         * will get EINVAL.&lt;br /&gt;         */&lt;br /&gt;&lt;br /&gt;        id = shmget(key, get_shm_min(), 0644|IPC_CREAT);&lt;br /&gt;&lt;br /&gt;        if(id == -1)&lt;br /&gt;            PDIE("shmget (first call)");&lt;br /&gt;&lt;br /&gt;        shm = shmat(id, NULL, 0);&lt;br /&gt;        if (shm == NULL)&lt;br /&gt;            PDIE("shmat(first call)");&lt;br /&gt;&lt;br /&gt;        if(shmctl(id, IPC_RMID, NULL) == -1)&lt;br /&gt;            PDIE("shmctl (marking shm segment for removal)");&lt;br /&gt;&lt;br /&gt;        if(shmdt(shm) == -1)&lt;br /&gt;            PDIE("shmdt (detaching shm segment to delete it)");&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    id = shmget(key, bytes, justreading ? 0 : 0644|IPC_CREAT);&lt;br /&gt;    if(id == -1)&lt;br /&gt;        PDIE("shmget (second call)");&lt;br /&gt;&lt;br /&gt;    shm = shmat(id, NULL, 0);&lt;br /&gt;    if (shm == NULL)&lt;br /&gt;        PDIE("shmat (second call)");&lt;br /&gt;&lt;br /&gt;    return shm;&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Mon, 11 Sep 2006 12:40:12 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/2585</guid>
      <author>mackstann (Nick Welch)</author>
    </item>
  </channel>
</rss>
