fcntl

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

走远了吗. 提交于 2019-11-28 03:03:11
问题 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

socket(7)

↘锁芯ラ 提交于 2019-11-28 02:54:15
SOCKET(7) NAME socket —— Linux套接字接口 SYNOPSIS #include <sys/socket.h>sockfd = socket(int socket_family, int socket_type, int protocol); DESCRIPTION 此手册描述Linux网络套接层用户接口。BSD兼容的通用接口位于用户矜持和内核的网络协议栈之间。协议模块以协议族和套接字类型分组,协议族如AF_INET,AF_IPX,AF_PACKET;套接字类型如SOCK_STREAM或SOCK_DGRAM。参见socket(2)以获取更多有关协议族和类型的信息。 套接字层函数 这些函数被用户进程用来发送或接受包以及其它套接字操作。参见它们各自的手册页以了解更多信息。 socket创建一个套接字,connect将一个套接字链接到远程套接字地址,bind将一个套接字绑定到本地套接字地址,listen告诉套接字准备接受连接请求,accept用于获取一个接入连接的套接字。socketpair返回两个互连的匿名套接字(只有少数几个本地协议族的实现像AF_UNIX)。 send,sendto和sendmsg在一个套接字上发送数据,recv,recvfrom和recvmsg从一个套接字接收数据。poll和select等待数据到达或数据发送准备就绪。此外,标准I

How to get hard disk serial number using Python

淺唱寂寞╮ 提交于 2019-11-27 07:05:57
How can I get the serial number of a hard disk drive using Python on Linux ? I would like to use a Python module to do that instead of running an external program such as hdparm . Perhaps using the fcntl module? Linux As you suggested, fcntl is the way to do this on Linux. The C code you want to translate looks like this: static struct hd_driveid hd; int fd; if ((fd = open("/dev/hda", O_RDONLY | O_NONBLOCK)) < 0) { printf("ERROR opening /dev/hda\n"); exit(1); } if (!ioctl(fd, HDIO_GET_IDENTITY, &hd)) { printf("%.20s\n", hd.serial_no); } else if (errno == -ENOMSG) { printf("No serial number

IPC之PIPE

眉间皱痕 提交于 2019-11-27 00:39:29
  管道是一种只允许用在有亲属关系的进程间通信的方式,由函数pipe创建一个管道,read,write进行读写操作。 #include <unistd.h> int pipe(int pipefd[2]); 参数pipefd[2]数组返回打开的读写描述符,pipefd[0]为读,pipefd[1]为写。   第一个问题:文件描述符怎么会出现一个只能读,一个只能写呢?猜想是对一个文件打开了2次,一个以只读打开,一个以只写打开。使用fcntl来验证下: #include <unistd.h> #include <fcntl.h> int fcntl(int fd, int cmd, ... /* arg */ ); F_GETFL (void) Get the file access mode and the file status flags; arg is ignored. cmd为F_GETFL时,最后一个参数arg被忽略。测试代码: 1 #include <sys/types.h> 2 #include <sys/stat.h> 3 #include <stdio.h> 4 #include <fcntl.h> 5 #include <signal.h> 6 #include <unistd.h> 7 #include <stdlib.h> 8 9 int main(void)

How to get hard disk serial number using Python

家住魔仙堡 提交于 2019-11-26 17:37:26
问题 How can I get the serial number of a hard disk drive using Python on Linux ? I would like to use a Python module to do that instead of running an external program such as hdparm. Perhaps using the fcntl module? 回答1: Linux As you suggested, fcntl is the way to do this on Linux. The C code you want to translate looks like this: static struct hd_driveid hd; int fd; if ((fd = open("/dev/hda", O_RDONLY | O_NONBLOCK)) < 0) { printf("ERROR opening /dev/hda\n"); exit(1); } if (!ioctl(fd, HDIO_GET