Sophie

Sophie

distrib > Mandriva > 2008.1 > i586 > by-pkgid > 369a24fb91079440c048ad598fc25e73 > files > 165

lpg-0.4-16mdv2008.1.noarch.rpm

<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<!--Converted with LaTeX2HTML 96.1-c (Feb 29, 1996) by Nikos Drakos (nikos@cbl.leeds.ac.uk), CBLU, University of Leeds -->
<HTML>
<HEAD>
<TITLE>The Source</TITLE>
<META NAME="description" CONTENT="The Source">
<META NAME="keywords" CONTENT="lpg">
<META NAME="resource-type" CONTENT="document">
<META NAME="distribution" CONTENT="global">
<LINK REL=STYLESHEET HREF="lpg.css">
</HEAD>
<BODY LANG="EN">
 <A NAME="tex2html1370" HREF="node82.html"><IMG WIDTH=37 HEIGHT=24 ALIGN=BOTTOM ALT="next" SRC="/icons//next_motif.gif"></A> <A NAME="tex2html1368" HREF="node73.html"><IMG WIDTH=26 HEIGHT=24 ALIGN=BOTTOM ALT="up" SRC="/icons//up_motif.gif"></A> <A NAME="tex2html1364" HREF="node80.html"><IMG WIDTH=63 HEIGHT=24 ALIGN=BOTTOM ALT="previous" SRC="/icons//previous_motif.gif"></A> <A NAME="tex2html1372" HREF="node1.html"><IMG WIDTH=65 HEIGHT=24 ALIGN=BOTTOM ALT="contents" SRC="/icons//contents_motif.gif"></A>  <BR>
<B> Next:</B> <A NAME="tex2html1371" HREF="node82.html">7 Sound Programming</A>
<B>Up:</B> <A NAME="tex2html1369" HREF="node73.html">shmtool: An interactive shared </A>
<B> Previous:</B> <A NAME="tex2html1365" HREF="node80.html">Examples</A>
<BR> <P>
<H4><A NAME="SECTION00744740000000000000">The Source</A></H4>
<P>
<P>
<HR><PRE>#include &lt;stdio.h&gt;
#include &lt;sys/types.h&gt;
#include &lt;sys/ipc.h&gt;
#include &lt;sys/shm.h&gt;

#define SEGSIZE 100

main(int argc, char *argv[])
{
        key_t key;
        int   shmid, cntr;
        char  *segptr;

        if(argc == 1)
                usage();

        /* Create unique key via call to ftok() */
        key = ftok(&quot;.&quot;, 'S');

        /* Open the shared memory segment - create if necessary */
        if((shmid = shmget(key, SEGSIZE, IPC_CREAT|IPC_EXCL|0666)) == -1) 
        {
                printf(&quot;Shared memory segment exists - opening as client\n&quot;);

                /* Segment probably already exists - try as a client */
                if((shmid = shmget(key, SEGSIZE, 0)) == -1) 
                {
                        perror(&quot;shmget&quot;);
                        exit(1);
                }
        }
        else
        {
                printf(&quot;Creating new shared memory segment\n&quot;);
        }

        /* Attach (map) the shared memory segment into the current process */
        if((segptr = shmat(shmid, 0, 0)) == -1)
        {
                perror(&quot;shmat&quot;);
                exit(1);
        }
        
        switch(tolower(argv[1][0]))
        {
                case 'w': writeshm(shmid, segptr, argv[2]);
                          break;
                case 'r': readshm(shmid, segptr);
                          break;
                case 'd': removeshm(shmid);
                          break;
                case 'm': changemode(shmid, argv[2]);
                          break;
                 default: usage();

        }
}

writeshm(int shmid, char *segptr, char *text)
{
        strcpy(segptr, text);
        printf(&quot;Done...\n&quot;);
}

readshm(int shmid, char *segptr)
{
        printf(&quot;segptr: %s\n&quot;, segptr);
}

removeshm(int shmid)
{
        shmctl(shmid, IPC_RMID, 0);
        printf(&quot;Shared memory segment marked for deletion\n&quot;);
}

changemode(int shmid, char *mode) 
{
        struct shmid_ds myshmds;

        /* Get current values for internal data structure */
        shmctl(shmid, IPC_STAT, &amp;myshmds);

        /* Display old permissions */
        printf(&quot;Old permissions were: %o\n&quot;, myshmds.shm_perm.mode);

        /* Convert and load the mode */ 
        sscanf(mode, &quot;%o&quot;, &amp;myshmds.shm_perm.mode);

        /* Update the mode */
        shmctl(shmid, IPC_SET, &amp;myshmds);

        printf(&quot;New permissions are : %o\n&quot;, myshmds.shm_perm.mode);
}

usage()
{
        fprintf(stderr, &quot;shmtool - A utility for tinkering with shared memory\n&quot;);
        fprintf(stderr, &quot;\nUSAGE:  shmtool (w)rite &lt;text&gt;\n&quot;);
        fprintf(stderr, &quot;                (r)ead\n&quot;);
        fprintf(stderr, &quot;                (d)elete\n&quot;);
        fprintf(stderr, &quot;                (m)ode change &lt;octal mode&gt;\n&quot;);
        exit(1);
}</PRE> 
<HR>
<P>
<BR> <HR>
<P><ADDRESS>
<I>Converted on: <BR>
Fri Mar 29 14:43:04 EST 1996</I>
</ADDRESS>
</BODY>
</HTML>