Closing file descriptor 3



sh/bash

# close fd 3 on *each* execution of student code
 ./student-code ... 3>&-
 
 # write to fd 3
 echo ... >&3

perl

# Open fd 3 setting close-on-exec once and for all
 open(RESULT, ">&=3");
 
 exec('student-code ...');
 
 # write to fd 3
 print RESULT "...";

python

# Open fd 3
 result = os.fdopen(3, 'w')
EITHER:
  # Set close-on-exec on fd 3 once and for all
   import os
   import fcntl
   import subprocess
 
   flags = fcntl.fcntl(3, fcntl.F_GETFD)
   fcntl.fcntl(3, fcntl.F_SETFD, flags | fcntl.FD_CLOEXEC)
OR:
  # close all file descriptors on *each* execution of student code
   sub = subprocess.Popen(["student-code ..."], close_fds = True);
# write to fd 3
 result.write("...")