Tuesday, November 17, 2009

Event Driven Programming

The traditional method of socket programing is multi thread (pthread) or multi process i.e(fork).
But using them requires synchronization, locking and others. People who have used them may realize that some times it becomes a nightmare to trace a problem/crash.

One can still process multiple client request in a single thread, without blocking on the request. This is achieved by using event driven programing.

How to do
1. Use event notifiers such as poll/select/epoll/kqueue

2. Put your connections (i.e the socket file descriptors ) in non-blocking mode.
By doing so you do not wait for the entire data to be written or the entire data to be read at one go. You will read/write in chunk if buffer space or data is available else you can go a head doing other works like process the other client requests, by putting the current connection back into event notifier.

3. Maintain state information, in order to take appropriate action when a event takes place. Unlike the multi threaded, where a single thread as sequential logic.In event driven you will process a connection when a event occurs, for this you need to keep track about the state in which you left it last time.

4. Minimize the use of systems call or your code which can go into blocking, to avoid starvation of other connections.

Advantages
1. It saves the time in context switching, due to threads/process getting scheduling.
2. Saves the trouble of maintaining locking/synchronization (you may still have to use them in event driven programing, but to a small extent)


Sunday, November 15, 2009

sqlite3 Quick reference

I always keep forgetting the syntax of sqlite command line queries

Here are some which I use more frequently

1. To see the table names present in the current database
sqlite> .tables

2. To see the databases and files
sqlite> .databases

3. To know the structure of table
sqlite> .schema

4. Create a table
CREATE TABLE dummy (key varchar(250) UNIQUE, value BLOB, pid bigint )

5. To alter a table
sqlite> alter table dummy add key varchar(250);

6. To exit from sqlite shell, this I always keep forgetting.
sqlite> .quit


7. For help
sqlite> .help

8. How to find which process is locking the database
fuser  path_to_sqlite_file

Followers