Comparison of BSD and Linux timer functions.

Comparison of BSD and Linux timer functions.

Post by FOO CHUN CHOO » Thu, 12 Sep 1996 04:00:00



Hi, in BSD there are two functions called timeout() and untimeout().
        timeout(void (*function), void *data, unsigned long expires);
        untimeout(void (* function), void *data);
where
function is the function to be called when the time is out,
data is some data structure that you will pass to the function to be called,
and expires is the time before the timer is called.

I know that in Linux, the structure that you need to use is the
struct timer_list.

And the functions to add events are add_timer(struct timer_list *),
while the function to delete timer events is del_timer(struct timer_list *).

The struct timer list is declared as follows (in linux/timer.h):
        struct timer_list *next;
        struct timer_list *prev;
        unsigned long expires;
        unsigned long data;
        void (*function)(unsigned long);

My question is how do I port the BSD timeout() and untimeout() functions into
the Linux kernel code. (Or is there any simpler way to do the job?). I've
tried using the code below, it's just that I'm not sure whether that it
will work properly.

/* Linux function definition for timeout() */
void timeout(   void (*function)(unsigned long),
                void *data,
                unsigned long expires)
{
        unsigned long flags;
        save_flags(flags);
        cli();

        data->timer.expires = expires;
        data->timer.function = function(data); /* Is this valid ??? */

        add_timer(timer);

        restore_flags(flags);

Quote:}

/* Linux function definition for untimeout() */
void untimeout(void (*function)(unsigned long), void *data)
{
        unsigned long flags;
        save_flags(flags);
        cli();

        del_timer(data->timer);

        restore_flags();

Quote:}

Also, must I initialize the struct first by calling init_timer(struct
timer_list *) first?

Thanks for the help in advance.
Foo Chun Choong 8-)

 
 
 

Comparison of BSD and Linux timer functions.

Post by Matthias Urlic » Sun, 29 Sep 1996 04:00:00


Hi,



Quote:

> My question is how do I port the BSD timeout() and untimeout() functions into
> the Linux kernel code. (Or is there any simpler way to do the job?). I've
> tried using the code below, it's just that I'm not sure whether that it
> will work properly.

You don't.

Quote:> /* Linux function definition for timeout() */
> void timeout(   void (*function)(unsigned long),
>                 void *data,
>                 unsigned long expires)
> { [...]
>         data->timer.expires = expires;
>         data->timer.function = function(data); /* Is this valid ??? */

ARRGH. You need to understand C. A void* is an opaque pointer with which
you really can't do anything useful.

The BSD interface works this way: The timeout function allocates a
structure where it remembers the function and data, and untimeout scans the
timer list until it finds your intended timeout function and deallocates
the thing. How to handle out-of-memory is a very good question.

Under Linux, you are expected to provide your own copy of the structure,
usually embedded in a data structure, and untimeout() is cheap -- the
structure is just unlinked from a doubly-linked list.

It's certainly possible to have a glue routine which implements the BSD
semantics -- I've written one myself -- but it's far better to just toss
the BSD way of handling timeouts, and replace them with the Linux way.

--
To keep your friends treat them kindly; to kill them, treat them often.
--
Matthias Urlichs         \  noris network GmbH  /  Xlink-POP Nrnberg

90491 Nrnberg (Germany)   \    Consulting+Programming+Networking+etc'ing
   PGP: 1024/4F578875   1B 89 E2 1C 43 EA 80 44  15 D2 29 CF C6 C7 E0 DE
       Click <A HREF="http://info.noris.de/~smurf/finger">here</A>.    42

 
 
 

1. Comparison needed: *BSD vs. Linux

I am new in non Micro$oft OS area. As I was learning about Linux, I found
out about *BSD.

I am wondering what are the differences between FreeBSD, NetBSD, OpenBSD
and Linux. You know, good sides and bad sides. Anyone with some spare
time?

Thanks,
Sasa

------------------
My real email is: sasab AT sezampro DOT yu

2. FreeBSD and PHP4 and SYBASE

3. Linux/BSD network performance comparison

4. I'm sorry [it was: Nazi Kernels]

5. Comparison between Linux and NT, FreeBSD, SCO and BSD/OS

6. Application Monitoring

7. Timer Function in Linux?

8. Question about init/inittab on SVR4

9. Timer function in Linux

10. Linux Timer Functions

11. Timer functions in Linux

12. Timer Function in Linux?

13. TIMER - FUNCTION under LINUX