mfisher91
mfisher91

Reputation: 807

Structs in shared memory in C using mmap

I've read earlier questions and answers to my question, however the majority appear to be using shmget rather than mmap.

I was using mmap for shared memory and it was all working fine, until I changed from using static variables to structs. Currently, I'm getting the following errors:

server.c: In function ‘interfaceComms’:
server.c:129:18: error: ‘interfaceConnection’ undeclared (first use in this function)
  n = write(sock, interfaceConnection->processingResults, 47);
                  ^
server.c:129:18: note: each undeclared identifier is reported only once for each function it appears in
server.c:139:12: error: ‘imageDetails’ undeclared (first use in this function)
  if(strcmp(imageDetails->prevImage, "") != 0) {
            ^
server.c: In function ‘robotComms’:
server.c:162:2: error: ‘imageDetails’ undeclared (first use in this function)
  imageDetails->prevImage = imageDetails->currImage;
  ^
server.c:213:9: error: ‘interfaceConnection’ undeclared (first use in this function)
  strcpy(interfaceConnection->processingResults, msg);

I understand the error, however I'm not sure how to resolve it using structs as this is the way I thought shared memory was done. I'm forking, so the structs are being used in child processes.

My code:

typedef struct Images {
    char prevImage;
    char currImage;
} image;

typedef struct Robot {
    char coordinates;
} robot;

typedef struct Interface {
    char processingResults;
} interface;

int main(int argc, char *argv[]){
int sockfd, newsockfd, portno, clilen, memid;
char buffer[256];
struct sockaddr_in serv_addr, cli_addr;
int n, pid;
interface *interfaceConnection;
image *imageDetails;

// First call to socket() function
sockfd = socket(AF_INET, SOCK_STREAM, 0);

if (sockfd < 0){
    perror("Error opening socket");
    exit(1);
}

// Initialise socket structure
bzero((char *) &serv_addr, sizeof(serv_addr));
portno = 5001;

serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = INADDR_ANY;
serv_addr.sin_port = htons(portno);

//Bind the socket
if(bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0){
    perror("Error binding socket");
    exit(1);
}

//Listen for client connections
listen(sockfd,5);
clilen = sizeof(cli_addr);

//Share memory before forking
interfaceConnection = mmap(NULL, sizeof(interface), PROT_READ | PROT_WRITE, 
                MAP_SHARED | MAP_ANONYMOUS, -1, 0);

imageDetails = mmap(NULL, sizeof(image), PROT_READ | PROT_WRITE, 
            MAP_SHARED | MAP_ANONYMOUS, -1, 0);

The function 'interfaceComms' and 'robotComms' are called later on after the fork has happened. But I'm guessing my issue is up there somewhere.

As requested, the intefaceComms and robotComms functions:

void checkMessage(int sock){
int n;
char message[256];
n = read(sock, message, 255);
int response;

if(n < 0){
    perror("Error reading the socket");
    exit(1);
}

//see if the message is from the robot or from the interface
if(strstr(message,"robot") != NULL) {
    //The robot wants the co-ordinates of the component
    robotComms(sock);
} else {
    //Someone has accessed the main page
    interfaceComms(sock);
}
}

void interfaceComms(int sock) {
int n;

n = write(sock, interfaceConnection->processingResults, 47);

if (n < 0){
    perror("Error writing to the socket");
    exit(1);
} else {
    printf("Results sent to interface\n");
}
}

void robotComms(int sock) {
int n;
srand ( time(NULL) ); //reset rand
//printf("Here is the message: %s\n", message);
printf("Getting image...\n");

//Change currImage to prevImage
imageDetails->prevImage = imageDetails->currImage;

Thanks in advance.

Upvotes: 1

Views: 1088

Answers (1)

acid1789
acid1789

Reputation: 139

Your problem is that interfaceConnection is a local variable in your main function.

interface *interfaceConnection;

I am assuming you want this at the global scope. Its hard to tell without seeing the other code.

Upvotes: 4

Related Questions