Lab 2: FSM Implementation

In this lab, you will build a table of transitions and function pointers to implement a finite state machine in C. FSM implementations frequently adhere to standard templates. Section 1.7 of the book illustrates a lot of the code that you will need for this lab.


Preliminaries

Set up a bare repository on stu.cs.jmu.edu based on the instructions in the CS 361 Submission Procedures, using the name lab2-fsm.git.


Implementing states and transitions

The FSM that you will implement is a model of states that a process goes through during its life cycle:

State model of a FSM with five states

Completing the following requirements will satisfy the minimum required functionality:

  • Edit the transition table defined in procmodel.c to match the model above. Correctly set up the fields of the fsm_t. in process_init().
  • Create a helper function that fsm->transition points to. This function will perform the transition lookup, returning the next state after the transition. Note that the function parameters must match the transition declaration in statemodel.h.
  • Complete the implementation of handle_event() in statemodel.c. Be sure to perform robust error handling for invalid events and transitions. Use an assert() to ensure that the passed fsm parameter is never NULL. Change state only if the table indicates that the next state is valid (something other than NST or a negative value).
  • Ensure that your code is passing all unit tests at this point.
  • Create lookup tables for the transition effects and entry actions for the given FSM. Update the transition function (from step 2) so that it returns the appropriate effect and entry through the call-by-reference parameters.
  • Update the main.c to loop through command-line arguments, treating each as an event. Note that you will need to convert these arguments (which are strings) to their equivalent integer value. Return EXIT_FAILURE for any bad arguments.


James Madison University logo


© 2011-2024 Michael S. Kirkpatrick.
This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.