file-descriptor

two file descriptors to same file

拟墨画扇 提交于 2019-11-28 18:25:21
Using the posix read() write() linux calls, is it guaranteed that if I write through one file descriptor and read through another file descriptor, in a serial fashion such that the two actions are mutually exclusive of each other... that my read file descriptor will always see what was written last by the write file descriptor? i believe this is the case, but I want to make sure and the man page isn't very helpful on this It depends on where you got the two file descriptors. If they come from a dup(2) call, then they share file offset and status, so doing a write(2) on one will affect the

Why is select used in Linux

亡梦爱人 提交于 2019-11-28 18:12:05
I was going through a serial program and I observed that they use select() before using read() . Why exactly is this required. Why cant we just directly call read() and check if it fails or not ? Also why do we have to increment the file descriptor by 1 and pass it while I am passing the file descriptor set already to select() ? Example: r=select(fd+1, &fds, NULL, NULL, &timeout); where fds already has the value of fd The select() system call tells you whether there is any data to read on the file descriptors that you're interested in. Strictly, it is a question of whether a read operation on

How do linux file descriptor limits work?

有些话、适合烂在心里 提交于 2019-11-28 17:16:20
问题 I was told that my server refused to accept client network connections at a specific port could be due to the lack of file descriptors. I looked up what this is all about and read about it here: http://www.netadmintools.com/art295.html So I tested my system and I got this: cat /proc/sys/fs/file-nr 1088 0 331287 What does this mean? My limit is quite high yet I have 0 available file descriptors? why? How do I solve this for my server? The second column actually stays at 0 even after I shutdown

How to redirect an output file descriptor of a subshell to an input file descriptor in the parent shell?

瘦欲@ 提交于 2019-11-28 17:14:37
(In BASH) I want a subshell to use a non-STDOUT non-STDERR file descriptor to pass some data back to the parent shell. How can I do that? Eventually I would love to save the data into some variable of the parent shell. ( # The following two lines show the behavior of the subshell. # We cannot change them. echo "This should go to STDOUT" echo "This is the data I want to pass to the parent shell" >&3 ) #... data_from_subshell=... # Somehow assign the value of &3 of the # subshell to this variable EDIT: The subshell runs a black-box program that writes to STDOUT and &3. Jan Hudec BEWARE, BASHISM

How to retrieve the user name from the user ID

纵然是瞬间 提交于 2019-11-28 13:35:32
I am implementing the (ls) command on Unix while learning from a book. During the coding part of my implementation of the (ls) command with the (-l) flag , I see that I have to prompt the user and group names of the file. So far I have the user and group IDs from the following lines: struct stat statBuf; statBuf.st_uid; //For the user id. statBuf.st_gid; //For the group id. In the default (ls) command on Unix, the information of the file is printed in such a way that the user name is shown instead of the user id. Can anyone help me to find the correct methodology to retrieve the user and group

Windows equivalent of ulimit -n

泄露秘密 提交于 2019-11-28 12:37:36
What is the windows equivalent of the unix command " ulimit -n" ? Basically, i want to set the maximum fd limit via command prompt. 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-site, serverfault.com - coming soon for this type of question. I don't believe that current Windows O/S have a

C read and thread safety (linux)

江枫思渺然 提交于 2019-11-28 09:15:23
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 same for other kernels (like BSDs) ? Edit : According to the close documentation , we should be sure

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

旧时模样 提交于 2019-11-28 08:36:57
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 the sense that when they start reading from the pty they get only the most recent data. In other words

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

生来就可爱ヽ(ⅴ<●) 提交于 2019-11-28 07:36:14
问题 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

FIFO pipe is always readable in select()

ε祈祈猫儿з 提交于 2019-11-28 07:20:31
问题 In C pseudo-code: while (1) { fifo = open("fifo", O_RDONLY | O_NONBLOCK); fd_set read; FD_SET(fifo, &read); select(nfds, &read, NULL, NULL, NULL); } The process sleeps as triggered by select() until another process writes into fifo . Afterwards it will always find fifo as a readable file descriptor. How to avoid this behavior (that is, after fifo has been read once, how to make it be found as unreadable until it gets another write?) 回答1: You opened that FIFO as read only (O_RDONLY), whenever