Event-driven Model in C with Sockets

iNDicator picture iNDicator · Jun 16, 2012 · Viewed 30.9k times · Source

I am really interested in event-driven programming in C especially with sockets so I am going to dedicate some time doing my researches.

Let's assume that I want to build a program with much File and Network I/O like a client/server app, basically, the first question is what is the philosophy behind this model. While in normal programming I would spawn new processes, how come a single process can actually serve many other requests. For example, there are some web-servers which can handle connections without creating threads or other processes, just one main process.

I know this is complicated but it's always nice to know how different solutions work.

Answer

Frunsi picture Frunsi · Jun 16, 2012

You definitely must read the following: http://www.kegel.com/c10k.html. That page is the perfect overview of event-driven and asynchronous techniques.

However, a quick & dirty answer: event-driven is neither non-blocking, nor asynchronous.

Event-driven means, that the process will monitor its file descriptors (and sockets), and act only when some event occurs on some descriptor (events are: data received, error, became writeable, ...).

BSD sockets have the "select()" function. When called, the OS will monitor the descriptors, and return to the process as soon as some event on one of the descriptors occurs.

However, the website above has much better descriptions (and details about the different APIs).