Skip to content

Instantly share code, notes, and snippets.

@valtih1978
Last active August 29, 2015 14:17
Show Gist options
  • Save valtih1978/8b15c2932621a2a459ad to your computer and use it in GitHub Desktop.
Save valtih1978/8b15c2932621a2a459ad to your computer and use it in GitHub Desktop.
Remove executable fails after gcc > execute > kill > remove executable
File deletion does not have immediate effect at least process image file, at least when
process was killed ungracefully prematurely. I have to retry the delete in my app.
Also, EVEN IF FILE DELETED, IT IS REPORTED EXISTING and complire cannot reporduce the
file. I have to retry the compilation also.
object prog_gen extends App {
import scala.sys.process._, scala.concurrent._, ExecutionContext.Implicits.global, Paths.get
implicit def toPath(filename: String) = get(filename)
import java.nio.charset.{StandardCharsets}
def write(filePath:String, contents:String) =
Files.write(Paths.get(filePath), contents.getBytes(StandardCharsets.UTF_8), StandardOpenOption.CREATE)
def assertFile[A](fn: String, ref: String) = {
val source = scala.io.Source.fromFile(fn) ; try assert(source.mkString.equals(ref), "invalid contents in " + fn) finally source.close()
}
implicit def s2f(s: String) = new File(s)
@scala.annotation.tailrec
def ensureRemove(f: File) {
f.delete
if (f.exists) { Thread.sleep(100) ; ensureRemove(f); }
}
var (success, failure, unterminated) = (0, 0, 0)
(1 to 10) foreach { _ =>
val perfRefProg = // "#include <windows.h>\n int main() { Sleep(5000); return 0; }" // this almost never fails
"""#include <stdio.h>
|int main(int argc, char *argv[]) { int mem[100]; int i; for (i = 0 ; i != 10000000 ; i++)
|mem[atoi("10")] = mem[atoi("10")] + atoi("1") ;
|printf("%d, performance test done\n", mem[atoi("10")]); return 0; }""".stripMargin
ensureRemove("prog.c"); write("prog.c", perfRefProg) ;
assertFile("prog.c", perfRefProg)
import scala.sys.process._, scala.concurrent._, ExecutionContext.Implicits.global
// I have to retry here
ensureRemove("prog.exe")
/*
val p = {
//this can be terminated
Files.copy(Paths.get("""C:\Program Files (x86)\Windows Resource Kits\Tools\sleep.exe"""), "prog.exe", StandardCopyOption.REPLACE_EXISTING)
"prog.exe 5".run()
}
*/
// I have to retry here
def compile {
if ("gcc prog.c -o prog -lm".! != 0) { // somethime this fails because prog.exe is locked, despite removed
println("failed to compile")
Thread.sleep(100);
compile
}
}
compile
val p = ("prog.exe 1 2 3 4 5 6 7 8 9").run() // start asynchronously
val f = Future(blocking(p.exitValue())) // wrap in Future
val result = try {
println("running")
Await.result(f, duration.Duration(500, "millis"))
Some(p.exitValue())
} catch {
case _: TimeoutException =>
println("TIMEOUT!")
p.destroy()
None
}
result match {
case Some(0) => success += 1
case Some(_) => failure += 1
case None => unterminated += 1
}
}
println ("(success, failure, unterminated) = " + (success, failure, unterminated))
}
Filemon table, recording access to proc.exe is here
https://docs.google.com/document/d/1IkIIc23FluNRDUXyUn6Gzi21BAgkuDCvdQRcukET09E/pub
/*
,,,,,
,,,,,
,,,,,
,,,,,
,,,,,
,,,,,
,,,,,
,,,,,
,,,,,
,,,,,
,,,,,
,,,,,
,,,,,
,,,,,
,,,,,
,,,,,
,,,,,
,,,,,
Time of Day,msec,Proc Name,PID,Operation,Path
,,,,,
,,,,Java runs 'gcc prog.c -o prog -lm',
,,,,This creates proc.exe (written by ld),
20:40:14,75685,java,364788,SetDispositionInformationFileprog,SUCCESS
20:40:14,76226,java,364788,CloseFileprog,SUCCESS
20:40:14,80707,java,364788,CreateFileprog,NAME NOT FOUND
20:40:14,4067049,ld,363672,QueryDirectoryprog,NO SUCH FILE
20:40:14,4068522,ld,363672,CreateFileprog,SUCCESS
20:40:14,4281062,ld,363672,CloseFileprog,SUCCESS
20:40:14,4617703,ld,363672,CreateFileprog,SUCCESS
20:40:14,4619998,ld,363672,WriteFileprog,SUCCESS
20:40:14,4621512,ld,363672,WriteFileprog,SUCCESS
20:40:14,4624309,ld,363672,WriteFileprog,SUCCESS
,,,,,
,,,,Skipped wriging prog.exe,
,,,,,
20:40:14,4739452,ld,363672,CloseFileprog,SUCCESS
,,,,,
,,,,Here java starts prog.exe,
,,,,and forcefully kills the child,
,,,,and complies prog c-> exe again,
,,,,This time however, ld cannot write,
20:40:14,9982091,prog,363936,QueryNameInformationFileprog,SUCCESS
20:40:14,9999540,java,364788,CreateFileprog,SUCCESS
20:40:14,9999941,java,364788,SetBasicInformationFileprog,SUCCESS
20:40:15,616,java,364788,CloseFileprog,SUCCESS
20:40:15,2885,java,364788,CreateFileprog,SUCCESS
20:40:15,3232,java,364788,QueryBasicInformationFileprog,SUCCESS
20:40:15,3429,java,364788,CloseFileprog,SUCCESS
20:40:15,4666,java,364788,CreateFileprog,SUCCESS
20:40:15,5027,java,364788,QueryAttributeTagFileprog,SUCCESS
20:40:15,5271,java,364788,SetDispositionInformationFileprog,SUCCESS
20:40:15,6002,java,364788,CloseFileprog,SUCCESS
20:40:15,8164,java,364788,CreateFileprog,DELETE PENDING
20:40:15,2717200,ld,365372,QueryDirectoryprog,SUCCESS
20:40:15,2718671,ld,365372,QueryDirectoryprog,SUCCESS
20:40:15,2719900,ld,365372,CreateFileprog,DELETE PENDING
20:40:15,2720903,ld,365372,CreateFileprog,DELETE PENDING
20:40:15,2782947,collect2,363760,QueryDirectoryprog,SUCCESS
20:40:15,2784210,collect2,363760,CreateFileprog,DELETE PENDING
20:40:15,6265962,ld,359936,QueryDirectoryprog,SUCCESS
20:40:15,6267312,ld,359936,QueryDirectoryprog,SUCCESS
20:40:15,6268455,ld,359936,CreateFileprog,DELETE PENDING
20:40:15,6269341,ld,359936,CreateFileprog,DELETE PENDING
20:40:15,6325577,collect2,365336,QueryDirectoryprog,SUCCESS
20:40:15,6326723,collect2,365336,CreateFileprog,DELETE PENDING
20:40:15,9926289,ld,365564,QueryDirectoryprog,NO SUCH FILE
20:40:15,9927502,ld,365564,CreateFileprog,SUCCESS
20:40:16,109444,ld,365564,CloseFileprog,SUCCESS
,,,,,
,,,,Ld could not write,
,,,,because file was in pending,
,,,,delete mode. Java retried,
,,,,C -> exe compilation until success,
,,,,Why should I do it myslef?,
*/
val p = Process(executable + " 1 2 3 4 5 6 7 8 9").run()
p.destroy()
executable.delete // File does not disappear at all in 50% of cases. Retry is needed or detele after delay.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment