공유 메모리에서 IPC를 시도하는 두 개의 파일이 있습니다. 두 파일에 비슷한 문장을 사용하여 할당합니다. 서버 파일에서 :C 메모리 매핑 된 구조체 배열 누수
int fd;
int size = MAX_LEN;
int bigSize = sizeof(struct region)+ size * sizeof(struct client_message) + size * sizeof(struct server_message);
struct region *rptr = (struct region*)malloc(bigSize);
printf("region size: %d clientMessage size: %d serverMessage size: %d and the total size: %d\n", sizeof(struct region), size * sizeof(struct client_message), size * sizeof(struct server_message), bigSize);
fd = shm_open("/myregion", O_CREAT | O_RDWR, S_IRUSR | S_IWUSR);
if (ftruncate(fd, bigSize) == -1)
printf("error creating ftruncate\n");
rptr = mmap(NULL, sizeof(struct region),
PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
동적으로 공유 메모리를 할당 아래의 링크에서 예제를 사용했습니다 : C Windows - Memory Mapped File - dynamic array within a shared struct
struct client_message {
pthread_t client_id;
int question;
};
struct server_message {
pthread_t client_id;
pid_t server_id;
int answer;
};
struct region { /* Defines "structure" of shared memory */
int len;
struct client_message ptr_client_message[0];
struct server_message ptr_server_message[0];
};
내가이 서버에서 while 루프 및 증가 J에 지정 파일,
(rptr->ptr_client_message[(j)%size]).question = 30;
(rptr->ptr_server_message[(j)%size]).answer = 20;
나는 같은 클라이언트 파일에서 읽기 :
,printf("rptr len is %d and question of client %d is: %d, answer of server is %d \n", size, k%size, (rptr->ptr_client_message[(k)%size]).question, (rptr->ptr_server_message[(k)%size]).answer);
출력이 mindboggling된다 를 서버 단말로부터 I 얻을 :
rptr len is 10 and question of client 0 is: 30, answer of server is 20
rptr len is 10 and question of client 1 is: 30, answer of server is 20
rptr len is 10 and question of client 2 is: 30, answer of server is 20
...
는 [MAX_LEN 상기 클라이언트 단말 I에서
즉 클라이언트까지의 client_message 어레이 (10 개)의 요소의 변경 get :
rtpr len is 10 and question of client 0 is: 30, answer of server is 20
rptr len is 10 and question of client 1 is: 30, answer of server is 30
rptr len is 10 and question of client 2 is: 30, answer of server is 20
rptr len is 10 and question of client 3 is: 30, answer of server is 30
rptr len is 10 and question of client 4 is: 30, answer of server is 20
rptr len is 10 and question of client 5 is: 30, answer of server is 30
rptr len is 10 and question of client 6 is: 30, answer of server is 20
rptr len is 10 and question of client 7 is: 30, answer of server is 20
rptr len is 10 and question of client 8 is: 30, answer of server is 20
rptr len is 10 and question of client 9 is: 30, answer of server is 20
rptr len is 10 and question of client 0 is: 30, answer of server is 20
rptr len is 10 and question of client 1 is: 30, answer of server is 30
rptr len is 10 and question of client 2 is: 30, answer of server is 20
rptr len is 10 and question of client 3 is: 30, answer of server is 30
rptr len is 10 and question of client 4 is: 30, answer of server is 20
rptr len is 10 and question of client 5 is: 30, answer of server is 30
rptr len is 10 and question of client 6 is: 30, answer of server is 20
rptr len is 10 and question of client 7 is: 30, answer of server is 20
rptr len is 10 and question of client 8 is: 30, answer of server is 20
그래서 구조체 영역의 항목은 다른 프로세스에서 도달 할 때 혼합됩니다. 이 문제를 어떻게 방지 할 수 있습니까?
struct에 유연한 배열 멤버가 2 개있을 수 없습니다. 기본적으로'rptr-> ptr_client_message'와'rptr-> ptr_server_message' 별칭입니다. –
감사합니다. 개선이 도움이 되셨습니까? 그건 그렇고, 당신은 어떻게 그것을 확실히 알고 있습니까, 나는 어떤 참조를 의미합니까? –
http://port70.net/~nsz/c/c11/n1570.html#6.7.2.1p18 : "특별한 경우로, 둘 이상의 명명 된 멤버가있는 구조의 마지막 요소에 불완전한 배열 유형이있을 수 있습니다. 이것을 유연한 배열 구성원이라고 부릅니다. " –