RE: Linux question , please help a newbie!

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

 



Hi,
please find my response inline. See if it suits your requirement.
>-----Original Message-----
>From: kernelnewbies-bounce@xxxxxxxxxxxx [mailto:kernelnewbies-
>bounce@xxxxxxxxxxxx] On Behalf Of Garcia Jérémie
>Sent: Monday, June 27, 2005 6:51 PM
>To: tomazb@xxxxxxxxxxx
>Cc: kernelnewbies@xxxxxxxxxxxx
>Subject: Linux question , please help a newbie!
>
>Hi Tom,
>I'm looking for a solution to a problem you posted on the net 2 years ago.
>I need to be able to interrupt a task that is waiting for a semaphore to be
>free.
>Indeed I use "down_interruptible()" and I'd like to be able to tell to the
>task
>after a period of time : "give up, awake and do something else".

One possible way is to declare a timer which will send a signal to task blocked on down_interruptible after a specified period of time.
This will cause the down_interruptible to return and you can do something else. In case the down_interrutible succeeds the signal will be ignored.

I have tested the above functionality with this sample code:
===========================

int tid;
struct semaphore test_sem;
void test_fn(){
	/* send the signal to bring the task out of down call*/
	kill_proc(tid, SIGALRM /*any signal which the task can ignore*/, 1);
}

struct timer_list test_timer;

int init_module(){

	init_MUTEX(&test_sem);
	tid=current->pid;
	
	/*this down call is to make sure that any subsequent call 
	to down_interruptible blocks :-) */

	if(down_interruptible(&test_sem))
		printk("failed to acquired the semaphore\n");
	else
		printk("sem acquired\n");
	
	
	init_timer(&test_timer);
	test_timer.data=0;
	test_timer.function=test_fn;
	add_timer(&test_timer)

	/*try to acquire the sem again, which will
	block it and bring the signal into picture*/
	if(down_interruptible(&test_sem)){
		printk("interrupted\n");
	}
	else
		printk("semaphore acquired");

	up(&test_sem);
	return 0;
}
void cleanup_module()
{
}
MODULE_LICENSE("GPL");
==============================
>You did not give the solution on the list. So do you have it or do you
>remember what to
>do cause it was a long time ago lol!
>Tks, Jérémie

Hope this helps.
Sanjay
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
http://sanjayku.blogspot.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/



[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