The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > High Level Programming
.
google unix.com



High Level Programming Post questions about C, C++, Java, SQL, and other programming languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Problem in read() from a pipe JDS High Level Programming 2 10-13-2006 11:19 AM
awk problem broken pipe zemanel UNIX for Dummies Questions & Answers 1 04-24-2006 09:45 AM
Pipe Problem vivivo2000 High Level Programming 2 01-11-2006 04:43 AM
Problem with pipe into sed 98_1LE Shell Programming and Scripting 2 08-13-2005 05:40 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 05-27-2002
denoir denoir is offline
Registered User
  
 

Join Date: May 2002
Posts: 3
Wierd pipe problem

I have encountered a strange problem dealing with pipes
and forking. The program basicaly does this:

cat file | tbl | eqn | groff

Now, I have a parent process that forks children
that that exec the stuff that they should. The pipes defined
in the parent are the ones used.

The chain goes like this

Parent creates N pipes (R/W pairs)

for i=N; i>0;i--
{
Parent forks child and gives it the read part of pipe i and
the write part of pipe i-1 (relative parent)
Child sets its stdin to pipe i and stdout to pipe i-1
Child execs program
}
Parent reads from the last pipe and writes to file.


Pipe: 0 1 2 3 4 5 6 7 8 9
parent: |R|W|R|W|R|W|R|W|R|W|
child: <-| | | (going backwards)


(I hope I formulated it right here, it's a bit messy)

Now the program works for small files, but for larger ones it truncates the file size to a multiple of the pipe buffer size k * (4096 bytes). I have no clue why this is, since the read() function waits for an EOF. The read function should also remove the things that it reads, so the pipe buffer should be continuously emptied.

I have asked several persons that know IPC far better then I do and they all agree that the code should work. But it doesnt.
Any ideas would be very welcome since I've been pulling my hair over this the last few days, without coming any closer to any explanation.


------------------------------------------------------
#include <stdio.h>
#include <string.h>
#include <errno.h>
#define NOLL NULL
#define TROFF "/usr/bin/groff"
#define TBL "/usr/bin/tbl"
#define EQN "/usr/bin/eqn"
#define VIEWER "/mpkg/gv/3.5.8/bin/gv"
#define PRINTER "/usr/bin/lpr"
#define KATT "/bin/cat"



/*to handle variables in a bunldle*/
typedef struct
{
char** files;
char* toGroff;
int doPrint;
int doView;
int numFiles;
}param;

void doParse(param* pars, int argc, char **argv);
void handleFile(param* pars, int file);
void processChild(char* prg, char** arg, int* inout, int pass);

/*
Pipe: 0 1 2 3 4 5 6 7 8 9
parent: |R|W|R|W|R|W|R|W|R|W|
child: <-| | | (inverted and going backwards)
*/

void processChild(char* prg, char** arg, int* inout, int pass)
{
int eRet; //error
int cPid; //pod
int* status; //status
char buff[10000]; //out buffer
cPid = 0;
switch (cPid = fork()) //new clone
{

case 0:
dup2(inout[0], 1); // invert the pipe so that parent stdout is child stdin
dup2(inout[1], 0);
for(eRet = 3; eRet < 3+10; close(eRet++)); //close all the
unused pipes except stdin, stdout and stderr
if (!pass)
{
eRet = execv(prg, arg); //exec with args
printf("return %d, %s\n",eRet, strerror(errno)); //should not happen
}
else
{
eRet = read(0, buff, 10000);
write(1, buff, eRet);
eRet = 0;
}
exit(0);
case -1:
printf("unhable to fork! error processing %s\t%s\n",prg,
arg[0]);
exit(6);


}
}



void handleFile(param* pars, int file)
{
FILE* dafile;
int i;
int afpid;
int pipor[10]; //pipes
int inout[2]; //current pipe
int* inoutinout; //pointer to current pipe
char* tmpFile;
char* buff[500000]; //final output buffer
int rch;
//naturally organized optional parameters
char* noop[] = {"cat", pars->files[file], NOLL, NOLL}; //parameters
to program

for(i = 0; i<50000;buff[i++]=NOLL);

for(i = 0; i<10;i++) //go througbht the pipes
{
if(pipe(inout) == -1)
{
printf("Error processing %s, unable to open pipe\n",
pars->files[file]);
exit(9);
}
pipor[i++] = inout[0]; //stdin
pipor[i] = inout[1]; //std out
}

close(pipor[9]); //cat doesnt need stdin


inoutinout = pipor+7; //move pointer five steps (going backwards)
processChild(KATT, noop, inoutinout, 0); //process the cat

//close pipes
close(pipor[8]);
close(pipor[7]);

//parameters to tbl
noop[1] = NOLL;
noop[0] = "tbl";
inoutinout = pipor+5; //move pointer three steps
processChild(TBL, noop, inoutinout, 0);

close(pipor[6]);
close(pipor[5]);
//parameters to eqn - we have a problem
noop[1] = NOLL;
noop[0] = "eqn";


inoutinout = pipor+3; // three steps
processChild(EQN, noop, inoutinout, 0);

close(pipor[4]);
close(pipor[3]);

noop[1] = pars->toGroff;
noop[0] = "groff";

inoutinout = pipor+1; //one step
processChild(TROFF, noop, inoutinout, 0);

close(pipor[2]);
close(pipor[1]);

tmpFile = tmpnam(NOLL);
dafile = fopen(tmpFile,"a");
rch = read(pipor[0], buff, sizeof(buff));



printf("Size:%d\n",fputs(buff,dafile));
fclose(dafile);
printf("%s\n", tmpFile);


/// <SNIP> some irreleavant code

}

/// <SNIP> some irreleavant code
  #2 (permalink)  
Old 05-28-2002
denoir denoir is offline
Registered User
  
 

Join Date: May 2002
Posts: 3
Never mind. Problem solved!
My read function didn't wait for EOF so it didn't get the entire buffer. The solution was instead of doing one read to do:

while((rch = read(pipor[0], buff, sizeof(buff)))>0)
fwrite(buff,1,rch,dafile);
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 08:05 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0