shm_map.c

00001 #include <stdio.h>
00002 #include <sys/shm.h>
00003 #include <sys/stat.h>
00004 #include "map_definitions.h"
00005 
00006 int main ()
00007 {
00008         int segment_id;
00009         char* shared_memory;
00010         const int shared_segment_size = sizeof(char) * MAP_WIDTH * MAP_HEIGHT;
00011         int i, j;
00012 
00013         /* Allocate a shared memory segment.  */
00014         segment_id = shmget (SHM_MAP_KEY , shared_segment_size,
00015                              IPC_CREAT | IPC_EXCL | S_IRUSR | S_IWUSR);
00016 
00017         /* Attach the shared memory segment.  */
00018         shared_memory = (char*) shmat (segment_id, 0, 0);
00019 
00020         /* Initialize Map Memory */
00021         for (j=0;j<MAP_HEIGHT;j++){
00022                 for(i=0;i<MAP_WIDTH;i++){
00023                         *(shared_memory+i+j*MAP_WIDTH)=' ';
00024                 }
00025         }
00026         printf("Map memory initialized\n");
00027         
00028         /* Main boucle */
00029         do {
00030                 printf("Enter a modification on the map (-1 to exit)\n");
00031                 printf("x="); scanf("%d",&i);
00032                 printf("y="); scanf("%d",&j);
00033                 if ((i<MAP_WIDTH) && (j<MAP_HEIGHT) && (i>=0) && (j>=0)) *(shared_memory+i+j*MAP_WIDTH)='#';
00034         } while((i!=-1) && (j!= -1));
00035         
00036         /* Deatch the shared memory segment.  */
00037         shmdt (shared_memory);
00038 
00039         /* Deallocate the shared memory segment.  */
00040         shmctl (segment_id, IPC_RMID, 0);
00041 
00042         return 0;
00043 }

Generated on Thu Sep 13 11:28:28 2007 for DCE-Eurobot by  doxygen 1.5.3