file-descriptor

Monitoring file using inotify

喜你入骨 提交于 2019-12-04 09:52:23
问题 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

Where does Ruby keep track of its open file descriptors?

早过忘川 提交于 2019-12-04 09:13:50
问题 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)

socket: Too many open files (24) apache bench lighttpd

眉间皱痕 提交于 2019-12-04 07:40:58
问题 When I start Apache Bench test: ab -n 10000 -c 1300 http://domain.com/test.php I get error: socket: Too many open files (24) When i change to '-c 1000' it works fine. Because I can have more than 1000 concurrent users I would like to fix socket too many open files problem or increase parameter. How to do this and where? I use lighttpd on centos 5. 回答1: ulimit -n 10000 That might not work depending on you system configuration Consult this to configure your system. 回答2: to permernent change max

Fork, Ruby, ActiveRecord and File Descriptors on Fork

余生颓废 提交于 2019-12-04 06:45:14
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 file = nil Question 1: Shouldn't the forked process which is sleeping for 10 seconds lose its file

How to read exactly one line?

Deadly 提交于 2019-12-04 06:29:04
I have a Linux file descriptor (from socket), and I want to read one line. How to do it in C++? 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" as a delimiter. (I didn't test that code snippet ;-) ) On a UDP socket, that is another story. The emiter

How to get file path using file descriptor on Android?

可紊 提交于 2019-12-04 05:57:34
I need to get file absolute path using file descriptor which was returned by getFileDescriptor() . How can i do it? 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 process running on an android device $ ls -l /proc/3528/fd lrwx------ shell shell 2013-06-06 10:31 0 -> /dev/pts

fopen and getting system file descriptor

不羁岁月 提交于 2019-12-04 03:31:52
问题 I want to get a system file descriptor of the returned resource when I open a file using open. I assume the descriptor is an INT value which is normally inside /dev/fd/ I know that I can read from the descriptor by doing something like: fread("php://fd/$descriptor", $buflen); But now I want to get the descriptor for a resource opened by PHP's fopen (). Is there a way? 回答1: This is a rather hacky way around it but it works! function fd($realpath) { $dir = '/proc/self/fd/'; if ($dh = opendir(

Duplicate file descriptor with its own file offset

醉酒当歌 提交于 2019-12-03 15:51:50
问题 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

passing a file descriptor to a C library function through ctypes on windows

最后都变了- 提交于 2019-12-03 13:26:55
问题 I am trying to pass a file descriptor through ctypes, to a C function where writes are performed on the fd. On linux it works. On windows it doesn't and I don't understand why (I have no experience as a developer on windows) //C func signature: void fun(struct bah *opaque, int fd) from python (details ommited): mylib.fun.argtypes = [POINTER(bah), c_int] fh = open(filename,'wb') #doesn't work on windows, works on linux/unix mylib.fun(some_ctypes_struct, fh.fileno()) #doesn't work on windows

On Windows/mingw, what is the equivalent of `fcntl(fd, F_GETFL) | O_ACCMODE`?

落爺英雄遲暮 提交于 2019-12-03 13:13:06
I am compiling a program on Windows with Mingw. How can I get the access mode for an open file descriptor? sqykly According to Win32.hlp, the API supplies the function BOOL GetFileInformationByHandle(HANDLE hFile, LPBY_HANDLE_FILE_INFORMATION lpFileInformation) in KERNEL32. LPBY_HANDLE_FILE_INFORMATION is a BY_HANDLE_FILE_INFORMATION* , where BY_HANDLE_FILE_INFORMATION is as follows: typedef struct _BY_HANDLE_FILE_INFORMATION { // bhfi DWORD dwFileAttributes; FILETIME ftCreationTime; FILETIME ftLastAccessTime; FILETIME ftLastWriteTime; DWORD dwVolumeSerialNumber; DWORD nFileSizeHigh; DWORD