Page 1 of 1

Implementing events (C)

Posted: Mon May 17, 2010 11:41 pm
by chibicitiberiu
I know what events are, really useful for an os. The question I have, is how to implement them in C?

Re: Implementing events (C)

Posted: Tue May 18, 2010 12:28 am
by Solar
Usually you register callbacks for an event. Check out the documentation for the function signal() in <signal.h> - it does just that.

Re: Implementing events (C)

Posted: Tue May 18, 2010 6:14 am
by gravaera
Hi,

You have the userspace process register an entry point for that event. The kernel keeps a list of events, and a linked list of all handlers and their process IDs for each event. When the event takes place, you spawn threads for each handler entry in the list, thereby generating an asynchronous call into each process.

There's a caveat which goes with this:

If the process registers a commonly generated event, and it fails to properly terminate its handler, (or it maliciously places an idle loop in the handler), you'll end up with N threads for that process, N is the number of times that event has been generated since the kernel took that process's event into its accounting. You could have a count of the number of currently dispatched events per process, and limit it to a certain maximum.

--All the best,
gravaera

Re: Implementing events (C)

Posted: Tue May 18, 2010 12:45 pm
by chibicitiberiu
Thanks for replies.

The problem is I'm not so good at programming threads... my kernel barely shows stuff on the screen and has some basic drivers (keyboard,...). There's a lot to learn until then...

Re: Implementing events (C)

Posted: Tue May 18, 2010 4:16 pm
by Gigasoft
"Event" is a generic term which can mean several things.

On Windows, an event is something that you wait for. A thread can wait for multiple events (and other objects) at once, up to 64 at the same time. When one of the events is signaled, or if an event was already signaled when the wait was issued, the event is reset and the thread is woken up. So, here an event is similar to a semaphore with a maximum count of 1.

Unix signals that are sent from a thread to another are similar to what is known as Asyncronous Procedure Calls in Windows. This is a callback registered with a thread that is called the next time the system is about to return to that thread. I have never found an use for this technique.

To implement waitable events (or semaphores), each event should contain a waiting list. When a thread waits for a set of events, an array of entries describing the wait is allocated and they are added to their respective events' wait lists. Each entry should reference the waiting thread. The address and size of the array is stored with the thread. When an event is signaled and there are threads waiting for it, an entry should be removed from the waiting list, and all the other entries from the table whose address is saved in the thread should be removed from their respective lists. The thread is then woken up, and an identifier is passed to it identifying the event that caused it to wake up. This could be the index of the wait entry within its array.