Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Event-driven Model in C with Sockets

Tags:

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.

like image 378
iNDicator Avatar asked Jun 16 '12 10:06

iNDicator


People also ask

What is event-driven programming in C?

Event driven programming is based on an event loop. The loop simply waits for a new event, dispatches code to handle the event, then loops back to wait for the next event.

What is an example of event-driven programming?

Event-driven programming is a programming paradigm in which the flow of program execution is determined by events - for example a user action such as a mouse click, key press, or a message from the operating system or another program.

What is an event drive model?

Overview. Event-driven architecture is a software architecture and model for application design. With an event-driven system, the capture, communication, processing, and persistence of events are the core structure of the solution. This differs from a traditional request-driven model.

What is an event-driven program and what is it used for?

An event-driven application is a computer program that is written to respond to actions generated by the user or the system. In a computing context, an event is any identifiable occurrence that has significance for system hardware or software.


2 Answers

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).

like image 117
Frunsi Avatar answered Oct 03 '22 21:10

Frunsi


"what is the philosophy behind this model"

Event driven means there is no "monitoring", but that the event itself initiates the action.

Usually this is initiated by an interrupt, which is a signal to the system from an external device, or (in the case of a software interrupt) an asynchronous process.

https://en.wikipedia.org/wiki/Interrupt

Further reading seems to be here:

https://docs.oracle.com/cd/E19455-01/806-1017/6jab5di2m/index.html#sockets-40 - "Interrupt-Driven Socket I/O"

Also http://cs.baylor.edu/~donahoo/practical/CSockets/textcode.html has some examples of Interrupt-Driven Sockets, as well as other socket programming examples.

like image 38
Graham Avatar answered Oct 03 '22 21:10

Graham