counter in the handler routine.
Every time a character is sent from the keyboard, the SIGPOLL signal will be sent to the
process. The signal handler will read the character, and reset itself to be the handler.
Implementing a Finite State Machine in C
A finite state machine is a mathematical concept that can be very useful when embodied in a program.
It's a protocol for progressing through a limited ("finite") number of subroutines ("states"), each of
which does some processing and then chooses the next state, usually based on the next piece of input.
A finite state machine (FSM) can be used as the control structure of a program. FSMs are well-suited
to programs that loop over several different alternative actions based on input. A coin-operated
vending machine is a good candidate for an FSM. It will have states like "accept coin", "select item",
"deliver item", and "make change". The inputs will be coins, and the outputs will be the items for sale.
The basic idea is to have a table that holds all the possible states, and lists the actions to do when you
enter each state. The last action is to calculate (often by a further table lookup based on the state you
are in and the next input token) what state to enter next. You start in a state known as the "initial
state." Along the way, your transition table might tell you to enter an error state, signifying an
unexpected or erroneous input. You continue to make state transitions until you arrive at the end state.
There are several ways to express an FSM in C, but most of them are based on an array of pointers to
functions. An array of pointers to functions can be declared like this:
void ( *state[MAX_STATES] )();
If you know the function names, you can initialize the array like so:
extern int a(), b(), c(), d();
int (*state[])() = { a, b, c, d };
A function can be called through a pointer in the array like this:
(*state[i])();
The functions must all take the same arguments and have the same return value (unless you make the
array element a union...). Pointers to functions are funny. Notice, too, how the pointer can be dropped,
so our call can equally be made as