fcntl

fcntl 加锁模块

与世无争的帅哥 提交于 2019-12-02 23:43:48
#!/usr/bin/python # coding:utf8 import os import sys import time import fcntl # 导入模块 class FLOCK(object): def __init__(self, name): """ :param name: 文件名 """ self.fobj = open(name, 'w') self.fd = self.fobj.fileno() def lock(self): try: fcntl.lockf(self.fd, fcntl.LOCK_EX | fcntl. LOCK_NB) # LOCK_NB: 使用了fcntl.LOCK_NB,已有进程对该文件已加锁,本进程得不到锁时直接退出,不阻塞。如果不加非阻塞参数,得不到锁就卡在这里一直傻等着直到拿到锁 print('给文件加锁,稍等 ... ...') time.sleep(20) return True except Exception as e : print('文件加锁,无法执行,请稍后运行。\n',e) return False def unlock(self): self.fobj.close() print('已解锁') if __name__ == "__main__": locker = FLOCK(sys.argv[1]) a

加载pwn模块时报错与解决

匿名 (未验证) 提交于 2019-12-02 23:35:02
错误一: File "D:\Program Files\Python37\lib\site-packages\pwnlib\term\term.py", line 167 SyntaxError: invalid syntax 原因: python3不再支持元组作为函数参数。 解决方案: 打开term.py,修改部分代码,去除元组参数。 def goto(r, c): #改了这里 do('cup', r - scroll + height - 1, c) def render_from(i, force = False, clear_after = False): e = None # `i` should always be a valid cell, but in case i f***ed up somewhere, I'll # check it and just do nothing if something went wrong. if i < 0 or i >= len(cells): return goto(cells[i].start[0],cells[i].start[1]) #改了这里 for c in cells[i:]: if not force and c.start == e: goto(cells[-1].end[0],cells[-1].end[1

python 中给文件加锁

匿名 (未验证) 提交于 2019-12-02 22:51:30
python 中给文件加锁―― fcntl 模块 import fcntl 打开一个文件##当前目录下 test 文件要先存在,如果不存在会报错。或者以写的方式打开 f = open ( './test' )对该文件加密: fcntl . flock ( f , fcntl . LOCK_EX )这样就对文件 test 加锁了,如果有其他进程对 test 文件加锁,则不能成功,会被阻塞,但不会退出程序。解锁: fcntl . flock ( f , fcntl . LOCK_UN ) fcntl 模块: flock () : flock ( f , operation ) operation : 包括: fcntl . LOCK_UN 解锁 fcntl . LOCK_EX 排他锁 fcntl . LOCK_SH 共享锁 fcntl . LOCK_NB 非阻塞锁 LOCK_SH 共享锁:所有进程没有写访问权限,即使是加锁进程也没有。所有进程有读访问权限。 LOCK_EX 排他锁:除加锁进程外其他进程没有对已加锁文件读写访问权限。 LOCK_NB 非阻塞锁:如果指定此参数,函数不能获得文件锁就立即返回,否则,函数会等待获得文件锁。 LOCK_NB 可以同 LOCK_SH 或 LOCK_NB 进行按位或(|)运算操作。 fcnt . flock ( f , fcntl . LOCK_EX

Linux五种IO模型

匿名 (未验证) 提交于 2019-12-02 21:56:30
Ŀ¼ Linux下可用的IO模型有5种,分别是: 阻塞式IO 非阻塞式IO IO复用 信号驱动式IO(SIGIO) 异步IO(Posix的aio_系列函数) 其中, 除了异步IO,其余都属于同步IO模型。 在这5种模型中,我们目前只关注前3种,并且把IO复用放在网络编程专题中讲,本文只简单介绍阻塞式IO和非阻塞式IO的概念与区别。 阻塞式IO是Linux中最基本、最常用的IO模型,指的是可能会使进程永远阻塞的函数, 一般表现为: 进程或线程调用某个函数,该函数需要满足特定条件才能向下执行 如果条件不满足,则会使调用进程或线程阻塞,让出CPU控制权,并一直持续到条件满足为止 在Linux中,阻塞式IO一般作为默认属性出现, 如mq_receive、sem_wait、sem_post等 在默认情况下,所有的套接字都是阻塞的, 我们以UDP套接字为例来展示阻塞式IO模型,如下图所示。 进程调用recvfrom接收数据,但由于内核还未准备好,进程就会阻塞;直到内核准备好数据,recvfrom完成数据复制工作,进程才能解除阻塞状态。 顾名思义, 非阻塞式IO不会使调用进程或线程永远阻塞, 具体表现为: 如果IO操作不能完成,则立即出错返回, 调用进程或线程继续向下执行。 对于一个给定的描述符,有两种将其指定为非阻塞式IO的方法: 调用open创建或打开文件时指定O_NONBLOCK标志

重读APUE(4)-fcntl和ioctl的区别

有些话、适合烂在心里 提交于 2019-12-02 21:13:48
fcntl(File Control)-文件控制 ioctl(In/Out Control)-I/O控制 1. fcntl 作用于文件,提供对文件的基础控制; ioctl 作用于文件和设备对象,一般用来向设备发送命令,或者控制设备属性; 2. fcntl 是系统预先定义好的命令选项,不能自定义; ioctl 可以通过驱动程序自定义,驱动程序中可以通过file_operations->unlocked_ioctl实现针对特定设备的定制命令控制; 来源: https://www.cnblogs.com/wanpengcoder/p/11762723.html

Is O_NONBLOCK being set a property of the file descriptor or underlying file?

喜夏-厌秋 提交于 2019-12-02 19:12:13
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-blocking I/O with the other. Experimenting with a FIFO, however, it appears that it is not possible to

Having issues with flock() function

懵懂的女人 提交于 2019-12-01 14:03:04
I have a question about how flock() works, particularly in python. I have a module that opens a serial connection (via os.open() ). I need to make this thread safe. It's easy enough making it thread safe when working in the same module using threading.Lock() , but if the module gets imported from different places, it breaks. I was thinking of using flock() , but I'm having trouble finding enough information about how exactly flock works. I read that flock() unlocks the file once the file is closed. But is there a situation that will keep the file open if python crashes? And what exactly is

Is a Java FileLock a POSIX advisory (fcntl) lock

徘徊边缘 提交于 2019-12-01 09:24:33
I have a C++ program that locks files using POSIX advisory locks. That is, it uses the POSIX fcntl system call for lock operations. I want a Java program to interoperate with that C++ program, so I want my Java program to also use POSIX advisory locks. File locking in Java should use the standard FileLock class. But the API documentation is understandably vague on just how locking is implemented: This file-locking API is intended to map directly to the native locking facility of the underlying operating system. Thus the locks held on a file should be visible to all programs that have access to

Why fcntl(fd, F_SETFL, 0) use in serial port programming

只愿长相守 提交于 2019-12-01 07:36:00
I am starting serial port programming in Linux. After reading several examples on the web, I don't understand exact effect of fcntl(fd, F_SETFL, 0) ? It is clearing bits, but what flags does it affect? What does it set and or clear? Take one by one 1) Function call used fcntl() - It perform the operation on file descriptor passed in argument. 2) 2nd argument in call F_SETFL (int) Set the file status flags to the value specified by arg. File access mode (O_RDONLY, O_WRONLY, O_RDWR) and file creation flags (i.e., O_CREAT, O_EXCL, O_NOCTTY, O_TRUNC) in arg are ignored. On Linux this command can

Is a Java FileLock a POSIX advisory (fcntl) lock

谁都会走 提交于 2019-12-01 06:24:11
问题 I have a C++ program that locks files using POSIX advisory locks. That is, it uses the POSIX fcntl system call for lock operations. I want a Java program to interoperate with that C++ program, so I want my Java program to also use POSIX advisory locks. File locking in Java should use the standard FileLock class. But the API documentation is understandably vague on just how locking is implemented: This file-locking API is intended to map directly to the native locking facility of the