test.c

First example shows just basic functionality of bare sharedmem_t object. Second one shows basic functionality of sharedmem_alloc_t allocator object. Third example shows really simple use case.

00001 /* $Id: test.c,v 1.1 2004/12/22 14:10:29 jlaako Exp $ */
00002 
00003 /*
00004 
00005     Shared memory object for POSIX/IEEE-1003.1 compliant systems.
00006 
00007     Copyright (C) 2005 Nokia Corporation.
00008 
00009     Contact: Jussi Laako <jussi.laako@nokia.com>
00010 
00011     This library is free software; you can redistribute it and/or
00012     modify it under the terms of the GNU Lesser General Public
00013     License as published by the Free Software Foundation; either
00014     version 2.1 of the License, or (at your option) any later version.
00015 
00016     This library is distributed in the hope that it will be useful,
00017     but WITHOUT ANY WARRANTY; without even the implied warranty of
00018     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00019     Lesser General Public License for more details.
00020 
00021     You should have received a copy of the GNU Lesser General Public
00022     License along with this library; if not, write to the Free Software
00023     Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00024 
00025 */
00026 
00027 
00028 #include <stdio.h>
00029 #include <unistd.h>
00030 #include <sys/wait.h>
00031 #include <pthread.h>
00032 
00033 #include "sharedmem.h"
00034 
00035 
00036 typedef struct _data_t
00037 {
00038     int i;
00039     int r;
00040 } data_t;
00041 
00042 
00043 void parent ()
00044 {
00045     int cntr = 0;
00046     sharedmem_t shm;
00047     data_t *shmdata;
00048 
00049 puts("parent entry");    
00050     if (sharedmem_create(&shm, "/sharedmem.test", sizeof(data_t)))
00051     {
00052         fprintf(stderr, "%s\n", sharedmem_get_error_string(&shm));
00053         return;
00054     }
00055     shmdata = (data_t *) SHAREDMEM_PTR(&shm);
00056     shmdata->r = 1;
00057     while (cntr < 15)
00058     {
00059 puts("parent lock");
00060         pthread_mutex_lock(SHAREDMEM_MUTEX(&shm));
00061         shmdata->i = cntr;
00062 puts("parent signal");
00063         pthread_cond_signal(SHAREDMEM_COND(&shm));
00064 puts("parent unlock");
00065         pthread_mutex_unlock(SHAREDMEM_MUTEX(&shm));
00066         cntr++;
00067         sleep(1);
00068     }
00069 puts("parent final lock");
00070     pthread_mutex_lock(SHAREDMEM_MUTEX(&shm));
00071     shmdata->r = 0;
00072 puts("parent final broadcast");
00073     pthread_cond_broadcast(SHAREDMEM_COND(&shm));
00074 puts("parent final unlock");
00075     pthread_mutex_unlock(SHAREDMEM_MUTEX(&shm));
00076     if (sharedmem_close(&shm))
00077     {
00078         fprintf(stderr, "%s\n", sharedmem_get_error_string(&shm));
00079         return;
00080     }
00081 }
00082 
00083 
00084 void child ()
00085 {
00086     int r = 1;
00087     int l;
00088     sharedmem_t shm;
00089     data_t *shmdata;
00090     
00091 puts("child entry");
00092     if (sharedmem_open(&shm, "/sharedmem.test"))
00093     {
00094         fprintf(stderr, "%s\n", sharedmem_get_error_string(&shm));
00095         return;
00096     }
00097     shmdata = (data_t *) SHAREDMEM_PTR(&shm);
00098     while (r)
00099     {
00100 puts("child lock");
00101         pthread_mutex_lock(SHAREDMEM_MUTEX(&shm));
00102 puts("child wait");
00103         pthread_cond_wait(SHAREDMEM_COND(&shm), SHAREDMEM_MUTEX(&shm));
00104         l = shmdata->i;
00105         r = shmdata->r;
00106 puts("child unlock");
00107         pthread_mutex_unlock(SHAREDMEM_MUTEX(&shm));
00108         fprintf(stdout, "child: %d\n", l);
00109     }
00110     if (sharedmem_close(&shm))
00111     {
00112         fprintf(stderr, "%s\n", sharedmem_get_error_string(&shm));
00113         return;
00114     }
00115 }
00116 
00117 
00118 int main (int argc, char *argv[])
00119 {
00120     pid_t pid_child;
00121     
00122     pid_child = fork();
00123     if (pid_child != 0)
00124     {
00125         parent();
00126         waitpid(pid_child, NULL, 0);
00127     }
00128     else
00129     {
00130         sleep(1);
00131         child();
00132     }
00133 
00134     return 0;
00135 }

Generated on Thu Sep 13 18:14:21 2007 for libsharedmem by  doxygen 1.5.1