Problem with Shared Memory

Hi,

I have been trying to implement shared memory using C++ in Linux (Ubuntu) and there is a problem that I have been unable to solve for one week.

Ok this is my dilemena. I have been able to impelement shared memory successfully when I hardcode some values into the shared variable in the "server" program. Then the "client" program can then access the shared memory and print out the values of the shared variable correctly. So i know i dont have a problem implementing shared memory in general.

The problem arises when instead of hardcoding some values into the shared variable, I try and allocate some values by invoking a function (that reads data output of a sensor). Then I find that my "client" program can not access the values from shared memory correctly (but the "server" program successfully writes the values to shared memeory upon invocation of the function). Can someone pls figure out what my problem is


//server.cpp
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <iostream>
// for sensor use - more details given at the end of this post
#include "scipConnection.h"
// for shared memory
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>


// macro definitions
#define SHM_size 10240 // size of shared memory segment


// Global declarations
FILE *dev; // variables related to sensor
int stepmin = 44, stepmax = 725, stepskip = 1;
int numstep;
int *shm_scan_ptr; // pointer pointing to shared memory. will contain
//output of sensor



// Main Program
int main(int argc){

key_t key;
int shmid;

key = 001122334455; // simpler for me to harcode key instead of ftok()


// connect
if ((shmid = shmget(key,SHM_size,0666 | IPC_CREAT )) == -1){
perror ("shmget error");
exit (1);
}

printf("id is %d\n", shmid);

// attach pointer
shm_scan_ptr = (int*) shmat (shmid, NULL, 0);
if ((int)shm_scan_ptr == -1){
perror("shmat error");
exit(1);
}

// sensor variables
char devName[255];
printf("sensor reading data...\n");
strcpy(devName,"/dev/ttyACM0");
dev = openSerial(devName,19200);

// fill the pointer variable with data from scanner
while(1){

printf("Writing data to shared memory...\n");

/* // manually providing input into shared variable - this is working
values of 0 and 681 are sensor dependant
for (int h = 0; h <= 681; h++){
shm_scan_ptr[h] = h*2; // just hardcoding some arbitary values: h * 2
std::cout << shm_scan_ptr[h] << std::endl;
}*/

// using sensor function to provide input into shared variable
// when i use this instead of above code, then client program
// cannot read values from shared memeory. This function is
// in scipConnection.h which is given after the client.cpp prgram

shm_scan_ptr = scipGetScan(dev,stepmin,stepmax,stepskip,&numstep);
printf("Writing data to shared memory...\n");
//print data values
for (int j = 0; j <= 50; j++) {
std::cout << shm_scan_ptr[j] << std::endl;
}

}

return 0;
}


// client.cpp
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <iostream>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>

// macro definitions
#define SHM_size 10240 // size of shared memory segment = 10k


// Global declarations
int *shm_scan_ptr; // pointer to array that contains sensor data in shared
// memory.

// Function Prototype
void Access_SM();
void Read_Values():

// Main Program
int main(){

Access_SM();

while (1) {

Read_Values();

}

return 0;
}

// Function Definitions
void Access_SM(){

key_t key;
int shmid;

printf("accessing shared memory\n");

key = 001122334455;


// connect
if ((shmid = shmget(key,SHM_size,0666 )) == -1){
perror ("shmget error (client)");
exit (1);
}
printf("shmget ok\n");
printf("id is %d\n", shmid);

// attach a pointer
shm_scan_ptr = (int*) shmat (shmid, NULL, 0);
if ((int)shm_scan_ptr == -1){
perror("shmat error (client)");
exit(1);
}
printf("shmat ok\n");


}


// Read values from shared memory
void Read_Values(){

//print data values
// why i use value 681 is because sensor returns that many outputs
for (int h = 0; h<=681; h++){
std::cout << shm_scan_ptr[h] << std::endl;
}

}


This is the implementation of the scipGetScan() function of the sensor, which when i use, my client program cannot read data from shard memory. Basically this function returns an interger pointer.

int* scipGetScan(FILE* aPort, int aStart, int aEnd, int aSkip, int *aNum)

{

char req[16];

int *scan,*pt;

char line[80];

int i;



sprintf(req,"G%03d%03d%02d\n",aStart,aEnd,aSkip);

i=fwrite(req,sizeof(char),strlen(req),aPort);

if(i==0)

return NULL;



// echo-back

if(fgets(line,sizeof(line),aPort)==NULL)

return NULL;

if(strcmp(req,line)!=0) // invalid

return NULL;



// status

if(fgets(line,sizeof(line),aPort)==NULL)

return NULL;

if(strcmp("0\n",line)!=0) // invalid

return NULL;



// result

if((aEnd-aStart)%aSkip==0)

*aNum=(aEnd-aStart)/aSkip+1;

else

*aNum=(aEnd-aStart)/aSkip+2;



scan=(int*)malloc(sizeof(int)*(*aNum));

pt=scan;



while(1)

{

if(fgets(line,sizeof(line),aPort)==NULL)

{

free(scan);

return NULL;

}

if(!strcmp("\n",line))

{

//end of result

break;

}

for(i=0;i<strlen(line)-1;++pt)

{



*pt=((line[i] - 0x30)<<6) + line[i+1] - 0x30;

i=i+2;

}

}

return scan;

}

Please note that i have removed other redundant code to shorten the code for posting so if there are minor errors, they are just typos. can anyone see what my problem is?

Thanks
Topic archived. No new replies allowed.