2012-04-11 8 views

答えて

3
  • (ALSが言ったように):

mutex_lock()mutex_unlock()と我々我々は(#include <linux/mutex.h>から)mutex_init()でそれを使用する前にと同等のため

  • をミューテックスを初期化する必要があり

wait_event_interruptible()wake_up_interruptible()、我々は(#include <linux/wait.h>から)init_waitqueue_head()

3

カーネルモジュールがカーネルに直接リンクするため、カーネルスペースではライブラリ呼び出しを使用できません。

を使用でき

mutex_lock() & mutex_unlock()

彼らを通じ提供されていますlinux/mutex.h

+0

でwait_queue_headを初期化し、pthread_cond_waitのためにすべき? – MOHAMED

+0

カーネルモジュールを他のモジュールから待機させる方法。私はpthread_cond_waitに相当するものを探しています – MOHAMED

+2

@MohamedKALLEL:そうするには[Wait Queue](http://people.netfilter.org/rusty/unreliable-guides/kernel-hacking/queues.html)のようなものが必要です –

1

私はずっと前(1999っぽい)Linuxカーネルのプログラミングのためのミューテックスと条件ライブラリを作り、以来、さまざまなプロジェクトで使用されています。

私はそれをLMC(linux mutexesと条件変数)と呼びました。これは、カーネルにミューテックスタイプが存在する前であった。

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; 
関連する問題