I have a loop which checks to see if a certain process is active. If the process isn't active, the main program launches it using the system() call. Example:
#define MODERATING_INTERVAL 1000
...
while (true) {
if (!isProcessRunning())
system("helper.exe");
Sleep(MODERATING_INTERVAL);
}
My problem is that everything runs fine, but the main program is paused as long as the helper.exeprogram is running, so the loop will not iterate.
I couldn't find a solution myself, so I'm asking: is it possible to make the example loop continue iterating while the invoked program is running?
Thanks in advance.
This is the normal behaviour of system. If you want to start a new process that runs concurrently with your current program you need to use CreateProcess.
Related
My program runs a console program and captures the output using these functions:
CreatePipe, CreateProcess, WaitForSingleObject, ReadFile, and CloseHandle.
My program works great and displays the output in a window control.
If I am going to run a console app that will take a long time, instead of calling WaitForSingleObject, I'd like to call ReadFile within a loop and have the loop end when the console app does and the last message has been read.
All my attempts at this result in a forever loop and I have to kill the main program. Any advice would be appreciated. Thanks!
I want to execute one exe from another exe. but the other exe can not run if first exe is running.
So i want to run the exe and exit from the application before the second exe gets executed.
Any help on this.
Consider a third application, which you launch from your first app. The third one checks to be sure the first one has terminated, then launches the second app and terminates itself. I have had to do this in the past; it works fine.
I am not sure exactly how it is done in Windows, but I think that the general guidelines are the same between linux and windows:
You need to fork a child process, in Linux this is done using fork() function, in Windows I think you can use CreateProcess().
In this child process, you need to call one of the exec functions which changes the code of this child process to the code of any executable that you can specify as a parameter to the exec function.
The code, thus, should be something like this pseudo-code:
c= CreateProcess()
if (c == child)
{
exec("My other executable.exe")
}
This is the general procedure, but you need to figure out the syntax
You are going to need a process to sit in the middle if you are not allowed to have the two main processes executing simultaneously. Which means that you need three processes in total. The two main processes, A and C, and the broker in the middle, B. Here's how it goes down:
Process A executes.
Process A starts process B passing in its process
handle.
Process A terminates.
Process B waits on process handle for process A. That becomes signaled when process A has terminated.
Process B starts process C.
Process B terminates.
I'm assuming that you already know how to create processes, pass arguments to process, duplicate handles, wait on handles and so on.
After starting a process with QProcess::startDetached, how can I stop it later?
Say the main program runs, then starts the detached process, which runs independently. The user closes the main program, then later opens it up again and wants to stop the process. How would I find the process and then stop it?
Is there a way I could prevent the application from the same process twice?
No, it will be decoupled from your application. You could get the the PID of it and then send a SIGSTOP on Linux, but this is platform specific and will not work without POSIX support, like with msvc. You would need to hand-craft your version therein.
Is there a way I could prevent the application from the same process twice?
Yes, by using lock file in the detached process. If that detached process happens to be written in at least partially Qt, you could use the QLockFile class.
If you happen to detach some platform specific process, then you have the same recurring issue again, for sure.
Here's the answer I figured out:
I first start the detached process that generates a unique id. That process write to a file whenever it runs (was a 1 minute timer). When it runs, it writes its id to a file. Then, if there happens to be another one that ran, if it sees a previous one ran, it just writes its id to the file and doesn't run, then, when the next one runs, it sees if its id is already in the file and if it is, it shuts itself off and clears the file, then the next run ends up running freely, being the only one running. This may end up skipping some time.
You can add a timestamp, too, as that might indicate it wasn't run recently and help with deciding whether or not to shut it down. The issue was if I just write the id to a file, when I turn the phone off, the file will say it's still running. The same applies to if it crashes.
I'm launching a bat file with system() in my software and it's can go to an infinit loop.
the question is how can I detect it in my cpp application ?
I'm using VS2010.
thanks
You can create a thread, and let the thread do the run of your batch file, and then set a timer with a timeout in the main thread to check whether the thread has ended its execution. If it takes longer than the timeout period, stop it and claim that it has an infinite loop.
I don't see any other way, because you practically can't access the batch file.
For threads, you may use boost threads or Qt threads, and there's many more different libraries for threads.
I am facing strange issue on Windows CE:
Running 3 EXEs
1)First exe doing some work every 8 minutes unless exit event is signaled.
2)Second exe doing some work every 5 minutes unless exit event signaled.
3)Third exe while loop is running and in while loop it do some work at random times.
This while loop continues until exit event signaled.
Now this exit event is global event and can be signaled by any process.
The Problem is
When I run First exe it works fine,
Run second exe it works fine,
run third exe it works fine
When I run all exes then only third exe runs and no instructions get executed in first and second.
As soon as third exe gets terminated first and second starts get processing.
It that can be the case that while loop in third exe is taking all CPU cycles?
I havn't tried putting Sleep but I think that can do some tricks.
But OS should give CPU to all processes ...
Any thoughts ???
Put the while loop in the third EXE to Sleep each time through the loop and see what happens. Even if it doesn't fix this particular probem, it isn't ever good practice to poll with a while loop, and even using Sleep inside a loop is a poor substitute for a proper timer.
On the MSDN, I also read that CE allows for (less than) 32 processes simultaneously. (However, the context switches are lightning fast...). Some are already taken by system services.
(From Memory) Processes in Windows CE run until completion if there are no higher priority processes running, or they run for their time slice (100ms) if there are other processes of equal priority running. I'm not sure if Windows CE gives the process with the active/foreground window a small priority boost (just like desktop Windows), or not.
In your situation the first two processes are starved of processor time so they never run until the third process exits. Some ways to solve this are:
Make the third process wait/block on some multi-process primitives (mutex, semaphore, etc) and a short timeout. Using WaitForMultipleObjects/WaitForSingleObject etc.
Make the third process wait using a call to Sleep every time around the processing loop.
Boost the priority of the other processes so when they need to run they will interrupt the third process and actually run. I would probably make the least often called process have the highest priority of the three processes.
The other thing to check is that the third process does actually complete its tasks in time, and does not peg the CPU trying to do its thing normally.
Yeah I think that is not good solution . I may try to use timer and see the results..