NoPaste

bc als Rechner in einem C-Programm nutzen

von heinz

SNIPPET_TEXT:
  1. #include <unistd.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <sys/wait.h>
  5. #include <string.h>
  6.  
  7.  
  8. const int PIPE_LESEN=0;
  9. const int PIPE_SCHREIBEN=1;
  10. int pipesE[2]; /* Eltern-Pipes */
  11. int pipesK[2]; /* Kind-Pipes */
  12. int kind_pid=0; /* Prozess-Pid */
  13.  
  14.  
  15. bool Pipe_verbinden(char*);/* (externes_programm) */
  16.  
  17.  
  18.  
  19.  
  20. int main()
  21. {
  22. /* Programmname */
  23.         char bc_programm[]={"/usr/bin/bc"};
  24.  
  25. /* Pipes erzeugen und bc starten */
  26.         if(!Pipe_verbinden(bc_programm))// (externes_programm)
  27.         {
  28.                 exit(1);
  29.         }
  30.  
  31.  
  32. /* Schreib und lese-speicher */
  33.         char bufs[32]={""};
  34.         char bufl[32]={""};
  35.  
  36. /* Mehrere testformeln verschicken */
  37.         for(int z=10;z<200;z+=10)
  38.         {
  39. /* Formel erstellen */
  40. /*              sprintf(bufs,"%i+%i\n",z,z);*/
  41.                 sprintf(bufs,"scale=6;(%i+%i)/3\n",z,z);
  42. /* Formel senden */
  43.                 write(PIPE_SCHREIBEN,bufs,strlen(bufs));
  44. /* Lesespeicher leeren */
  45.                 memset(bufl,0,32);
  46. /* Lesen */
  47.                 read(PIPE_LESEN,bufl,32);
  48. /* Ausgeben */
  49.                 fprintf(stderr,">%i>\033[0;33;40m%s\033[0m=\033[0;32;40m%f\033[0m<\n",z,bufs,atof(bufl));
  50.         }
  51.  
  52. /* bc beenden */
  53.         write(PIPE_SCHREIBEN,"quit\n",strlen(bufs));
  54.  
  55. /* Auf ende des Kind-Prozesses warten */
  56.         wait(&kind_pid);
  57.  
  58. /* Pipes schliessen */
  59.         close(PIPE_LESEN);
  60.         close(PIPE_SCHREIBEN);
  61. }
  62.  
  63.  
  64.  
  65.  
  66.  
  67.  
  68.  
  69.  
  70.  
  71. bool Pipe_verbinden(char* EP)// (externes_programm)
  72. {
  73. /* Pipes erzeugen */
  74.         if(pipe(pipesE)<0)
  75.         {
  76.                 return(false);
  77.         }
  78.         if(pipe(pipesK)<0)
  79.         {
  80.                 return(false);
  81.         }
  82.         fflush(0);
  83.  
  84. /* Prozess forken */
  85.         kind_pid=fork();
  86. fprintf(stderr,"kind_pid=%i\n",kind_pid);
  87. /* Bei Fehler */
  88.         if(kind_pid<0)
  89.         {
  90.                 close(pipesE[PIPE_LESEN]);
  91.                 close(pipesE[PIPE_SCHREIBEN]);
  92.                 close(pipesK[PIPE_LESEN]);
  93.                 close(pipesK[PIPE_SCHREIBEN]);
  94.                 return(false);
  95.         }
  96.  
  97.  
  98. /* Kind Prozess */
  99.         if(kind_pid==0)
  100.         {
  101. fprintf(stderr,"bc-start\n");
  102.  
  103. /* Pipes bearbeiten */
  104.                 close(PIPE_LESEN);
  105.                 dup2(pipesE[PIPE_LESEN],PIPE_LESEN);
  106.                 close(pipesE[PIPE_LESEN]);
  107.                 close(pipesE[PIPE_SCHREIBEN]);
  108.  
  109.                 close(PIPE_SCHREIBEN);
  110.                 dup2(pipesK[PIPE_SCHREIBEN],PIPE_SCHREIBEN);
  111.                 close(pipesK[PIPE_SCHREIBEN]);
  112.                 close(pipesK[PIPE_LESEN]);
  113.  
  114. /* bc Starten */
  115.                 execl(EP,EP,"-q","-l",NULL);
  116.  
  117. /* Pipes schliessen */
  118.                 close(PIPE_LESEN);
  119.                 close(PIPE_SCHREIBEN);
  120.         }
  121.  
  122.  
  123. /* Eltern Prozess */
  124.         if(kind_pid>0)
  125.         {
  126. /* Pipes bearbeiten */
  127.                 close(PIPE_LESEN);
  128.                 dup2(pipesK[PIPE_LESEN],PIPE_LESEN);
  129.                 close(pipesK[PIPE_LESEN]);
  130.                 close(pipesK[PIPE_SCHREIBEN]);
  131.  
  132.                 close(PIPE_SCHREIBEN);
  133.                 dup2(pipesE[PIPE_SCHREIBEN],PIPE_SCHREIBEN);
  134.                 close(pipesE[PIPE_SCHREIBEN]);
  135.                 close(pipesE[PIPE_LESEN]);
  136.         }
  137.  
  138.  
  139.         return(true);
  140. }
  141.  
  142.  

Quellcode

Hier kannst du den Code kopieren und ihn in deinen bevorzugten Editor einfügen. PASTEBIN_DOWNLOAD_SNIPPET_EXPLAIN