Solved

Problems reading from pipe in subprocess

Posted on 2004-10-11
1
225 Views
Last Modified: 2010-08-05
I am working on a program that executes a database that I created and then accepts commands, pipes them to the database, and pipes the answer back to the main program.  Everytime I try to read from my database, my program just sits and does nothing.  Can anybody help?   Here is code:

---------------------------------------------interface pgm

#include <string.h>
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>

#define BUFLEN 100


main(int argc, char *argv[], char *envp[])
{
      char *buffer="";
      char pipeToChild[20];  char pipeToParent[20];
   int err;   int pid; int status;
   int toChild[2];  int toParent[2];
            /*create pipes*/
   err = pipe(&toChild[0]);
   if (err == -1) {
      printf("Error on pipe formation creation to child: %d\n", err);
      exit(1);  }

   err = pipe(&toParent[0]);
   if (err == -1) {
      printf("Error on pipe creation to Parent: %d\n",err);
      exit(2);  }

               /*create child*/

   pid = fork();

  if (pid == 0) {  /*in child*/
      close(toChild[1]);  close(toParent[0]);
      
sprintf(pipeToChild,"%d",toChild[0]);
       sprintf(pipeToParent,"%d",toParent[1]);

        err = execlp("d.exe","d",pipeToChild,pipeToParent,(char *)NULL);
      if( err == -1)
         printf("Error in execl: %d\n",err);
      exit(0);
   }
   else if (pid > 0) {  /*in parent*/

               close(toChild[0]);
      close(toParent[1]);
/*
      err = write(toChild[1],buffer,strlen(buffer));
      printf("\nmessage sent\n");
buffer = "";
printf("\waiting to read from child  buffer = %s\n",buffer);

read(toParent[0],buffer,BUFLEN);
      printf("\n the total = %.2f", buffer);
*/      /*
while ( buffer != "exit")
      {*/
         printf("Input command ( account,id | date,mm/dd/yy | exit ) \n");
            gets(buffer);


      err = write(toChild[1],buffer,strlen(buffer));
      if( err == -1)   {
         printf("Error in write to child: %d\n",err);
         exit(10);      }
 
      /*      while(err = read(toParent[0], buffer, BUFLEN)!=0)
            if( err == -1)   {
         printf("Error in read from child: %d\n",err);
         exit(11);      }*/
      /*}*/            

      exit(0);
   }
   else  {
    printf("Error in fork: %d",pid);
      exit(3);  }

}

------------------------here is the database-------------------------
#include <string.h>    
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>

#define DELIM ","
#define BUFLEN 100

/*
This is the database program*/

typedef struct account{
 long id;
 int checkNumber;
 char *date;
 float amount;  } bank;

main (int argc, char *argv[], char *envp[])
{
      char *buffer="";
char *command;  char *param;
int i;  int err;  float total;
int toChild; int toParent;
bank accounts[12];

accounts[0].id = 1234567; accounts[0].checkNumber=100;
accounts[0].date = "08/11/02";  accounts[0].amount=4.00;
accounts[1].id=1234567; accounts[1].checkNumber=101;
accounts[1].date="08/14/02"; accounts[1].amount=14.00;
accounts[2].id=3456787;  accounts[2].checkNumber=9873;
accounts[2].date="08/30/02"; accounts[2].amount=100.00;
accounts[3].id=1234567;  accounts[3].checkNumber=102;
accounts[3].date="08/16/02"; accounts[3].amount=35.00;
accounts[4].id=3456787;  accounts[4].checkNumber=9874;
accounts[4].date="09/30/02";  accounts[4].amount=4.00;
accounts[5].id=1001001;  accounts[5].checkNumber=902;
accounts[5].date="08/14/02"; accounts[5].amount=9.00;
accounts[6].id=1001001;  accounts[6].checkNumber=903;
accounts[6].date="08/30/02";  accounts[6].amount=11.00;
accounts[7].id=3456787;  accounts[7].checkNumber=9875;
accounts[7].date="08/14/02"; accounts[7].amount=6.00;
accounts[8].id=1234567;  accounts[8].checkNumber=103;
accounts[8].date="09/01/02";  accounts[8].amount=13.00;
accounts[9].id=1001001;  accounts[9].checkNumber=904;
accounts[9].date="09/01/02"; accounts[9].amount=1.00;
accounts[10].id=1001001;  accounts[10].checkNumber=905;
accounts[10].date ="08/12/02";  accounts[10].amount=3.00;
accounts[11].id=3456787;  accounts[11].checkNumber=9876;
accounts[11].date="08/12/02";  accounts[11].amount=50.00;

for(i=0; i<12; i++)
   printf("account: %d  id: %d  check number: %d  date: %s amount: %.2f\n",
      i,accounts[i].id,accounts[i].checkNumber,accounts[i].date,accounts[i].amount);

toChild = atoi(argv[1]); toParent = atoi(argv[2]);


while (err=read(toChild, buffer, BUFLEN) != 0)
   if (err == -1) {
      printf("Error on read from Parent: %d",err);
      exit(6);      }

  if (buffer != NULL)
  {
      command=strtok(buffer,DELIM);
      param = strtok(NULL,DELIM);
  }
printf("\ncommand = %s  param=  %s\n",command,param);
/*      
while(command != "exit")
{*/
       total = 0.0;
      
        if (strcmp(command,"account") ==0) {
            for (i=0; i<12; i++)
             if (accounts[i].id == atoi(param))      
                  total += accounts[i].amount;
                  sprintf(buffer,"Total for account %d:  %.2f",atoi(param),total);
                              printf("\n%s\n",buffer);
      /*       err = write(toParent,buffer ,strlen(buffer));
                  if (err == -1) {
                        printf("Error on write to Parent: %d",err);
                         exit(8);      }*/
            
            exit(0);
        }
        else if (strcmp(command , "date")==0)      {
            for (i=0;  i<12; i++)
             if (strcmp(param,accounts[i].date)==0)      
                  total += accounts[i].amount;
                  
                  sprintf(buffer,"Total on date:  %.2f",total);
                        printf("\n%s\n",buffer);
      /*       err = write(toParent,buffer ,strlen(buffer));
                  if (err == -1) {
                        printf("Error on write to Parent: %d",err);
                        exit(9);      }*/
                         }
        else
            printf("That is not a valid command, please re-enter\n");
/*
 while( err = read(toChild, buffer, BUFLEN)!=0)
  if (err == -1) {
      printf("Error on read from Parent: %d",err);
      exit(7);      }

  if (buffer != NULL)
  {
      command=strtok(buffer,DELIM);
      param = strtok(NULL,DELIM);
  }      
}      */

exit(0);
}


0
Comment
Question by:Tim_Heldberg
1 Comment
 
LVL 84

Accepted Solution

by:
ozo earned 500 total points
Comment Utility
 if (pid == 0) {  /*in child*/
     close(toChild[1]);  close(toParent[0]);
     dup2(toChild[0],fileno(stdin));
     dup2(toParent[1],fileno(stdout));
     sprintf(pipeToChild,"%d",toChild[0]);
      sprintf(pipeToParent,"%d",toParent[1]);

       err = execlp("d.exe","d",pipeToChild,pipeToParent,(char *)NULL);
 
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now