fcntl

python 中给文件加锁

安稳与你 提交于 2019-12-01 02:31:37
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|fcntl.LOCK_NB)请看事例: 1 import sys 2 import time 3 import fcntl 4 5

Python fcntl does not lock as expected

吃可爱长大的小学妹 提交于 2019-11-30 10:57:28
On a Debian-based OS (Ubuntu, Debian Squeeze), I'm using Python (2.7, 3.2) fcntl to lock a file. As I understand from what I read, fnctl.flock locks a file in a way, that an exception will be thrown if another client wants to lock the same file. I built a little example, which I would expect to throw an excepiton, since I first lock the file, and then, immediately after, I try to lock it again: #!/usr/bin/env python # -*- coding: utf-8 -*- import fcntl fcntl.flock(open('/tmp/locktest', 'r'), fcntl.LOCK_EX) try: fcntl.flock(open('/tmp/locktest', 'r'), fcntl.LOCK_EX | fcntl.LOCK_NB) except

F_SETPIPE_SZ undeclared

旧时模样 提交于 2019-11-30 09:04:01
问题 I have included following headers: #include <stdio.h> #include <stdlib.h> #include <string.h> #include <errno.h> #include <sys/types.h> #include <sys/stat.h> #include <unistd.h> #include <fcntl.h> I have also tried to use #define _GNU_SOURCE before #include <unistd.h> , but it also does not help. I try to use fcntl and pass it F_SETPIPE_SZ as second argument, but I keep getting this error message: error: ‘F_SETPIPE_SZ’ undeclared (first use in this function) I actually found out that I don't

专题 17 SOCKET并发程序设计

独自空忆成欢 提交于 2019-11-29 21:40:17
非阻塞并发模型 函数 fcntl 设置套接字描述符的 O_NONBLOCK 标志后,即可将 I/O 方式更改为非阻塞方式。此时函数 read,recv,recvfrom,write, send 以及 accept 在调用不成功后立即返回。 设置套接字描述符 nSock 设置为非阻塞方式: int nType; nType = fcntl(nSock, F_GETFL, 0); fcntl(nSock, F_SETFL,nType | O_NONBLOCK); 设置阻塞套接字程序设计的基本流程: 非阻塞套接字的程序一般包含一段循环代码,在循环代码中采用轮询的方式,分别调用套接字的输入、输出、申请连接或连接处理函数,从而达到并发处理多个套接字的目的。 /**************************************************************************/ /********************套接字的非阻塞并发实例********************************/ /**************************************************************************/ //定义设置非阻塞模式宏 #define Fsetnonblock(a) \ {\ nType =

linux常见指令

我是研究僧i 提交于 2019-11-29 19:00:18
lseek tar fcntl shutdown及其他关机 init dd aptitude及其他安装 查找命令which、whereis、locate、find grep lseek 首先看下函数: off_t lseek(int fd, off_t offset, int whence); 所需要头文件: #include <sys/types.h> #include <unistd.h> 参数: fd 表示要操作的文件描述符 offset是相对于whence(基准)的偏移量 whence 可以是SEEK_SET(文件指针开始),SEEK_CUR(文件指针当前位置) ,SEEK_END为文件指针尾 返回值:文件读写指针距文件开头的字节大小,出错,返回-1 lseek 主要作用是移动文件读写指针,因此还有以下两个作用 1.拓展文件,不过一定要一次写的操作。迅雷等下载工具在下载文件时候先扩展一个空间,然后再下载的。 https://blog.csdn.net/clamercoder/article/details/38361815空洞文件 #include<stdio.h> #include<stdlib.h> #include<sys/types.h> #include<unistd.h> #include <sys/stat.h> #include <fcntl.h> void

python脚本避免启动多个副本程序

橙三吉。 提交于 2019-11-29 11:31:20
在linux系统中,当写好一个python脚本,需要定时启动,但是要保证系统中只有该程序的一个进程在运行,不 允许出现多个副本程序运行, Linux下的许多程序都是按此原理实现的, 可以看到/var/run/*.pid文件。 解决此问题的思路,利用程序对文件锁的状态下,只能有一个进程获得锁,当脚本启动时候,检查文件锁的状态,就可以保证不会出现多个相同进程在运行。 实现代码如下, import fcntl import sys lock_filename = '/tmp/sample-locking.lock' lock_file = open(lock_filename, 'w') try: fcntl.lockf(lock_file, fcntl.LOCK_EX | fcntl.LOCK_NB) except IOError: print('Cannot lock: ' + lock_filename) sys.exit(1) print('Locked! Running code...') quit = False while quit is not True: quit = input('Press q to quit ') quit = str(quit) == 'q' print('Bye!') PS:如果使用此代码在类里面实现,记得要将打开的文件lock

F_SETPIPE_SZ undeclared

爱⌒轻易说出口 提交于 2019-11-29 11:18:18
I have included following headers: #include <stdio.h> #include <stdlib.h> #include <string.h> #include <errno.h> #include <sys/types.h> #include <sys/stat.h> #include <unistd.h> #include <fcntl.h> I have also tried to use #define _GNU_SOURCE before #include <unistd.h> , but it also does not help. I try to use fcntl and pass it F_SETPIPE_SZ as second argument, but I keep getting this error message: error: ‘F_SETPIPE_SZ’ undeclared (first use in this function) I actually found out that I don't need this, but I'm just curious why I can't use it. Thank you. So here's solution, thanks to Chrono

Python fcntl does not lock as expected

谁都会走 提交于 2019-11-29 11:01:04
问题 On a Debian-based OS (Ubuntu, Debian Squeeze), I'm using Python (2.7, 3.2) fcntl to lock a file. As I understand from what I read, fnctl.flock locks a file in a way, that an exception will be thrown if another client wants to lock the same file. I built a little example, which I would expect to throw an excepiton, since I first lock the file, and then, immediately after, I try to lock it again: #!/usr/bin/env python # -*- coding: utf-8 -*- import fcntl fcntl.flock(open('/tmp/locktest', 'r'),

Is it possible (and safe) to make an accepting socket non-blocking?

点点圈 提交于 2019-11-29 09:39:27
I'm looking for a way to interrupt an accept() call on a blocking socket. Using signals is not an option, as this is meant to be in a library and I don't want to clutter the user signals. Using select() is another option, buf for various reason it's not very appealing in my case. What would work well, if possible, is to set the socket to non-blocking mode (using fcntl() and O_NONBLOCK ) from another thread, while the socket is blocked on an accept() call. The expected behaviour is that the accept() call will return with EAGAIN or EWOULDBLOCK in errno . Would it indeed work like that? Is it

文件IO

荒凉一梦 提交于 2019-11-28 07:57:16
目录 文件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)