file-descriptor

Bash, overwrite using file descriptor?

。_饼干妹妹 提交于 2019-12-07 18:43:34
问题 If you want to overwrite a file with Bash, this is easy echo "Hello world" > hosts This does not seem to work with a file descriptor $ exec 3<> hosts $ echo "Hello world" >&3 $ cat hosts Hello world $ echo "Hello world" >&3 $ cat hosts Hello world Hello world 回答1: That's correct. The mode in which a file is opened is determined when the shell calls open(2) . When you DUP2 an FD (in any language), the flags that were set when the file was opened are shared between open FDs. In your case, O

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

泪湿孤枕 提交于 2019-12-07 06:28:55
问题 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); 回答1: 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. 回答2: To complement what @Drew McGowen has said, In fact, in this case, when you call open() twice on the same file, you get two

How to determine if a file descriptor is seekable?

一个人想着一个人 提交于 2019-12-06 19:20:53
问题 Is there any portable way (on POSIX systems) to determine if a file descriptor is seekable? My thought is to use lseek(fd, 0, SEEK_CUR); and check if the return value is -1, but I'm uncertain if this could give false negatives or false positives. Using fstat and making assumptions about what types of files are seekable/nonseekable does not sound like a good idea. Any other ideas? 回答1: The lseek method seems reasonable. It certainly can't cause a false negative - if it did, something is

How to redirect stdout to stdin in Matlab? Aka how to manage file-descriptors?

我只是一个虾纸丫 提交于 2019-12-06 16:26:23
问题 My peers have created an extremely-hard-to-test novel structure where they use stdout for outputting results. Now I cannot change their code and I need to use their functions. Suppose a function mlfpprint that uses stdout for displaying results -- now I need to check whether its results are correct so I need to get the stdout to a variable for comparison with the correct result. So How can I manage file-descriptors in Matlab? For example, how can I get stdout to stdin? 回答1: One idea would be

Create a file descriptor

冷暖自知 提交于 2019-12-06 15:12:48
I want to create a file descriptor in C whose value i will specify in code. I have a integer variable which specifies the value of file descriptor to be created. For example i may need a file descriptor whose value is 5 and later associate that with the file named "sample.dat" . You need dup2() http://linux.die.net/man/2/dup fd = open ("sample.dat", O_RDONLY); open the file dup2 (fd, 5); and copy the file descriptor fd into the descriptor number 5 now you can do read (5, buffer, BUFF_MAX); or also use fd to access the same file. You need to close the fd explicitly if you do not need it. As

Share a file descriptor between parent and child after fork and exec

有些话、适合烂在心里 提交于 2019-12-06 12:27:28
I have two processes on Linux, A & B. I want to share the file descriptor from process A with process B, now I just serialize it to a char* and pass it to the execl parameters, but that doesn't work. A.c looks like this: union descriptor{ char c[sizeof(int)]; int i; } fd; pid_t pid; fd.i = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); // Perform other socket functions pid = fork(); if(pid == 0){ // Read data from socket if(execl("./B", fd.c, NULL) < 0){ exit(EXIT_FAILURE); }else( exit(EXIT_SUCCESS); } }else if(pid < 0){ exit(EXIT_FAILURE); }else{ waitpid(pid, NULL, 0); } B.c looks like this:

Writing to multiple file descriptors with a single function call

时光怂恿深爱的人放手 提交于 2019-12-06 11:45:24
I had a use case for a group chat server where the server had to write a common string to all clients' socket. I had then addressed this by looping through the list of file descriptors and writing the string to each of the file descriptors. Now I am thinking of finding a better solution to the problem. Is it possible to do this by a single function call from the server by using the tee system call in linux. I want the output of one tee to go to the next tee as well to a clients socket. I am wondering if I can dup the file descriptor of one end of the tee to the clients socket and get the

Bash, overwrite using file descriptor?

一曲冷凌霜 提交于 2019-12-06 08:31:18
If you want to overwrite a file with Bash, this is easy echo "Hello world" > hosts This does not seem to work with a file descriptor $ exec 3<> hosts $ echo "Hello world" >&3 $ cat hosts Hello world $ echo "Hello world" >&3 $ cat hosts Hello world Hello world That's correct. The mode in which a file is opened is determined when the shell calls open(2) . When you DUP2 an FD (in any language), the flags that were set when the file was opened are shared between open FDs . In your case, O_TRUNC can only be specified when the file is actually opened. The important thing to know is that the mode and

Nginx File descriptor limit

假装没事ソ 提交于 2019-12-06 08:25:29
How can I increase the file descriptors limit in nginx? There are several ways to increase file descriptors: Edit /etc/security/limits.conf and set nofile soft and hard limits for the nginx user. Set $ULIMIT in /etc/default/nginx. The nginx init.d script set ulimit $ULIMIT https://gist.github.com/aganov/1121022#file-nginx-L43 Set worker_rlimit_nofile in nginx.conf http://wiki.nginx.org/NginxHttpMainModule#worker_rlimit_nofile Does setting limits in limits.conf affect nginx when started using init.d script on boot? Do I have to use ulimit $ULIMIT in the init script or can worker_rlimit_nofile

Could an existing fd always be duplicated both as input and output?

*爱你&永不变心* 提交于 2019-12-06 08:23:59
问题 In bash, once a fd is in use (either as input or output): exec 7>AFile It seems that that fd number could be duplicated, both as input or output: true <&7; echo $? true >&7; echo $? The tests could be repeated for a variable as: fd=7 rco="$(true >&${fd} 2>/dev/null; echo $?)" rci="$(true <&${fd} 2>/dev/null; echo $?)" And both exit values joined in one word as: [[ "$rco$rci" = "11" ]] && echo "The fd number $fd is free" The question is: Under which conditions will the exit value of "$rco$rci"