title: Running a C program with multiple threads in the background when it requires user input
body: <p>I've made a fairly simple C program to compute the terms of Fibonacci sequence, which I'm running on Ubuntu. I made some rather clumsy data structures so that it can do very large integers, but the specifics of the program aren't terribly important -- what matters is that the program can take quite a while to perform the computations.</p>
  
  <p>Out of curiosity, I decided I'd like to make the program start the computation and then allow the user to input a character to see how far along the computation is.  So in this case, if the program is computing the n'th term of the Fibonacci sequence and it isn't done yet, inputting the number '1' will get the program to output the term k that it is currently computing. I've attempted to do that using the following approach.</p>
  
  <p>The program uses scanf to get a long integer which represents the term of the sequence that needs to be computed.  The program then creates a thread with the routine that I wrote to compute and print the n'th Fibonacci term, which exits once it has finished doing that.  Following that, the program creates an integer variable i used to store input and initialises it so some non-zero value. It then continuously enters a while loop so long as that int is non-zero, and in each iteration of the loop it performs a scanf("%d", &amp;i). It then compares that value to 1, and if it's 1 then it'll print the value of a counter that I've set up to track the progress of the Fibonacci computation.</p>
  
  <p>Anyway, all of the above works very smoothly despite my being terribly out of depth with things like threads. However, the problem that I'm having is that when I have to compute, say, the millionth term of the sequence the program takes several minutes to finish, and it would be nice to simply run it in the background.  However, if I put the process in the background with ctrl+z and then type bg, the process starts but immediately stops again.  I'm guessing that this is because it is constantly requiring input from the user and thus it stops until it gets that input.</p>
  
  <p>Any suggestions for how to circumvent the above issue would be greatly appreciated. I'm not especially worried about this specific issue (computation of the Fibonacci numbers) since that's just a fairly random problem I chose to use for the computation.  I'm more interested in the general problem of creating a basic way for the user to input commands to the program, which the program then executes in separate threads, but which still enables the user to run the program in the background if necessary.</p>
  
  <p>Apologies for the rather long-winded question, and thanks in advance for any help!</p>
  
  <p>Phil</p>
  
  <p>Edit: By request, I added (a very simplified version of) the code here. The basic idea is the same: the program launches a lengthy computation in a new thread, then loops for input with scanf. Inputting 0 quits the program, inputting 1 displays a counter indicating the progress of the computation. I'd like to be able to run the program in the background, but since it's continuously asking for input it stops the process immediately.  Ignore the arithmetic overflow on the counter; my actual program has data structures to deal with this kind of stuff but I tried to simplify my code as much as possible for readability.</p>
  
  <pre><code>//Simple program to test threading and input.
  #include &lt;stdio.h&gt;
  #include &lt;pthread.h&gt;
  #define NUM_THREADS 2
  
  void *stuff();
  int counter; //keeps track of the progress of the computation
  
  int main(){
    counter=1;
    pthread_t threads[NUM_THREADS];
    pthread_create(&amp;threads[0], NULL, stuff, NULL);
  
    //loop while the input is non-zero so that the program can
    //accept commands
    int input=10;
    while(input){
      input=10;
      printf("Enter 0 to exit or 1 to display progress: ");
      scanf("%d", &amp;input);
      if(input==1){
        printf("Currently iterating for the %dth time.\n", counter);
      }
    }
  
  return 0;
  }
  
  //Randomly chosen computation that takes a while.
  void *stuff(){
    long i,j,n=1000000000;
    for(i=0; i&lt;=n; i++){
      for(j=0; j&lt;=n; j++){
        i*i*i*i*i*i*i*i*i*i*i;
        j*j*j*j*j*j*j*j*j*j*j;
        counter++;
      }
    }
    printf("Done.\n");
    pthread_exit(NULL);
  }
  </code></pre>