X-Git-Url: https://gapil.gnulinux.it/gitweb/?a=blobdiff_plain;ds=sidebyside;f=listati%2FSharedMem.c;fp=listati%2FSharedMem.c;h=9f5b438525062302744cd2be227959896989741e;hb=06661f47754a536098afe2b30cb04469918f2fa3;hp=0000000000000000000000000000000000000000;hpb=bdf6e88eeb9b3aef06d57930ec8b89083639e56d;p=gapil.git diff --git a/listati/SharedMem.c b/listati/SharedMem.c new file mode 100644 index 0000000..9f5b438 --- /dev/null +++ b/listati/SharedMem.c @@ -0,0 +1,51 @@ +/* Function ShmCreate Create a SysV shared memory segment */ +void * ShmCreate(key_t ipc_key, int shm_size, int perm, int fill) +{ + void * shm_ptr; + int shm_id; /* ID of the IPC shared memory segment */ + shm_id = shmget(ipc_key, shm_size, IPC_CREAT|perm); /* get shm ID */ + if (shm_id < 0) { + return NULL; + } + shm_ptr = shmat(shm_id, NULL, 0); /* map it into memory */ + if (shm_ptr < 0) { + return NULL; + } + memset((void *)shm_ptr, fill, shm_size); /* fill segment */ + return shm_ptr; +} +/* Function ShmFind: Find a SysV shared memory segment */ +void * ShmFind(key_t ipc_key, int shm_size) +{ + void * shm_ptr; + int shm_id; /* ID of the SysV shared memory segment */ + shm_id = shmget(ipc_key, shm_size, 0); /* find shared memory ID */ + if (shm_id < 0) { + return NULL; + } + shm_ptr = shmat(shm_id, NULL, 0); /* map it into memory */ + if (shm_ptr < 0) { + return NULL; + } + return shm_ptr; +} +/* Function ShmRemove: Schedule removal for a SysV shared memory segment */ +int ShmRemove(key_t ipc_key, void * shm_ptr) +{ + int shm_id; /* ID of the SysV shared memory segment */ + /* first detach segment */ + if (shmdt(shm_ptr) < 0) { + return -1; + } + /* schedule segment removal */ + shm_id = shmget(ipc_key, 0, 0); /* find shared memory ID */ + if (shm_id < 0) { + if (errno == EIDRM) return 0; + return -1; + } + if (shmctl(shm_id, IPC_RMID, NULL) < 0) { /* ask for removal */ + if (errno == EIDRM) return 0; + return -1; + } + return 0; +}