[英]strange mmap behavior
我正在做一個實驗,我有一個程序可以創建一個 16 字節的共享文件,然后每秒顯示每個字節的值。
我希望能夠在程序運行時更改文件,並讓程序識別更改。
該程序似乎確實也將寫入內存寫入文件; 但是如果我在運行時修改文件,盡管文件被更改,它仍會繼續使用舊值,並且文件將停止更新。
這是無法識別更改的版本。
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <stdlib.h>
#include <sys/mman.h>
#include <fcntl.h>
#include <unistd.h>
#include <signal.h>
// | bag of global state.
struct {
uint8_t *state;
char *filename;
int length;
} global = {
(uint8_t *)0,
"state.bin",
16
};
// | clean up when ctrl-c is pressed.
static void onSIGINT(int unused)
{
puts("caught SIGINT; cleaning up.");
munmap(global.state, global.length);
unlink(global.filename);
exit(0);
}
// | display each byte of global shared memory
static void inspect()
{
int i;
for (i = 0; i < global.length; ++i) {
printf("state[%d] = %d.\n", i, global.state[i]);
}
}
int main(int argc, char **argv)
{
/* anonymous scope: initialize shared memory */
{
// | mmap arguments
void *addr = (void *)0;
int prot = PROT_READ | PROT_WRITE;
int flags = MAP_SHARED;
int offset = 0;
int fd = open(global.filename, O_CREAT | O_RDWR, S_IRUSR | S_IWUSR);
// | initialize memory to 16 zerod out bytes.
uint8_t dummy[16];
memset(&dummy, 0, global.length);
write(fd, dummy, global.length);
global.state = (uint8_t *)mmap (
addr,
global.length,
prot,
flags,
fd,
offset
);
if (global.state == MAP_FAILED) {
close(fd);
perror("could not create map.\n");
unlink(global.filename);
}
}
signal(SIGINT, onSIGINT);
/* anonymous scope: mainloop */
{
int count = 0;
for (;;) {
system("clear");
printf("refresh number: %d.\n", count);
++count;
inspect();
sleep(1);
}
}
return 0;
}
這是在每次顯示迭代時也會增加每個字節的版本,以表明它實際上正在使用共享文件。
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <stdlib.h>
#include <sys/mman.h>
#include <fcntl.h>
#include <unistd.h>
#include <signal.h>
// | bag of global state.
struct {
uint8_t *state;
char *filename;
int length;
} global = {
(uint8_t *)0,
"state.bin",
16
};
// | clean up when ctrl-c is pressed.
static void onSIGINT(int unused)
{
puts("caught SIGINT; cleaning up.");
munmap(global.state, global.length);
unlink(global.filename);
exit(0);
}
// | prints length bytes starting at address given.
static void inspect()
{
int i;
for (i = 0; i < global.length; ++i) {
printf("state[%d] = %d.\n", i, global.state[i]);
++global.state[i];
}
}
int main(int argc, char **argv)
{
/* anonymous scope: initialize shared memory */
{
// | mmap arguments
void *addr = (void *)0;
int prot = PROT_READ | PROT_WRITE;
int flags = MAP_SHARED;
int offset = 0;
int fd = open(global.filename, O_CREAT | O_RDWR, S_IRUSR | S_IWUSR);
// | initialize memory to 16 zerod out bytes.
uint8_t dummy[16];
memset(&dummy, 0, global.length);
write(fd, dummy, global.length);
global.state = (uint8_t *)mmap (
addr,
global.length,
prot,
flags,
fd,
offset
);
if (global.state == MAP_FAILED) {
close(fd);
perror("could not create map.\n");
unlink(global.filename);
}
}
signal(SIGINT, onSIGINT);
/* anonymous scope: mainloop */
{
int count = 0;
for (;;) {
system("clear");
printf("refresh number: %d.\n", count);
++count;
inspect();
sleep(1);
}
}
return 0;
}
請注意,它成功地不斷增加每個字節,直到我更改 vim 中的一個字節為止。 當我覆蓋文件時,它會停止修改文件,但無論如何都會繼續從它所在的位置開始計數。
為什么會這樣; 我怎樣才能讓它按預期運行?
這是由 vim 用來創建備份文件的方法引起的。 這是為備份復制選項記錄的,典型的默認值是重命名原始文件並編輯副本。 這會導致內存映射視圖與備份相關聯,而不是與正在編輯的文件相關聯。
如果您檢查文件的 inode,您可以看到這種情況:
$ echo z>a && ls -i a
14551885 a
$ vim a
$ ls -i a
14551887 a
如您所見,inode 現在不同了。 例如,如果您使用 python 打開和修改文件,您可以就地編輯文件,它會按預期工作。
使用 python 的示例:
$ ls -i a
14551886 a
$ python
Python 2.7.6 (default, Oct 26 2016, 20:30:19)
[GCC 4.8.4] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> f = open('a', 'r+')
>>> f.write('22')
>>> f.close()
>>>
$ ls -i a
14551886 a
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.