#include <stdio.h>
#include "sharememory.h"



void* create_sharememory(key_t key,size_t size)
{
	int shmid;
	void* addr;
	if((shmid = shmget(key, size, IPC_CREAT|0777)) == -1)
	{
		perror("shmget error");
		return NULL;
	}
	
	if((addr = shmat(shmid,(void*)0,0)) == (void*)-1)
	{
		perror("shmat error");
		return NULL;
	}
	
	return addr;
}

void* open_sharememory(key_t key)
{
	int shmid;
	void* addr;
	if((shmid = shmget(key, 0, 0777)) == -1)
	{
		perror("shmget error");
		return NULL;
	}
	
	if((addr = shmat(shmid,(void*)0,0)) == (void*)-1)
	{
		perror("shmat error");
		return NULL;
	}
	
	return addr;
}

int delete_sharememory(key_t key)
{
	int shmid;
	if((shmid = shmget(key, 0, 0777)) == -1)
	{
		return -1;
	}
	
//	if(shmdt(addr) == -1)
//	{
//		perror("shmdt error");
//		return -1;
//	}

   if(shmctl(shmid, IPC_RMID, 0) == -1)
	{
		perror("shmctl error");
		return -1;
	}
	
	return 0;
}

