The content of file 'hello' is hello
.
$ od -tx1 -tc hello
0000000 68 65 6c 6c 6f 0a
h e l l o \n
0000006
Below is my code to make some changes to the file '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;
}
The code above I used MAP_PRIVATE
, and it seems that the child thread does not work.
If I change MAP_PRIVATE
to MAP_SHARED
, I see it makes the difference I expect.
$ od -tx1 -tc hello
0000000 33 32 31 30 6f 0a
3 2 1 0 o \n
0000006
But I have no idea how it happens.