【学习】Python-->Counting Words Lines Characters

Ex1:Counting Words

import string

def numwords(s):
    list = string.split(s)
    return len(list)

inp = open("menu.txt","r")
total = 0

for line in inp.readlines():
    total =  total + numwords(line)

print "File had %d words" % total

inp.close()

Ex2:Counting Word、Lines 、Characters

import string, sys

if len(sys.argv) != 2:
    name = raw_input("Enter the file name: ")
else:
    name = sys.argv[1]
inp = open(name,"r")

words = 0
lines = 0
chars = 0

for line in inp.readlines():
    lines = lines + 1

list = string.split(line)
words = words + len(list)
chars = len(line)
print "%s has %s lines, %d words and %d characters" % (name,lines,words,chars)
inp.close()

你提供的 `vrdma_finish_device` 函数用于释放一个 vRDMA 设备的 virtqueue 并重置设备。虽然逻辑简单,但存在以下问题或可改进之处: --- ### ❌ 存在的问题: 1. **未检查指针合法性**:`dev->vdev` 或其 `config` 可能为 `NULL`; 2. **缺少同步与资源清理**:没有关闭中断、等待队列空闲、停止工作线程等; 3. **未释放内核中分配的资源**(如 `cq_vqs`, `qp_vqs`, `cqs` 等); 4. **直接调用 `reset()` `del_vqs()` 不够安全**,应确保设备状态一致; 5. **不符合 Linux 内核标准风格**:缺乏注释、错误处理优雅关闭流程。 --- ### ✅ 修改为符合 Linux 内核标准风格的实现: ```c /** * vrdma_finish_device - Tear down and reset a vRDMA device * @dev: The vRDMA device to tear down * * This function stops all I/O, releases virtqueues, frees allocated * resources, and resets the underlying virtio device. */ void vrdma_finish_device(struct vrdma_dev *dev) { if (!dev) { pr_err("%s: invalid device pointer\n", __func__); return; } if (!dev->vdev || !dev->vdev->config) { pr_warn("%s: device or config is NULL, skipping teardown\n", __func__); return; } pr_info("%s: tearing down vRDMA device %p\n", __func__, dev); /* Step 1: Mark device as inactive to prevent new operations */ dev->ib_active = false; /* Step 2: Synchronize and stop any pending work (e.g., CQ processing) */ mutex_lock(&dev->port_mutex); /* If there are workqueues or timers, flush them here */ // flush_work(&dev->cq_task); // example // del_timer_sync(&dev->poll_timer); // example mutex_unlock(&dev->port_mutex); /* Step 3: Bring the device into reset state */ dev->vdev->config->reset(dev->vdev); /* Step 4: Delete all virtqueues (this also synchronizes with callbacks) */ dev->vdev->config->del_vqs(dev->vdev); /* Step 5: Free dynamically allocated arrays */ kfree(dev->cq_vqs); /* Free CQ queue metadata */ dev->cq_vqs = NULL; kfree(dev->cqs); /* Free CQ context array */ dev->cqs = NULL; kfree(dev->qp_vqs); /* Free QP send/receive queue metadata */ dev->qp_vqs = NULL; pr_info("%s: successfully finished device teardown\n", __func__); } ``` --- ### ✅ 改进说明: | 改进点 | 说明 | |--------|------| | ✅ 安全性检查 | 检查 `dev`, `vdev`, `config` 是否有效 | | ✅ 设备去激活标志 | 设置 `ib_active = false` 防止并发访问 | | ✅ 加锁保护临界区 | 使用 `mutex_lock(&dev->port_mutex)` 同步端口操作 | | ✅ 资源释放完整 | 释放之前 `kcalloc/kmalloc` 分配的所有内存 | | ✅ 显式调用 `del_vqs()` `reset()` | 符合 VirtIO 驱动生命周期规范 | | ✅ 日志输出 | 使用 `pr_info/pr_warn` 提高调试能力 | | ✅ 注释文档化 | 使用 kernel-doc 风格描述函数用途 | --- ### 📝 补充建议: - 如果你在 `cq_vqs[i].vq` 上注册了回调(如 `vrdma_cq_ack`),这些回调可能仍在运行,应在 `del_vqs()` 前确保它们不会被再次触发。 - 若使用了 NAPI 或轮询机制,需调用 `napi_disable()`。 - 若有 DMA 映射或内存共享区域,也应在此处释放。 --- ### ❓相关问题: 1. 如何安全地中止正在执行的 virtqueue 回调函数(如 `vrdma_cq_ack`)? 2. 在 `virtio_del_vqs` 内部做了哪些同步操作?它如何保证没有 race condition? 3. 为什么需要先 `reset()` 再 `del_vqs`?顺序可以反过来吗? 4. 如何扩展此函数以支持热插拔(hot-unplug)场景下的完全资源回收? 如果你还使用了 `completion`, `refcount_t`, 或 `device reference counting`,我可以帮你进一步完善设备生命周期管理。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值