Setting a fmemopen ed file descriptor to be the standard input for a child process
I have an fmemopen file descriptor(pointing to a buffer in the parent) in Linux and I would like to be able to, in C, set this file descriptor as the standard input for a child process(for whom I do not have access to the code)
Is this possible? If so how do I do 开发者_开发百科it? I would like to avoid having to write to disk if at all possible.
This is not possible. Inheriting stdin/out/err is based purely on file descriptors, not stdio FILE streams. Since fmemopen
does not create a file descriptor, it cannot become a new process's stdin/out/err or be used for inter-process communication in any way. What you're looking for is a pipe, unless you need seeking, in which case you need a temporary file. The tmpfile
function could be used to create one without having to worry about making a visible name in the filesystem.
I'm not sure if you can directly use your fd (if, then you would have do dup2()
), but if not, you can create a pipe()
where you feed your in-memory data and the client can receive it. Here as well, you probably would have to dup2()
after fork()
ing.
Just replace file descriptor 0
by your mmaped file descriptor before forking:
int fd = ...;
dup2(fd, 0);
fork();
精彩评论