기본 콘텐츠로 건너뛰기

라벨이 lock인 게시물 표시

공유메모리에 40바이트 읽고 쓰는데 얼마나 충돌날까?

가끔씩 공유메모리에 있는 자료를 n개 프로세스/쓰레드가 읽고 쓰기 위해 접근할 때, 정말 충돌이 나긴할까? 물론 이론은 반드시 나긴 나는데, 이론은 이론이고... 임계영역이 I/O wait이 안 일어날 정도로 짧은 구간이더라도 충돌이 날까? 결론은 충돌난다 . ㅡ_-) 덴장... 이래서 잠금장치가 여러개 있는 것이지... 아래 소스를 대충 컴파일해서 돌려보고 Ctrl+C를 눌러보면... #include <sys/ipc.h> #include <sys/shm.h> #include <unistd.h> #include <string.h> #include <stdlib.h> #include <stdio.h> #include <errno.h> #include <sys/types.h> #include <signal.h> enum { SHM_STR_LEN = 32 }; typedef struct str_t { int len; char buf[SHM_STR_LEN+1]; } str_t; volatile size_t g_total_count(0); volatile size_t g_crash_count(0); int g_shmfd(-1); str_t* g_buf(NULL); // make random string... void makeRandStr(str_t& buf) { memset(&buf, 0x00, sizeof(buf)); int i, max(rand()%SHM_STR_LEN); for ( i = 0; i < max; i++ ) { buf.buf[i] = (rand()%10) + '0'; } buf.len = max; buf.buf[SHM_STR_LEN] = 0x00; } // check str_t struc...