Real Time Programs, Databases and Threads.

I'm trying to understand real time programming and found this sample program online.
Why the need for threading?
Assuming there is a genuine need for threading, what about a separate task rather than a separate thread?
What's the advantage of doing it this way rather than using a timer.
What's the best way to update a csv file with the contents of a Database table as near to real time as possible?


using System;
using System.Windows.Forms;
using System.Threading;
using System.Diagnostics;
namespace Project1
{
    public partial class Form1 : Form
    {
        private Thread cpuThread;
        private double[] cpuArray = new double[30];

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }
        private void getPerformanceCounter()
        {
            //override 3 of 6
            var cpuPerfCounter = new PerformanceCounter("Processor Information", "% Processor Time", "_Total");
            while (true)
            {
                cpuArray[cpuArray.Length - 1] = Math.Round(cpuPerfCounter.NextValue(), 0);
                Array.Copy(cpuArray, 1, cpuArray,0, cpuArray.Length - 1);
                if (cpuChart.IsHandleCreated)
                {
                    this.Invoke((MethodInvoker)delegate { UpdateCpuChart(); }); 
                }
                else
                {

                }
                Thread.Sleep(1000);
            }
        }
        private void UpdateCpuChart()
        {
            cpuChart.Series["Series1"].Points.Clear();
            for (int i=0;i<cpuArray.Length-1;i++)
            {
                cpuChart.Series["Series1"].Points.Add(cpuArray[i]);
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            cpuThread = new Thread(new ThreadStart(this.getPerformanceCounter));
            cpuThread.IsBackground = true;
            cpuThread.Start();
        }
    }
}

Open in new window

AlHal2Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

nociSoftware EngineerCommented:
realtime programming is about meeting deadlines all the time or 99.999+% of the time....
Every Real time task needs to be predictable in resource consumption, latency, run time.

Mostly real time programming has to deal with real world events.
Think about the ABS system in your car.   It needs to monitor wheels and if needed modulate brakes to ensure they keep turning...
And the car is still stopping .....  If it starts to miss pulses from wheels due to sampling too late, the wheel might be  seen as stopped...and breaks  released... causing stopping problems.

In other processes a production line needs to be controlled. (many actuators,  man sensors) and you still need 1000's - 100000's of products to leave the line / hour.  Missing one item could mean a pile-up of (semi)products  inside some machine....

Now searching databases can be a real issue in realtime systems.
How would you implement a timer? runing a loop wit some known latencies inside the loop?  or waiting for a foreign event.
Using task can make use of a sleep so there is no scheduling overhead, and the wakeup would mean some data is available or some process is needed.  tight looping for delays takes CPU capacity  that could be needed elsewhere.
0
AlHal2Author Commented:
Thanks for explaining the issues and giving meaningful examples.  Do you have any sample code or a link to some code?
0
Martyn SpencerSoftware Developer / Linux System Administrator / Managing DirectorCommented:
Noci's explanation is good and I think you will find that providing code examples is possibly a little meaningless unless you have a very specific task in mind.

To answer your questions:


Why the need for threading?
Assuming there is a genuine need for threading, what about a separate task rather than a separate thread?
What's the advantage of doing it this way rather than using a timer.
What's the best way to update a csv file with the contents of a Database table as near to real time as possible?

Threads are used to allow multiple tasks to occur at the same time. Its important to note that this may just be the impression of multiple tasks occurring at the same time. Naturally, if you have a CPU that has 8 cores, it can run 8 threads simultaneously, but there may be contention issues between the threads that affect performance (the need to access and store shared data, for example).

A simple example may be a monitoring thread, an action thread (I can't think of a better name for this at the moment) and a database thread. The monitoring thread needs to be able to guarantee that it will sample the environment at fixed and guaranteed intervals. The action thread must guarantee to take the monitoring events and react and produce certain output in a guaranteed time frame. The database thread could be used for storing monitoring data, exporting data etc.

A real time system can be made to guarantee that critical threads are granted enough time to complete their required actions. A non-real-time system may be able to meet the requirement, but it will offer no guarantees. In some situations, the guarantee is vital and in others it is not. it depends on the situation.

For example, many database operations atomic in nature but a database server probably will not guarantee response times, using a thread would allow the operating environment to take this into consideration. While a program is waiting for a database response, the database thread can be suspended thus releasing resources to other threads - the monitoring thread, for example. This does not mean that the database thread is necessarily responding in real-time. It may be, but then again it may not. This comes down to how you design your database and the functions it carries out.

If create a database, and through testing guarantee that 99.999% of the time it will carry out an action within the required time interval it is allowed, you could consider it to be "real-time" for your purposes. You would still need to potentially allow for the 0.001% chance of it not responding in time.

Remember, "real-time" could be different in different systems. It may be sufficient to respond to real world events every minute. It may be necessary to respond every 0.00001 seconds. The point of a real time system is that it can give these guarantees.

When you say
What's the best way to update a csv file with the contents of a Database table as near to real time as possible?
What you really need to ask is "What is the best way to extract the contents of a database table into a CSV file so that I can guarantee that X rows will be exported in X timeframe". That then becomes an issue of designing a system that is able to achieve this and to then test that it can guarantee it for the purposes of your problem.

With regard to using a timer, they are usually implemented by some form of thread or interrupt based event. Exporting records from a database using a timer may happen in a different thread, but that would depend on the timer implementation. Also, timer events should generally be kept simple and extracting data from a database and exporting it (in my mind) does not qualify under that principle. So, I would not use timer events to code such a thing.

If you do not use threads, it is almost impossible to guarantee that a process will not become blocked waiting for something, so generally where an application needs to appear to perform several things simultaneously, threads are used.

My explanation may well miss out important detail, but it's all I have time to write at the moment. Apologies for any typos - I don't have time to re-check what I have typed, but I will come back later and re-read.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

nociSoftware EngineerCommented:
I did my share on realtime programming.  every case needs to be examined on merits.  
#1:  What are the specs to be followed... (this might highly depend on available sensors, hardware..)
#2:  Analyze those and translate them to the requirements for software.
#3:  Select you tool of choice (That might include offloading specific tasks to specific controllers...)
    f.e. to see if a wheel still turns you could count some pulses from a wheel (say 1000 pulse / 1 rotation)  on your CPU or you can build a small counter sensor that just gives the number of pulses since last query...  and query the new sensor slightly less often. (think 4 or 6 wheel and you are looking at 4000-6000 interrupts / few seconds. requiring a fast cpu).  of something that can be queried a few times per second...
#4 select the hardware to run on...
#5 build in available toolchains.

This might be iterative...  ALL code needs to be as efficient as possible....

w.r.t. Databases the MIMER database can guarantee response times for some type of data handling that is the only one i can think of right now.
0
AlHal2Author Commented:
In other processes a production line needs to be controlled. (many actuators,  man sensors) and you still need 1000's - 100000's of products to leave the line / hour.  Missing one item could mean a pile-up of (semi)products  inside some machine....

Is this example specific enough for requesting a code sample?  If not, please tell me what is missing?
By the way, I thought this article looked interesting http://dev.ti.com/tirex/content/simplelink_academy_cc2640r2sdk_1_14_02_04/modules/rtos_concepts/rtos_concepts.html
0
Martyn SpencerSoftware Developer / Linux System Administrator / Managing DirectorCommented:
Is this example specific enough for requesting a code sample?
Not for me. It would depend on the sensors, the exact number and type, their characteristics, the physical connection method, communication protocol, timings etc. It would also depend on the hardware that will be controlling the process. As the article linked mentions, you may choose a bare metal solution (something as simple as an Arduino could be used in a deterministic fashion to control a real-time process) or a real time OS.

Personally, I am not really sure what you want to see illustrated in code. One could possibly produce some pseudo-code to describe a particular problem but I don't think it's what you are asking for. You yourself could try creating a theoretical situation and then try describing what needs to happen in pseudo code. We could then comment on what you do to help you identify issues that we feel need to be addressed.
0
AlHal2Author Commented:
Thanks both.  I'll ask a new question when I have something more specific.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Programming

From novice to tech pro — start learning today.