title: gnuplot and usleep on C
body: <p>i'm trying to do a realtime plot on Linux. Everything runs ok, it's just that when the program ends, there's a lot of CPU usage for a while. Besides, because i use a very short time on usleep (like 0.3 sec) i don't know why the bash shell is dominated by the gnuplot interface, even for others bash windows i try to open, so i cannot use ctrl+c to stop the program.</p>
  
  <p>Is there any other function i could use instead of usleep? Or something i could correct to avoid the gnuplot dominance over the windows? </p>
  
  <p>Thanks in advance and sorry for my bad english </p>
  
  <p>I let the code here</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;time.h&gt;
  float s=10.;
  float r=28.;
  float b=8.0/3.0;
  /* Definimos las funciones */
  float f(float x,float y,float z){
      return s*(y-x);
  }
  float g(float x,float y,float z){
      return x*(r-z)-y;
  }
  float h(float x,float y,float z){
      return x*y-b*z;
  }
  FILE *output;
  FILE *gp;
  
  int main(){
      gp = popen("gnuplot -","w");
      output = fopen("lorenzgplot.dat","w");
      float t=0.; 
      float dt=0.01;
      float tf=30;
      float x=3.;
      float y=2.;
      float z=0.;
      float k1x,k1y,k1z, k2x,k2y,k2z,k3x,k3y,k3z,k4x,k4y,k4z;
      fprintf(output,"%f %f %f \n",x,y,z);
      fprintf(gp, "splot '/home/david/documents/lorenzgplot.dat' with lines \n");
  /* Ahora Runge Kutta de orden 4 */  
      while(t&lt;tf){
          /* RK4 paso 1 */
          k1x = f(x,y,z)*dt;
          k1y = g(x,y,z)*dt;
          k1z = h(x,y,z)*dt;
          /* RK4 paso 2 */
          k2x = f(x+0.5*k1x,y+0.5*k1y,z+0.5*k1z)*dt;
          k2y = g(x+0.5*k1x,y+0.5*k1y,z+0.5*k1z)*dt;
          k2z = h(x+0.5*k1x,y+0.5*k1y,z+0.5*k1z)*dt;
          /* RK4 paso 3 */
          k3x = f(x+0.5*k2x,y+0.5*k2y,z+0.5*k2z)*dt;
          k3y = g(x+0.5*k2x,y+0.5*k2y,z+0.5*k2z)*dt;
          k3z = h(x+0.5*k2x,y+0.5*k2y,z+0.5*k2z)*dt;
          /* RK4 paso 4 */
          k4x = f(x+k3x,y+k3y,z+k3z)*dt;
          k4y = g(x+k3x,y+k3y,z+k3z)*dt;
          k4z = h(x+k3x,y+k3y,z+k3z)*dt;
          /* Actualizamos las variables y el tiempo */
          x += (k1x/6.0 + k2x/3.0 + k3x/3.0 + k4x/6.0);
          y += (k1y/6.0 + k2y/3.0 + k3y/3.0 + k4y/6.0);
          z += (k1z/6.0 + k2z/3.0 + k3z/3.0 + k4z/6.0);
          /* finalmente escribimos sobre el archivo */
  
          fprintf(output,"%f %f %f \n",x,y,z);
          fflush(output); 
          usleep(100000);
          fprintf(gp, "replot \n");
          fflush(gp);
          t += dt;
      }
      fclose(gp);
      fclose(output);
      return 0;
  }
  </code></pre>