Scheduling Task in Using C++ on Linux and Windows Machine

Scheduling Task in using C++ On linux and Windows machine

In QT, scheduling can be done with QTime and QTimer.

  • Simple: start a timer per scheduled event based on the difference between now and event-time
  • more scalable (1000 requests): maintain a container of events and start 1 timer to the first event.

Schedule task on precise periods in Linux or Windows

Yes, you can do that in MS-DOS because it is not a multi-user or multi-tasking operating system. However, the same thing will not work in Windows because it is a mult-user and multi-tasking operating system. It's also not real-time, which means there's no guarantee that your task will be executed exactly when you ask for it to be executed. Everything is pre-emptively scheduled, meaning that any number of other processes and tasks (either user-mode or system-level) could effectively "bump" your process down the priority list and force it to wait to be executed until those other tasks completed or were themselves interrupted to give your process a chance to run for a while.

I don't know about Linux, but I imagine most of the major distributions are written similarly to Windows.

You will need to find a real-time, single-user operating system to do this. A Unix-derivative is probably the best place to start looking, but I won't be the person able to suggest one.

Alternatively, you could continue using MS-DOS (or alternatives such as FreeDOS), but switch to a different interface technology that is available on newer boards. There's no reason to update something that works for you, especially if the updates are counter-productive to your goal.

how to schedule two tasks?

Your example is trivial and can be scheduled without resorting to any OS provided scheduling or even OS timing services, however in general (for non-trivial requirements) in Windows, you would use multi-threading and allow the OS to perform the scheduling. main() is already a thread, so you only need create one other. In its simplest form:

#include <stdio.h>
#include <windows.h>

DWORD WINAPI OneMinthread( LPVOID lpParam )
{
for(;;)
{
printf("It will be printed in every 1 min \n");
Sleep(60000) ;
}
}

int main()
{
CreateThread( NULL, 0, OneMinthread, 0, 0, 0) ;
for(;;)
{
printf("It will be printed in every 2 min \n");
Sleep(120000) ;
}
}

See Creating Threads for a more complete treatment of threading in Win32. Be aware that the .Net framework also provides a simpler class based interface to threading.

Schedule `at` jobs with cygwin

The problem is that even though the path to python is in the system path, when a task is scheduled, this path does not take effect. Therefore, the path to python.exe needs to be explicitly supplied. Therefore, replacing

python "c:\\cygwin\\home\\%(home)s\\project\\dir\\scenario%(scenario)s\\p%(popsize)sh%(height)st%(tournsize)sc%(crossprob)sm%(mutprob)s\\GP%(run)s.py"

with

"c:\\python27\python.exe" "c:\\cygwin\\home\\%(home)s\\project\\dir\\scenario%(scenario)s\\p%(popsize)sh%(height)st%(tournsize)sc%(crossprob)sm%(mutprob)s\\GP%(run)s.py"

fixes the problem

How to create an efficient multi-threaded task scheduler in C++?

You can avoid both having a separate "manager" thread, and having to wake up a large number of tasks when the next-to-run task changes, by using a design where a single pool thread waits for the "next to run" task (if there is one) on one condition variable, and the remaining pool threads wait indefinitely on a second condition variable.

The pool threads would execute pseudocode along these lines:

pthread_mutex_lock(&queue_lock);

while (running)
{
if (head task is ready to run)
{
dequeue head task;
if (task_thread == 1)
pthread_cond_signal(&task_cv);
else
pthread_cond_signal(&queue_cv);

pthread_mutex_unlock(&queue_lock);
run dequeued task;
pthread_mutex_lock(&queue_lock);
}
else if (!queue_empty && task_thread == 0)
{
task_thread = 1;
pthread_cond_timedwait(&task_cv, &queue_lock, time head task is ready to run);
task_thread = 0;
}
else
{
pthread_cond_wait(&queue_cv, &queue_lock);
}
}

pthread_mutex_unlock(&queue_lock);

If you change the next task to run, then you execute:

if (task_thread == 1)
pthread_cond_signal(&task_cv);
else
pthread_cond_signal(&queue_cv);

with the queue_lock held.

Under this scheme, all wakeups are directly at only a single thread, there's only one priority queue of tasks, and there's no manager thread required.

How to run program on ( ubuntu bash windows 10 ) from windows task scheduler

can run command on ubuntu bash with add -c flag in args

c:\Windows\System32\bash.exe -c <command>

and write it in .bat file and then add to windows task scheduler.

How to check trigger of a task in Task scheduler using c++?

The following is code piece works fore me you can have a try:

for (LONG i = 0; i < numTasks; i++)
{
IRegisteredTask* pRegisteredTask = NULL;
hr = pTaskCollection->get_Item(_variant_t(i + 1), &pRegisteredTask);

if (SUCCEEDED(hr))
{
BSTR taskName = NULL;
hr = pRegisteredTask->get_Name(&taskName);
if (SUCCEEDED(hr))
{
printf("\nTask Name: %S", taskName);
SysFreeString(taskName);

hr = pRegisteredTask->get_State(&taskState);
if (SUCCEEDED(hr))
printf("\n\tState: %d", taskState);
else
printf("\n\tCannot get the registered task state: %x", hr);
}
else
{
printf("\nCannot get the registered task name: %x", hr);
}

ITaskDefinition* taskDef = NULL;
hr = pRegisteredTask->get_Definition(&taskDef);
if (SUCCEEDED(hr))
{
ITriggerCollection* triggers = NULL;
taskDef->get_Triggers(&triggers);
LONG trigCnt = 0;
triggers->get_Count(&trigCnt);

for (LONG i = 0; i < trigCnt; i++)
{
ITrigger* trig = NULL;
TASK_TRIGGER_TYPE2 pType = TASK_TRIGGER_EVENT;

triggers->get_Item(_variant_t(i + 1), &trig);
trig->get_Type(&pType);
DWORD errCode = GetLastError();
if(pType != NULL)
printf("\nTrigger Type : %d", pType);
}

}
else
{
printf("\nCannot get the registered task definition: %x", hr);
}

pRegisteredTask->Release();
}
else
{
printf("\nCannot get the registered task item at index=%d: %x", i + 1, hr);
}
}

Refer to "Displaying Task Names and States (C++)"



Related Topics



Leave a reply



Submit