1. 14
    1. 8

      Interesting! Did you consider using expect to implement this? I’ve seen some pretty wild implementations using expect!

      1. 2

        I did not! I am not familiar with expect. How might it help?

        1. 10

          expect is a great program for driving interactive programs however you choose, check this out.

          Here is test.expect:

          spawn bash
          
          set timeout 1
          
          send "echo input1 | rev\n"
          expect {
            "1tupni" {
              puts "Got 1tupni!"
            }
            timeout {
              puts "didn't get 1tupni soon enough..."
              exit 1
            }
          }
          
          
          send "echo input2 | rev\n"
          expect {
            "2tupni" {
              puts "Got 2tupni!"
            }
            timeout {
              puts "didn't get 2tupni soon enough..."
              exit 1
            }
          }
          
          # Note I used `input3` here but look for `input4` 
          send "echo input3 | rev\n"
          expect {
            "4tupni" {
              puts "Got 4tupni!"
            }
            timeout {
              puts "didn't get 4tupni soon enough..."
              exit 1
            }
          }
          
          exit 0
          

          And running it:

          Morbo> expect ./test.expect
          spawn bash
          echo input1 | rev
          
          [grahamc@Morbo:~/projects/student-programs]$ echo input1 | rev
          1tupni
          Got 1tupni!
          echo input2 | rev
          
          [grahamc@Morbo:~/projects/student-programs]$ echo input2 | rev
          2tupni
          Got 2tupni!
          echo input3 | rev
          
          [grahamc@Morbo:~/projects/student-programs]$ echo input3 | rev
          3tupni
          
          [grahamc@Morbo:~/projects/student-programs]$ didn't get 4tupni soon enough...
          Morbo> echo $?
          1
          
          1. 3

            Whoa, that’s nuts. Good to know for the future, definitely!

            1. 2

              lots of languages have “expect” libraries, I had good results with a python one.

    2. 2

      I think your code is missing some calls to make_pipe? The child_* arrays are never initialized.

      1. 1

        You’re absolutely right. In the cleanup for my post I accidentally removed those. I will fix that.

    3. 1

      One potential issue with your program: it pushes all input to the program before reading any of its output. In a case where the program does a significant amount of output, it may block - and in fact, deadlock, since it will stop consuming input, which may cause your driver program to block as it attempts to write to the pipe.

      This might not be a real concern, though, if the amount of input/output is always limited.

      Edit: a more comprehensive solution might be to loop while both pushing some input to the student program using non-blocking I/O and read some of its output (also using non-blocking I/O). Once you’ve pushed all input, you could leave the loop and assume the pipe buffer contains all significant output (as you do now), or perhaps better, flush the buffer and then sleep for a little before checking for more output (using non-blocking I/O).

      1. 1

        Hm, interesting. The good news is there is not generally a significant amount of output – mostly user-interactive output, ~500B at a time. The bad news is you’re right. I didn’t realize non-blocking I/O in C was possible. Will investigate…

Stories with similar links:

  1. Driving student programs via raymii 1 year ago | 2 points | 3 comments