file-descriptor

Closing/cleaning up “mixed” file descriptors / sockets

坚强是说给别人听的谎言 提交于 2019-12-10 13:32:00
问题 When I create a socket using accept() and make a FILE out of it using fdopen(), what do I have to do to clean everything up? Do I need to do fclose() on the FILE, shutdown() and close() on the socket, or only the shutdown() and or close() or fclose()? If I don't do fclose(), do I have to free() the FILE pointer manually? 回答1: From man fdopen: The file descriptor is not dup’ed, and will be closed when the stream created by fdopen() is closed So I would just use fclose(), which also closes the

Create a file descriptor

坚强是说给别人听的谎言 提交于 2019-12-10 11:44: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" . 回答1: You need dup2() http://linux.die.net/man/2/dup 回答2: 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,

Nginx File descriptor limit

点点圈 提交于 2019-12-10 11:13:07
问题 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

file descriptor leak in java program : too many open files

孤人 提交于 2019-12-10 10:49:51
问题 I have a program which suffer from file descriptor increasing. I see when I execute the command ls -l /proc/5969/fd where 5969 is the pid of the java program the number of file descriptor continuously increasing. but I am unable to open one of those files decriptors to see what file remains open : here is an example of the listing : lrwx------ 1 root root 64 oct 24 16:08 52295 -> socket:[2577706264] lrwx------ 1 root root 64 oct 24 16:08 52296 -> socket:[2579543392] lrwx------ 1 root root 64

Writing to multiple file descriptors with a single function call

左心房为你撑大大i 提交于 2019-12-10 10:39:55
问题 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

Difference between os.close(0) & sys.stdin.close()

我怕爱的太早我们不能终老 提交于 2019-12-10 10:33:09
问题 I'm working on some Python code which is a CGI script called from Apache. The first thing the code does is (I believe) attempt to close stdin/stdout/stderr with the following: for fd in [0, 1, 2]: try: os.close(fd) except Exception: pass Normally this works, however if they're not open, I get a "python.exe has stopped working", "A problem caused the program to stop working correctly" error message (Win32 exception). Couple of questions: What's the difference between closing via os.close

redirect stdout/stderr to file under unix c++ - again

人走茶凉 提交于 2019-12-09 10:55:32
问题 What I want to do redirect stdout and stderr to one or more files from inside c++ Why I need it I am using an external, precompiled third-party library that produces a ridiculous amount of output, which I would like to redirect to a log file to keep the console clean. Conditions Compatibility is not a problem, the code will only run on Unix systems. The redirection should not only affect c++-style printing (std::cout << "hello world" << std::endl), but also c-style printing (printf("hello

C: dup2() before execv

若如初见. 提交于 2019-12-08 11:53:57
问题 For a homework assignment I have to write a basic shell including redirection. The program uses readline to prompt for input, parses the input string, and breaks it down into the executable name, the arguments, and the input/output file(s), if applicable. After parsing the string, it forks and the child execv()'s to the executable that was passed in. I'm using dup2() to change the file descriptors after the fork and before the execv, but am having a problem once the program has execv'd to the

socket.fromfd for Windows

我与影子孤独终老i 提交于 2019-12-08 04:34:32
问题 I am porting a Python-based web server from Linux to Windows. There is a call to socket.fromfd, which is only available in Unix and I've been trying to find a Windows-equivalent with no luck. I came upon WinSock and WSADuplicateSocket, but these don't seem to be available in Python based on this post: Can I use the winsock api from python? I know there is also DuplicateHandle, but it is not reliable for sockets. Is there any reliable way to reproduce socket.fromfd for Windows? 回答1: I think

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

跟風遠走 提交于 2019-12-08 02:01:20
问题 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