@@ -259,19 +259,34 @@ int SHM_create(PTR_VOID* new_shm_base, const char* db_name, int db_name_length,
259259return AGMDB_ERROR_SHM_ENTRY_NUM_NEGATIVE;
260260
261261 shm_id =shmget (shm_key, shm_size, IPC_CREAT | IPC_EXCL);
262-
263- if (shm_id == -1 ){// create a new SHM
264- shm_id =shmget (shm_key, shm_size, IPC_CREAT);
265- if (shm_id == -1 )
266- return AGMDB_ERROR_SHM_LINUX_CREATE_FAIL;
262+ if (shm_id == -1 ) {
263+ if (errno == EEXIST) {// There has been a SHM with the given name
264+ shm_id =shmget (shm_key, shm_size, IPC_CREAT);
265+ if (shm_id == -1 )
266+ return AGMDB_ERROR_SHM_LINUX_CREATE_FAIL;
267+
268+ // Map the SHM into the address space of this process
269+ *new_shm_base = (PTR_VOID)shmat (shm_id,NULL ,0 );
270+ if (*new_shm_base == (PTR_VOID)-1 )
271+ return AGMDB_ERROR_SHM_LINUX_MAP_FAIL;
272+ return AGMDB_SUCCESS_SHM_OPEN;
273+ }
274+ else if (errno == EACCES) {
275+ return AGMDB_ERROR_SHM_LINUX_OPEN_ACCESS_FAIL;
276+ }
277+ else {
278+ return AGMDB_ERROR_SHM_LINUX_OPEN_FAIL;
279+ }
280+ }
281+ else {// Map the new SHM into the address space of this process
282+ *new_shm_base = (PTR_VOID)shmat (shm_id,NULL ,0 );
283+ if (*new_shm_base == (PTR_VOID)-1 )
284+ return AGMDB_ERROR_SHM_LINUX_MAP_FAIL;
285+ rc =SHM_init (*new_shm_base, shm_size, entry_num);
286+ if (rc != AGMDB_SUCCESS)
287+ return rc;
288+ return AGMDB_SUCCESS_SHM_CREATE;
267289 }
268- *new_shm_base = (PTR_VOID)shmat (shm_id,NULL ,0 );
269- if (*new_shm_base == (PTR_VOID)-1 )
270- return AGMDB_ERROR_SHM_LINUX_MAP_FAIL;
271- rc =SHM_init (*new_shm_base, shm_size, entry_num);
272- if (rc != AGMDB_SUCCESS)
273- return rc;
274- return AGMDB_SUCCESS_SHM_OPEN;
275290#else
276291 HANDLE shm_handle;
277292if (AGMDB_isstring (db_name, db_name_length) != AGMDB_SUCCESS)
@@ -300,7 +315,7 @@ int SHM_create(PTR_VOID* new_shm_base, const char* db_name, int db_name_length,
300315return rc;
301316return AGMDB_SUCCESS_SHM_CREATE;
302317 }
303- else {// There has been a SHM with the given name
318+ else {// There has been a SHM with the given name
304319return AGMDB_SUCCESS_SHM_OPEN;
305320 }
306321#endif
@@ -1126,6 +1141,10 @@ const char* AGMDB_getErrorInfo(int error_no){
11261141
11271142case AGMDB_ERROR_SHM_LINUX_CREATE_FAIL:
11281143return " In Linux system, failed when creating the shared memory." ;
1144+ case AGMDB_ERROR_SHM_LINUX_OPEN_ACCESS_FAIL:
1145+ return " In Linux system, no access permission when opening the shared memory." ;
1146+ case AGMDB_ERROR_SHM_LINUX_OPEN_FAIL:
1147+ return " In Linux system, failed when opening the shared memory." ;
11291148case AGMDB_ERROR_SHM_LINUX_MAP_FAIL:
11301149return " In Linux system, failed when mapping the shared memory into the process's address space." ;
11311150