There is other program which write and read in same memory so I have added two sleep to match there both of them reading and writing.
also I have used
for(i = 0; i < strlen(msg)-1; i++)
{
newstring[i] = msg[i];
}
because when we take input by fgets, it also take '\n' so need to remove that to compare the 'bye' input.
Here is my ouptput
Communicating with BS1....!!
Current time: Mon Nov 2 08:41:33 2009
Message received from MS2 : 1@@
Enter message to BS1 :
Current time: Mon Nov 2 08:41:44 2009
Message received from MS2 : @@¨
Enter message to BS1 :2
Measuring signal strength to BS1 and BS2......
Signal strength of BS1 is greater than signal strength of BS2
Communicating with BS1....!!
Current time: Mon Nov 2 08:42:06 2009
Message received from MS2 : 3@@
Enter message to BS1 :
Bold part what I was talking about
"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 :");)"
and here is my other program code
printf("\nYou are in Conversation state");
sleep(4);
curnt = time(NULL);
dtime = ctime(&curnt);
printf("\n Current time: %s", dtime);
printf("\nEnter message to MS1: ");
fgets(msg_m2,STR_SIZE, stdin);
len = strlen(msg_m2);
newstring = (char *)malloc(len-1);
for(i = 0; i < strlen(msg_m2)-1; i++)
{
newstring[i] = msg_m2[i];
}
strcpy(mptrm2->who,"MS2");
strcpy(mptrm2->whom,"BS1");
strcpy(mptrm2->msgType,"CON
strncpy(mptrm2->msg, newstring, STR_SIZE);
if((strcmp(newstring,"bye"
{
currentState2= 13;
strcpy(mptrm2 -> msgType,"bye");
}
else
{
sleep(10);
curnt = time(NULL);
dtime = ctime(&curnt);
printf("\n Current time: %s", dtime);
printf("\nMessage received from MS1 : %s", mptrm2->msg);
}
break;
Thank you.
dpd.





by: mnhPosted on 2009-11-01 at 22:16:21ID: 25717364
Hi,
I see two sleep() calls in your code: sleep(9) and sleep(4).
If the delay is about 13 or 14 seconds, then obviously this is the cause.
Additionally, it's not clear why you need the malloc statement, and the loop
for(i = 0; i < strlen(msg)-1; i++)
{
newstring[i] = msg[i];
}
Couldn't you simply use msg, as follows:
strncpy(mptr->msg, msg, STR_SIZE);
if((strcmp(msg,"bye")==0))
etc...