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

Finding open file descriptors for a process linux ( C code )?

Here’s some code I used to use, I didn’t know about /proc/self (thx Donal!), but this way is probably more generic anyway. I’ve included the required includes for all the functions at the top. #include <string.h> #include <stdio.h> #include <dirent.h> #include <fcntl.h> #include <unistd.h> #include <errno.h> #include <sys/resource.h> #ifndef FALSE #define FALSE (0) #endif #ifndef … Read more

is it a good practice to close file descriptors on exit [closed]

The classic guide to POSIX programming “Advanced programming in UNIX environment” states: When a process terminates, all of its open files are closed automatically by the kernel. Many programs take advantage of this fact and don’t explicitly close open files. You did not mention the OS in your question but such behavior should be expected … Read more

Socket and file descriptors

Yes, sockets are also indices into the same table as files. At least for UNIX systems (like Linux and OSX), Windows is different, which is why you can’t use e.g. read and write to receive and send data. Each process has its own “file” descriptor table.

How to redirect an output file descriptor of a subshell to an input file descriptor in the parent shell?

BEWARE, BASHISM AHEAD (there are posix shells that are significantly faster than bash, e.g. ash or dash, that don’t have process substitution). You can do a handle dance to move original standard output to a new descriptor to make standard output available for piping (from the top of my head): exec 3>&1 # open 3 … Read more