def fork():
 try:
  (pid, fd) = os.forkpty()
 except (AttributeError, OSError):
  pass
 else:
  if (pid == CHILD):
   try:
    os.setsid()
   except OSError:
    pass
  return (pid, fd)
 (master_fd, slave_fd) = openpty()
 pid = os.fork()
 if (pid == CHILD):
  os.setsid()
  os.close(master_fd)
  os.dup2(slave_fd, STDIN_FILENO)
  os.dup2(slave_fd, STDOUT_FILENO)
  os.dup2(slave_fd, STDERR_FILENO)
  if (slave_fd > STDERR_FILENO):
   os.close(slave_fd)
  tmp_fd = os.open(os.ttyname(STDOUT_FILENO), os.O_RDWR)
  os.close(tmp_fd)
 else:
  os.close(slave_fd)
 return (pid, master_fd)

def fork ( ) : try : ( pid , fd ) = os . forkpty ( ) except ( AttributeError , OSError ) : pass else : if ( pid == CHILD ) : try : os . setsid ( ) except OSError : pass return ( pid , fd ) ( master_fd , slave_fd ) = openpty ( ) pid = os . fork ( ) if ( pid == CHILD ) : os . setsid ( ) os . close ( master_fd ) os . dup2 ( slave_fd , STDIN_FILENO ) os . dup2 ( slave_fd , STDOUT_FILENO ) os . dup2 ( slave_fd , STDERR_FILENO ) if ( slave_fd > STDERR_FILENO ) : os . close ( slave_fd ) tmp_fd = os . open ( os . ttyname ( STDOUT_FILENO ) , os . O_RDWR ) os . close ( tmp_fd ) else : os . close ( slave_fd ) return ( pid , master_fd )
fork() -> fork and make the child a session leader with a controlling terminal .