Event-driven finite-state machine

In computation, a finite-state machine (FSM) is event driven if the transition from one state to another is triggered by an event or a message. This is in contrast to the parsing-theory origins of the term finite-state machine where the machine is described as consuming characters or tokens.

Often these machines are implemented as threads or processes communicating with one another as part of a larger application. For example, a telecommunication protocol is most of the time implemented as an event-driven finite-state machine.

Example in C

This code describes the state machine for a very basic car radio system. It is basically an infinite loop that reads incoming events. The state machine is only 2 states: radio mode, or CD mode. The event is either a mode change from radio to cd back and forth, or a go to next (next preset for radio or next track for CD).

/********************************************************************/
#include <stdio.h>

/********************************************************************/
typedef enum {
        ST_RADIO,
        ST_CD
} STATES;

typedef enum {
        EVT_MODE,
        EVT_NEXT
} EVENTS;

EVENTS readEventFromMessageQueue(void);

/********************************************************************/
int main(void)
{
  /* Default state is radio */  
  STATES state = ST_RADIO;
  int stationNumber = 0;
  int trackNumber = 0;

  /* Infinite loop */
  while (1)
  {
    /* Read the next incoming event. Usually this is a blocking function. */
    EVENTS event = readEventFromMessageQueue();

    /* Switch the state and the event to execute the right transition. */
    switch (state)
    {
      case ST_RADIO:
        switch (event)
        {
          case EVT_MODE:
            /* Change the state */
            state = ST_CD;
            break;
          case EVT_NEXT:
            /* Increase the station number */
            stationNumber++;
            break;
        }
        break;

      case ST_CD:
        switch (event)
        {
          case EVT_MODE:
            /* Change the state */
            state = ST_RADIO;
            break;
          case EVT_NEXT:
            /* Go to the next track */
            trackNumber++;
            break;
        }
        break;
    }
  }
}
gollark: Notilluminati.
gollark: Not *everything* is illuminati.
gollark: Because he's the Illuminati. 3 sides on a triangle - this is elementary stuff.
gollark: From the Rust compiler.
gollark: int_set_to_infinityec_GFp_simple_group_initCONF_modules_load_fileX509v3_asid_add_id_or_rangeEC_POINT_set_compressed_coordinatestls_process_client_helloSSL_CONF_CTX_finishADMISSIONS_itRSA_OAEP_PARAMS_iti2d_DSAPrivateKeydtls1_buffer_messageOPENSSL_sk_reserveASN1_TIME_to_tmlzma_lzma_decoder_inittls13_export_keying_materialEVP_MD_CTX_test_flagstls_construct_key_updateclock_gettime@@GLIBC_2.2.5lzma_stream_flags_compareX509at_get_attrRSA_PSS_PARAMS_freeASN1_STRING_get0_dataGENERAL_NAME_cmpBN_freepolicy_node_matchSHA384_UpdateBN_mod_expOPENSSL_fork_childEVP_cast5_cbcsigaltstack@@GLIBC_2.2.5tls_parse_ctos_cookieossl_statem_check_finish_initreadv@@GLIBC_2.2.5ssl_undefined_void_functionCRYPTO_gcm128_tagpthread_mutex_unlock@@GLIBC_2.2.5X509_get_default_cert_dirossl_ctype_checkconf_modules_free_inti2d_SCT_LISTASN1_TYPE_getOPENSSL_LH_doall_argDES_ede3_cfb64_encryptrand_pool_entropy_neededtls_validate_all_contextsRC2_cfb64_encryptEVP_bf_ecbtls_parse_ctos_session_ticketEVP_sha1fcntl@@GLIBC_2.2.5ASN1_STRING_set0bn_group_4096OCSP_SINGLERESP_get1_ext_d2isetgroups

See also

Further reading

  • Peatman, John B. (1977). Microcomputer-based Design. New York: McGraw-Hill, Inc. ISBN 0-07-049138-0.
  • Brookshear, J. Glenn (1989). Theory of Computation: Formal Languages, Automata, and Complexity. Redwood City, California: Benjamin/Cummings Publish Company, Inc. ISBN 0-8053-0143-7.
This article is issued from Wikipedia. The text is licensed under Creative Commons - Attribution - Sharealike. Additional terms may apply for the media files.