[dvsim] Kill subprocesses more gracefully

Before this patch, we just used subprocess.Popen.kill(), which sends a
SIGKILL signal. This doesn't work well if the process has some child
processes, because it doesn't have a chance to clean up. This patch
sends a SIGTERM first, waits up to a couple of seconds for the process
to terminate, and then sends a SIGKILL if necessary. Hopefully, this
will leave fewer simv processes lying around.

Signed-off-by: Rupert Swarbrick <rswarbrick@lowrisc.org>
diff --git a/util/dvsim/Deploy.py b/util/dvsim/Deploy.py
index 4be4454..bdb6516 100644
--- a/util/dvsim/Deploy.py
+++ b/util/dvsim/Deploy.py
@@ -385,7 +385,15 @@
         '''
         if self.status == "D" and self.process.poll() is None:
             self.kill_remote_job()
-            self.process.kill()
+
+            # Try to kill the running process. Send SIGTERM first, wait a bit,
+            # and then send SIGKILL if it didn't work.
+            self.process.terminate()
+            try:
+                self.process.wait(timeout=2)
+            except subprocess.TimeoutExpired:
+                self.process.kill()
+
             if self.log_fd:
                 self.log_fd.close()
             self.status = "K"