#include    "unpthread.h"
#include    "unpipc.h"

#define TEST_SHM_NAME "test_shm_mem"
struct abc
{
	int a;
	int b;
	char c[16];
};
	
int main()
{
	struct abc *abc_ptr, *start_ptr;
	int fd, fd2, i;
	int numRows;

	numRows = 300;
	shm_unlink(TEST_SHM_NAME);
	fd = shm_open(TEST_SHM_NAME, O_RDWR | O_CREAT | O_EXCL | O_TRUNC, FILE_MODE);
	ftruncate(fd, (sizeof(struct abc)*numRows));

	abc_ptr = mmap(NULL, (sizeof(struct abc)*numRows), PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
	close(fd);

	start_ptr = abc_ptr;
	
	fprintf(stderr, "total memory size : %d\n", sizeof(struct abc)*numRows);
	sleep(5);
	for (i=0; i < numRows; i++)
	{
		start_ptr->a = 22;
		start_ptr->b = 33;
		strncpy(start_ptr->c, "I am a boy!!!", 16);

		start_ptr++;
	}
	start_ptr = NULL;

	fprintf(stderr, "#########################\n");
	start_ptr = abc_ptr;
	for (i=0; i < numRows; i++)
	{
		fprintf(stderr, "[%d] -- %d -- %d -- %s\n", i+1, start_ptr->a, start_ptr->b, start_ptr->c);
		start_ptr++;
	}

	//fd2 = shm_open(TEST_SHM_NAME, O_RDWR | O_CREAT | O_EXCL | O_TRUNC, FILE_MODE);
	//munmap((void *)abc_ptr, (sizeof(struct abc)*numRows));
	fd2 = shm_open(TEST_SHM_NAME, O_RDWR | O_CREAT | O_TRUNC, FILE_MODE);
	ftruncate(fd2, 0);
	sleep(10);

	//ftruncate(fd, (sizeof(struct abc)*numRows));
	numRows = 150;
	ftruncate(fd2, (sizeof(struct abc)*numRows));
	//abc_ptr = mmap(NULL, (sizeof(struct abc)*numRows), PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
	close(fd2);

	start_ptr = abc_ptr;
	
	for (i=0; i < numRows; i++)
	{
		start_ptr->a = 22;
		start_ptr->b = 33;
		strncpy(start_ptr->c, "I am a boy!!!", 16);
		start_ptr++;
	}
	start_ptr = NULL;

	start_ptr = abc_ptr;
	for (i=0; i < numRows; i++)
	{
		fprintf(stderr, "[%d] -- %d -- %d -- %s\n", i+1, start_ptr->a, start_ptr->b, start_ptr->c);
		start_ptr++;
	}

	sleep(10);
	return 1;
}
