Take the 2-minute tour ×

I will start with the question and then proceed to explain the need:

Given a single C++ source code file which compiles well in modern g++ and uses nothing more than the standard library, can I set up a single-task operating system and run it there?

EDIT: Following the comment by Chris Lively, I would have better asked: What's the easiest way you can suggest to try to tweak linux into effectively giving me a single-tasking behavior. Nevertheless, it seems like I did get a good answer although I did not phrase my question well enough. See the second paragraph in sarnold's answer regarding the scheduler.

Motivation: In some programming competitions the communication between a contestant's program and the grading program involves a huge number of very short interactions.

Thus, using getrusage to measure the time spent by a contestant's program is inaccurate because getrusage works by sampling a process at constant intervals (usually around once per 10ms) which are too large compared to the duration of each interaction.

Another approach to timing would be to measure time before and after the program is run using something like *clock_gettime* and then subtract their values. We should also subtract the amount of time spent on I/O and this can be done be intercepting printf and scanf using something like LD_PRELOAD and accumulate the time spent in each of these functions by checking the time just before and just after each call to printf/scanf (it's OK to require contestants to use these functions only for I/O).

The method proposed in the last paragraph is ofcourse only true assuming that the contestant's program is the only program running which is why I want a single-tasking OS.

To run both the contestant's program and the grading program at the same time I would need a mechanism which, when one of these program tries to read input and blocks, runs the other program until it write enough output. I still consider this to be single tasking because the programs are not going to run at the same time. The "context switch" would occur when it is needed.

Note: I am aware of the fact that there are additional issues to timing such as CPU power management, but I would like to start by addressing the issue of context switches and multitasking.

share|improve this question
Not sure that this belongs here. "Can you do something" type questions are almost always answered in the affirmative. This question is pretty vague. I'd suggest that you might first try implementing this and if/when you run into issues post the exact nature of your problem here for help. –  Chris Lively Oct 23 '11 at 1:28
Are you asking as someone who runs a contest? or as a contestant? –  Mike Dunlavey Oct 23 '11 at 1:41
possible duplicate of Grading Program - Compile/executing c++ code within c++ –  Ben Voigt Oct 23 '11 at 2:00
@Chris: I changed the phrasing a bit now to be as specific as I currently can be. Thanks for the comment. –  user302099 Oct 23 '11 at 6:16
@Mike: I am asking as someone who runs a C++ programming contest. –  user302099 Oct 23 '11 at 6:16

1 Answer 1

up vote 2 down vote accepted

First things first, what I think would suit your needs best would actually be a language interpreter -- one that you can instrument to keep track of "execution time" of the program in some purpose-made units like "mems" to represent memory accesses or "cycles" to represent the speeds of different instructions. Knuth's MMIX in his The Art of Computer Programming may provide exactly that functionality, though Python, Ruby, Java, Erlang, are all reasonable enough interpreters that can provide some instruction count / cost / memory access costs should you do enough re-writing. (But losing C++, obviously.)

Another approach that might work well for you -- if used with extreme care -- is to run your programming problems in the SCHED_FIFO or SCHED_RR real-time processing class. Programs run in one of these real-time priority classes will not yield for other processes on the system, allowing them to dominate all other tasks. (Be sure to run your sshd(8) and sh(1) in a higher real-time class to allow you to kill runaway tasks.)

share|improve this answer
As I cannot allow not to run the contest in C++, I will try the approach suggested by the second paragraph. –  user302099 Oct 23 '11 at 6:26

Your Answer


