首页 > 解决方案 > 如何在不冻结系统的情况下处理长中断

问题描述

我正在实现一个中断处理,它可以从 Linux 内核模块关闭一个打开的 txt 文件。我能够获得 txt 文件路径(见图)。但是,当我尝试关闭该文件时,我的计算机总是死机。我使用 close_fd() 但它不起作用 来自 dmesg 的结果,在 pid 10367 中检测到一个 txt 文件 这是我的代码:

for_each_process(task_list) {
    pr_info("Process: %s\t PID:[%d]\t State:%s\n", 
            task_list->comm, task_list->pid,
            get_task_state(task_list->state));
    if (task_list->files == NULL) continue; // Ignore processes without files
    fdt = files_fdtable(task_list->files);
    int i=0;
    while(fdt->fd[i] != NULL) { 
        files_path = fdt->fd[i]->f_path;
        mode_path = fdt->fd[i]->f_mode;
        cwd = d_path(&files_path,buf,100*sizeof(char));

        printk(KERN_INFO "Open file with fd %d  %s mode: %d", i,cwd, mode_path);
        //Suppose that I can detect a txt file is running in pid 10367. And I want to safety close it
        if (task_list->pid == 10367){
            printk(KERN_INFO "This below function does not work");
            close_fd(fdt->fd[i]); 
        }  
        i++;
    }
}

我不知道我犯了哪些错误。有人可以评论和支持我吗?此致

标签: clinux-kernelpidinterrupt-handling

解决方案


我终于找到了解决方案。这是filp_close()

flush_cache_all() ;             
close_result = filp_close(fdt->fd[i], NULL);

整个系统不会被冻结。非常感谢您的建议。问候


推荐阅读