944,130 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 467
  • C RSS
Nov 7th, 2009
0

Problem with piping commands in C

Expand Post »
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. }
Reputation Points: 10
Solved Threads: 0
Newbie Poster
symmet is offline Offline
7 posts
since Nov 2009
Nov 7th, 2009
0
Re: Problem with piping commands in C
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. }
Reputation Points: 499
Solved Threads: 367
Postaholic
gerard4143 is online now Online
2,198 posts
since Jan 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: Zeroing bits
Next Thread in C Forum Timeline: check 1 number is a prime or not





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC