RE: sleep process, wait queues

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



>-----Original Message-----
>From: kernelnewbies-bounce@xxxxxxxxxxxx [mailto:kernelnewbies-
>bounce@xxxxxxxxxxxx] On Behalf Of Gaurav Dhiman
>Sent: Tuesday, July 05, 2005 10:06 PM
>To: Deepak Joshi
>Cc: kernelnewbies@xxxxxxxxxxxx
>Subject: Re: sleep process, wait queues
>
>On 7/3/05, Deepak Joshi <deepak_cins@xxxxxxxxxxx> wrote:
>> #include <linux/module.h>
>> #include <linux/kernel.h>
>> #include <linux/wait.h>
>> #include <linux/sched.h>
>> #include <linux/list.h>
>> MODULE_LICENSE("GPL");
>>
>>
>> static DECLARE_WAIT_QUEUE_HEAD (my_queue);
>> int init_module(void)
>> {
>>         struct task_struct *my_task ;
>>         my_task = find_task_by_pid(1477);
>>         DECLARE_WAITQUEUE(wait, my_task);
>>         my_task->state = TASK_UNINTERRUPTIBLE ;
>>         init_waitqueue_entry( &wait, my_task);
>>         add_wait_queue( &my_queue , &wait);
>>         sleep_on(&my_queue);
>>         interruptible_sleep_on(&my_queue);
>>         schedule( );
>

One possible way to achieve the above task can be as follows.
I used kernel linux-2.6.8
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/wait.h>
#include <linux/sched.h>
#include <linux/list.h>

static struct task_struct *my_task;
static DECLARE_WAIT_QUEUE_HEAD(my_queue);
wait_queue wait;

static int pid=1;       //pass process id to module as parameter		
MODULE_PARM(pid, "i");

int init_module()
{
	my_task=find_task_by_pid(pid);
	DECLARE_WAITQUEUE(wait1, my_task);
	wait=wait1;
	my_task->state = TASK_INTERRUPTIBLE;  
	init_waitqueue_entry(&wait, my_task);
	add_wait_queue(&my_queue, &wait);
	return 0;
}
void cleanup_module()
{
	printk("exiting\n");
	wake_up_interruptible(&my_queue);
	my_task->state = TASK_RUNNING;
	remove_wait_queue(&my_queue, &wait);
}
MODULE_LICENSE("GPL");
========================

It worked for me perfectly. 
Kindly let me know if I am missing something.

Sanjay.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
http://sanjayku.blogspot.com



>Its not the right way to put another process to sleep, because all the
>sleep functions put the current running process to sleep and in your
>case the current is insmod itself. You should rather hack control flow
>of any of the common system calls, like open(). Once you hack that
>(you can do that in init_module() function), in your hack function
>check if the current process is the process you want to put to sleep,
>if yes, do the above sleeping mechanism in your hack function.
>
>regards,
>-Gaurav
>
>>         return 0;
>> }
>>
>>
>> void cleanuo_module()
>> {
>>         printk("Bye");
>>         wake_up( &my_queue );
>> }
>>
>>
>>
>>
>> Hi  everyone,
>>
>> In the above module I want to sleep the process having
>> pid 1477 ( which is mpg123 player in my case in wait
>> queue ). Instead of sleeping the process with the
>> given pid it is hanging insmod command. so plz tell me
>> what is wrong in the above code.
>>
>> Thanks for replays in Advance,
>> Thanks and regards
>> Deepak Joshi.
>>
>>
>>
>>
>> ___________________________________________________________
>> How much free photo storage do you get? Store your holiday
>> snaps for FREE with Yahoo! Photos http://uk.photos.yahoo.com
>>
>> --
>> Kernelnewbies: Help each other learn about the Linux kernel.
>> Archive:       http://mail.nl.linux.org/kernelnewbies/
>> FAQ:           http://kernelnewbies.org/faq/
>>
>>
>
>--
>Kernelnewbies: Help each other learn about the Linux kernel.
>Archive:       http://mail.nl.linux.org/kernelnewbies/
>FAQ:           http://kernelnewbies.org/faq/

--
Kernelnewbies: Help each other learn about the Linux kernel.
Archive:       http://mail.nl.linux.org/kernelnewbies/
FAQ:           http://kernelnewbies.org/faq/



[Index of Archives]     [Newbies FAQ]     [Linux Kernel Mentors]     [Linux Kernel Development]     [IETF Annouce]     [Git]     [Networking]     [Security]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux RAID]     [Linux SCSI]     [Linux ACPI]
  Powered by Linux