[x]
Posted via EE Mobile

Search, ask, and monitor your questions on the go with EE Mobile. Visit Experts Exchange from your mobile device and never be out of touch again.

11/01/2009 at 06:29PM PST, ID: 24862636 | Points: 500
[x]
Attachment Details

shared memory writing and reading

Asked by dpd3891 in C Programming Language

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;
 
Keywords: shared memory writing and reading
Title
1 memory fault
 
Loading Advertisement...
 
[+][-]11/01/09 10:16 PM, ID: 25717364

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]11/02/09 06:49 AM, ID: 25719794

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]11/02/09 07:13 AM, ID: 25720058

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]11/02/09 07:22 AM, ID: 25720153

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]11/02/09 08:07 AM, ID: 25720606

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]11/02/09 09:23 AM, ID: 25721375

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]11/03/09 12:53 AM, ID: 25727055

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]11/03/09 08:58 AM, ID: 25730982

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]11/12/09 08:09 PM, ID: 25811310

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
 
Loading Advertisement...
20090824-EE-VQP-74 - Hierarchy / EE_QW_3_20080625