Skip to content

Instantly share code, notes, and snippets.

@jkctech
Created September 21, 2021 15:44
Show Gist options
  • Save jkctech/367fad4aa01c820ffb1b8d29d1ecaa4d to your computer and use it in GitHub Desktop.
Save jkctech/367fad4aa01c820ffb1b8d29d1ecaa4d to your computer and use it in GitHub Desktop.
Test the 42 Philosophers project by testing if it dies within X seconds or not.
#!/usr/bin/python
import subprocess
import fcntl
import time
import os
import signal
import sys
if len(sys.argv) != 2:
print "Please (only) give command uWu"
exit(1)
# === CHANGE THIS ===
command = sys.argv[1]
timeout = 10
# === NO TOUCHY ===
# Vars
pipe = None
pid = 0
# Create datastream from demodulator
pipe = subprocess.Popen(command, stdout=subprocess.PIPE, shell=True)
# Make subprocess non-blocking
fcntl.fcntl(pipe.stdout.fileno(), fcntl.F_SETFL, os.O_NONBLOCK)
time.sleep(1)
# If cannot open:
if pipe.poll() != None:
print "Never started..."
exit(1)
else:
pid = pipe.pid
print "PID: %s" % (pid)
# Do some counting
for x in range(0, timeout):
print timeout - x
if pipe.poll() != None:
print "Died :("
exit(1)
time.sleep(1)
# Kill dem!
if pipe != None:
os.kill(pid, signal.SIGKILL)
pipe.kill()
pipe = None
# STONKS!
print "Worked! :D"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment