我已经阅读了我的问题的早期问题和答案,但是大多数人似乎都在使用shmget而不是mmap。
我正在使用mmap作为共享内存,它一切正常,直到我从使用静态变量更改为结构。目前,我收到以下错误:
<project>
<echo message="Hello!"/>
<groovy> <!-- Would be nice if I could treat this like a built-in -->
println "Hello from Groovy!"
</groovy>
</project>
我理解错误,但是我不确定如何使用结构来解决它,因为这是我认为共享内存完成的方式。我正在分叉,所以这些结构正在儿童过程中使用。
我的代码:
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);
功能&#39; interfaceComms&#39;和&#39; robotComms&#39;在fork发生后稍后调用。但我猜测我的问题就在那里。
根据要求,intefaceComms和robotComms功能:
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);
提前致谢。
答案 0 :(得分:3)
您的问题是interfaceConnection是主函数中的局部变量。
interface *interfaceConnection;
我假设您希望在全球范围内使用此功能。没有看到其他代码就很难分辨。