mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* repeat a function after fixed time period
@ 2005-03-23 19:43 sounak chakraborty
  2005-03-23 20:23 ` linux-os
  0 siblings, 1 reply; 9+ messages in thread
From: sounak chakraborty @ 2005-03-23 19:43 UTC (permalink / raw)
  To: linux-kernel

dear sir 
i want to call my own function inside the kernel 
after a fixed interval(i.e some kind of timer)
how to do that which function i have to use to 
repeat the function


anather way is that making my own system call 
which calls my function and 
this sytem call is being access by a user program
which calls it after a fixed inter val of time
 will this be correct
thanks 
sounak

________________________________________________________________________
Yahoo! India Matrimony: Find your partner online. http://yahoo.shaadi.com/india-matrimony/

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: repeat a function after fixed time period
  2005-03-23 19:43 repeat a function after fixed time period sounak chakraborty
@ 2005-03-23 20:23 ` linux-os
  2005-03-23 20:48   ` Arjan van de Ven
  0 siblings, 1 reply; 9+ messages in thread
From: linux-os @ 2005-03-23 20:23 UTC (permalink / raw)
  To: sounak chakraborty; +Cc: linux-kernel

On Wed, 23 Mar 2005, sounak chakraborty wrote:

> dear sir
> i want to call my own function inside the kernel
> after a fixed interval(i.e some kind of timer)
> how to do that which function i have to use to
> repeat the function
>
> anather way is that making my own system call
> which calls my function and
> this sytem call is being access by a user program
> which calls it after a fixed inter val of time
> will this be correct
> thanks
> sounak
>

This kernel code should do just fine.



struct INFO {
     struct timer_list timer;            // For test timer
     atomic_t running;                   // Timer is running
     };

//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
//
//   This stops the timer. This must NOT be called with a spin-lock
//   held.
//
static void stop_timer()
{
     if(atomic_read(&info->running))
     {
         atomic_dec(&info->running);
         if(info->timer.function)
             del_timer(&info->timer);
     }
}
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
//
//    This starts the timer. This must NOT be called with a spin-lock
//    held.
//
static void start_timer(void)
{
     if(!atomic_read(&info->running))
     {
         atomic_inc(&info->running);
         init_timer(&info->timer);
         info->timer.expires = jiffies + HZ;
         info->timer.data = 0;
         info->timer.function = test_timer;
         add_timer(&info->timer);
     }
}
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
//
//   This is the code that executes at a timer-interval.
//
static void test_timer(unsigned long data)
{
     info->timer.expires = jiffies + HZ;         // New expiration time
     if(atomic_read(&info->running))             // If it's still running
     {
         code_to_execute_every_timer_interval();
         add_timer(&info->timer);                // Into timer-queue again
     }
}
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=


Cheers,
Dick Johnson
Penguin : Linux version 2.6.11 on an i686 machine (5537.79 BogoMips).
  Notice : All mail here is now cached for review by Dictator Bush.
                  98.36% of all statistics are fiction.

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: repeat a function after fixed time period
  2005-03-23 20:23 ` linux-os
@ 2005-03-23 20:48   ` Arjan van de Ven
  2005-03-23 20:56     ` linux-os
  0 siblings, 1 reply; 9+ messages in thread
From: Arjan van de Ven @ 2005-03-23 20:48 UTC (permalink / raw)
  To: linux-os; +Cc: sounak chakraborty, linux-kernel


> 
> This kernel code should do just fine.
> 
> 
> 
> struct INFO {
>      struct timer_list timer;            // For test timer
>      atomic_t running;                   // Timer is running
>      };
> 
> //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
> //
> //   This stops the timer. This must NOT be called with a spin-lock
> //   held.
> //
> static void stop_timer()
> {
>      if(atomic_read(&info->running))
>      {
>          atomic_dec(&info->running);

this is a race.

>          if(info->timer.function)
>              del_timer(&info->timer);

you probably want del_timer_sync() here.


> static void start_timer(void)
> {
>      if(!atomic_read(&info->running))
>      {
>          atomic_inc(&info->running);

same race.



^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: repeat a function after fixed time period
  2005-03-23 20:48   ` Arjan van de Ven
@ 2005-03-23 20:56     ` linux-os
  2005-03-23 21:27       ` Arjan van de Ven
  0 siblings, 1 reply; 9+ messages in thread
From: linux-os @ 2005-03-23 20:56 UTC (permalink / raw)
  To: Arjan van de Ven; +Cc: sounak chakraborty, linux-kernel

On Wed, 23 Mar 2005, Arjan van de Ven wrote:

>
>>
>> This kernel code should do just fine.
>>
>>
>>
>> struct INFO {
>>      struct timer_list timer;            // For test timer
>>      atomic_t running;                   // Timer is running
>>      };
>>
>> //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
>> //
>> //   This stops the timer. This must NOT be called with a spin-lock
>> //   held.
>> //
>> static void stop_timer()
>> {
>>      if(atomic_read(&info->running))
>>      {
>>          atomic_dec(&info->running);
>
> this is a race.

No, never. stop_timer() can be called at any time, even from
interrupt context. The last guy to touch info->running wins.
The logic works just perfectly.

>
>>          if(info->timer.function)
>>              del_timer(&info->timer);
>
> you probably want del_timer_sync() here.
>
>
>> static void start_timer(void)
>> {
>>      if(!atomic_read(&info->running))
>>      {
>>          atomic_inc(&info->running);
>
> same race.

No such race at all.


>

Cheers,
Dick Johnson
Penguin : Linux version 2.6.11 on an i686 machine (5537.79 BogoMips).
  Notice : All mail here is now cached for review by Dictator Bush.
                  98.36% of all statistics are fiction.

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: repeat a function after fixed time period
  2005-03-23 20:56     ` linux-os
@ 2005-03-23 21:27       ` Arjan van de Ven
  2005-03-23 22:00         ` linux-os
  0 siblings, 1 reply; 9+ messages in thread
From: Arjan van de Ven @ 2005-03-23 21:27 UTC (permalink / raw)
  To: linux-os; +Cc: sounak chakraborty, linux-kernel

On Wed, 2005-03-23 at 15:56 -0500, linux-os wrote:
> >> static void start_timer(void)
> >> {
> >>      if(!atomic_read(&info->running))
> >>      {
> >>          atomic_inc(&info->running);
> >
> > same race.
> 
> No such race at all.

here there is one; you use add_timer() which isn't allowed on running
timers, only mod_timer() is. So yes there is a race.



^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: repeat a function after fixed time period
  2005-03-23 21:27       ` Arjan van de Ven
@ 2005-03-23 22:00         ` linux-os
  2005-03-24  6:59           ` Arjan van de Ven
  0 siblings, 1 reply; 9+ messages in thread
From: linux-os @ 2005-03-23 22:00 UTC (permalink / raw)
  To: Arjan van de Ven; +Cc: sounak chakraborty, linux-kernel

On Wed, 23 Mar 2005, Arjan van de Ven wrote:

> On Wed, 2005-03-23 at 15:56 -0500, linux-os wrote:
>>>> static void start_timer(void)
>>>> {
>>>>      if(!atomic_read(&info->running))
>>>>      {
>>>>          atomic_inc(&info->running);
>>>
>>> same race.
>>
>> No such race at all.
>
> here there is one; you use add_timer() which isn't allowed on running
> timers, only mod_timer() is. So yes there is a race.
>

Well add_timer() is only executed after the timer has expired
or hasn't started yet so the "isn't allowed" is pretty broad.
If I should use mod_timer(), then there are a _lot_ of buggy
drivers in the kernel because that's how a lot repeat the
sequence. Will mod_timer() actually restart the timer???

If so, I'll change it and thank you for the help.


Cheers,
Dick Johnson
Penguin : Linux version 2.6.11 on an i686 machine (5537.79 BogoMips).
  Notice : All mail here is now cached for review by Dictator Bush.
                  98.36% of all statistics are fiction.

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: repeat a function after fixed time period
  2005-03-23 22:00         ` linux-os
@ 2005-03-24  6:59           ` Arjan van de Ven
  0 siblings, 0 replies; 9+ messages in thread
From: Arjan van de Ven @ 2005-03-24  6:59 UTC (permalink / raw)
  To: linux-os; +Cc: sounak chakraborty, linux-kernel

On Wed, 2005-03-23 at 17:00 -0500, linux-os wrote:
> On Wed, 23 Mar 2005, Arjan van de Ven wrote:
> 
> > On Wed, 2005-03-23 at 15:56 -0500, linux-os wrote:
> >>>> static void start_timer(void)
> >>>> {
> >>>>      if(!atomic_read(&info->running))
> >>>>      {
> >>>>          atomic_inc(&info->running);
> >>>
> >>> same race.
> >>
> >> No such race at all.
> >
> > here there is one; you use add_timer() which isn't allowed on running
> > timers, only mod_timer() is. So yes there is a race.
> >
> 
> Well add_timer() is only executed after the timer has expired
> or hasn't started yet so the "isn't allowed" is pretty broad.

See and the "hasn't started yet" isn't true due to the race. Assume 2
threads both hit the atomic_read() test at the same time, before they
each hit the inc. At some later point one of them will start the timer
and then the other will *again* add_timer on it.

> sequence. Will mod_timer() actually restart the timer???

yes



^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: repeat a function after fixed time period
  2005-03-23 23:13 sounak chakraborty
@ 2005-03-24 12:09 ` linux-os
  0 siblings, 0 replies; 9+ messages in thread
From: linux-os @ 2005-03-24 12:09 UTC (permalink / raw)
  To: sounak chakraborty; +Cc: linux kernel

On Wed, 23 Mar 2005, sounak chakraborty wrote:
>
> --- linux-os <linux-os@analogic.com> wrote:
>> On Wed, 23 Mar 2005, Arjan van de Ven wrote:
>>
>>> On Wed, 2005-03-23 at 15:56 -0500, linux-os wrote:
>>>>>> static void start_timer(void)
>>>>>> {
>>>>>>      if(!atomic_read(&info->running))
>>>>>>      {
>>>>>>          atomic_inc(&info->running);
>>>>>
>>>>> same race.
>>>>
>>>> No such race at all.
>>>
>>> here there is one; you use add_timer() which isn't
>> allowed on running
>>> timers, only mod_timer() is. So yes there is a
>> race.
>>>
>>
>> Well add_timer() is only executed after the timer
>> has expired
>> or hasn't started yet so the "isn't allowed" is
>> pretty broad.
>> If I should use mod_timer(), then there are a _lot_
>> of buggy
>> drivers in the kernel because that's how a lot
>> repeat the
>> sequence. Will mod_timer() actually restart the
>> timer???
>>
>> If so, I'll change it and thank you for the help.
>
>
>   i have applied the code
> as i was intedded to call a function repeated ly in
> fork.c i written the code over there
> it compiled smoothly
> but while booting
> it is showing
> kernel panic no init found
> kjournal starting .commit interval after 5 seconds
>
> sounak
>

*** AWWK *** Sanity check failed.


Cheers,
Dick Johnson
Penguin : Linux version 2.6.11 on an i686 machine (5537.79 BogoMips).
  Notice : All mail here is now cached for review by Dictator Bush.
                  98.36% of all statistics are fiction.

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: repeat a function after fixed time period
@ 2005-03-23 23:13 sounak chakraborty
  2005-03-24 12:09 ` linux-os
  0 siblings, 1 reply; 9+ messages in thread
From: sounak chakraborty @ 2005-03-23 23:13 UTC (permalink / raw)
  To: linux-os, linux kernel


--- linux-os <linux-os@analogic.com> wrote:
> On Wed, 23 Mar 2005, Arjan van de Ven wrote:
> 
> > On Wed, 2005-03-23 at 15:56 -0500, linux-os wrote:
> >>>> static void start_timer(void)
> >>>> {
> >>>>      if(!atomic_read(&info->running))
> >>>>      {
> >>>>          atomic_inc(&info->running);
> >>>
> >>> same race.
> >>
> >> No such race at all.
> >
> > here there is one; you use add_timer() which isn't
> allowed on running
> > timers, only mod_timer() is. So yes there is a
> race.
> >
> 
> Well add_timer() is only executed after the timer
> has expired
> or hasn't started yet so the "isn't allowed" is
> pretty broad.
> If I should use mod_timer(), then there are a _lot_
> of buggy
> drivers in the kernel because that's how a lot
> repeat the
> sequence. Will mod_timer() actually restart the
> timer???
> 
> If so, I'll change it and thank you for the help.
    

   i have applied the code
as i was intedded to call a function repeated ly in
fork.c i written the code over there 
it compiled smoothly 
but while booting 
it is showing
kernel panic no init found
kjournal starting .commit interval after 5 seconds

sounak

________________________________________________________________________
Yahoo! India Matrimony: Find your partner online. http://yahoo.shaadi.com/india-matrimony/

^ permalink raw reply	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2005-03-24 12:09 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2005-03-23 19:43 repeat a function after fixed time period sounak chakraborty
2005-03-23 20:23 ` linux-os
2005-03-23 20:48   ` Arjan van de Ven
2005-03-23 20:56     ` linux-os
2005-03-23 21:27       ` Arjan van de Ven
2005-03-23 22:00         ` linux-os
2005-03-24  6:59           ` Arjan van de Ven
2005-03-23 23:13 sounak chakraborty
2005-03-24 12:09 ` linux-os

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®