2009年2月24日 星期二

popen and mmap

轉載

http://groups.google.com/group/comp.os.linux.development.system/browse_thread/thread/113038b65f4d3257

On Sat, 24 Jan 2009 12:32:40 -0800 (PST) spasmous2 <spasm...@gmail.com> wrote:
| I want to pipe the output of a system utility to a C program I'm
| writing. I tried popen() and it works nicely. The return type is a
| FILE* which I would like to map to memory using mmap() in the
| following way:
|
|    file = popen(command,"r");
|
|    data = (char*)mmap(NULL, size, PROT_READ, MAP_PRIVATE, fileno
| (file), 0);
|
| However the mmap command returns an error 'No such device', which I
| looked up to mean 'The fildes parameter does not refer to a *TYPE2
| stream file (*STMF) in the "root" (/), QOpenSys, or user-defined file
| systems.'
|
| I am unsure really what this means - do you know if it's a deal
| breaker or is there a solution?

You've been told by another followup that you cannot mmap a pipe.  The reason
you cannot do this is because a pipe is not data storage that can be accessed
by random position.  In an extreme way, a tape could be, but even it does not
work that way with existing drivers.  A pipe is a _sequential_ data stream
produced by the writing of another program.


If you want random memory access to data from another program, what you want
is a shared mapping between these programs.  There are a couple ways to do
that between processes.  Or you could use threads (if willing to spend time
to understand the complexities of threads).

以上的意思是說mmap不能用在任何streaming的應用

殘念


沒有留言:

張貼留言