IO 上下文 io_context

本文详细介绍了Linux中用于跟踪进程I/O状态的io_context结构及其创建过程。io_context可通过引用计数在多个进程间共享,并介绍了create_task_io_context函数如何初始化和安装此结构到任务上下文中。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

此结构用于跟踪进程的IO处理,此结构可在多个进程之间共享。
/*
 * I/O subsystem state of the associated processes.  It is refcounted
 * and kmalloc'ed. These could be shared between processes.
 */
struct io_context {
	atomic_long_t refcount;
	atomic_t active_ref;
	atomic_t nr_tasks;

	/* all the fields below are protected by this lock */
	spinlock_t lock;
	unsigned short ioprio;
	/*
	 * For request batching
	 */
	int nr_batch_requests;     /* Number of requests left in the batch */
	unsigned long last_waited; /* Time last woken after wait for request */
	struct radix_tree_root	icq_tree;
	struct io_cq __rcu	*icq_hint;
	struct hlist_head	icq_list;
	struct work_struct release_work;
};

函数create_task_io_context()用于创建io_context
int create_task_io_context(struct task_struct *task, gfp_t gfp_flags, int node)
{
	struct io_context *ioc;
	int ret;

	ioc = kmem_cache_alloc_node(iocontext_cachep, gfp_flags |
			 __GFP_ZERO,node);
	if (unlikely(!ioc))
		return -ENOMEM;


	/* initialize */
	atomic_long_set(&ioc->refcount, 1);
	atomic_set(&ioc->nr_tasks, 1);
	atomic_set(&ioc->active_ref, 1);
	spin_lock_init(&ioc->lock);
	INIT_RADIX_TREE(&ioc->icq_tree, GFP_ATOMIC | __GFP_HIGH);
	INIT_HLIST_HEAD(&ioc->icq_list);
	INIT_WORK(&ioc->release_work, ioc_release_fn);


	/*
	 * Try to install.  ioc shouldn't be installed if someone else
	 * already did or @task, which isn't %current, is exiting.  Note
	 * that we need to allow ioc creation on exiting %current as exit
	 * path may issue IOs from e.g. exit_files().  The exit path is
	 * responsible for not issuing IO after exit_io_context().
	 */
	task_lock(task);
	if (!task->io_context && (task == current || !(task->flags & PF_EXITING)))
		task->io_context = ioc;
	else
		kmem_cache_free(iocontext_cachep, ioc);


	ret = task->io_context ? 0 : -EBUSY;


	task_unlock(task);


	return ret;
}

static struct kmem_cache *iocontext_cachep;
create_task_io_context()
void ioc_release_fn(struct work_struct *work)
int __init blk_ioc_init(void)
{
	iocontext_cachep = kmem_cache_create("blkdev_ioc",
			sizeof(struct io_context), 0, SLAB_PANIC, NULL);
	return 0;
}
subsys_initcall(blk_ioc_init);


get_task_io_context
	create_task_io_context

set_task_ioprio:ioprio_set, ext4_fill_super, ext4_remount
copy_io:fork




















                
IO上下文是什么意思 #include "rclcpp/rclcpp.hpp" #include "serial_driver/serial_driver.hpp" using namespace serial_driver; class Stm32SerialNode : public rclcpp::Node { public: Stm32SerialNode() : Node("stm32_serial_node") { // 1. 参数配置 declare_parameter("device_name", "/dev/ttyUSB0"); declare_parameter("baud_rate", 115200); // 2. 创建IO上下文 io_context_ = std::make_shared<IoContext>(); // 3. 配置串口参数 const SerialPortConfig config( BaudRate::BAUD_115200, FlowControl::NONE, Parity::NONE, StopBits::ONE ); // 4. 创建串口驱动 try { driver_ = std::make_unique<SerialDriver>( *io_context_, get_parameter("device_name").as_string(), config ); } catch (const std::exception &ex) { RCLCPP_ERROR(get_logger(), "串口初始化失败: %s", ex.what()); rclcpp::shutdown(); } // 5. 创建接收回调 receiver_thread_ = std::thread([this]() { std::vector<uint8_t> buffer(128); while (rclcpp::ok()) { try { const size_t num_bytes = driver_->port().receive(buffer); processData(buffer.data(), num_bytes); } catch (...) { RCLCPP_ERROR(get_logger(), "接收数据异常"); } } }); } void sendToStm32(const std::vector<uint8_t>& data) { try { driver_->port().send(data); } catch (const std::exception &ex) { RCLCPP_ERROR(get_logger(), "发送失败: %s", ex.what()); } } private: void processData(const uint8_t* data, size_t length) { // 解析STM32发来的数据 // 示例:打印HEX数据 std::stringstream ss; for (size_t i = 0; i < length; ++i) { ss << std::hex << static_cast<int>(data[i]) << " "; } RCLCPP_INFO(get_logger(), "接收数据: %s", ss.str().c_str()); } std::shared_ptr<IoContext> io_context_; std::unique_ptr<SerialDriver> driver_; std::thread receiver_thread_; };
03-11
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值