Wierd pipe problem


 
Thread Tools Search this Thread
Top Forums Programming Wierd pipe problem
# 1  
Old 05-27-2002
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  
Old 05-28-2002
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);
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

The problem of pipe

Hi,guys: I want to use c to implement a pipe. For example: ps auxwww | grep fred | more I forked three child processes. Each is responsible for each command, and pipe to next one. for(i=0;i<2;i++) pipe(fd) if(child==1) // child 1 { close(1) dup2(fd,1) close(fd) }... (3 Replies)
Discussion started by: tomlee
3 Replies

2. Programming

pipe() and poll() problem in C

Hi all, Im trying to do a simple program which ask the user for a unix command with the arguments. The program fork and the two process communicate with pipes. The child process call execvp with the command and the father process read the result of the execvp via the pipe. This program works... (11 Replies)
Discussion started by: blackmamba21
11 Replies

3. UNIX for Dummies Questions & Answers

problem with pipe operator

hi i am having issues with extra pipe. i have a data file and i need to remove the extra pipe in the(example 4th and 7thline) in datafile. there are many other line and filed like this which i need to remove from files. The sample data is below: 270 31|455004|24/03/2010|0001235|72 271... (3 Replies)
Discussion started by: abhi_n123
3 Replies

4. Shell Programming and Scripting

problem using a pipe to grep

Hello ! I want to process a text file in order to extract desired data using sed and grep... Now I am facing a problem piping to grep... nothing happens.. The text consists of blocks of 3 lines that may (or not) contain the Desired data. the desired data is on each 2... (4 Replies)
Discussion started by: ShellBeginner
4 Replies

5. Programming

Pipe problem

Could anyone tell me whats wrong whit this piping? the commands that they execute are correct. the command I am trying is ls|wc. Both processes go to the right if statement. for(i=0;i<argc;i++){ if(i==0&&argc>1){//first command if(pipe(pipa1)==-1) ... (2 Replies)
Discussion started by: isato
2 Replies

6. UNIX for Advanced & Expert Users

wierd Blue Quartz GUI problem with Virtual domains

Hi all Sorry this will turn out to be a long post since the code is kinda long. I have a centos 4.x box running Blue Quartz management system. I manage like 70 virtual domains on this server. I asked this question at the Blue Quartz Forum site NuOnce Networks / Cobalt - BlueQuartz - CentOS... (1 Reply)
Discussion started by: mcraul
1 Replies

7. Programming

Problem in read() from a pipe

Hi, Can any one please help me with this. Am struggling hard to get a solution. I am doing telnet through a C program and getting the stdout file descriptor of the remote machine to pipe. read() function is getting data, But whenl it receives SOH character ie. ^A ( Start of heading = Console... (2 Replies)
Discussion started by: JDS
2 Replies

8. Programming

Pipe Problem

Is there a way to know whether is pipe is opened in read or write mode.I mean is there any signal that is generated when a pipe is opened in read or write mode. If you have some solution .please let me know ........ (2 Replies)
Discussion started by: vivivo2000
2 Replies

9. Shell Programming and Scripting

Problem with pipe into sed

Basically I am trying to write a short script to report total space used on /u0? file systems. This is what I was trying to do:df -k /u0? | grep -v kbytes | awk '{ printf $2 "+" }' | sed s/.$// | bcBut it returns no output. This works however: > A=`df -k /u0? |grep -v kbytes | awk '{ printf $2... (2 Replies)
Discussion started by: 98_1LE
2 Replies

10. Shell Programming and Scripting

read after pipe problem OSX10.4

I use read often in scripts to filter the right part into a variable like: $ print "abc cde efg" | read k l ; print "k=$k, l=$l" k=, l= This works on linux and unix versions I work with. On OSX 10.4 this doesn't work. I found a workaround but would like to know why the original line... (5 Replies)
Discussion started by: relyveld
5 Replies
Login or Register to Ask a Question