how to make kernel threads preemptive?

I wrote a C code programme that creates many threads (pthreads) which do all the same thing. Each of these threads tries to acquire a common mutex (one binary semaphore shared among these threads). When the first thread manages to acquire the mutex, all the other threads will find it not available (counter at 0) when they do a sem_wait (down) operation. Now, I want to do the same thing at kernel level, with a kernel module that uses Kthreads. What I've found out, is that linux kernel threads are not preemptive (preemption is not enabled on my kernel) so, as soon as the first kthread acquires the common semaphore, the others do nothing until it has finished. So, no kthread will find the mutex not available when doing a down operation on it. For my exercise, I need to have the mutex not available, every now and then, when I do the down operation, just like it happens with the version with posix threads (user level). One advice that I got is to use the schedule() function, which is called by a kthread and causes the cpu to schedule another kthread for execution. However, I am not sure how to use it. I don't understand very well what I've found on internet, since I am a newbie on kernel coding. Does anyone have an example on how to use schedule() in order to preempt kernel threads?

链接地址: http://www.djcxy.com/p/84844.html

上一篇: Linux内核:schedule()函数

下一篇: 如何使内核线程抢占?