Applies To:
  • CitectSCADA 1.00 1.01 1.10 1.11 1.20 2.00 2.01

Summary:
Question: How does Cicode multitasking work, is it truly preemptive?

Solution:
Citect provides true multitasking and preemption support in the cicode language. This is true even though Windows 3.x does not provide all of this functionality.

Multitasking

Multitasking is when you can run more than one task at the same time. Windows supports this feature at the application level. For example you can run Word and Excel at the same time.

Citect also supports multitasking internally, that is you can tell Citect to do something and before Citect has completed that task you can tell Citect to start some other task. Citect will perform both tasks at the same time. Most applications cannot do this, for example if you start a macro in Excel, while that macro is running you cannot do any other operation in Excel until that macro completes.

With Citect you can start one command and before that command completes you may start another command. This is a feature of Citect not the operating system. Even if the operating system supports true multitasking and preemption it does not mean that the applications support it. For example if you run Excel with Windows NT you still must wait for one macro to complete before you can start another. The multitasking support must be built into the application. This is true of most applications and programming tools. For example Visual Basic is also single tasking.

Multitasking operating systems just allow you to run more than one application at the same time. You should not get confused into believing that if an application is running under a multitasking operation system that it truly support multitasking. That is don't specify a multitasking operating system if you want multitasking support. That is only half the picture, the application itself must also provide support for multitasking.

You can usually tell if an application support multitasking if you can execute code as shown below. This code has a WHILE loop which executes continuously. If the application does not support multitasking it will either not provide a WHILE operator, or if it does it will hang the application if you execute an infinite loop, or the application will not hang but will not allow you to execute another WHILE operator at the same time.

Preemption

Preemption is when one task can interrupt the execution of another task. Windows version 3.xx does not provide this support directly. Windows provides a variation called nonpreemptive multitasking and relies on an application voluntarily relinquishing control to the operating system before letting another application execute. In a nonpreemptive multitasking scheme, the amount of time a task is allowed to run is determined by the task, whereas in a preemptive scheme, the time is determined by the operating system.

With cicode, Citect is acting as the operating system and Citect will preemptive any cicode when it has exhausted it's time slice or some other task has a higher priority. So for example it to totally valid to write cicode which goes around in endless loops without stopping other cicode and Citect tasks from executing. You may test this feature by calling the function below from the right mouse button on a blank page (use TaskNew("TestPreemption", "", 1) to call the function so this task will be killed when you close the page):

FUNCTION
TestPreemption()
   INT count, hAN, x, y;

   DspGetMouse(x, y);
   hAN = DspAnNew(x, y);
   WHILE TRUE DO
      DspStr(hAN, "", "Count " + count : ####);
      count = count + 1;
   END
END

This function will get the mouse position and then create a new animation point at this position and start displaying the value of the counter. You should be able to click the mouse on a page many times to show lots of cicode threads running at the same time. Each cicode thread does not stop the other thread or Citect from operating correctly. As you increase the number of cicode task you will be loading up the CPU of the computer so each thread will run slower. This is an example of what you can do and how Citect preemption multitasking is working. You should normally not write hard loops, but use the event system and always call the Sleep() function in examples like these.

You should also notice that Citect uses a round robin single priority scheduling for cicode. With this type of scheduling each cicode task gets the same priority and if they all want to use the CPU they will each get a turn in sequence. This is a simple and reliable method of scheduling. Citects internal tasks are scheduled at a higher priority than cicode and they get the CPU they want before the cicode. For example the alarms, trends and I/O Server tasks all get the CPU before any of your cicode tasks. The reports are scheduled at the same priority as your cicode and Citect background spoolers and other idle tasks are lower priority than your cicode.

In answer to the question Citect provides true preemptive multitasking of your cicode. This allows you to develop powerful real time process applications. Citect is partly preemptive between Windows applications. If a protocol driver supports hardware interrupts (this includes the COM port, DigiBoards and various PLC Interface cards), then that protocol will run under interrupt and will preemptive all other Windows applications. For example if Excel is running then a response is returned from a TIWAY PC card, Citects I/O Server TIWAY driver will preempt Excel, service the response then send the next request to the PLC. This keeps the real time data flowing when any Windows application is running. Citect is not preemptive between other Windows application for normal tasks, for example updating the graphic display. If Excel is running Citect must wait for Excel to give up the CPU before Citect can update the graphics display.


Keywords:
 

Attachments