NoPaste

Pipe in ein anderes Programm

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 pipes[2];
  11.  
  12.  
  13. bool Pipe_verbinden(char*);// (externes_programm)
  14.  
  15.  
  16.  
  17.  
  18. int main()
  19. {
  20. /* Programmname */
  21.         char bc_programm[]={"/usr/bin/bc"};
  22.  
  23.         Pipe_verbinden(bc_programm);// (externes_programm)
  24.  
  25.  
  26. /* Kurz warten bis bc gestartet */
  27. for(int z=0;z<100000;z++)
  28. {
  29.         printf("\r#%i#",z);
  30. }
  31. printf("\n");
  32.  
  33.  
  34. /* Schreib und lese-speicher */
  35.         char bufs[32]={""};
  36.         char bufl[32]={""};
  37.  
  38. /* Mehrere testformeln verschicken */
  39.         for(int z=10;z<200;z+=10)
  40.         {
  41. /* Formel erstellen */
  42.                 sprintf(bufs,"%i+%i\n",z,z);
  43. /* Formel senden */
  44.                 write(pipes[PIPE_SCHREIBEN],bufs,strlen(bufs));
  45. /* Lesespeicher leeren */
  46.                 memset(bufl,0,32);
  47. /* Lesen */
  48.                 read(pipes[PIPE_LESEN],bufl,32);
  49. /* Ausgeben */
  50.                 printf(">%i>=%s<\n",z,bufl);
  51.         }
  52.  
  53. /* bc beenden */
  54.         sprintf(bufs,"%s\n","quit");
  55.         write(pipes[PIPE_SCHREIBEN],bufs,strlen(bufs));
  56.  
  57. /* Pipes schliessen */
  58.         close(pipes[PIPE_LESEN]);
  59.         close(pipes[PIPE_SCHREIBEN]);
  60.  
  61. }
  62.  
  63.  
  64.  
  65.  
  66.  
  67.  
  68.  
  69.  
  70.  
  71. bool Pipe_verbinden(char* EP)// (externes_programm)
  72. {
  73.         if(pipe(pipes)<0)
  74.         {
  75.                 return(false);
  76.         }
  77.         fflush(0);
  78.  
  79.  
  80.         pid_t kind_pid=fork();
  81. printf("kind_pid=%i\n",kind_pid);
  82.         if(kind_pid<0)
  83.         {
  84.                 close(pipes[PIPE_LESEN]);
  85.                 close(pipes[PIPE_SCHREIBEN]);
  86.                 return(false);
  87.         }
  88.  
  89.  
  90.         if(kind_pid==0)
  91.         {
  92. printf("bc-start\n");
  93.                 close(0);
  94.                 dup2(pipes[0],0);
  95.                 close(pipes[0]);
  96.  
  97.                 close(1);
  98.                 dup2(pipes[1],1);
  99.                 close(pipes[1]);
  100.  
  101.                 execl(EP,EP,"-q",NULL);
  102.  
  103.                 close(0);
  104.                 close(1);
  105.         }
  106.  
  107.         return(true);
  108. }
  109.  
  110.  
  111.  
  112.  
  113.  
  114.  
  115.  
  116.  
  117.  
  118.  
  119.  
  120.  
  121.  
  122.  
  123.  
  124.  
  125.  
  126.  
  127.  
  128.  
  129.  
  130.  
  131.  
  132.  
  133.  
  134. /*
  135. int main()
  136. {
  137.         char bc_programm[]={"/usr/bin/bc"};
  138.  
  139.         pipe_end pipe;
  140.         strlist list;
  141.  
  142.         int ok=pconnect(bc_programm,NULL,PIPE_WRITE,&pipe);
  143.  
  144.  
  145.  
  146.  
  147. }
  148. */
  149.  
  150.  
  151.  
  152.  
  153.  
  154.  
  155.  
  156.  
  157.  
  158.  
  159.  
  160.  
  161.  
  162.  
  163.  
  164.  
  165.  
  166.  
  167.  
  168.  
  169.  
  170.  
  171.  
  172.  
  173.  
  174.  
  175.  
  176.  
  177.  
  178.  
  179.  
  180.  
  181.  
  182. /*
  183. //const int PIPE_READ = 0;
  184. //const int PIPE_WRITE = 1;
  185.  
  186. int main()
  187. {
  188.         int pipefds[2];
  189.  
  190.         if(pipe(pipefds)<0)
  191.         {
  192.                 perror("pipe");
  193.                 exit(1);
  194.         }
  195.  
  196.         pid_t child=fork();
  197.         if(child<0)
  198.         {
  199.                 perror("fork");
  200.                 exit(1);
  201.         }
  202.  
  203.         if(child==0)
  204.         {
  205.                 close(pipefds[PIPE_WRITE]);
  206.  
  207.                 char buf[32];
  208.                 ssize_t nbytes;
  209.  
  210.                 while((nbytes=read(pipefds[PIPE_READ],buf,sizeof(buf)))>0)
  211.                 {
  212.                         if(write(1,buf,nbytes)<nbytes)
  213.                         {
  214.                                 exit(1);
  215.                         }
  216.                 }
  217.                 exit(0);
  218.         }
  219.  
  220.         close(pipefds[PIPE_READ]);
  221.  
  222.         const char message[] = "Hello!\n";
  223.  
  224.         write(pipefds[PIPE_WRITE],message,sizeof(message)-1);
  225.  
  226.         close(pipefds[PIPE_WRITE]);
  227.  
  228.         wait(0);
  229. }
  230. */
  231.  
  232.  
  233.  
  234.  
  235.  
  236.  
  237.  

Quellcode

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