Tim_Heldberg
asked on
Problems reading from pipe in subprocess
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:
-------------------------- ---------- ---------i nterface 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",t oChild[0]) ;
sprintf(pipeToParent,"%d", toParent[1 ]);
err = execlp("d.exe","d",pipeToC hild,pipeT oParent,(c har *)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,st rlen(buffe r));
printf("\nmessage sent\n");
buffer = "";
printf("\waiting to read from child buffer = %s\n",buffer);
read(toParent[0],buffer,BU FLEN);
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,st rlen(buffe r));
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); }
}
------------------------he re 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=10 0;
accounts[0].date = "08/11/02"; accounts[0].amount=4.00;
accounts[1].id=1234567; accounts[1].checkNumber=10 1;
accounts[1].date="08/14/02 "; accounts[1].amount=14.00;
accounts[2].id=3456787; accounts[2].checkNumber=98 73;
accounts[2].date="08/30/02 "; accounts[2].amount=100.00;
accounts[3].id=1234567; accounts[3].checkNumber=10 2;
accounts[3].date="08/16/02 "; accounts[3].amount=35.00;
accounts[4].id=3456787; accounts[4].checkNumber=98 74;
accounts[4].date="09/30/02 "; accounts[4].amount=4.00;
accounts[5].id=1001001; accounts[5].checkNumber=90 2;
accounts[5].date="08/14/02 "; accounts[5].amount=9.00;
accounts[6].id=1001001; accounts[6].checkNumber=90 3;
accounts[6].date="08/30/02 "; accounts[6].amount=11.00;
accounts[7].id=3456787; accounts[7].checkNumber=98 75;
accounts[7].date="08/14/02 "; accounts[7].amount=6.00;
accounts[8].id=1234567; accounts[8].checkNumber=10 3;
accounts[8].date="09/01/02 "; accounts[8].amount=13.00;
accounts[9].id=1001001; accounts[9].checkNumber=90 4;
accounts[9].date="09/01/02 "; accounts[9].amount=1.00;
accounts[10].id=1001001; accounts[10].checkNumber=9 05;
accounts[10].date ="08/12/02"; accounts[10].amount=3.00;
accounts[11].id=3456787; accounts[11].checkNumber=9 876;
accounts[11].date="08/12/0 2"; 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].checkNu mber,accou nts[i].dat e,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,DELI M);
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,DELI M);
param = strtok(NULL,DELIM);
}
} */
exit(0);
}
--------------------------
#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",t
sprintf(pipeToParent,"%d",
err = execlp("d.exe","d",pipeToC
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,st
printf("\nmessage sent\n");
buffer = "";
printf("\waiting to read from child buffer = %s\n",buffer);
read(toParent[0],buffer,BU
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,st
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); }
}
------------------------he
#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=10
accounts[0].date = "08/11/02"; accounts[0].amount=4.00;
accounts[1].id=1234567; accounts[1].checkNumber=10
accounts[1].date="08/14/02
accounts[2].id=3456787; accounts[2].checkNumber=98
accounts[2].date="08/30/02
accounts[3].id=1234567; accounts[3].checkNumber=10
accounts[3].date="08/16/02
accounts[4].id=3456787; accounts[4].checkNumber=98
accounts[4].date="09/30/02
accounts[5].id=1001001; accounts[5].checkNumber=90
accounts[5].date="08/14/02
accounts[6].id=1001001; accounts[6].checkNumber=90
accounts[6].date="08/30/02
accounts[7].id=3456787; accounts[7].checkNumber=98
accounts[7].date="08/14/02
accounts[8].id=1234567; accounts[8].checkNumber=10
accounts[8].date="09/01/02
accounts[9].id=1001001; accounts[9].checkNumber=90
accounts[9].date="09/01/02
accounts[10].id=1001001; accounts[10].checkNumber=9
accounts[10].date ="08/12/02"; accounts[10].amount=3.00;
accounts[11].id=3456787; accounts[11].checkNumber=9
accounts[11].date="08/12/0
for(i=0; i<12; i++)
printf("account: %d id: %d check number: %d date: %s amount: %.2f\n",
i,accounts[i].id,accounts[
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,DELI
param = strtok(NULL,DELIM);
}
printf("\ncommand = %s param= %s\n",command,param);
/*
while(command != "exit")
{*/
total = 0.0;
if (strcmp(command,"account")
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].
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,DELI
param = strtok(NULL,DELIM);
}
} */
exit(0);
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.