linux内核中的pthread_mutex_lock
和pthread_cond_wait
的等效物是什么。以及如何使用它们。你能否提供简单的(hello world)例子。Linux内核中的pthread_mutex_lock和pthread_cond_wait的等价物
2
A
回答
3
- 互斥体(如阿尔斯说):
mutex_lock()
和mutex_unlock()
我们应该初始化互斥我们与mutex_init()
使用它之前(从#include <linux/mutex.h>
)
- 对于相当于
wait_event_interruptible()
和wake_up_interruptible()
我们应该初始化与init_waitqueue_head()
(从#include <linux/wait.h>
)
3
1
我为Linux内核编程不久前(1999-ISH)一个互斥体和条件库和自那以后用于各种项目。
我称之为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;
相关问题
- 1. pthread_cond_wait和pthread_mutex_lock优先级?
- 2. CUDA内核中usleep()的等价物?
- 3. Linux中ASSOC和FTYPE的等价物?
- 4. `pthread_mutex_lock`和`pthread_cond_wait`是否在同一个队列中等待?
- 5. pthread_mutex_lock和pthread_cond_wait/signal导致死锁
- 6. FreeBSD在Linux中的__histhreaded等价物
- 7. Tasklist.exe在Linux中的等价物
- 8. Linux中的linux 2.6的path_lookup()的等价物3.7
- 9. 什么是SPIN_LOCK_UNLOCKED的Linux 3.16等价物?
- 10. Linux上_emit的等价物是什么?
- 11. 有没有Linux的SetWindowPos等价物?
- 12. 在Linux上DYLD_PRINT_LIBRARIES的等价物?
- 13. 什么是Kernel32.dll的Linux等价物?
- 14. 什么是termiox.h的Linux等价物?
- 15. file_get_contents()和CURL的等价物?
- 16. Windows内核模式下strstr的任何等价物?
- 17. modinfo()等价于INSIDE内核?
- 18. Linux内核 - 等待队列
- 19. AlertDialog FragmentDialog中的setTitle()和setMessage等价物
- 20. Linux内核和我的内核模块
- 21. NSURLRequest和NSURLConnection的核心基础等价物
- 22. AS2中Event.MOUSE_LEAVE的等价物
- 23. tcl中#define的等价物?
- 24. DataGrid中CellMouseEnter的等价物?
- 25. C#中func_get_arg的等价物?
- 26. WPF中的PagedCollectionView等价物?
- 27. Mathematica中的Sprintf等价物?
- 28. Ruby中subprocess.Popen()的等价物?
- 29. JavaScript中Rfc2898DeriveBytes的等价物?
- 30. revoScaleR中的等价物
和调用pthread_cond_wait的wait_queue_head? – MOHAMED 2012-04-11 11:22:25
如何从其他模块创建内核模块等待状态。我正在寻找相当于pthread_cond_wait – MOHAMED 2012-04-11 11:31:16
@MohamedKALLEL:您将需要像[等待队列](http://people.netfilter.org/rusty/unreliable-guides/kernel-hacking/queues.html)这样做 – 2012-04-11 11:33:54