-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex_python.php
executable file
·81 lines (62 loc) · 2.29 KB
/
index_python.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
<?php
$result = "Accepted";
$runtime = 0;
//proses
$lang = "python";
$command2 = "time python3 submission/python.py < soal/python.in";
$descriptorspec = array(
0 => array("pipe", "r"), // stdin is a pipe that the child will read from
1 => array("pipe", "w"), // stdout is a pipe that the child will write to
2 => array("pipe", "w") //stderr is a pipe that the child will write to
);
$memory_limit = 64 * 1024; //64MB
$time_limit = 120; //15second
$process = proc_open("bash -c 'ulimit -St $time_limit -Sm $memory_limit ; $command2'", $descriptorspec, $pipes);
if (is_resource($process)) {
// fwrite($pipes[0], '19\n');
// fwrite($pipes[0], '20');
// fclose($pipes[0]);
$stream = stream_get_contents($pipes[2]);
echo "Stream : " . $stream . "<br>";
fclose($pipes[2]);
$timelimitstring = "CPU time limit exceeded";
$memorylimitstring = "Memory size limit exceeded";
if (strstr($stream, $timelimitstring) != null) {
$result = 'Time Limit Exceeded';
}
if (strstr($stream, $memorylimitstring) != null) {
$result = "Memory Limit Exceeded";
}
if ($result == "Accepted" && substr($stream, 1, 4) != "real") {
$result = "Run Time Error";
}
if ($result == "Accepted") {
// Get output
$streamOutput = stream_get_contents($pipes[1]);
echo "Output ".$streamOutput . "<br>";
fclose($pipes[1]);
$output = fopen("temp/result_python.out", "w");
fwrite($output, $streamOutput);
fclose($output);
}
$return_value = proc_close($process);
$str = strstr($stream, "real");
$str = str_replace(",", ".", $str);
$im = strpos($str, "m");
$is = strpos($str, "s");
$m = substr($str, 5, $im - 5);
$s = substr($str, $im + 1, $is - $im - 1);
$runtime = number_format($m * 60 + $s, 3);
}
//jika tetap masih AC(uda lewat TL), harus dicek sama tidak dengan output yang diinginkan
if ($result == "Accepted") {
$process = proc_open('cmp temp/result_python.out soal/python.out', $descriptorspec, $pipes);
if (is_resource($process)) {
$return_value = proc_close($process);
if ($return_value != 0)
$result = "Wrong Answer";
}
}
echo "result : " . $result . "<br />";
echo "runtime : " . $runtime . "<br />";
// shell_exec('rm -r temp/*');