亚洲av成人无遮挡网站在线观看,少妇性bbb搡bbb爽爽爽,亚洲av日韩精品久久久久久,兔费看少妇性l交大片免费,无码少妇一区二区三区

  免費(fèi)注冊(cè) 查看新帖 |

Chinaunix

  平臺(tái) 論壇 博客 文庫(kù)
最近訪問(wèn)板塊 發(fā)新帖
查看: 4031 | 回復(fù): 5
打印 上一主題 下一主題

kill掉進(jìn)程時(shí),若正持有內(nèi)核的mutex,則不會(huì)立即生效? [復(fù)制鏈接]

論壇徽章:
0
跳轉(zhuǎn)到指定樓層
1 [收藏(0)] [報(bào)告]
發(fā)表于 2011-09-02 17:10 |只看該作者 |倒序?yàn)g覽
假設(shè)進(jìn)程A打開(kāi)設(shè)備文件B,在B的驅(qū)動(dòng)代碼中持有mutex(mutex_lock, mutex_lock_interruptable),且正在訪問(wèn)設(shè)備。則此時(shí)在shell下用kill命令殺掉進(jìn)程A時(shí),好像不會(huì)立即生效,而是一直要等到驅(qū)動(dòng)掉用了mutex_unlock后才會(huì)被kill掉。如果A一直不釋放mutex的話,則永遠(yuǎn)不會(huì)被kill掉了。是不是有問(wèn)題呀?

測(cè)試代碼如下,以proc下的一個(gè)文件為例,讀取時(shí)打開(kāi)鎖。

/*
# insmod mutex_debug.ko taskTime=10
# cat /proc/mutex_test
# kill  pid_of_cat
*/
#include <linux/module.h>
#include <linux/version.h>
#include <linux/delay.h>
#include <linux/proc_fs.h>
#include <linux/moduleparam.h>
#include <asm/param.h>
#include <linux/uaccess.h>
#include <linux/string.h>
#include <linux/mutex.h>
#include <linux/timer.h>

/* Module Argument */
static int taskTime = 5;
module_param(taskTime, int, S_IRUGO);
MODULE_PARM_DESC(taskTime, "task running time, default is 5");

#define MUTEX_TEST_FILE                "mutex_test"
struct mutex gMutexLock;

#define printk_dbg(format, arg...)  \
    do{ \
            printk("%s:[cpu=%d, pid=%04d] " format, MUTEX_TEST_FILE, smp_processor_id(), current->pid, ##arg );   \
    }while(0)


//////////////////////////////////////////////////////////////////////////////////////
// 生成一個(gè)執(zhí)行N秒的任務(wù)
static void foo(int seconds)
{
        unsigned long timeout = jiffies + HZ*seconds;
        unsigned long cur_time=0;
        int i=0;
       
        printk_dbg("i=%d, timeout=%u, jiffies=%u.\n", i++, timeout, jiffies);
       
        do{
//                printk_dbg("i=%d, timeout=%u, jiffies=%u.\n", i++, timeout, jiffies);
//                printk_dbg("goto sleep.\n");
//                udelay(500*1000);
//                delay(500);
//                printk_dbg("wake up.\n");
        }while(jiffies<timeout);
}


static int mutex_test_read(char *page, char **start, off_t off, int count, int *eof, void *data)
{
        printk_dbg("========== try to get mutext...\n");
        mutex_lock_interruptible(&gMutexLock);
        printk_dbg("---------- has got mutext.\n");
       
        foo(taskTime);        // work task
       
        printk_dbg("---------- try to release mutext...\n");
        mutex_unlock(&gMutexLock);
        printk_dbg("========== has released mutext.\n\n");

        {
                  int len;
                  if (off > 0) {
                    *eof = 1;
                    return 0;
                  }

                  len = sprintf(page, "taskTime=%d.\n\n", taskTime);
                  return len;
        }
}

ssize_t mutex_test_write( struct file *filp, const char __user *buff, unsigned long len, void *data )
{
        char buf[10]={0};
        printk_dbg( "Readonly file.\n");
       
        if (copy_from_user(buf, buff, 2))
        {
            return -EFAULT;
          }
  
        return len;
}


//////////////////////////////////////////////////////////////////////////////////////
static int __init  test_module_init(void)
{
        struct proc_dir_entry *mutex_test_dbg = NULL;
       
        mutex_init(&gMutexLock);
       
        mutex_test_dbg = create_proc_entry(MUTEX_TEST_FILE, 0666, NULL);
        if (!mutex_test_dbg)
        {
                printk_dbg( "Failed to creat /proc/%s!\n", MUTEX_TEST_FILE);
                return -1;
        }
        else
        {             
                mutex_test_dbg->read_proc  = mutex_test_read;
                mutex_test_dbg->write_proc = mutex_test_write;
                printk_dbg("/proc/%s created succeed.\n", MUTEX_TEST_FILE);
        }
       
        return 0;
}

static void __exit test_module_exit(void)
{
        remove_proc_entry(MUTEX_TEST_FILE, NULL);
        printk_dbg("/proc/%s removed.\n", MUTEX_TEST_FILE);
       
    return ;
}

module_init(test_module_init);
module_exit(test_module_exit);

MODULE_VERSION("1.0");
MODULE_LICENSE("GPL");



不知道是不是本測(cè)試方法不對(duì),各位有踫到過(guò)此問(wèn)題嗎?

論壇徽章:
0
2 [報(bào)告]
發(fā)表于 2011-09-03 00:09 |只看該作者
回復(fù) 1# coralonland
進(jìn)程能不能退出和你是否持有鎖無(wú)關(guān),你的例子里kill不掉是驅(qū)動(dòng)的邏輯決定的:
  1. do{
  2.                 udelay(500*1000);
  3.                 delay(500);
  4.                 printk_dbg("wake up.\n");
  5. }while(jiffies<timeout);
復(fù)制代碼
從睡眠中醒來(lái)有可能是由于接收到信號(hào),所以在從udelay/delay/schedule_timeout這樣的函數(shù)返回之后要加上

  1. if (signal_pending(current)) {
  2.         /*
  3.           * Do sth
  4.          */

  5.         return -EINTR;
  6. }
復(fù)制代碼
這樣的處理(也可以簡(jiǎn)單判斷一下睡眠時(shí)間是否為你指定的),然后向調(diào)用者返回 -EINTR 告知被信號(hào)中斷,讓調(diào)用者釋放鎖,然后返回用戶空間。調(diào)用者不釋放鎖也不會(huì)導(dǎo)致進(jìn)程無(wú)法退出,只會(huì)讓所有其它等待這把鎖的進(jìn)程永久等待。

論壇徽章:
0
3 [報(bào)告]
發(fā)表于 2011-09-04 01:12 |只看該作者
回復(fù) 2# vupiggy


    您說(shuō)的這種情況我考慮過(guò),所以在測(cè)試中,發(fā)do{}while(...)中的內(nèi)容全部注釋掉了,也就是說(shuō)實(shí)際上就是空循環(huán)著執(zhí)行N秒鐘而沒(méi)有進(jìn)入睡眠中。但結(jié)果正如前文所述,無(wú)法即時(shí)kill掉。

論壇徽章:
0
4 [報(bào)告]
發(fā)表于 2011-09-04 10:21 |只看該作者
本帖最后由 vupiggy 于 2011-09-04 03:26 編輯
回復(fù)  vupiggy


    您說(shuō)的這種情況我考慮過(guò),所以在測(cè)試中,發(fā)do{}while(...)中的內(nèi)容全部注釋掉 ...
coralonland 發(fā)表于 2011-09-03 18:12


你確切清楚 kill 命令做了什么?確切清楚信號(hào)是什么?確切清楚信號(hào)的遞送和處理?你把 mutex_lock 去掉,只保留 do {} while() 循環(huán),你的 kill 一樣不能即時(shí)生效。

好吧,給你點(diǎn)提示,谷歌/百度/必應(yīng)``linux 內(nèi)核 信號(hào) 機(jī)制''。看兩天還是認(rèn)為 kill 不掉和持有鎖有半毛錢關(guān)系再上來(lái)問(wèn)。happy hacking.

論壇徽章:
0
5 [報(bào)告]
發(fā)表于 2011-09-04 10:30 |只看該作者
那是由于信號(hào)函數(shù)是在從內(nèi)核態(tài)即將要返回用戶態(tài)的時(shí)候調(diào)用的。
你在內(nèi)核態(tài)長(zhǎng)期不返回,信號(hào)函數(shù)得不到執(zhí)行。

論壇徽章:
0
6 [報(bào)告]
發(fā)表于 2011-09-05 11:45 |只看該作者
非常感謝 奇門遁甲-lu  和 vupiggy 的指教。經(jīng)過(guò)驗(yàn)證,確實(shí)如此。
您需要登錄后才可以回帖 登錄 | 注冊(cè)

本版積分規(guī)則 發(fā)表回復(fù)

  

北京盛拓優(yōu)訊信息技術(shù)有限公司. 版權(quán)所有 京ICP備16024965號(hào)-6 北京市公安局海淀分局網(wǎng)監(jiān)中心備案編號(hào):11010802020122 niuxiaotong@pcpop.com 17352615567
未成年舉報(bào)專區(qū)
中國(guó)互聯(lián)網(wǎng)協(xié)會(huì)會(huì)員  聯(lián)系我們:huangweiwei@itpub.net
感謝所有關(guān)心和支持過(guò)ChinaUnix的朋友們 轉(zhuǎn)載本站內(nèi)容請(qǐng)注明原作者名及出處

清除 Cookies - ChinaUnix - Archiver - WAP - TOP