我有 2 个在 RedHat 5.5 中运行的 4.1.2 中编译的程序,测试共享内存是一项简单的工作,shmem1.c 如下所示:
#define STATE_FILE "/program.shared"
#define NAMESIZE 1024
#define MAXNAMES 100
typedef struct
{
char name[MAXNAMES][NAMESIZE];
int heartbeat ;
int iFlag ;
} SHARED_VAR;
int main (void)
{
int first = 0;
int shm_fd;
static SHARED_VAR *conf;
if((shm_fd = shm_open(STATE_FILE, (O_CREAT | O_EXCL | O_RDWR),
(S_IREAD | S_IWRITE))) > 0 ) {
first = 1; /* We are the first instance */
}
else if((shm_fd = shm_open(STATE_FILE, (O_CREAT | O_RDWR),
(S_IREAD | S_IWRITE))) < 0) {
printf("Could not create shm object. %s\n", strerror(errno));
return errno;
}
if((conf = mmap(0, sizeof(SHARED_VAR), (PROT_READ | PROT_WRITE),
MAP_SHARED, shm_fd, 0)) == MAP_FAILED) {
return errno;
}
if(first) {
for(idx=0;idx< 1000000000;idx++)
{
conf->heartbeat = conf->heartbeat + 1 ;
}
}
printf("conf->heartbeat=(%d)\n",conf->heartbeat) ;
close(shm_fd);
shm_unlink(STATE_FILE);
exit(0);
}//main
和 shmem2.c 如下:
#define STATE_FILE "/program.shared"
#define NAMESIZE 1024
#define MAXNAMES 100
typedef struct
{
char name[MAXNAMES][NAMESIZE];
int heartbeat ;
int iFlag ;
} SHARED_VAR;
int main (void)
{
int first = 0;
int shm_fd;
static SHARED_VAR *conf;
if((shm_fd = shm_open(STATE_FILE, (O_RDWR),
(S_IREAD | S_IWRITE))) < 0) {
printf("Could not create shm object. %s\n", strerror(errno));
return errno;
}
ftruncate(shm_fd, sizeof(SHARED_VAR));
if((conf = mmap(0, sizeof(SHARED_VAR), (PROT_READ | PROT_WRITE),
MAP_SHARED, shm_fd, 0)) == MAP_FAILED) {
return errno;
}
int idx ;
for(idx=0;idx< 1000000000;idx++)
{
conf->heartbeat = conf->heartbeat + 1 ;
}
printf("conf->heartbeat=(%d)\n",conf->heartbeat) ;
close(shm_fd);
exit(0);
}
编译后:
gcc shmem1.c -lpthread -lrt -o shmem1.exe
gcc shmem2.c -lpthread -lrt -o shmem2.exe
并使用 2 个终端几乎同时运行这两个程序:
[test]$ ./shmem1.exe
First creation of the shm. Setting up default values
conf->heartbeat=(840825951)
[test]$ ./shmem2.exe
conf->heartbeat=(1215083817)
我觉得很迷茫!!由于 shmem1.c 是一个循环 1,000,000,000 次,怎么可能得到像 840,825,951 这样的答案?
我以这种方式运行 shmem1.exe 和 shmem2.exe,大多数结果是 conf->heartbeat 将大于 1,000,000,000 ,但很少随机地看到结果 conf->heartbeat 将小于 1,000,000,000 ,
无论是在 shmem1.exe 还是shmem2.exe!!
如果只运行 shmem1.exe,总是打印 1,000,000,000,我的问题是,shmem1.exe 中 conf->heartbeat=(840825951) 的原因是什么?
更新:虽然不确定,但我想我弄清楚是怎么回事,例如如果 shmem1.exe 运行 10 次,则 conf->heartbeat = 10 ,此时 shmem1.exe 休息一下,然后返回,shmem1 .exe 从共享内存和 conf->heartbeat = 8 读取,所以 shmem1.exe 将从 8 继续,为什么 conf->heartbeat = 8 ?我认为这是因为 shmem2.exe 将共享内存数据更新为 8 , shmem1.exe 在休息之前没有将 10 写回共享内存......这只是我的理论......我不知道如何证明它!