linux共享内存中实现 malloc等函数功能

来源:百度知道 编辑:UC知道 时间:2024/05/16 13:21:05
还要把内存释放和分配的情况写到一个log上,我不知道内存分配情况如何获得,或者是shmat中第二个参数可以设置,我不太清楚这个过程!谢谢各位啊!
就是我自己写一个链表来管理共享内存。而MyMalloc(功能与malloc一样,只不过是在共享内存里)就是实现分配共享内存空间.

#define BUFSZ 2048
int main()
{
int shmid;
char *shmadd;
if((shmid=shmget(IPC_PRIVATE,BUFSZ,0666))<0){
perror("shmget");
exit(1);
}
else
printf("created shared-memory: %d\n",shmid);
system("ipcs -m");
if((shmadd=shmat(shmid,0,0))<(char *)0){
perror("shmat");
exit(1);
}
else
printf("attached shared-memory\n");
system("ipcs -m");
if((shmdt(shmadd))<0){
perror("shmdt");
exit(1);
}
else
printf("deleted shared-memory\n");
system("ipcs -m");
exit(0);
}

以上是共享内存的一个例子程序,楼主说的在共享内存里面实现malloc 没大明白你的意思,可以查看nattch信息来看出来
能得分鼓励下嘛,呵呵

goodluck

你自己都实现了一个mymalloc了,你就在你自己实现的函数里导出log出来啊.例如你malloc后你就记录本次分配的:时间:大小:基地址....