繁体   English   中英

在linux内核中进行一些代码的移植而不是schedubale

[英]Make some portation of code not schedubale in linux kernel

当我的每个ioctl被调用时,在我的驱动程序中调用一个magic()函数,并且只要在该代码中在开始和结束注释之间调用该函数,就不应该由任何其他进程或线程调度。

magic()
{
// start

some code is here.

// end
}

我想要做的是:每当处理器开始执行代码时,在启动之后,直到它完成结束,它不应由处理器调度。

那我该怎么办呢? 这是同样的东西,也被称为原子过程?

通过保持mutex之间的代码将提供该功能?

一旦进入类似场景,当我需要一个剪贴簿类型的地方,其中多个客户端线程将写入并由作为服务器线程的线程读取,我所做的有点像这样:我指的这些函数只是这里的例子,尽量不要担心参数和共享变量。

c1_thread_write() //let this be the unique function that client_thread_1 executes after it's inception  
{
   //some calls to open the scrapbook
   //the scrapbook thing I am referring to, is a buffer
   //Now I want to ensure that when client_thread_1 is writing, no other process or thread should be scheduled, which may somehow cause the client_thread_1 to exit this writing function without completing it.

   //so, I will create a MUTEX, lock it here as the first thing that gets executed in this function. MUTEX will be of ERROR_CHECK type
   //Start writing to the buffer, finish it
   //Unlock the mutex
   //call the pthread_join() and exit
}

这需要信号量/自旋锁,具体取决于具体情况。 这是一个简单的字符驱动程序代码。 当调用设备打开功能时,它会看到是否有其他进程打开了设备,如果是,则等待它关闭。 如果设备未被任何其他进程使用,则可以打开该设备。 这是通过使用信号量实现的。 进程必须通过获取信号量来访问代码,一旦完成,它必须/应该释放信号量。 以下是开放和关闭功能。

static int device_open(struct inode *inode, struct file *filp) {
    if(down_interruptible(&sem) != 0) {//take the semaphore
        printk(KERN_ALERT "megharajchard : the device has been opened by some other device, unable to open lock\n");
        return -1;
    }
    //buff_rptr = buff_wptr = device_buffer;
    printk(KERN_INFO "megharajchard : device opened succesfully\n");
    return 0;
}

这里的过程试图打开设备,它必须采取信号量,一旦采取,将不允许其他进程打开设备。

下面是关闭设备函数,其中进程将释放信号量,以便其他进程可以打开设备并使用它。

static int device_close(struct inode *inode, struct file *filp) {
    up(&sem);// release the semaphore
    printk(KERN_INFO "megharajchard : device has been closed\n");
    return ret;
}

原子意味着进程无法休眠的情况,通常在中断上下文中运行时遇到。 在哪里执行该过程不允许睡觉。 要在这种情况下实现锁定,我们必须使用自旋锁。

如果您还需要更多信息,请与我们联系。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM