On Die, 2002-03-19 at 14:32, Simon Marlow wrote:
> > The mmap system call provides this, on Unix systems. The man page says
> > it's POSIX.1b standard, so it might even be supported by Windows 2000.
>
> I don't think mmap() provides exactly the right behaviour. It lets you
> specify that modifications made by the current process aren't committed
> to the file, but what we want is to snapshot the file so that subsequent
> modifications by *other* processes aren't seen by the local process.
The Linux manpage isn't very explicit, so I've tried it. When making a
private mapping (MAP_PRIVATE), it makes a snapshot which is private to
the process, and isn't affected by other processes. It's exactly what's
needed. (see attachment)
> > This strategy won't work for general IO streams (pipes,
> > sockets...), of
> > course. If the spec would require all stream contents to behave like
> > pure values, this would limit lazy streams to files. So you could no
> > longer handle general streams and files the same way...
>
> These kinds of streams are immutable, so the problem is less severe. We
> don't have to worry about the interaction with other processes.
Well, several processes might read from/write to the same stream.
bye,
Volker
test : test.c
echo "x" > mapped
gcc -o $@ $<
#define _POSIX_MAPPED_FILES
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <assert.h>
#include <stdio.h>
#include <sys/mman.h>
main(int argc, char** argv)
{
int fd;
char* m;
assert(argc >= 2);
fd = open("mapped", O_RDONLY);
assert(fd != -1);
m = mmap(0, 1, PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0);
assert(m);
*m = *argv[1];
printf("wrote '%c' to private copy. press enter...\n", *m);
getchar();
printf("the contents now is '%c'\n", *m);
}