试图让在Linux上接近睡眠 [英] Trying to make close sleep on Linux
问题描述
我需要调查/试验条件下对Linux的一些code的行为,其中关闭
可能是由信号处理(不论是否被中断,而不 SA_RESTART
)。什么是最方便的设置,以使关闭
睡眠系统调用的时间衡量窗口在此期间,我可以尝试与信号打的过程?一些想法:
I need to investigate/test the behavior of some code on Linux under conditions where close
might be interrupted by signal handlers (either with or without SA_RESTART
). What is the most convenient setup to make the close
syscall sleep for a measurable window of time during which I could try to hit the process with a signal? Some ideas:
- 故意慢/无响应NFS挂载
- 自定义FUSE驱动程序
但是,由于这些都是一个痛苦位设置的,我想知道如果有什么更多的现成的架子,我可以使用,可以得到期望的行为。
But since these are a bit of a pain to setup, I'm wondering if there's anything more off-the-shelf I could use that could give the desired behavior.
推荐答案
如果没有其他人有更好的主意...
If nobody else has a better idea...
您可以实现自己的字符设备驱动程序。先从第3章中 Linux设备驱动程序的模板(第3版),并调整它什么都不做除了块上近一段时间()。 (您可以使用 msleep
或 msleep_interruptible
第7章做阻塞。)
You could implement your own character device driver. Start with the template from Chapter 3 in Linux Device Drivers (3rd edition), and tweak it to do nothing except block for a while on close(). (You can use msleep
or msleep_interruptible
from Chapter 7 to do the blocking.)
其实,如果没有人提出别的东西,我也许可以迅速通过调整现有的一些code我已经掀起这件事pretty。你们多久才需要它?
Actually, if nobody else suggests something else, I can probably whip this up pretty quickly by adapting some existing code I have. How soon do you need it?
好吧,试试这个...
OK, try this...
的Makefile:
ifneq ($(KERNELRELEASE),)
obj-m := closer.o
else
KERNELDIR ?= /lib/modules/$(shell uname -r)/build
PWD := $(shell pwd)
default: modules
%:
$(MAKE) -C $(KERNELDIR) M=$(PWD) "$@"
.PHONY: default
endif
closer.c:
closer.c:
#include <linux/init.h>
#include <linux/module.h>
#include <linux/miscdevice.h>
#include <linux/delay.h>
#include <linux/fs.h>
MODULE_DESCRIPTION("Block-on-close driver");
MODULE_AUTHOR("Nemo <nemo@self-evident.org>");
MODULE_LICENSE("GPL");
#define VERSION "20110705"
MODULE_VERSION(VERSION);
#define MY_NAME "closer"
int my_open(struct inode *, struct file *);
int my_release(struct inode *, struct file *);
ssize_t my_read(struct file *, char __user *, size_t, loff_t *);
ssize_t my_write(struct file *, const char __user *, size_t, loff_t *);
static struct file_operations my_fops = {
.owner = THIS_MODULE,
.open = my_open,
.read = my_read,
.write = my_write,
.release = my_release,
};
static struct miscdevice my_dev;
int __init
my_init(void)
{
int err = 0;
printk(KERN_INFO "%s: loading version %s\n", MY_NAME, VERSION);
my_dev.minor = MISC_DYNAMIC_MINOR;
my_dev.name = MY_NAME;
my_dev.fops = &my_fops;
err = misc_register(&my_dev);
if (err)
printk(KERN_ERR "%s: misc_register failed, error %d\n", MY_NAME, err);
return err;
}
int
my_open(struct inode *inode, struct file *filp)
{
return 0;
}
ssize_t
my_read(struct file *file, char __user *p, size_t n, loff_t *off) {
return 0;
}
ssize_t
my_write(struct file *file, const char __user *p, size_t n, loff_t *off) {
return n;
}
int
my_release(struct inode *inode, struct file *filp)
{
int err = 0;
/* 10 second sleep, interruptible. */
if (msleep_interruptible(10 * 1000) > 0)
err = -EINTR;
return err;
}
void __exit
my_exit(void)
{
misc_deregister(&my_dev);
printk(KERN_INFO "%s: unloaded\n", MY_NAME);
}
module_init(my_init);
module_exit(my_exit);
使用insmod的closer.o加载模块。如果你有一个合理的现代/完整的Linux环境下,会的udev醒来时产生的/ dev /接近自动。如果没有,你可以创建自己的设备节点:
Load the module using "insmod closer.o". If you have a reasonably modern/complete Linux environment, udev will wake up and generate /dev/closer automatically. If not, you can create the device node yourself:
mknod /dev/closer c `tr : ' ' </sys/class/misc/closer/dev`
(也就是说,/ SYS /类/其它/接近的/ dev表示主:未成年人使用)
(That is, /sys/class/misc/closer/dev indicates the major:minor to use.)
和读取类似的/ dev / null的写入工作;即EOF任何读,成功在任何写操作。
Reads and writes work like /dev/null; i.e., EOF on any read, success on any write.
我已经验证了猫&LT;的/ dev /接近,在块的close()
10秒。我还没有创建一个测试,以赶上 SIGINT
(或其他),并验证它实际上会导致 EINTR
。
I have verified that "cat < /dev/closer" blocks in close()
for 10 seconds. I have not created a test to catch SIGINT
(or whatever) and verify that it actually results in EINTR
.
内置针对2.6.32内核。让我知道它是如何为你工作。
Built against a 2.6.32 kernel. Let me know how it works for you.
这篇关于试图让在Linux上接近睡眠的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!