
#include <stdio.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <sys/sem.h>
#include <sys/shm.h>
#include <errno.h>
#include <unistd.h>
#include <time.h>


#include <netinet/in.h>

#include <arpa/inet.h>


#define TRUE			1	
#define FALSE			0		


typedef struct
{
    int     pid; 
    char  INFname[200];    

} PROC_STATE;

typedef int             BOOL;

static key_t    gSHM_CS_Key;        
static int      gSHM_CS_ID;        
PROC_STATE* parent;


int GetSharedMemory(key_t , char **, int , BOOL );
int DelSharedMemory(key_t );
int FreeSharedMemory(int );
void SHMError(void);

int main(int argc, char *argv[])
{

    gSHM_CS_Key = 3837247118;

	printf("Hello, world\n");
   
	if((gSHM_CS_ID = GetSharedMemory(gSHM_CS_Key, (char**)&parent, sizeof(PROC_STATE), TRUE))== -1)
    {
		printf("Shredmemory Already Exist\n");
	
	}

	
	return 0;
}


int GetSharedMemory(key_t key, char **data, int size, BOOL Flag)
{
    int shmid;

     /*If TRUE, Create Shared Memory*/
    if (Flag) /*Flag = TRUE*/
    {
        if ((shmid = shmget(key, size, 0666|IPC_CREAT|IPC_EXCL)) < 0)
        {
            if (errno == EEXIST) 
            {
			    return -1;
			     
               /* if ((shmid = shmget(key, size, 0666)) < 0)
                {
                    if (DelSharedMemory(key) == -1) return -1;

                    if ((shmid = shmget(key, size,
						0666|IPC_CREAT|IPC_EXCL)) < 0) return -1;
                }
				*/
            }
        }
    } 
    else  /*Flag = FALSE*/
    {
        if ((shmid = shmget(key, size, 0666)) < 0) 
		     return -1;
            /*return GetSharedMemory(key, data, size, TRUE);/*If there is no existing one, Create it again*/
    }

    if ((*data = (char *)shmat(shmid, 0, 0)) == (char *)-1)
    {
        shmctl(shmid, IPC_RMID, 0);
        close(shmid);
        return -1;
    }

    if (Flag)
        memset(*data, '\0', size);

    return shmid;
}

/*=============================================================================
int DelSharedMemory(
    key_t key  : Shared memory key
)
-------------------------------------------------------------------------------
Delete shared memory by using shared memory key.

Return Values
--------------
int   : success TRUE,fail : -1
Remarks
--------
==============================================================================*/

int DelSharedMemory(key_t key)
{
    int shmid;

    if ((shmid = shmget(key, 1, 0666)) < 0){
        /*SHMError();*/
		printf("DelSharedMemory Error");
	    return -1;
    }

    shmctl(shmid, IPC_RMID, 0);
    close(shmid);

    return TRUE;
}


/*=============================================================================
int FreeSharedMemory(
    int shmid  : shared memory ID
)
-------------------------------------------------------------------------------
Delete shared memory by using shared memory ID.

Return Values
--------------
int   : success TRUE,fail : -1 
Remarks
--------
==============================================================================*/

int FreeSharedMemory(int shmid)
{
    shmctl(shmid, IPC_RMID, 0);
    close(shmid);

	return TRUE;
}




void SHMError()
{

switch(errno)
{
case EACCES:
printf("A shared memory identifier exists for key but operation permission as specified by the low-order nine bits of shmflg would not be granted. See IPC.\n"); 
     break;
case EEXIST:
printf("A shared memory identifier exists for the argument key but (shmflg&IPC_CREAT)&&(shmflg&IPC_EXCL) is non-zero. \n"); 
     break;
case EINVAL:
printf("The value of size is less than the system-imposed minimum or greater than the system-imposed maximum, or a shared memory identifier exists for the argument key but the size of the segment associated with it is less than size and size is not 0. \n"); 
     break;
case ENOENT:
printf("A shared memory identifier does not exist for the argument key and (shmflg&IPC_CREAT) is 0. \n"); 
     break;
case ENOMEM:
printf("A shared memory identifier and associated shared memory segment are to be created but the amount of available physical memory is not sufficient to fill the request. \n"); 
     break;
case ENOSPC:
printf("A shared memory identifier is to be created but the system-imposed limit on the maximum number of allowed shared memory identifiers system-wide would be exceeded. \n"); 
     break;
default: 
printf("undefined shared memory error");

}

}