#include  <stdio.h>
#include  <stdlib.h>
#include  <sys/types.h>
#include  <sys/ipc.h>
#include  <sys/shm.h>

struct SharedMemory
{
    int  data;
};

void main(int  argc, char *argv[])
{
    key_t                 SharedMemoryUniqueKey;
    int                   SharedMemoryID;
    struct SharedMemory  *PointerToSharedMemoryStructure;

// Create a key using filepath and reference identifier - sharing program must do the same
    SharedMemoryUniqueKey = ftok(".", 255);
	printf("Key=%d\n",SharedMemoryUniqueKey);

// Get the ID of the shared memory and set its protection to -rw-rw-rw-
    SharedMemoryID = shmget(SharedMemoryUniqueKey, sizeof(struct SharedMemory), IPC_CREAT | 0666);
    if (0>SharedMemoryID)
	{
        printf("Error: not able to create 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;
    }
	
// Set the shared data
    PointerToSharedMemoryStructure->data=0;

// Just switch the shared value between 0 and 1 every 5 seconds
    while(1)
    {   PointerToSharedMemoryStructure->data=1-PointerToSharedMemoryStructure->data;
        printf("%d\n",PointerToSharedMemoryStructure->data);
        sleep(5);
    }

}