Setting a fmemopen ed file descriptor to be the standard input for a child process

匆匆过客 提交于 2019-12-04 12:09:31

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();
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!