Pipe error


 
Thread Tools Search this Thread
Top Forums Programming Pipe error
# 1  
Old 11-22-2010
Pipe error

hi guys, o have a big error
in this program but i cant solve

someone ?!

Code:
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>

int main(int argc, char *argv[]){
 

 int cont = 2, posicao; 
 char geraArquivo[100]= "|cat>>", espaco[3]=" ";
 char nomeArquivo[50], comando[1000], leitor[1000];

 if(argc>1)
 {
  strcpy(comando, argv[1]);
  while(cont< argc)
  {
   strcat(comando, espaco);
   strcat(comando, argv[cont]);
   cont++;
  }//fim do while

 printf("\nDigite o nome do arquivo: ");
 scanf("%s",nomeArquivo);
 printf("\n");
 strcat(geraArquivo,nomeArquivo);
 strcat(comando,geraArquivo);
 system(comando);
 }//fim do if

 else{
  printf("\nErro!\nDados para geração do arquivo não foram encontrados!\nPor favor entre com um comando do Sistema!\n");
  return 0;
 }//fim do else
 
 FILE *arq, *arq1, *arq2;
 arq = fopen(nomeArquivo,"r");
   arq1 = fopen("arquivoPai.txt","w+");
  arq2 = fopen("arquivoFilho.txt","w+");
 
   posicao = ftell(arq);


 int fp[2];
 int pf[2];
 int rc;
 int nbytes;
 char reader[1000];
 pipe(fp);
 pipe(pf);
 pid_t pid;
 pid = fork();

   while(!feof(arq))  //varrer arquivo
 {
  printf("OLHAE");
     if(pid == 0 && (posicao%2)==0)
  {
   close(fp[0]); // filho mandando pro pai fechar o "input"
   close(pf[1]);
   nbytes = read(pf[0], reader, sizeof(reader));
   fprintf(arq1,"%s \n",reader);
   fscanf(arq,"%s",leitor);
   write(fp[1],leitor, (strlen(leitor)+1));

  }
 
     else
  {
   close(fp[1]); // recebendo dados do filho fecharo "output"
   close(pf[0]);
   nbytes = read(fp[0],reader, sizeof(reader));
   fprintf(arq2,"%s \n",reader);
   fscanf(arq,"%s",leitor);
   write(pf[1],leitor,(strlen(leitor)+1));

  }

  posicao = ftell(arq);
 }
 
 exit(0);
 fclose(arq);
 fclose(arq1);
 fclose(arq2);
 

return 0;
}//fim da main

# 2  
Old 11-22-2010
Quote:
Originally Posted by beandj
hi guys, o have a big error
What, exactly, is this "big error"? what is happening or not happening?
# 3  
Old 11-22-2010
Code:
3include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>

int main(){
 

     int pos;
     char leitor[1000];
     char reader[1000];

    FILE *arq, *arq1, *arq2;
     arq = fopen("teste.txt","r");
        arq1 = fopen("arquivoPai.txt","w+");
        arq2 = fopen("arquivoFilho.txt","w+");


     pipe(fp);
     pipe(pf);
     pid_t pid;
     pid = fork();

    pos = ftell(arq);
       while(!feof(arq))
     {
             if(pid == 0 && (pos%2)==0)
          {
               
               close(fp[0]);
               close(pf[1]);
               read(pf[0], reader, sizeof(reader));
               fprintf(arq1,"%s \n",reader);
               fscanf(arq,"%s",leitor);
               write(fp[1],leitor, (strlen(leitor)+1));
          }
  
             else
          {
  
               close(fp[1]);
               close(pf[0]);
               read(fp[0],reader, sizeof(reader));
               fprintf(arq2,"%s \n",reader);
               fscanf(arq,"%s",leitor);
               write(pf[1],leitor,(strlen(leitor)+1));

          }

      pos = ftell(arq);
     }
 
     exit(0);
     fclose(arq);
     fclose(arq1);
     fclose(arq2);
 

return 0;
}

i dont know how to create a two-way pipes

i need to send father-to-child a pair line of teste.txt
and send child-to-faher a odd line of teste.txt

and i need to print in two diferent .txt

but the program do nothing
# 4  
Old 11-22-2010
Both are reading and nobody is writing. That is why they devise protocols. You need one, too.
# 5  
Old 11-22-2010
i read this...

have another ways to do?!

2 pipes
father read... send to child... and child print in child.txt (a pair line)
after child read... send to father... and father print in father.txt ( a odd line)
# 6  
Old 11-22-2010
The same problem can occur on sockets (tcp). That is why the http client speaks first, saying get or post or whatever. Otherwise, you need threads, processes (one to read and one to write), nonblocking IO (fcntl(), errno EAGAIN when would block) or poll/select (read when there is data, write when there is buffer space) to ensure reading supports writing. And then there are the high priority messages in the stream, EOF handling, error handling, and errno EINTR now and then as signals interrupt I/O subroutines. Oh, and on some systems, I have seen non blocking i/o still block on pipes when reading more than one byte.
# 7  
Old 11-23-2010
Here are some of the errors, you may correct to improve your program ...

Quote:
Originally Posted by beandj
Code:
3include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>

int main(){
 

     int pos;
     char leitor[1000];
     char reader[1000];

    FILE *arq, *arq1, *arq2;
     arq = fopen("teste.txt","r");
        arq1 = fopen("arquivoPai.txt","w+");
        arq2 = fopen("arquivoFilho.txt","w+");


     pipe(fp);
     pipe(pf);
     pid_t pid;
     pid = fork();

    pos = ftell(arq);
       while(!feof(arq))
     {
             if(pid == 0 && (pos%2)==0)
          {
               
               close(fp[0]);
               close(pf[1]);
               read(pf[0], reader, sizeof(reader));
               fprintf(arq1,"%s \n",reader);
               fscanf(arq,"%s",leitor);
               write(fp[1],leitor, (strlen(leitor)+1));
          }
  
             else
          {
  
               close(fp[1]);
               close(pf[0]);
               read(fp[0],reader, sizeof(reader));
               fprintf(arq2,"%s \n",reader);
               fscanf(arq,"%s",leitor);
               write(pf[1],leitor,(strlen(leitor)+1));

          }

      pos = ftell(arq);
     }
 
     exit(0);
     fclose(arq);
     fclose(arq1);
     fclose(arq2);
 

return 0;
}

i dont know how to create a two-way pipes

i need to send father-to-child a pair line of teste.txt
and send child-to-faher a odd line of teste.txt

and i need to print in two diferent .txt

but the program do nothing
There are atleast 3 errors, I could figure out. They are following:

1) You have not declered & defined pf,
2) You have not declered & defined fp,
3) You have not initilize pos; its using a garbage value.

The worst part is that you are controlling the program flow using and AND condition with (pid == 0) && (pos % 2 == 0). You are taking the mod of a garbage value, isn't it?

Now could you please try to analyse the result of that if() expression for the below two cases:
1) When pos has a garbage value which is an odd number,
2) When pos has a garbage value which is an even number

I think its pretty enough for you to know what went wrong.

Also, just declare & define pf and fp as below:
Code:
int pf[2], fp[2];

Rest is okey in your program.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

Pipe usage error while visiting directories

To begin with FYI, I really struggled with the question before asking to simplify as much as I can around 1 hour and to increase the code's readability I neglect error checks. I intend to communicate parent and child using PIPE. In the following program I do traverse given path and its... (2 Replies)
Discussion started by: beginnerboy
2 Replies

2. Shell Programming and Scripting

Pipe, permission denied error

Hello, I am trying to stream lines written into file. When first source is down, I expect the code to swap to second line and run it. Script below works as usual and it prints out each line of the input file (s.txt): #!/bin/bash while read -r line; do echo "$line" done <"$1" exit 0Output... (12 Replies)
Discussion started by: baris35
12 Replies

3. Shell Programming and Scripting

Capture error before pipe

Hi, I have a script that runs a tar command to standard out then pipes to a gzip: tar cfE - * | gzip -c > OUT.gz At the moment, even if the tar fails (e.g. because of lack of disk space), the gzip still runs successfully. Is there a way to make the whole line exit with a non-zero error... (6 Replies)
Discussion started by: Catullus
6 Replies

4. Shell Programming and Scripting

Pipe causing last command error to not function

Hi I am quite new to scripting and cannot work out how to do the following - I want to pipe to a log file and then use the "last statement error" in an if statement after, and this doesn't work because it checks the pipe statement instead of the script. Example: executteTheScript $var |... (4 Replies)
Discussion started by: erjorgito
4 Replies

5. UNIX for Dummies Questions & Answers

broken pipe error

I'm new to scripting, and this forum has been invaluable in helping me out. I'm hoping I can get some personal help now though. I have a korn script that takes a list of servers and either telnets or sshs into it (only some are set up for ssh). What I'm doing now is trying to telnet first, and... (10 Replies)
Discussion started by: aimeet
10 Replies

6. Shell Programming and Scripting

Broken Pipe error

Hello while doing sftp over server "A" , i am getting a broken pipe error i.e cat: write error: Broken pipe what does that mean? please let me know if you want any other info on this.. (3 Replies)
Discussion started by: urfrnddpk
3 Replies

7. UNIX for Dummies Questions & Answers

echo: write error: Broken pipe ??

I want to try the unix pipe, the command is like this: echo new | find . the standard output of the echo should be "new", then I guess find command will use this output as input to find the file named "new". But the output is all the file names in my current dir, the last line is "echo: write... (5 Replies)
Discussion started by: andrewust
5 Replies

8. Programming

Broken Pipe error

All, I am using the below code The C code : if ((fp2=fopen(szout_fname,"r"))==NULL) { sprintf(stream_ptr1,"cat %s | sort -t, -rn -k 11,11 | awk -F\",\" '{ \ if ( \$3 ==\"%s\" ) {print... (0 Replies)
Discussion started by: arunkumar_mca
0 Replies

9. Programming

gzip on pipe error handling

Hi all... I have the following code: FILE *fp = popen(" gzip -dc /somemount/somefile.gz", "r"); while(fgets(buffer, 1024, fp)) { some code.... } "/somemount" is a mount of some network drive. Sometimes error occurs in while loop - I can see the following "Input/Output error"... (4 Replies)
Discussion started by: adm1n
4 Replies

10. AIX

broken pipe error

Hi, I am working on AIX 5.3 . I have client-server program which is in ProC.while sending packet to server i am getting error as broken pipe and program exiting. please help?/? (1 Reply)
Discussion started by: ajaysahoo
1 Replies
Login or Register to Ask a Question