- 论坛徽章:
- 0
|
从输出看mmap确实映射了,但myread读到的同mywrite所写的并不实时对应,有什么办法改吗,即使阻塞也可以,和msync没关吧应该?先谢过
- (myread.c)
- #include
- int main()
- {
- int fd;
- int *shm;
-
- fd = open("data.tmp",O_CREAT|O_RDWR,00777);
- if(fd<0)
- {
- ...
- }
- int len;
- len = lseek(fd,0,SEEK_END);
- if(len<1)
- {
- lseek(fd,1,SEEK_SET);
- write(fd,'1',1);
- }
-
- shm = mmap(NULL,1,PROT_READ|PROT_WRITE,MAP_SHARED,fd,0);
- if(shm == (int *)-1)
- {
- ....
- }
-
- while(1)
- {
- printf("%d read: %d\n",getpid(),*shmq);
- }
- }
- +++++++++++++++++++++
- (mywrite.c)
- #include
- int main()
- {
- int fd;
- int *shm;
-
- fd = open("data.tmp",O_CREAT|O_RDWR,00777);
- if(fd<0)
- {
- ...
- }
- int len;
- len = lseek(fd,0,SEEK_END);
- if(len<1)
- {
- lseek(fd,1,SEEK_SET);
- write(fd,'1',1);
- }
-
- shm = mmap(NULL,1,PROT_READ|PROT_WRITE,MAP_SHARED,fd,0);
- if(shm == (int *)-1)
- {
- ....
- }
-
- while(1)
- {
- *shm = (*shm+1)%10000;
- printf("%d read: %d\n",getpid(),*shmq);
- }
- }
复制代码
输出:
1024 write :4
1024 write :5
1024 write :6
1024 write :7 ....
1023 read : 4
1023 read : 4
.
.
1023 read : 2206
1023 read : 2206
.
.
--------------------------------------
MAP_SHARED : share this mapping with all other process that map this obhect .
storing to the region is equivalent to writing to the file .the file may not
actually be updated until msync or munmap are called. |
|