fcntl

linux fcntl - unsetting flag

穿精又带淫゛_ 提交于 2019-12-10 13:51:44
问题 How do i unset a already set flag using fcntl? For e.g. I can set the socket to nonblocking mode using fcntl(sockfd, F_SETFL, flags | O_NONBLOCK) Now, i want to unset the O_NONBLOCK flag. I tried fcntl(sockfd, F_SETFL, flags | ~O_NONBLOCK). It gave me error EINVAL 回答1: int oldfl; oldfl = fcntl(sockfd, F_GETFL); if (oldfl == -1) { /* handle error */ } fcntl(sockfd, F_SETFL, oldfl & ~O_NONBLOCK); Untested, but hope this helps. :-) 回答2: val = fcntl(fd, F_GETFL, 0); flags = O_NONBLOCK; val &=

How to check if a file is locked or not?

拟墨画扇 提交于 2019-12-10 13:25:37
问题 I have the following code where I want to check if the file is locked or not. If not then I want to write to it. I am running this code by running them simultaneously on two terminals but I always get "locked" status every time in both tabs even though I haven't locked it. The code is below: #include <fcntl.h> #include <stdio.h> #include <unistd.h> int main() { struct flock fl,fl2; int fd; fl.l_type = F_WRLCK; /* read/write lock */ fl.l_whence = SEEK_SET; /* beginning of file */ fl.l_start =

文件IO

允我心安 提交于 2019-12-05 17:56:58
目录 文件IO 文件描述符 获取最大支持的描述符 open/openat/creat close lseek off_t 类型说明 lseek与OAPPEND的区别 内核维护的文件信息 dup/dup2 FD_CLOEXEC 缓存同步到存储 fcntl改变文件属性 ioctl 文件截断 代码附录 获取最大文件描述符 使用openat来实现一种相对路径的打开 lseek测试管道等 lseek文件跨越写 fcntl获取文件状态 title: 文件IO date: 2019/11/23 10:49:52 toc: true --- 文件IO 文件描述符 文件描述符是非负的整数,一般是系统调用的,这个与 file_struct 区别开来. STDIN_FILENO, STDOUT_FILENO, and STDERR_FILENO 被定义在 <unistd.h> 获取最大支持的描述符 新的 linux 已经不支持 OPEN_MAX 来直接获取这个最大描述符了, sysconf(_SC_OPEN_MAX) shell 下这么查看,其中的 open files (-n) 1024 就是了 reallin@ubuntu:/work/pan/apue$ ulimit -a core file size (blocks, -c) 0 data seg size (kbytes, -d)

How do I atomically create a locked file in Linux?

半世苍凉 提交于 2019-12-05 16:50:48
问题 Scenario: I have many processes running that need to fetch files over the net. If the file is already downloaded, I want it cached on disk. If another process is downloading the file, block until it is finished downloading. I've been trying to find the easiest way to do to this. The obvious way is to: create file w/ an exclusive lock active on it only if it doesn't exist (O_CREAT | O_EXCL) if file exists already: open file and acquire exclusive lock else: download to newly created file

How to properly convert a C ioctl call to a python fcntl.ioctl call?

旧巷老猫 提交于 2019-12-04 03:57:27
Following an example on resetting a serial port in Linux I wanted to translate the following snippet fd = open(filename, O_WRONLY); ioctl(fd, USBDEVFS_RESET, 0); close(fd); into valid python code. Here is what I have tried so far file_handler = open(self._port, 'w') fcntl.ioctl(file_handler, termios.USBDEVFS_RESET) file_handler.close() which ends with an error 'module' object has no attribute 'USBDEVFS_RESET' . The termios documentation is not very helpful in this point, as it does not list the possible properties of termios . See also the fcntl documentation for an example of such a termios

How can I make a non-blocking request for an exclusive lock using File#flock?

我的未来我决定 提交于 2019-12-03 12:13:45
问题 How Should I Request a Non-Blocking Lock? Why doesn't Ruby's File#flock work as expected when separate attempts are made to lock a file? Locking the file in a block is not the correct solution for this issue because the point is to show the behavior of locking on persistent locks. Using File#flock inside a block releases the lock when the block exits, so it doesn't demonstrate the problem properly. File#flock fails in a variety of ways, especially when requesting a non-blocking lock. Some

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

How can I make a non-blocking request for an exclusive lock using File#flock?

你说的曾经没有我的故事 提交于 2019-12-03 02:40:12
How Should I Request a Non-Blocking Lock? Why doesn't Ruby's File#flock work as expected when separate attempts are made to lock a file? Locking the file in a block is not the correct solution for this issue because the point is to show the behavior of locking on persistent locks. Using File#flock inside a block releases the lock when the block exits, so it doesn't demonstrate the problem properly. File#flock fails in a variety of ways, especially when requesting a non-blocking lock. Some examples follow. Failing Examples with File#flock Infinite wait when using multiple exclusive locks, since

How to do “hit any key” in python?

匿名 (未验证) 提交于 2019-12-03 02:26:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: How would I do a "hit any key" (or grab a menu option) in Python? raw_input requires you hit return. Windows msvcrt has getch() and getche(). Is there a portable way to do this using the standard libs? 回答1: try: # Win32 from msvcrt import getch except ImportError: # UNIX def getch(): import sys, tty, termios fd = sys.stdin.fileno() old = termios.tcgetattr(fd) try: tty.setraw(fd) return sys.stdin.read(1) finally: termios.tcsetattr(fd, termios.TCSADRAIN, old) 回答2: try: os.system('pause') #windows, doesn't require enter except whatever_it_is:

高级IO:五种IO模型

匿名 (未验证) 提交于 2019-12-03 00:04:02
五种IO模型: 一.在总结五种IO模型之前我们了解一下什么是IO??? I表示input,O表示output,合在一起就是IO―表示输入输出设备;每个设备都有一个专用的IO地址,用来处理自己的输入输出信息; 需要注意:IO地址绝对不能有重复,如果两个IO地址有冲突则会造成系统硬件不能正常工作; 二. IO模型: 对于一次IO访问(以read举例),数据会先被拷贝到操作系统内核的缓冲区中, 然后才会从操作系统内核的缓冲区拷贝到应用程序的地址空间。 所以说,当一个read操作发生时,它会经历两个阶段: (1) 等待数据准备 (2)将数据从内核拷贝到进程中 在网络编程环境中,一次IO操作主要包括两个部分: 等数据准备 拷贝数据 所以如果想要提高IO效率,就应该想办法让等的比重减少。 Linux下常见五种IO模型分类: 三.五种IO模型及基本概念: 1.阻塞IO: 在内核将数据准备好之前, 系统调用会一直等,所有的套接字, 默认都是阻塞方式; 具体的过程: (1)当用户进程调用了recvfrom这个系统调用,kernel就开始了IO的第一个阶段: 准备数据(对于网络IO来说,很多时候数据在一开始还没有到达;比如,还没有收到一个完整的UDP包。这个时候kernel就要等待足够的数据到来);这个过程需要等待,也就是说数据被拷贝到操作系统内核的缓冲区中是需要一个过程的;而在用户进程这边