文件 'hello' 的内容是hello.
$ od -tx1 -tc hello
0000000 68 65 6c 6c 6f 0a
h e l l o \n
0000006
Run Code Online (Sandbox Code Playgroud)
下面是我对文件“hello”进行一些更改的代码。
static void *task();
int main(void)
{
int *p;
pthread_t Thread;
int fd = open("hello", O_RDWR);
if (fd < 0) {
perror("open hello");
exit(1);
}
p = mmap(NULL, 6, PROT_WRITE, MAP_PRIVATE, fd, 0);
if (p == MAP_FAILED) {
perror("mmap");
exit(1);
}
close(fd);
pthread_create(&Thread, NULL, &task, p)
printf("Help");
pthread_join(Thread, 0);
munmap(p, 6);
return 0;
}
static void * task(int *r)
{
r[0] = 0x30313233;
}
Run Code Online (Sandbox Code Playgroud)
上面的代码我用的MAP_PRIVATE,貌似子线程不行。如果我更改MAP_PRIVATE为MAP_SHARED,我会发现它与我期望的不同。
$ od -tx1 -tc hello
0000000 33 32 31 30 6f 0a
3 2 1 0 o \n
0000006
Run Code Online (Sandbox Code Playgroud)
但我不知道它是如何发生的。