Problem with piping commands in C

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Nov 2009
Posts: 1
Reputation: symmet is an unknown quantity at this point 
Solved Threads: 0
symmet symmet is offline Offline
Newbie Poster

Problem with piping commands in C

 
0
  #1
Nov 7th, 2009
Hi,

I'm trying to create a simple shell in C for Unix. I've been able to do all the parsing of commands and execution, but I'm having a problem with piping. I think the problem is that I'm not hooking into the correct pipe for the input of the second command.

For example, if I type "ls | wc", it will pause after the "wc" command, which I think is because its waiting for input. I think the problem is when I use dup2(reading[i],0), and its not hooking into the correct pipe.

I know this is a bit of a broad question, but if there are any pointers I could get, I would appreciate it. Here is the code that creates new processes and tries to pipe them.

  1. int fileds[2];
  2. int reading[num_cmds];
  3. int writing[num_cmds];
  4.  
  5. int p;
  6. for(p=0; p < num_cmds; p++)
  7. {
  8. reading[p] = -1;
  9. writing[p] = -1;
  10. }
  11.  
  12. int j;
  13. for(j=0; j < num_cmds-1; j++) //Create pipes for commands
  14. {
  15. int fileds[2];
  16. pipe(fileds);
  17. reading[j+1] = fileds[0];
  18. writing[j] = fileds[1];
  19. }
  20.  
  21. int i = 0;
  22. for(i = 0; i < num_cmds;i++)
  23. {
  24. cmd_args = parse_cmd(cmds[i],output_file,input_file,&run_bg); //Get command and args
  25.  
  26. pid_t childpid;
  27. int status;
  28. childpid=fork();
  29.  
  30. if (childpid >= 0)
  31. {
  32. if (childpid == 0)
  33. {
  34. if(writing[i] != -1)
  35. {
  36. dup2(writing[i],1);
  37. close(writing[i]);
  38. }
  39.  
  40. if(reading[i] != -1)
  41. {
  42. dup2(reading[i],0);
  43. close(reading[i]);
  44. }
  45.  
  46. int h;
  47. for(h = 0; h < num_cmds; h++)
  48. {
  49. close(writing[h]);
  50. close(reading[h]);
  51. }
  52.  
  53. if(execvp(cmd_args[0],cmd_args) == -1)
  54. {
  55. perror("Problem with command");
  56. exit(0);
  57. }
  58. }
  59. else
  60. {
  61. wait(&status);
  62. int m;
  63. for(m = 0; m < num_cmds; m++)
  64. {
  65. if( writing[m] != -1) close(writing[m]);
  66. if( reading[m] != -1) close(reading[m]);
  67. }
  68. }
  69. }
  70. else
  71. {
  72. perror("fork");
  73. continue;
  74. }
  75.  
  76.  
  77. input_file[0] = 0;
  78. output_file[0] = 0;
  79. run_bg = 0;
  80. }
  81.  
  82. }
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 403
Reputation: gerard4143 is on a distinguished road 
Solved Threads: 47
gerard4143's Avatar
gerard4143 gerard4143 is offline Offline
Posting Pro in Training
 
0
  #2
Nov 7th, 2009
I have this old piping program example...maybe you'll get something out of it.

Usage ./filename ps wc

will pipe the output of ps into wc

  1. #include <unistd.h>
  2.  
  3. enum PIPES {READ, WRITE};
  4.  
  5. int main(int argc, char**argv)
  6. {
  7. if (argv[2])
  8. {
  9. int hpipe[2];
  10. pipe(hpipe);
  11.  
  12. if (fork())
  13. {
  14. close (hpipe[WRITE]);
  15. dup2 (hpipe[READ], 0);
  16. close (hpipe[READ]);
  17. execlp (argv[2],argv[2],NULL);
  18. }
  19. else
  20. {
  21. close (hpipe[READ]);
  22. dup2 (hpipe[WRITE], 1);
  23. close (hpipe[WRITE]);
  24. execlp (argv[1],argv[1],NULL);
  25. }
  26. }
  27. return 0;
  28. }
Reply With Quote Quick reply to this message  
Reply

Message:


Thread Tools Search this Thread



Tag cloud for C
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC