In the client you have this:
shmstruct->flag=1;
printf("Enter meeeage:");
fgets( shmstruct->buffer,sizeof(shm_buf),stdin);
Which notifies the server before the data has been read into shmstruct->buffer
.
You should probably set shmstruct->flag
to 1 only when it has read the data into shmstruct->buffer
:
printf("Enter meeeage:");
fgets( shmstruct->buffer,sizeof(shm_buf),stdin);
shmstruct->flag=1;
In production code you may like to use process shared mutex and condition variable to protect your shared state from corruption and notify the waiting process that the state has changed.