How to write data to existing process’s STDIN from external process?

Your code will not work. /proc/pid/fd/0 is a link to the /dev/pts/6 file. $ echo ‘foobar’ > /dev/pts/6 $ echo ‘foobar’ > /proc/pid/fd/0 Since both the commands write to the terminal. This input goes to terminal and not to the process. It will work if stdin intially is a pipe. For example, test.py is : … Read more

Retrieving file descriptor from a std::fstream [duplicate]

You can go the other way: implement your own stream buffer that wraps a file descriptor and then use it with iostream instead of fstream. Using Boost.Iostreams can make the task easier. Non-portable gcc solution is: #include <ext/stdio_filebuf.h> { int fd = …; __gnu_cxx::stdio_filebuf<char> fd_file_buf{fd, std::ios_base::out | std::ios_base::binary}; std::ostream fd_stream{&fd_file_buf}; // Write into fd_stream. // … Read more

What’s the difference between a file descriptor and a file pointer?

A file descriptor is a low-level integer “handle” used to identify an opened file (or socket, or whatever) at the kernel level, in Linux and other Unix-like systems. You pass “naked” file descriptors to actual Unix calls, such as read(), write() and so on. A FILE pointer is a C standard library-level construct, used to … Read more

Getting the highest allocated file descriptor

While portable, closing all file descriptors up to sysconf(_SC_OPEN_MAX) is not reliable, because on most systems this call returns the current file descriptor soft limit, which could have been lowered below the highest used file descriptor. Another issue is that on many systems sysconf(_SC_OPEN_MAX) may return INT_MAX, which can cause this approach to be unacceptably … Read more

Increasing limit of FD_SETSIZE and select

Per the standards, there is no way to increase FD_SETSIZE. Some programs and libraries (libevent comes to mind) try to work around this by allocating additional space for the fd_set object and passing values larger than FD_SETSIZE to the FD_* macros, but this is a very bad idea since robust implementations may perform bounds-checking on … Read more

What’s the difference between a file descriptor and file pointer?

A file descriptor is a low-level integer “handle” used to identify an opened file (or socket, or whatever) at the kernel level, in Linux and other Unix-like systems. You pass “naked” file descriptors to actual Unix calls, such as read(), write() and so on. A FILE pointer is a C standard library-level construct, used to … Read more

Sending file descriptor by Linux socket

Stevens (et al) UNIX® Network Programming, Vol 1: The Sockets Networking API describes the process of transferring file descriptors between processes in Chapter 15 Unix Domain Protocols and specifically §15.7 Passing Descriptors. It’s fiddly to describe in full, but it must be done on a Unix domain socket (AF_UNIX or AF_LOCAL), and the sender process … Read more