file-descriptor

How to find next available file descriptor in Bash?

寵の児 提交于 2019-12-03 12:55:44
问题 How can I figure out if a file descriptor is currently in use in Bash? For example, if I have a script that reads, writes, and closes fd 3, e.g. exec 3< <(some command here) ... cat <&3 exec 3>&- what's the best way to ensure I'm not interfering with some other purpose for the descriptor that may have been set before my script runs? Do I need to put my whole script in a subshell? 回答1: In pure bash , you can use the following method to see if a given file descriptor ( 3 in this case) is

bridging between two file descriptors

若如初见. 提交于 2019-12-03 12:04:13
I have a socket I'm doing select() on it, waiting for other process to write. Once it write, I read the data, and write it to another file descriptor. My question is, if there's a way to bridge the socket to the file descriptor, so when there's data ready, it will be automatically written to the other file descriptor ? This way, I could throw a way the buffer I'm using, and omit a thread in the system. thuovila On linux, using splice() might be more suitable, when the direction is from socket to file. Using splice() is a bit more complicated, but you get both directions. Also, I think sendfile

How to use a variable to indicate a file descriptor in bash?

有些话、适合烂在心里 提交于 2019-12-03 11:53:17
问题 I want to use a bash variable to indicate a file descriptor, like this: id=6 file=a exec $id<>$file But the usage is wrong: -bash: exec: 6: not found So, how to use a variable to indicate a file descriptor in exec command? 回答1: You have to use eval and put the entire expression in quotes. eval "exec $id<>$file" And do that every time you want to use $id . 回答2: The accepted answer is correct, but as of bash 4.1, you can use automatic file descriptor allocation, and in that case you don't need

Golang bad file descriptor

强颜欢笑 提交于 2019-12-03 10:12:11
I am getting a bad file descriptor when trying to append to a logging file within my go routine. write ./log.log: bad file descriptor The file exists and has 666 for permissions. At first I thought well maybe it is because each one of them is trying to open the file at the same time. I implemented a mutex to try and avoid that but got the same issue so I removed it. logCh := make(chan string, 150) go func() { for { msg, ok := <-logCh if ok { if f, err := os.OpenFile("./log.log", os.O_APPEND, os.ModeAppend); err != nil { panic(err) } else { logTime := time.Now().Format(time.RFC3339) if _, err :

Is O_NONBLOCK being set a property of the file descriptor or underlying file?

偶尔善良 提交于 2019-12-03 05:49:23
问题 From what I have been reading on The Open Group website on fcntl, open, read, and write, I get the impression that whether O_NONBLOCK is set on a file descriptor, and hence whether non-blocking I/O is used with the descriptor, should be a property of that file descriptor rather than the underlying file. Being a property of the file descriptor means, for example, that if I duplicate a file descriptor or open another descriptor to the same file, then I can use blocking I/O with one and non

Duplicate file descriptor with its own file offset

不羁的心 提交于 2019-12-03 05:16:00
How can one create a new file descriptor from an existing file descriptor such that the new descriptor does not share the same internal file structure/entry in the file table? Specifically attributes such as file offset (and preferably permissions, sharing and modes) should not be shared between the new and old file descriptors. Under both Windows and Linux, dup() will duplicate the file descriptor, but both descriptors still point to the same file structure in the process' file table. Any seeking on either descriptor will adjust the position for the other descriptors as well. Note I've since

Inter-process communication without FIFOs

偶尔善良 提交于 2019-12-03 05:15:41
问题 Inside a BASH script we can have multiple processes running in background which intercommunicate using named pipes, FIFOs registered on the filesystem. An example of this could be: #!/bin/bash mkfifo FIFO # BG process 1 while :; do echo x; done & >FIFO # BG process 2 while :; do read; done & <FIFO exit I wonder if it's possible to do the same intercommunication between background processes of a script without using a FIFO on filesystem, maybe with some kind of file-descriptor redirection. 回答1

Monitoring file using inotify

喜欢而已 提交于 2019-12-03 03:31:53
I am using inotify to monitor a local file, for example "/root/temp" using inotify_add_watch(fd, "/root/temp", mask). When this file is deleted, the program will be blocked by read(fd, buf, bufSize) function. Even if I create a new "/root/temp" file, the program is still block by read function. I am wondering if inotify can detect that the monitored file is created and the read function can get something from fd so that read will not be blocked forever. Here is my code: uint32_t mask = IN_ALL_EVENTS; int fd = inotify_init(); int wd = inotify_add_watch(fd, "/root/temp", mask); char *buf = new

Where does Ruby keep track of its open file descriptors?

五迷三道 提交于 2019-12-03 02:32:27
What This Question Is Not About This question is not about how to auto-close a file with File#close or the File#open block syntax. It's a question about where Ruby stores its list of open file descriptors at runtime. The Actual Question If you have a program with open descriptors, but you don't have access to the related File or IO object, how can you find a reference to the currently-open file descriptors? Take this example: filename='/tmp/foo' %x( touch "#{filename}" ) File.open(filename) filehandle = File.open(filename) The first File instance is opened, but the reference to the object is

Multiprocessing and sockets in Python

老子叫甜甜 提交于 2019-12-02 21:22:27
I am trying to make multiprocessing and socket programming work together, but, I am stuck at this point. Problem is that, I am getting this error: File "multiprocesssockserv.py", line 11, in worker clientsocket = socket.fromfd(clientfileno, socket.AF_INET, socket.SOCK_STREAM) error: [Errno 9] Bad file descriptor Complete code that causing the error is as following: import multiprocessing as mp import logging import socket logger = mp.log_to_stderr(logging.WARN) def worker(queue): while True: clientfileno = queue.get() print clientfileno clientsocket = socket.fromfd(clientfileno, socket.AF_INET