title: start and terminate cu with a C program
body: <p>I'm trying to communicate with another UNIX device via ttyS0 using cu (google "cu unix" to find out more about cu). My program works perfectly fine, but the problem is, that after the first execution of the program (establish connection, read logfiles and some other stuff) the terminal is not accessible anymore. I've just posted the core of my problem in a simplified version of the code where I'm only focussing on the actual question that I have:</p>
  
  <p>When I'm doing these commands by hand "cu -l /dev/ttyS0 -s 115200" and "~." (just like in the man pages of cu: ~. terminates the connection) everything works fine. A sequential program like</p>
  
  <pre><code>system("cu -l /dev/ttyS0 -s 115200");
  system("~.");
  </code></pre>
  
  <p>isn't working, because cu is still active, and nothing is executed after that....the program just sits there waiting for cu... same thing would happen in a simple bash script... cu would be preventing the program/script from proceeding - that's why I'm using threads, and like I said, my actual program works, but the program isn't terminating like I want it to and the Terminal has to be restarted.
  When I execute the following program, I only get</p>
  
  <pre><code>Connected
  sh: ~.: not found
  </code></pre>
  
  <p><em>pressing enter</em></p>
  
  <pre><code>cu: can't restore terminal: Input/Output error
  Disconnected
  </code></pre>
  
  <p>and an unusable terminal is left open (can't type or do anything with it)...</p>
  
  <pre><code>#define _BSD_SOURCE
  #include &lt;termios.h&gt;
  #include &lt;unistd.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;fcntl.h&gt;
  #include &lt;time.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;pthread.h&gt;
  
  void first(){
      system("cu -l /dev/ttyS0 -s 115200");
      pthread_exit(NULL);
  }
  void second(){
      system("~."); //also "~.\n" isn't changing anything
      pthread_exit(NULL);
  
  int main(){
      pthread_t thread1, thread2;
      pthread_create ( &amp;thread1, NULL, (void*)first, NULL );
      sleep(3);
      pthread_create ( &amp;thread2, NULL, (void*)second, NULL );
      sleep(4);
  
      exit(0);
      return 0;
  }
  </code></pre>