943,942 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 860
  • C++ RSS
Nov 2nd, 2009
0

Create UNIX Shell Pipes handler

Expand Post »
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

C++ Syntax (Toggle Plain Text)
  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. }
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
jerrytouille is offline Offline
5 posts
since Oct 2009
Nov 4th, 2009
0
Re: Create UNIX Shell Pipes handler
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.
Reputation Points: 36
Solved Threads: 9
Newbie Poster
boblied is offline Offline
24 posts
since Mar 2009
Nov 5th, 2009
0
Re: Create UNIX Shell Pipes handler
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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
jerrytouille is offline Offline
5 posts
since Oct 2009

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: Menu
Next Thread in C++ Forum Timeline: Array separation





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


Follow us on Twitter


© 2011 DaniWeb® LLC