yairy
asked on
Sheard memory functions in unix
Does anybody have some simple source code using
the sheard memory functions (shmem) in Unix OS up to the point
we allocate memory for a structre ?
Yair
the sheard memory functions (shmem) in Unix OS up to the point
we allocate memory for a structre ?
Yair
Btw. I know exactly how it goes, but it is not direclty available in my head, but i know the concepts.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I thought that after you attach
the segment to the process, you can
use simple new / malloc functions.
Am I wrong ?
the segment to the process, you can
use simple new / malloc functions.
Am I wrong ?
Hi yeiry,
The purpose of shared memory is to allocate you a chunk from the global
memory pool.Nobody actually owns this chunk.But it will get mapped in the address
space of each process opening a reference to it (shmget).Now then on
you can use it to communicate with other processes.As it not a
process specific data area (as heap ) so you can not do free/malloc on it
which are C RTL functions defined to allocate from the process heap area.
The purpose of shared memory is to allocate you a chunk from the global
memory pool.Nobody actually owns this chunk.But it will get mapped in the address
space of each process opening a reference to it (shmget).Now then on
you can use it to communicate with other processes.As it not a
process specific data area (as heap ) so you can not do free/malloc on it
which are C RTL functions defined to allocate from the process heap area.
Ufolk123, YOU SUCK!! You answer what i commented. stealing points......
yairy, please reject his answer. here is the promised source. this source is used during my last project. i wont compile, but i will give you a very nice overview of shared memory in Unix.
Alex.
..h:
#ifndef MEMORY_H
typedef struct
{
void *pMemory;
int iMemory;
} tMemory;
/* prototypes */
extern tMemory mMEM_Create(
int iKey,
int iSize,
int iAccess = 0660,
bool bExclusive = true,
void *pAttach = NULL,
int iAttach = 0);
extern tMemory mMEM_Open(
int iKey,
int iAccess = 0660,
void *pAttach = NULL,
int iAttach = 0);
extern void *pMEM_GetAddress(
tMemory mMemory);
extern void MEM_Resize(
tMemory mMemory,
int iSize);
extern void MEM_Close(
tMemory mMemory);
extern void MEM_Delete(
tMemory mMemory);
extern void MEM_DeleteExist(
int iKey);
#define MEMORY_H
#endif
/* eof */
..cpp:
#include "bool.h"
#include <sys/shm.h>
#include <errno.h>
#include "log.h"
#include "trace.h"
#include "error.h"
#include "memory.h"
/* implementation */
tMemory mMEM_Create(
int iKey,
int iSize,
int iAccess,
bool bExclusive,
void *pAttach,
int iAttach)
{
int iFlag;
int iResult;
void *pAddress;
tMemory mResult;
mResult.pMemory = NULL;
mResult.iMemory = -1;
if (iSize <= 0)
{
THROW_STR("MEM_Create - Invalid size");
}
if (iAccess > 0777)
{
THROW_STR("MEM_Create - Invalid access flag");
}
iFlag = IPC_CREAT | iAccess;
if (bExclusive)
{
iFlag |= IPC_EXCL;
}
iResult = shmget(iKey,iSize,iFlag);
if (iResult == -1)
{
THROW_SYSTEM("memory","MEM _Create - shmget");
}
mResult.iMemory = iResult;
pAddress = shmat(iResult,pAttach,iAtt ach);
if (pAddress == NULL)
{
(void)shmctl(iResult,IPC_R MID,NULL);
mResult.iMemory = -1;
THROW_STR("MEM_Create - shmat");
}
mResult.pMemory = pAddress;
return mResult;
}
tMemory mMEM_Open(
int iKey,
int iAccess,
void *pAttach,
int iAttach)
{
int iFlag;
int iResult;
void *pAddress;
tMemory mResult;
mResult.pMemory = NULL;
mResult.iMemory = -1;
if (iAccess > 0777)
{
THROW_STR("MEM_Open - Invalid access flag");
}
iFlag = iAccess;
iResult = shmget(iKey,0,iFlag);
if (iResult == -1)
{
THROW_STR("MEM_Open - shmget");
}
mResult.iMemory = iResult;
pAddress = shmat(iResult,pAttach,iAtt ach);
if (pAddress == NULL)
{
(void)shmctl(iResult,IPC_R MID,NULL);
mResult.iMemory = -1;
THROW_STR("MEM_Open - shmat");
}
mResult.pMemory = pAddress;
return mResult;
}
void *pMEM_GetAddress(
tMemory mMemory)
{
if (mMemory.pMemory == NULL)
{
THROW_ASSERT(0,"memory not initialized");
}
return mMemory.pMemory;
}
void MEM_Resize(
tMemory mMemory,
int iSize)
{
struct shmid_ds sdResize;
int iResult;
if (iSize <= 0)
{
THROW_STR("MEM_Create - Invalid size");
}
sdResize.shm_segsz = iSize;
iResult = shmctl(mMemory.iMemory,SHM _SIZE,&sdR esize);
if (iResult != 0)
{
THROW_SYSTEM("memory","res ize");
}
return;
}
void MEM_Close(
tMemory mMemory)
{
int iResult;
if (mMemory.pMemory != NULL)
{
iResult = shmdt(mMemory.pMemory);
}
mMemory.pMemory = NULL;
mMemory.iMemory = -1;
return;
}
void MEM_Delete(
tMemory mMemory)
{
int iResult;
struct shmid_ds dummy;
if (mMemory.pMemory != NULL)
{
iResult = shmdt(mMemory.pMemory);
}
mMemory.pMemory = NULL;
if (mMemory.iMemory != -1)
{
iResult = shmctl(mMemory.iMemory,IPC _RMID,&dum my);
if (iResult != 0)
{
THROW_SYSTEM("memory","shm ctl");
}
mMemory.iMemory = -1;
}
return;
}
void MEM_DeleteExist(
int iKey)
{
tMemory mMemory;
try
{
mMemory = mMEM_Open(iKey);
}
catch(...)
{
return;
}
MEM_Delete(mMemory);
return;
}
/* eof */
yairy, please reject his answer. here is the promised source. this source is used during my last project. i wont compile, but i will give you a very nice overview of shared memory in Unix.
Alex.
..h:
#ifndef MEMORY_H
typedef struct
{
void *pMemory;
int iMemory;
} tMemory;
/* prototypes */
extern tMemory mMEM_Create(
int iKey,
int iSize,
int iAccess = 0660,
bool bExclusive = true,
void *pAttach = NULL,
int iAttach = 0);
extern tMemory mMEM_Open(
int iKey,
int iAccess = 0660,
void *pAttach = NULL,
int iAttach = 0);
extern void *pMEM_GetAddress(
tMemory mMemory);
extern void MEM_Resize(
tMemory mMemory,
int iSize);
extern void MEM_Close(
tMemory mMemory);
extern void MEM_Delete(
tMemory mMemory);
extern void MEM_DeleteExist(
int iKey);
#define MEMORY_H
#endif
/* eof */
..cpp:
#include "bool.h"
#include <sys/shm.h>
#include <errno.h>
#include "log.h"
#include "trace.h"
#include "error.h"
#include "memory.h"
/* implementation */
tMemory mMEM_Create(
int iKey,
int iSize,
int iAccess,
bool bExclusive,
void *pAttach,
int iAttach)
{
int iFlag;
int iResult;
void *pAddress;
tMemory mResult;
mResult.pMemory = NULL;
mResult.iMemory = -1;
if (iSize <= 0)
{
THROW_STR("MEM_Create - Invalid size");
}
if (iAccess > 0777)
{
THROW_STR("MEM_Create - Invalid access flag");
}
iFlag = IPC_CREAT | iAccess;
if (bExclusive)
{
iFlag |= IPC_EXCL;
}
iResult = shmget(iKey,iSize,iFlag);
if (iResult == -1)
{
THROW_SYSTEM("memory","MEM
}
mResult.iMemory = iResult;
pAddress = shmat(iResult,pAttach,iAtt
if (pAddress == NULL)
{
(void)shmctl(iResult,IPC_R
mResult.iMemory = -1;
THROW_STR("MEM_Create - shmat");
}
mResult.pMemory = pAddress;
return mResult;
}
tMemory mMEM_Open(
int iKey,
int iAccess,
void *pAttach,
int iAttach)
{
int iFlag;
int iResult;
void *pAddress;
tMemory mResult;
mResult.pMemory = NULL;
mResult.iMemory = -1;
if (iAccess > 0777)
{
THROW_STR("MEM_Open - Invalid access flag");
}
iFlag = iAccess;
iResult = shmget(iKey,0,iFlag);
if (iResult == -1)
{
THROW_STR("MEM_Open - shmget");
}
mResult.iMemory = iResult;
pAddress = shmat(iResult,pAttach,iAtt
if (pAddress == NULL)
{
(void)shmctl(iResult,IPC_R
mResult.iMemory = -1;
THROW_STR("MEM_Open - shmat");
}
mResult.pMemory = pAddress;
return mResult;
}
void *pMEM_GetAddress(
tMemory mMemory)
{
if (mMemory.pMemory == NULL)
{
THROW_ASSERT(0,"memory not initialized");
}
return mMemory.pMemory;
}
void MEM_Resize(
tMemory mMemory,
int iSize)
{
struct shmid_ds sdResize;
int iResult;
if (iSize <= 0)
{
THROW_STR("MEM_Create - Invalid size");
}
sdResize.shm_segsz = iSize;
iResult = shmctl(mMemory.iMemory,SHM
if (iResult != 0)
{
THROW_SYSTEM("memory","res
}
return;
}
void MEM_Close(
tMemory mMemory)
{
int iResult;
if (mMemory.pMemory != NULL)
{
iResult = shmdt(mMemory.pMemory);
}
mMemory.pMemory = NULL;
mMemory.iMemory = -1;
return;
}
void MEM_Delete(
tMemory mMemory)
{
int iResult;
struct shmid_ds dummy;
if (mMemory.pMemory != NULL)
{
iResult = shmdt(mMemory.pMemory);
}
mMemory.pMemory = NULL;
if (mMemory.iMemory != -1)
{
iResult = shmctl(mMemory.iMemory,IPC
if (iResult != 0)
{
THROW_SYSTEM("memory","shm
}
mMemory.iMemory = -1;
}
return;
}
void MEM_DeleteExist(
int iKey)
{
tMemory mMemory;
try
{
mMemory = mMEM_Open(iKey);
}
catch(...)
{
return;
}
MEM_Delete(mMemory);
return;
}
/* eof */
#include <windows.h>
what a **** example!!!!
your example sucks
windows on Unix, ha ha ha ha, how stupid....
what a **** example!!!!
your example sucks
windows on Unix, ha ha ha ha, how stupid....
aperdon,
OK I was my mistake to put< windows.h >in that code but otherwise the code is OK.
You should not complain as yriary had asked for a source code example illustrating use of storing structures.
Anyway you should not be so rude for this.
If will myself withdraw my answer if yairy agrees on it.He can reject it if he wants.
You do not have any right to comment on my answer.Please keep your emotions with yourself.
OK I was my mistake to put< windows.h >in that code but otherwise the code is OK.
You should not complain as yriary had asked for a source code example illustrating use of storing structures.
Anyway you should not be so rude for this.
If will myself withdraw my answer if yairy agrees on it.He can reject it if he wants.
You do not have any right to comment on my answer.Please keep your emotions with yourself.
It has nothing to do with emotions. But I donot like to spent half an hour to find some missing source, and someone else get the points. You should commented it instead of answering it.
But how you can be sure the code is OK? Did you compile and test it. It cant compile.
Mine code is nice. It shows you which steps to take in order to perform some shared memory functions. Remove the includes and rewrite THROW... into throw and it will compile.
The server will do: MEM_Create, GetAddress and read the info at that address.
The client will do: MEM_Open, GetAddress and store the info at that address.
Btw. be sure of synchronisation....
But how you can be sure the code is OK? Did you compile and test it. It cant compile.
Mine code is nice. It shows you which steps to take in order to perform some shared memory functions. Remove the includes and rewrite THROW... into throw and it will compile.
The server will do: MEM_Create, GetAddress and read the info at that address.
The client will do: MEM_Open, GetAddress and store the info at that address.
Btw. be sure of synchronisation....
ASKER
Ufolk123 gets the points.
I don't think the nature of this pannel is to fight on points.
I myself an Experts and I never seen such a behiaver.
Yair
I don't think the nature of this pannel is to fight on points.
I myself an Experts and I never seen such a behiaver.
Yair
You shouldn't accepted his answer then also. For me I donot mind the points. But I dont like one gives answers while knowing someone else already is busy with it. In mine opinion ufolk shouldnot have been rewared for such bad attitude.
The following functions are available
shmget - create/open segment
shmat - attach segment to process space
shmdt - detach segment
shmctl - control segment (delete)
I dont know exactly how it works, but it goes something like this.
id = shmget(code,O_CREAT | 0x666,size)
address = shmat(id)
the retreived address can be used to store your date. allocation is done through the shmget-call.
if meanwhile you dont accept any answer i'm willing to send you an example. i dont have the source here at this place, but can have it tomorrow if you want. this source is a kind of abstraction above the shared-memory functions. it will help you a lot i promise!!
Alexander.