following is the part of my code. I am using shared memory between two programs. when one write other will see read after some delay and vice versa.
for reading i.e. displaying it works proper.........but after that it delays long for printing Enter message to BS1 and accepting value.
For the first time it skips accepting data from standard input and second time it works properly. (for this line printf("\nEnter message to BS1 :");)
I don't understand what is exact problem?
while(keep_going)
{
sleep(9);
//checking time for debug
curnt = time(NULL);
dtime = ctime(&curnt);
printf("\n Current time: %s", dtime);
printf("\nMessage received from MS2 : %s", mptr->msg);
printf("\nEnter message to BS1 :");
fgets(msg,STR_SIZE,stdin);
len = strlen(msg);
newstring = (char *)malloc(len-1);
for(i = 0; i < strlen(msg)-1; i++)
{
newstring[i] = msg[i];
}
strcpy(mptr->who,"MS1");
strcpy(mptr->whom,"BS1");
strcpy(mptr->msgType,"CONV
");
strncpy(mptr->msg, newstring, STR_SIZE);
if((strcmp(newstring,"bye"
)==0))
{
currentState1= 13;
strcpy(mptr -> msgType,"bye");
}
sleep(4);
//curnt = time(NULL);
//dtime = ctime(&curnt);
//printf("\n Current time: %s", dtime);
//printf("\nMessage received from BS1 : %s", mptr->msg);
}
for creating shared memory, I have used following code..........here I have given some part of my code
key = 1234;
if ((shmid = shmget(key, SHMSZ , IPC_CREAT | 0666)) < 0) {
perror("shmget");
exit(1);
}
if ((mptr = shmat(shmid,NULL, 0)) == (char *) -1) {
perror("shmat");
exit(1);
struct messageFormat *mptr;