code: def execute(command, env=None, stdout=None, stderr=None):
      # Make a pipe for the subprocess stdout/stderr.
      (stdout_r, stdout_w) = os.pipe()
      (stderr_r, stderr_w) = os.pipe()
  
      # Make a pipe for notifying the child that parent has died.
      (r, w) = os.pipe()
  
      middleman_pid = os.fork()
      if middleman_pid == 0:
          # Close unused file descriptors to enforce PIPE behavior.
          os.close(w)
          os.setsid()
  
          executor_shell = subprocess.Popen(
              command,
              shell=True,
              env=env,
              stdout=stdout_w,
              stderr=stderr_w,
          )
  
          sigterm_received = threading.Event()
  
          def set_sigterm_received(signum, frame):
              sigterm_received.set()
  
          signal.signal(signal.SIGINT, set_sigterm_received)
          signal.signal(signal.SIGTERM, set_sigterm_received)
  
          def kill_executor_children_if_parent_dies():
              # This read blocks until the pipe is closed on the other side
              # due to the process termination.
              os.read(r, 1)
              terminate_executor_shell_and_children(executor_shell.pid)
  
          bg = threading.Thread(
              target=kill_executor_children_if_parent_dies
          )
          bg.daemon = True
          bg.start()
  
          def kill_executor_children_if_sigterm_received():
              sigterm_received.wait()
              terminate_executor_shell_and_children(executor_shell.pid)
  
          bg = threading.Thread(
              target=kill_executor_children_if_sigterm_received
          )
          bg.daemon = True
          bg.start()
  
          exit_code = executor_shell.wait()
          os._exit(exit_code)
  
      # Close unused file descriptors to enforce PIPE behavior.
      os.close(r)
      os.close(stdout_w)
      os.close(stderr_w)
  
      # Redirect command stdout & stderr to provided streams or sys.stdout/sys.stderr.
      # This is useful for Jupyter Notebook that uses custom sys.stdout/sys.stderr or
      # for redirecting to a file on disk.
      if stdout is None:
          stdout = sys.stdout
      if stderr is None:
          stderr = sys.stderr
      stdout_fwd = threading.Thread(
          target=forward_stream, args=(stdout_r, stdout)
      )
      stderr_fwd = threading.Thread(
          target=forward_stream, args=(stderr_r, stderr)
      )
      stdout_fwd.start()
      stderr_fwd.start()
  
      try:
          _, status = os.waitpid(middleman_pid, 0)
      except:
          # interrupted, send middleman TERM signal which will terminate children
          os.kill(middleman_pid, signal.SIGTERM)
          while True:
              try:
                  _, status = os.waitpid(middleman_pid, 0)
                  break
              except:
                  # interrupted, wait for middleman to finish
                  pass
  
      stdout_fwd.join()
      stderr_fwd.join()
      exit_code = status >> 8
      return exit_code
  
file_path: bluefog/bluefog/run/horovodrun/common/util/safe_shell_exec.py
imports: import os
  import psutil
  import signal
  import subprocess
  import sys
  import threading
  import time
label_desc: Use image node_id 3 for calling a global function with example usage: execute(command, env, stdout, stderr) and returns: exit_code