使用mmap在C中共享内存中的结构
Structs in shared memory in C using mmap
我已经阅读了之前的问题和我的问题的答案,但是大多数人似乎使用的是 shmget 而不是 mmap。
我将 mmap 用于共享内存并且一切正常,直到我从使用静态变量改为使用结构。目前,我收到以下错误:
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);
我理解这个错误,但是我不确定如何使用结构来解决它,因为这是我认为共享内存的完成方式。我正在分叉,所以这些结构正在子进程中使用。
我的代码:
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);
函数 'interfaceComms' 和 'robotComms' 在分叉发生后稍后调用。但我猜我的问题出在某处。
根据要求,intefaceComms 和 robotComms 函数:
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;
提前致谢。
您的问题是 interfaceConnection 是您主函数中的局部变量。
interface *interfaceConnection;
我假设您希望在全球范围内使用它。不看其他代码很难判断。
我已经阅读了之前的问题和我的问题的答案,但是大多数人似乎使用的是 shmget 而不是 mmap。
我将 mmap 用于共享内存并且一切正常,直到我从使用静态变量改为使用结构。目前,我收到以下错误:
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);
我理解这个错误,但是我不确定如何使用结构来解决它,因为这是我认为共享内存的完成方式。我正在分叉,所以这些结构正在子进程中使用。
我的代码:
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);
函数 'interfaceComms' 和 'robotComms' 在分叉发生后稍后调用。但我猜我的问题出在某处。
根据要求,intefaceComms 和 robotComms 函数:
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;
提前致谢。
您的问题是 interfaceConnection 是您主函数中的局部变量。
interface *interfaceConnection;
我假设您希望在全球范围内使用它。不看其他代码很难判断。