file-descriptor

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

久未见 提交于 2019-11-27 07:50:31
I'm seeking for ways to write data to the existing process's STDIN from external processes, and found similar question How do you stream data into the STDIN of a program from different local/remote processes in Python? in stackoverlow. In that thread, @Michael says that we can get file descriptors of existing process in path like below, and permitted to write data into them on Linux. /proc/$PID/fd/ So, I've created a simple script listed below to test writing data to the script's STDIN (and TTY ) from external process. #!/usr/bin/env python import os, sys def get_ttyname(): for f in sys.stdin,

Windows equivalent of ulimit -n

大城市里の小女人 提交于 2019-11-27 07:07:45
问题 What is the windows equivalent of the unix command " ulimit -n" ? Basically, i want to set the maximum fd limit via command prompt. 回答1: hmm... I may have been wrong before - setmaxstdio (see here) - but it is per-process, not system wide. I may be wrong, but I didn't think there was a limit to set in Windows... but unless you can say how this relates to programming, I expect this answer will be closed soon. If you are in the "IT Pro" area (rather than development), then there is a sister

C read and thread safety (linux)

喜你入骨 提交于 2019-11-27 02:45:33
问题 What would happen if you call read (or write , or both) in two different thread, on the same file descriptor (lets says we are interested about a local file, and a it's a socket file descriptor), without using explicitly a synchronization mechanism? Read and Write are syscall, so, on a single core CPU, it's probably unlucky that two read would be executed "at the same time". But with multiple cores... What the linux kernel will do? And let's be a bit more general : is the behavior always the

How do file descriptors work?

拈花ヽ惹草 提交于 2019-11-27 02:42:24
Can someone tell me why this does not work? I'm playing around with file descriptors, but feel a little lost. #!/bin/bash echo "This" echo "is" >&2 echo "a" >&3 echo "test." >&4 The first three lines run fine, but the last two error out. Why? File descriptors 0, 1 and 2 are for stdin, stdout and stderr respectively. File descriptors 3, 4, .. 9 are for additional files. In order to use them, you need to open them first. For example: exec 3<> /tmp/foo #open fd 3. echo "test" >&3 exec 3>&- #close fd 3. For more information take a look at Advanced Bash-Scripting Guide: Chapter 20. I/O Redirection

How can I detect when someone opens the slave side of a pty (pseudo-terminal) in Linux?

倖福魔咒の 提交于 2019-11-27 02:19:30
问题 Having more than one process read from a serial device (/dev/ttyXX) makes it so that both processes can't get all of the data -- the data will be split between them in some way. I'd like to write a program that reads from a serial device, creates several master/slave pty pairs, and then allows programs that were made to read from the serial device to instead read from the ptys so that all reading processes receive the data from the serial device and have the ptys act like the serial device in

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

♀尐吖头ヾ 提交于 2019-11-27 02:08:35
Possible Duplicate: Getting a FILE* from a std::fstream I am working on Linux and file descriptors are the main model in this OS. I was wondering whether is there any library or any way to retrieve the native Linux file descriptor starting from a C++ std::fstream . I thought about boost::iostream since there is a class called file_descriptor but I understood that its purpose is different from the one I want to achieve. Do you know some way to do that? 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

Sending file descriptor by Linux socket

假如想象 提交于 2019-11-27 01:37:30
I am trying to send some file descriptor by linux socket, but it does not work. What am I doing wrong? How is one supposed to debug something like this? I tried putting perror() everywhere it's possible, but they claimed that everything is ok. Here is what I've written: #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <string.h> #include <sys/wait.h> #include <sys/socket.h> #include <sys/types.h> #include <fcntl.h> void wyslij(int socket, int fd) // send fd by socket { struct msghdr msg = {0}; char buf[CMSG_SPACE(sizeof fd)]; msg.msg_control = buf; msg.msg_controllen =

Getting the highest allocated file descriptor

╄→гoц情女王★ 提交于 2019-11-27 00:37:01
Is there a portable way (POSIX) to get the highest allocated file descriptor number for the current process? I know that there's a nice way to get the number on AIX, for example, but I'm looking for a portable method. The reason I'm asking is that I want to close all open file descriptors. My program is a server which runs as root and forks and execs child programs for non-root users. Leaving the privileged file descriptors open in the child process is a security problem. Some file descriptors may be opened by code I cannot control (the C library, third party libraries, etc.), so I cannot rely

How to check if a given file descriptor stored in a variable is still valid?

喜夏-厌秋 提交于 2019-11-26 22:21:24
问题 I have a file descriptor stored in a variable say var. How can I check whether that descriptor is valid at a later stage? fdvar1= open(.....); fdvar2 = fdvar1; // Please ignore the bad design .... // lots of loops , conditionals and threads. It can call close(fdvar2) also. .... if(CheckValid(fdvar1)) // How can I do this check ? write(fdvar1, ....); Now i want to check whether var1 (which still holds the opened descriptor) is still valid. Any API's for that ? 回答1: fcntl(fd, F_GETFD) is the

Why does open make my file descriptor 0?

我与影子孤独终老i 提交于 2019-11-26 22:09:44
问题 I'm working on a program that is using a pipe and forks and need to change the write end to an output file. But when I open a file the file descriptor is 0 which is usually stdin but which I think is the cause of some of my problems. Here is my code if (outputfd = open("file", O_RDWR | O_CREAT | O_TRUNC) == -1) { // open failed } Can someone let me know why it is 0? Or how to fix it? 回答1: It's because you're comparing it to -1 . outputfd doesn't get the result of open . It gets the result of