file-descriptor

Strange behavior performing library functions on STDOUT and STDIN's file descriptors

旧城冷巷雨未停 提交于 2019-11-29 13:41:01
Throughout my years as a C programmer, I've always been confused about the standard stream file descriptors. Some places, like Wikipedia [1] , say: In the C programming language, the standard input, output, and error streams are attached to the existing Unix file descriptors 0, 1 and 2 respectively. This is backed up by unistd.h : /* Standard file descriptors. */ #define STDIN_FILENO 0 /* Standard input. */ #define STDOUT_FILENO 1 /* Standard output. */ #define STDERR_FILENO 2 /* Standard error output. */ However, this code (on any system): write(0, "Hello, World!\n", 14); Will print Hello,

C's printf and fprintf(stdout,) are not printing

穿精又带淫゛_ 提交于 2019-11-29 13:13:08
This is a bit of an odd one. My code wasn't outputting what I thought it should. I added some print statements at various stages to see where it was going wrong. Still nothing. So I added a printf statement at the start of main. That's where I got really confused. So I presumed something funny was happening with the file descriptors. I changed the printf to a fprintf . Still nothing. Printing to stderr with fprintf does work! Why is this happening? Removing all of the body from main except the initial print statement and the return does print. Code int main(void) { fprintf(stdout, "STARTED!");

Read/write from file descriptor at offset

守給你的承諾、 提交于 2019-11-29 10:47:22
I've been using the read(2) and write(2) functions to read and write to a file given a file descriptor. Is there any function like this that allows you to put an offset into the file for read/write? Yes, you're looking for lseek . http://linux.die.net/man/2/lseek There are pread/pwrite functions that accept file offset: ssize_t pread(int fd, void *buf, size_t count, off_t offset); ssize_t pwrite(int fd, const void *buf, size_t count, off_t offset); Yes. You use the lseek function in the same library . You can then seek to any offset relative to the start or end of file, or relative to the

How to pass file descriptors from parent to child in python?

与世无争的帅哥 提交于 2019-11-29 10:45:33
I am using multiprocessing module, and using pools to start multiple workers. But the file descriptors which are opened at the parent process are closed in the worker processes. I want them to be open..! Is there any way to pass file descriptors to be shared across parent and children? There isn't a way that I know of to share file descriptors between processes. If a way exists, it is most likely OS specific. My guess is that you need to share data on another level. On Python 2 and Python 3, functions for sending and receiving file descriptors exist in multiprocessing.reduction module. Example

How can I read from a specific raw file descriptor in Rust?

烂漫一生 提交于 2019-11-29 07:34:43
Editor's note: This question is for a version of Rust prior to 1.0. Some answers have been updated to cover Rust 1.0 or up, but not all. I am writing a systemd socket activated service in Rust. My process is being handed an open file descriptor by systemd. Are there any Rust IO functions that take a raw file descriptor? I'm using a Rust nightly before Rust 1.0. I think right now your best bet is probably using the libc crate for working with raw file descriptors. The movement of FileDesc to private scope was fallout from the runtime removal a few months back. See this RFC for some more context

Why does poll keep returning although there is no input?

你离开我真会死。 提交于 2019-11-29 07:25:25
I wrote a small test program to figure out how to talk to poll . I created three files testa , testb , testc and wrote the string hello\n to the first. So, here is my invocation of poll : poll(polls.data(),polls.size(),-1) According to the manpage, a timeout of -1 should indicate that the syscall never times out. However, it keeps returning without having anything to read. I always consume one byte of the input and can see the hello\n being printed, but poll doesn't stop there. It just keeps on pretending there to be something to read. #include <sys/types.h> #include <sys/stat.h> #include <sys

Connection pool and File handles

旧时模样 提交于 2019-11-29 03:59:48
问题 We use Retrofit/OkHttp3 for all network traffic from our Android application. So far everything seems to run quite smoothly. However, we have now occasionally had our app/process run out of file handles. Android allows for a max of 1024 file handles per process OkHttp will create a new thread for each async call Each thread created this way will (from our observation) be responsible for 3 new file handles (2 pipes and one socket). We were able to debug this exactly, where each dispached async

How to use sendmsg() to send a file-descriptor via sockets between 2 processes?

穿精又带淫゛_ 提交于 2019-11-29 02:26:50
After @cnicutar answers me on this question , I tried to send a file-descriptor from the parent process to its child. Based on this example , I wrote this code: int socket_fd ,accepted_socket_fd, on = 1; int server_sd, worker_sd, pair_sd[2]; struct sockaddr_in client_address; struct sockaddr_in server_address; /* ======================================================================= * Setup the network socket. * ======================================================================= */ if((socket_fd = socket(AF_INET, SOCK_STREAM, 0)) < 0) { perror("socket()"); exit(EXIT_FAILURE); } if(

Why do operating systems limit file descriptors?

人盡茶涼 提交于 2019-11-29 02:17:53
问题 I ask this question after trying my best to research the best way to implement a message queue server. Why do operating systems put limits on the number of open file descriptors a process and the global system can have? My current server implementation uses zeromq, and opens a subscriber socket for each connected websocket client. Obviously that single process is only going to be able to handle clients to the limit of the fds. When I research the topic I find lots of info on how to raise

how is select() alerted to an fd becoming “ready”?

荒凉一梦 提交于 2019-11-28 18:46:19
I don't know why I'm having a hard time finding this, but I'm looking at some linux code where we're using select() waiting on a file descriptor to report it's ready. From the man page of select: select() and pselect() allow a program to monitor multiple file descriptors, waiting until one or more of the file descriptors become "ready" for some class of I/O operation So, that's great... I call select on some descriptor, give it some time out value and start to wait for the indication to go. How does the file descriptor (or owner of the descriptor) report that it's "ready" such that the select(