【问题标题】:Equivalent of pthread_mutex_lock and pthread_cond_wait in the linux kernellinux内核中pthread_mutex_lock和pthread_cond_wait的等价物
【发布时间】:2012-04-11 11:14:27
【问题描述】:

linux内核中pthread_mutex_lockpthread_cond_wait的等价物是什么。以及如何使用它们。您能否提供简单的(hello world)示例。

【问题讨论】:

    标签: c linux-kernel pthreads


    【解决方案1】:
    • 对于互斥体(正如 Als 所说):

    mutex_lock()mutex_unlock() 我们应该在与mutex_init() 一起使用之前初始化互斥锁(来自#include <linux/mutex.h>

    • 相当于pthread_cond_wait

    wait_event_interruptible()wake_up_interruptible() 我们应该用init_waitqueue_head()(来自#include <linux/wait.h>)初始化wait_queue_head

    【讨论】:

      【解决方案2】:

      您不能在内核空间中使用任何库调用,因为内核模块直接链接到内核。

      你可以使用:

      mutex_lock() & mutex_unlock()

      它们通过以下方式提供:linux/mutex.h

      【讨论】:

      • 如何使内核模块等待其他模块的条件。我正在寻找相当于 pthread_cond_wait
      • @MohamedKALLEL:你需要Wait Queue 之类的东西才能这样做
      【解决方案3】:

      我很久以前(1999 年左右)为 Linux 内核编程制作了一个互斥锁和条件库,并在此后的各种项目中使用。

      我称之为 LMC(Linux 互斥体和条件变量)。这是在内核中没有互斥体类型之前。

      http://www.kylheku.com/~kaz/lmc.html

      最近,我添加了一个很酷的新函数,其语义是“放弃互斥锁以等待条件变量,同时轮询多个文件描述符,直到给定超时。”

      我在内核线程中使用了它来监视各种共享对象的更新,并同时与内核套接字通信。

      检查一下:

      /**
       * Atomically give up the mutex and wait on the condition variable.
       * Wake up if the specified timeout elapses, or if a signal is delivered.
       * Additionally, also wait on the specified file descriptors to become
       * ready, combining condition waiting with poll().
       * KCOND_WAIT_SUCCESS means the condition was signaled, or one or more
       * file descriptors are ready.
       * Also, a negative value can be returned indicating an error!
       * (The poll needs to dynamically allocate some memory for the wait table).
       * The timeout is relative to the current time, specifying how long to sleep in
       * jiffies (CPU clock ticks).
       */
      int kcond_timed_wait_rel_poll(kcond_t *, kmutex_t *, long, 
                                    kcond_poll_t *, unsigned int);
      

      kcond_poll_t 结构的数组是你必须创建并填写自己的东西,结构看起来像这样。有一个类型字段,因此您可以等待套接字 (struct socket *) 或文件 (struct file *):

      /**
       * Structure for file-descriptor polling condition waits.
       * This resembles struct pollfd, but uses a direct file descriptor
       * pointer rather than a file descriptor number.  Also,
       * it contains the wait queue by which the process is enqueued
       * to wait on that descriptor. Thus our poll function doesn't
       * have to dynamically allocate wait queue tables. It gets
       * them from this array! (But this means that the array cannot
       * be used by multiple threads at the same time to do polling!)
       */
      typedef struct {
              kcond_poll_type_t type;  /* Must set this. */
              union {                 /* Must set union field according to type. */
                      struct file *file;      
                      struct socket *sock;
              } obj;
              short events;           /* And this. */
              short revents;          /* Check response in this. */
              wait_queue_t wait;          /* Internal, don't set. */
              wait_queue_head_t *queue;   /* Internal, don't set */
      } kcond_poll_t;
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-11-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-04-17
        • 1970-01-01
        • 1970-01-01
        • 2013-09-08
        相关资源
        最近更新 更多