
#include  <stdio.h>
#include  <stdlib.h>
#include  <sys/types.h>
#include  <sys/ipc.h>
#include  <sys/shm.h>

struct SharedMemory
{
    int data;
};

void main(void)
{
    key_t                 SharedMemoryUniqueKey;
    int                   SharedMemoryID;
    struct SharedMemory  *PointerToSharedMemoryStructure;

    SharedMemoryUniqueKey = ftok(".", 255);
    printf("Key=%d\n",SharedMemoryUniqueKey);

    SharedMemoryID = shmget(SharedMemoryUniqueKey, sizeof(struct SharedMemory), 0666);
    if (0>SharedMemoryID)
	{
        printf("Error: not able to find shared memory\n");
        return;
    }

     PointerToSharedMemoryStructure = (struct SharedMemory *) shmat(SharedMemoryID, NULL, 0);
     if (-1==(int)PointerToSharedMemoryStructure)
	 {
        printf("Error: not able to attach shared memory\n");
         return;
     }

// Write out the shared data every second
     while(1)
	 {   printf("%d\n",PointerToSharedMemoryStructure->data);
		 sleep(1);
     }
 
}