Skip to content

Instantly share code, notes, and snippets.

@ashwanthkumar
Created November 22, 2011 05:09
Show Gist options
  • Save ashwanthkumar/1384942 to your computer and use it in GitHub Desktop.
Save ashwanthkumar/1384942 to your computer and use it in GitHub Desktop.
Mini Online Judge script - 22/11/2011
<?php
/**
* My mini online judge script in php. This was done as a part of 6 hour hackathon.
*
* @author Ashwanth Kumar
* @version 0.1
* @date 22/11/2011
**/
/**
There are things to note here as,
1. This was developed in windows platform
2. Script is static, most of the variables values are pre-assigned
3. This is no where near production-ready. Still a lot of improvements to be made.
4. Supported Languages are C/C++/Objective-C/Fortran77/Ada - Languages supported by GCC (GNU Compiler Collection)
Improvements
1. Check the source code for any critical system functions and remove them
2. Build a neat web interface around
**/
// Current working directory
$cwd = 'c:/Users/krishna/';
// Time limit for the current program to execute
$time_limit = 1; // This has to be an integer.
// Environmental variable values to set for execution
$env = array();
// Start time of the script
$start_time = 0;
// End time of the script
$end_time = 1;
// Step 1 - Compile the program
// Compiler execution descriptor
$compiler_descriptorspec = array(
0 => array("pipe", "r"), // stdin is a pipe that the child will read from
1 => array("file", "./hello.compile.output", "w"), // stdout is a file that the child will write to
2 => array("file", "./error-output.txt", "w") // stderr is a file to write to
);
// Execute the compiler
// I assume that GCC is present in your executable path
$process = proc_open('gcc hello.c -o hello.exe -ftime-report -fmem-report', $compiler_descriptorspec, $pipes, $cwd);
// Validate the compiler output
if (is_resource($process)) {
$return_value = proc_close($process);
// echo "Compiler output is $return_value";
if($return_value != 0) die("Compile Error");
}
// Step 2 - Execute the program, only if there is no compile error
$descriptorspec = array(
0 => array("file", "./hello.input", "r"), // stdin is a pipe that the child will read from
1 => array("file", "./hello.output", "w"), // stdout is a pipe that the child will write to
2 => array("file", "./error-output.txt", "a") // stderr is a file to write to
);
// Original file which is used as output reference
$output_reference = './hello.output.test';
// Start the Counter
$start_time = time();
// Start the program execution
$process = proc_open('hello.exe', $descriptorspec, $pipes, $cwd, $env);
// Time to sleep, for the program to complete
sleep($time_limit);
// Now awake up to see if the execution is complete
$status = proc_get_status($process);
if($status['running']) {
$end_time = time();
// echo "Total Time taken - " . date('H:m:s');
// If the process is still running, terminate it
proc_terminate($process);
die("ERROR: Time limit exceeded");
}
if(is_resource($process)) {
// It is important that you close any pipes before calling
// proc_close in order to avoid a deadlock
$return_value = proc_close($process);
$end_time = time();
// echo "Total Time taken - " . date('H:m:s', $end_time - $start_time);
if($return_value == 0) {
// Time to compare the program output with output reference
$test_output = trim(file_get_contents($output_reference));
$command_output = trim(file_get_contents('./hello.output'));
$cmp_value = strcmp($test_output, $command_output);
if($cmp_value === 0) {
die("SUCCESS: Accepted!");
} else {
die("ERROR: Output does not match!");
}
} else {
die("ERROR: Program Exited with code oter than 0");
}
} else {
// proc_open did not work at all
die("TECHICAL_ERROR: proc_open did nt work properly, Please chk that out for me. ");
}
// Sample program for testing the online judge script
int main() {
int a, b, c, n, i;
scanf("%d", &n);
// for(i = 0; i < n + 10000000000000; i++) { -- For time limit execeeded
for(i = 0; i < n; i++) {
scanf("%d%d", &a, &b);
// c = a - b; Wrong output case
c = a + b; // Success in execution
printf("%d\n", c);
}
// return 5; -- ERROR with invalid return code
return 0;
}
3
2
3
1
1
2
2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment