#include <sys/ipc.h>
#include <sys/shm.h>
#define BUF 1024
#define MEM_FLG 2012
int main()
{
int shm_id;
int ret;
void *mem = NULL;
//共享内存创建
shm_id = shmget(MEM_FLG, BUF, 0666|IPC_CREAT);
if (shm_id < 0)
{
printf("shmget error.\n");
return 1;
}
//共享内存连接
mem = shmat(shm_id, (const void *)0, 0);if (mem != NULL)
{
(void)strcpy((char*)mem,"111111111111111\n");
}
printf("mem is %s\n",(char*)mem);
//断开连接
ret=shmdt( mem );return 0;
}
#include <stdio.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#define MEM_FLG 2012
int main()
{
int shm_id;
int ret;
void *mem = NULL;
shm_id = shmget(MEM_FLG, 0, 0);
if (shm_id < 0)
{
printf("shmget error.\n");
return 1;
}
mem = shmat(shm_id, (const void *)0, 0);
printf( "mem is %s\n",(char*)mem );
if (mem != NULL)
{
(void)strcpy((char*)mem, "22222222222222222\n");
}
printf("mem is %s\n",(char*)mem);
ret=shmdt(mem);
//删除共享内存
ret=shmctl(shm_id, IPC_RMID, NULL);
return 0;
}