FSM in C?

M

Michael

Hello world,

maybe this query is a little out of topic,
but I need a finite state machine in C.
I think plenty of people have already done
that, so I'm looking for some hints and
maybe strategies.
I already tried google, but the results are,
well, lets say uncountable :)

My approach was to define a state via a
struct, consisting mainly of three function
pointers:
stateAction ........... Does the Activity
stateTransition ....... Cares about the next states
stateInit ............. Called once to init

I have second struct combining holding an array of
pointers to all states and an Information about the
current one. My problem is how to put all the needed
data to the state-functions without global variables?
So far a created a struct of pointers and added a
pointer to this to the "state-struct". Is there a better
way (sure there is, isn't it)
Thanks in advance,
Michael
 
T

Thomas Matthews

Michael said:
Hello world,

maybe this query is a little out of topic,
but I need a finite state machine in C.
I think plenty of people have already done
that, so I'm looking for some hints and
maybe strategies.
I already tried google, but the results are,
well, lets say uncountable :)

My approach was to define a state via a
struct, consisting mainly of three function
pointers:
stateAction ........... Does the Activity
stateTransition ....... Cares about the next states
stateInit ............. Called once to init

I have second struct combining holding an array of
pointers to all states and an Information about the
current one. My problem is how to put all the needed
data to the state-functions without global variables?
So far a created a struct of pointers and added a
pointer to this to the "state-struct". Is there a better
way (sure there is, isn't it)
Thanks in advance,
Michael

There are run-time versions (where the state transition
description is in a file) to hard-coded versions. Some
implementations use tables, others use switch statements.

I just recently (last month) came up with an implementation
that uses a "global" function pointer. I thought it would
be more efficient, but since it was "new and risky" code,
it was rejected. :-(

If the transistions are of a uniform type, you could
use a table of function pointers.

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book
 
R

Roberto Nunnari

Michael said:
Hello world,

maybe this query is a little out of topic,
but I need a finite state machine in C.

You may take a look at my FSM generator and look at the code it
generates.. there may be better solutions, but for my needs
is good enough.. in practice it uses a modified version
of the state patter.. and has structures with function pointers.

http://nunnifsmgen.nunnisoft.ch/en/

It's open source and distributed with GNU LGPL licence.

You'll need the java runtime to run it.

Best regards.
--
Roberto Nunnari -software engineer-
mailto:[email protected]
http://www.nunnisoft.ch
Residenza Boschetto 12 tel/fax: +41-91-6046511
6935 Bosco Luganese """ mobile: +41-76-3208561
Switzerland (o o)
========================oOO==(_)==OOo========================
 
J

Jason Spashett

Like OO, Opinions on State oriented programing vary. There are some good
resources on the net about this. www.embedded.com has a reasonable one if I
remember rightly.

I have used a "nested state machine" in an XML parser. It uses a stack of
states to facilitate state reuse/decomposition. (
http//mantrid.freeshell.org/ConveX )

The question to ask when thinking of state machine might be do I want a flat
state machine, or a hierarchical or nested one.
( There is the question of machine generated state machines also )

Flat state machines are fairly simple, and are often implemented with a
single level of C switch statements.

Hierarchical usualy follow the UML state chart spec in some way, and you
often define the state heirarchy before program execution.

Nested state machines are those machines that can execute other state
machines, and return afterwards to the caller state machine. This semantics
are similar to functional call/return, but have the advantage of
asynchronicity.
 
E

Ed Morton

Michael said:
Hello world,

maybe this query is a little out of topic,
but I need a finite state machine in C.
I think plenty of people have already done
that, so I'm looking for some hints and
maybe strategies.
I already tried google, but the results are,
well, lets say uncountable :)

My approach was to define a state via a
struct, consisting mainly of three function
pointers:
stateAction ........... Does the Activity
stateTransition ....... Cares about the next states
stateInit ............. Called once to init

So, if you were designing the control for some hardware device that can
be either on or off, like a light bulb, you'd presumably have states
named "On" and "Off", each of which has a corresponding action
"ActivateHW" and "DeactivateHW" and a transition to the other state
based on someone flicking a switch, right?

Some questions to consider are:

1) What if you chenge state from On to Off and invoke your DeactivateHW
routine but deactivating the HW fails? You're SW is already in the Off
state, but your HW device is still running - could be dangerous and
handling it can get awkward.

2) What if you want to accept some secondary stimuli in some or all of
your states, e.g. status requests from other entities? Would you need to
change state to some state like "OnAndGotAStatusRequest" just to process
that request and then transition back to "On" where you'd again attempt
to activate the already active HW? Or maybe you'd use a hierarchical
model where you descend a level to handle that stimulus and then come
back up (carefully avoiding re-executing any of the higher-level
state-specific actions). You can create a lot of states either way that
have no equivalent in the real world, and you can run into some nasty
problems performing the same actions repeatedly.

Bascially, if this is a real commercial application and not just a
simple model, you might want to consider a Mealy FSM instead of Moore,
and take a look at some of the documentation at
http://www.stateworks.com/active/index.html before adopting any specific
implementation.

Ed.
 
G

Gregory Pietsch

Michael said:
Hello world,

maybe this query is a little out of topic,
but I need a finite state machine in C.
I think plenty of people have already done
that, so I'm looking for some hints and
maybe strategies.
I already tried google, but the results are,
well, lets say uncountable :)

My approach was to define a state via a
struct, consisting mainly of three function
pointers:
stateAction ........... Does the Activity
stateTransition ....... Cares about the next states
stateInit ............. Called once to init

I have second struct combining holding an array of
pointers to all states and an Information about the
current one. My problem is how to put all the needed
data to the state-functions without global variables?
So far a created a struct of pointers and added a
pointer to this to the "state-struct". Is there a better
way (sure there is, isn't it)
Thanks in advance,
Michael


I did a FSM in C for problem 1.23 on Richard Heathfield's site.

Gregory Pietsch
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,040
Latest member
papereejit

Latest Threads

Top