Thanks for taking a look at it. I am only posting the relevant sections.
Code:
...
/****************************/
/* Global variables and constants: */
/****************************/
#define RSIZE 80
#define BUFSIZE 10
#define NEWBUFSIZE 200
#define FD_READ 0
#define FD_WRITE 1
#define STDIN 0
#define STDOUT 1
#define NO_OF_OPERANDS 5
#define NO_OF_OPERATORS 4
...
establishProcessPipe(pipeFDs);
fdin = fopen("./data.in","r");
fdout = fopen("./data.out","w");
/* Create child process to load the pipe w/input. Process the result */
/* as the parent: */
pid = fork();
switch(pid)
{
case -1: /* fork() ERROR */
printf("ERROR: Could not spawn child!");
exit(1);
case 0: /* I am the child */
close(STDOUT);
dup2(pipeFDs[FD_WRITE]);
close(FD_WRITE);
processInputFile(pipeFDs,fdin,pid);
fclose(fdin);
exit(0);
default: /* I am the parent */
close(STDIN);
dup2(pipeFDs[FD_READ]);
close(FD_READ);
processLoadedPipe(pipeFDs,fdout,pid);
fclose(fdout);
exit(0);
}
...
void establishProcessPipe(int pipeFDs[])
{
if(pipe(pipeFDs) == -1)
{
printf("ERROR: Could not create pipe!");
exit(1);
}
}
void processLoadedPipe(int pipeFDs[],FILE *fdout,int pid)
{
/******************************/
/* Create output file from result pipe: */
/******************************/
int nread;
char buf[NEWBUFSIZE];
char newbuf[NEWBUFSIZE] = {""};
nread = 0;
close(pipeFDs[FD_WRITE]); /* Close write descriptor */
while((nread = read(pipeFDs[FD_READ],buf,NEWBUFSIZE)) != 0)
{
if(strcmp(buf, "EOF") == 0) /* Detect end of file message */
break;
performCalculationsUsingReadBuffer(buf,newbuf);
fprintf(fdout,"%s\n",newbuf);
}
close(pipeFDs[FD_READ]); /* Close read descriptor */
}
void processInputFile(int pipeFDs[],FILE *fdin,int pid)
{
char buf[BUFSIZE];
char newbuf[NEWBUFSIZE] = {""};
int newbufsize = NEWBUFSIZE;
int i;
while(fscanf(fdin,"%s\n",buf) != EOF)
{
performTranslationOfReadBuffer(buf,newbuf,newbufsize,pid);
writeToPipe(pipeFDs,newbuf,newbufsize,pid);
}
writeToPipe(pipeFDs,"EOF",newbufsize,pid); /* EOF message */
}
void writeToPipe(int pipeFDs[],char buf[],int bufsize,int pid)
{
close(pipeFDs[FD_READ]); /* Close read descriptor */
write(pipeFDs[FD_WRITE],buf,bufsize);
}