Create UNIX Shell Pipes handler

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Oct 2009
Posts: 3
Reputation: jerrytouille is an unknown quantity at this point 
Solved Threads: 0
jerrytouille jerrytouille is offline Offline
Newbie Poster

Create UNIX Shell Pipes handler

 
0
  #1
29 Days Ago
Let's start with 1 pipe such as ex: "ls | grep hello"

What I'm trying to do is to split the *args[] into 2 that one contains "ls" (left-of-the-pipe command) and the other contains "grep hello" (right command) -> so I can execute the child pipe as producer and parent as consumer.

My problem is it seems like I can't create another char *args1[] because it would mess up the current args[] that passed in.

I search everywhere and tried "memcpy or strcpy" but didn't solve the problem, since I might not use them correctly.

Thanks for your help

  1. void exec_pipe(char *args[], pid_t childpid, int pipeno1)
  2. {
  3. //pipeno1 is the integer position of the pipe in the args.
  4.  
  5. int pd[2];
  6. pipe(pd); //creates the pipe
  7.  
  8. cout << args[0] << endl; // <-- This output fine with the correct content of args[0]
  9.  
  10. char *args1[pipeno1];
  11. cout << args[0] << endl; // <-- This output nothing, seems like the content is messed up after creating args1
  12.  
  13. for (int i=0; i<pipeno1; i++)
  14. args1[i] = args[i];
  15.  
  16. if ((childpid = fork()) == 0)
  17. {
  18. // child does producer
  19. close(1); // close stdout
  20. dup(pd[1]); // dup into stdout
  21. close(pd[0]); close(pd[1]); // goody
  22. execvp(args1[0], args1);
  23. }
  24.  
  25. // parent does consumer
  26. close(0); // close stdin
  27. dup(pd[0]); // dup into stdin
  28. close(pd[0]); close(pd[1]); // goody
  29. execvp(args[pipeno1+1], args);
  30. }
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 24
Reputation: boblied is an unknown quantity at this point 
Solved Threads: 9
boblied boblied is offline Offline
Newbie Poster
 
0
  #2
27 Days Ago
I can't see an obvious explanation for why your second cout statement doesn't print anything. However, one flaw in this function is that the args[] and args1[] arrays must end with a 0 as their last element (a Unix requirement, not a C++ one) -- otherwise how does execvp() know where the arguments end?. In the child's execvp(), args1 isn't terminated with a 0; and in the parent's execvp(), the second argument should be args+pipeno1+1.

Your function works for me if I fix that, so maybe your args array and pipeno1 aren't set up the way you think they are.
You might try restructuring the code a little to do all the args[] string manipulation without doing the fork/exec. Verify that the strings are as expected, then add the fork/exec. In the current form, since the code goes on to fork/exec and starts manipulating file descriptors, you may be getting I/O interactions that are hiding your cout statements.
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 3
Reputation: jerrytouille is an unknown quantity at this point 
Solved Threads: 0
jerrytouille jerrytouille is offline Offline
Newbie Poster
 
0
  #3
26 Days Ago
Thanks for ur input. I figured out the problem which I didn't setup the 2 pipe args in correctly prior to the pass in. everything works fine now.
Reply With Quote Quick reply to this message  
Reply

Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC