I’ve got a series of cpp source file and I want to write another program to JUDGE if they can run correctly (give input and compare their output with standart output) . so how to:
- call/spawn another program, and give a file to be its standard input
- limit the time and memory of the child process (maybe setrlimit thing? is there any examples?)
- donot let the process to read/write any file
- use a file to be its standard output
- compare the output with the standard output.
I think the 2nd and 3rd are the core part of this prob. Is there any way to do this?
ps. system is Linux
To do this right, you probably want to spawn the child program with
fork, notsystem.This allows you to do a few things. First of all, you can set up some pipes to the parent process so the parent can supply the input to the child, and capture the output from the child to compare to the expected result.
Second, it will let you call
seteuid(or one of its close relatives likesetreuid) to set the child process to run under a (very) limited user account, to prevent it from writing to files. Whenforkreturns in the parent, you’ll want to call setrlimit to limit the child’s CPU usage.Just to be clear: rather than directing the child’s output to a file, then comparing that to the expected output, I’d capture the child’s output directly via a pipe to the parent. From there the parent can write the data to a file if desired, but can also compare the output directly to what’s expected, without going through a file.