top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

What is meant by FSM in c and what are the uses?

+5 votes
461 views

What is meant by FSM in c and what is the uses and where we are using can anyone explain with an Example?

posted Oct 22, 2013 by Giri Prasad

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

0 votes

FSM stands for Finite State Machine, it has no relation with C rather it has more generic meaning which is a State Machine which has finite number of states.

A state is a description of the status of a system that is waiting to execute a transition. A transition is a set of actions to be executed when a condition is fulfilled or when an event is received. In some finite-state machine representations, it is also possible to associate actions with a state:
Entry action: performed when entering the state,
Exit action: performed when exiting the state.

See the following sample (simple) state machine

typedef void transition_func_t( instance_data_t *data );

void do_initial_to_foo( instance_data_t *data );
void do_foo_to_bar( instance_data_t *data );
void do_bar_to_initial( instance_data_t *data );
void do_bar_to_foo( instance_data_t *data );
void do_bar_to_bar( instance_data_t *data );

transition_func_t * const transition_table[ NUM_STATES ][ NUM_STATES ] = {
    { NULL,              do_initial_to_foo, NULL },
    { NULL,              NULL,              do_foo_to_bar },
    { do_bar_to_initial, do_bar_to_foo,     do_bar_to_bar }
};

state_t run_state( state_t cur_state, instance_data_t *data ) {
    state_t new_state = state_table[ cur_state ]( data );
    transition_func_t *transition =
               transition_table[ cur_state ][ new_state ];

    if ( transition ) {
        transition( data );
    }

    return new_state;
};
answer Oct 22, 2013 by Bob Wise
...