file-descriptor

Use multiple output stream in python?

痞子三分冷 提交于 2019-12-06 03:12:24
What I am going to do is to create multiple output steams in a python function, and refer them as 1 , 2 , 3 .....: In test.py : def main(): ... print >>fd1, 'words1' print >>fd2, 'words2' print >>fd3, 'words3' ... Redirect it while using: python test.py 1>1.txt 2>2.txt 3>3.txt The content of these files: 1.txt -> words1 2.txt -> words2 3.txt -> words3 The question is, how to create those fd1 , fd2 , fd3 ? Added: I have used this: outfiles = {} for _ in range(3): fd = os.dup(1) outfiles[fd] = os.fdopen(fd, 'w') def main(): for no in outfiles: print >>outfiles[no], "foo" print >>outfiles[no],

General explanation of how epoll works?

☆樱花仙子☆ 提交于 2019-12-06 02:41:28
问题 I'm doing a technical write-up on switching from a database-polling (via synchronous stored procedure call) to a message queue (via pub/sub). I'd like to be able to explain how polling a database is vastly different and much heavier than setting up a connection to a AMQP broker and configuring a message handler. Can someone maybe provide an explanation here, or point me to a good high level tutorial on how epoll works when notifying of new data becoming available on a socket? 回答1: I assume by

How to read exactly one line?

好久不见. 提交于 2019-12-06 02:13:55
问题 I have a Linux file descriptor (from socket), and I want to read one line. How to do it in C++? 回答1: I you are reading from a TCP socket you can't assume when the end of line will be reached. Therfore you'll need something like that: std::string line; char buf[1024]; int n = 0; while(n = read(fd, buf, 1024)) { const int pos = std::find(buf, buf + n, '\n') if(pos != std::string::npos) { if (pos < 1024-1 && buf[pos + 1] == '\n') break; } line += buf; } line += buf; Assuming you are using "\n\n"

Fork, Ruby, ActiveRecord and File Descriptors on Fork

别来无恙 提交于 2019-12-06 01:43:10
问题 I understand that when we fork a process the child process inherits a copy of the parents open file descriptors and offsets. According to the man pages this refers to the same file descriptors used by the parent. Based on that theory in the following program puts "Process #{Process.pid}" file = File.open('sample', 'w') forked_pid = fork do sleep(10) puts "Writing to file now..." file.puts("Hello World. #{Time.now}") end file.puts("Welcome to winter of my discontent #{Time.now}") file.close

How to get file path using file descriptor on Android?

我是研究僧i 提交于 2019-12-06 01:03:43
问题 I need to get file absolute path using file descriptor which was returned by getFileDescriptor(). How can i do it? 回答1: On a system running under a Linux kernel, which would be all current Android implementations, each file descriptor will have an entry in /proc/[processid]/fd which is a symbolic link to the target of the file descriptor - not only for regular files, but also for many other possible targets such as pipes and sockets. Here's a partial example for a cat > /mnt/sdcard/foo

How to see file handles like with “lsof -l”?

回眸只為那壹抹淺笑 提交于 2019-12-05 22:52:54
I did the commands ( source ): $ exec 3>/tmp/thirdfile $ exec 4>/tmp/fourthfile $ echo drib >&3 $ echo drab >&4 $ echo another drib >&3 $ echo another drab >&4 $ exec 3>&- $ exec 4>&- How can I see the file handles, something like with lsof -l ? I don't understand, why not just use lsof: lsof -p $$ $$ being a shell variable that holds the shell's process ID You can also limit to just file descriptors like: lsof -a -d0-65535 -p $$ On Linux, you can do something like ls -l /proc/$$/fd , which will show you what file descriptors are open in your shell. Of course, substitute $$ with other numbers

Bad file descriptor

独自空忆成欢 提交于 2019-12-05 12:51:00
问题 I'm learning about file descriptors and I wrote this code: #include <unistd.h> #include <stdio.h> #include <stdlib.h> #include <fcntl.h> int fdrd, fdwr, fdwt; char c; main (int argc, char *argv[]) { if((fdwt = open("output", O_CREAT, 0777)) == -1) { perror("Error opening the file:"); exit(1); } char c = 'x'; if(write(fdwt, &c, 1) == -1) { perror("Error writing the file:"); } close(fdwt); exit(0); } , but I'm getting: Error writing the file:: Bad file descriptor I don't know what could be

Why does closing file descriptors after fork affect the child process?

孤者浪人 提交于 2019-12-05 10:15:54
I want to run programs in linux by a button click an therefore I wrote a function execute : void execute(const char* program_call, const char* param ) { pid_t child = vfork(); if(child == 0) // child process { int child_pid = getpid(); char *args[2]; // arguments for exec args[0] = (char*)program_call; // first argument is program_call args[1] = (char*)param; // close all opened file descriptors: const char* prefix = "/proc/"; const char* suffix = "/fd/"; char child_proc_dir[16]; sprintf(child_proc_dir,"%s%d%s",prefix,child_pid, suffix); DIR *dir; struct dirent *ent; if ((dir = opendir (child

open() what happens if I open twice the same file?

本秂侑毒 提交于 2019-12-05 09:47:28
If I open the same file twice, will it give an error, or will it create two different file descriptors? For example a = open("teste.txt", O_RDONLY); b = open("teste.txt", O_RDONLY); In this case, since you're opening both files as read-only, you will get two different file descriptors that refer to the same file. See the man page for open for more details. To complement what @Drew McGowen has said, In fact, in this case, when you call open() twice on the same file, you get two different file descriptors pointing to the same file (same physical file). BUT , the two file descriptors are

Bash double process substitution gives bad file descriptor

孤者浪人 提交于 2019-12-05 04:10:22
When I try to refer to two process substitution pipes in a bash function, only the first one referenced works. The second one gives a "bad file descriptor" error like so: $ foo(){ > cat "$1" > cat "$2" > } $ foo <(echo hi) <(echo bye) hi cat: /dev/fd/62: Bad file descriptor $ It appears that the second pipe is dropped once one is referenced, but a) I cannot seem to confirm this behavior in any documentation and b) I wish it wouldn't. =) Any ideas on what I'm doing wrong? FWIW I'm doing this to make a wrapper to use Mac OS X's FileMerge graphical diff tool instead of the command line one, which