how to check running time and how to force stop program - algorithm

I'm making Online Judge, but I can't check need to be judged file's running time and I can't stop it. If submitted program have infinite loop, my judge system falls to infinite loop too.
how should I do?

You should simply run the "to be judged files" in a separate thread and kitt it after a time-limit if it has not terminated successfully before.
This way you only need to wait in your time limiting thread and than kill the other one, if it has not finished already.

Related

Using JMeter is it possible to wait for all threads to finish before ending the test?

I need to run a jmeter test with N users over the course of a fixed time period. I am planning on using an "Ultimate Thread Group" for this as it meets my requirements. However, at the end of the time period and during the ramp down it simply kills threads even if they are not finished. This causes me problems because I end up in a situation where I have half completed records left lying around. Is there any way, either using this type of thread group or any other type to do as I require?
I have already got my test script ready, and have been exploring different thread groups and UTG seems like the best option, apart from the fact it kills threads without waiting for completion.
I would recommend to use stepping thread instead of ultimate thread group, this will surely help you out with your scenario. You can adjust stepping thread parameters according to your needs.

Jmeter execute sampler only on last thread iteration

I'm trying to make a part of my thread group to be executed only in a last iteration of a thread. A thread will be stopped at some moment of time (by pressing stop at gui while debugging, or by sending stoptest.sh signal to the headless load stations during the load test itself), so I don't know, how many iterations will be executed to that moment. If I knew the exact number, I'd use an If controller with condition like ${__iterationNum} == 50, but that's not the case.
There is a tearDown thread group for such a thing as far as I know, but I need to access my threads local context for the operation I am executing. So basically the question can be asked as - What's the way to determine a "Stop_thread signal was allready sent, and the current iteration is the last one executed"? If i knew how to get it, I could then implement an If controller.
Hope I explained it clear enough, thanks in advance, guys
PS Jmeter 4.0

Bash script - Maintain multiple instances running

How can I ensure, that multiple instances of certain program are always running?
Let's say that I want to make sure that 4 instances of a certain program are always running.
If one instance is killed, new one should start.
If 5 instances are running, one should be killed.
This is not really a shell question, because the approach is the same, whichever shell you are using.
I think the cleanest solution is to have a "watchdog", which checks the running processes (using ps) and, if necessary, starts a new one or kills an unnecessary one.
One way - which I have used in a similar situation - is to write a cron job, which regularly (say: every 5 minutes) starts the watchdog and let it do his work.
If such an interval is too long for your case (i.e. if you need checking it more often than every minute), you could have the watchdog run continuously, in a loop. Still, you will need a cron job, which controls in turn the watchdog from time to time - just in case the watchdogs dies. In this case you might consider running it as a daemon.

How to restart a program in terminal periodically?

I am calling a program, let say myprogram, from the terminal (in OS X Mavericks) but some times it gets stuck due to external problems out of my control. This tends to happen approximately every half an hour.
myprogram basically has to perform a large quantity of small subtasks, which are saved in a file that is read in every new execution, so there is no need to recompute everything from the beginning.
I would like to fully automatize the restarting of the program by killing and restarting it again, in the following way:
Start the program.
Kill it after 30 minutes (the program will be probably stuck).
Restart it (back to step 1).
Any ideas on how to do this? My knowledge of bash scripting is not great precisely...
The following script can serve as a wrapper script for myprogram
#!/bin/bash
while true #begin infinite loop (you'll have to manually kill)
do
./myprogram & #execute myprogram and background
PID=$! #get PID of myprogram
sleep 1800 #sleep 30 minutes (30m might work as parameter)
kill -9 $PID #kill myprogram
done
You could use a wrapper, but, an infinite loop is not an optimal solution. If you are looking to relaunch a program on timer, or not, depending on the exit code and are on OS X, you should use launchd configuration (xml property list) files and load them with launchctl.
KeepAlive <boolean or dictionary of stuff>
This optional key is used to control whether your job is to be kept continuously running or to let
demand and conditions control the invocation. The default is false and therefore only demand will start
the job. The value may be set to true to unconditionally keep the job alive. Alternatively, a dictio-nary dictionary
nary of conditions may be specified to selectively control whether launchd keeps a job alive or not. If
multiple keys are provided, launchd ORs them, thus providing maximum flexibility to the job to refine
the logic and stall if necessary. If launchd finds no reason to restart the job, it falls back on
demand based invocation. Jobs that exit quickly and frequently when configured to be kept alive will
be throttled to converve system resources.
SuccessfulExit <boolean>
If true, the job will be restarted as long as the program exits and with an exit status of zero.
If false, the job will be restarted in the inverse condition. This key implies that "RunAtLoad"
is set to true, since the job needs to run at least once before we can get an exit status.
...
ExitTimeOut <integer>
The amount of time launchd waits before sending a SIGKILL signal. The default value is 20 seconds. The
value zero is interpreted as infinity.
For more information on launchd & plists visit :
https://developer.apple.com/library/mac/documentation/MacOSX/Conceptual/BPSystemStartup/Chapters/CreatingLaunchdJobs.html
https://developer.apple.com/library/mac/documentation/Darwin/Reference/ManPages/man5/launchd.plist.5.html

Infinite loop or repetitive run for daemon

Which is the better to write a "daemon" based on oracle schedules:
The one that is run once and then is in infinite loop and sleeps for 5 seconds if there is nothing to do (to not waste CPU cycles).
The one that is started, checked if it is something to do. If not - ends execution and is run after 5 seconds by schedule.
Which one and why do you prefer? Or may be it is some another implementation?
I personally prefer an infinite loop to a scheduled task. With an infinite loop you can see a broader cross-activation overview - Eg You can count number of failures in a row/similar very easily and add error-recovery.
A scheduled task is effectively stateless unless you manually give it state (File/Db/???)
It sounds like you might want to look at using an a queue to do the processing rather than a schedule job. The process can block on the queue waiting for new work.

Resources