| | |
Create UNIX Shell Pipes handler
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Oct 2009
Posts: 3
Reputation:
Solved Threads: 0
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
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)
void exec_pipe(char *args[], pid_t childpid, int pipeno1) { //pipeno1 is the integer position of the pipe in the args. int pd[2]; pipe(pd); //creates the pipe cout << args[0] << endl; // <-- This output fine with the correct content of args[0] char *args1[pipeno1]; cout << args[0] << endl; // <-- This output nothing, seems like the content is messed up after creating args1 for (int i=0; i<pipeno1; i++) args1[i] = args[i]; if ((childpid = fork()) == 0) { // child does producer close(1); // close stdout dup(pd[1]); // dup into stdout close(pd[0]); close(pd[1]); // goody execvp(args1[0], args1); } // parent does consumer close(0); // close stdin dup(pd[0]); // dup into stdin close(pd[0]); close(pd[1]); // goody execvp(args[pipeno1+1], args); }
•
•
Join Date: Mar 2009
Posts: 24
Reputation:
Solved Threads: 9
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
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.
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.
![]() |
Similar Threads
- Can't create system shell Notification Icon (Windows Software)
- Unix shell script (Shell Scripting)
- Beginner in UNIX/Shell Script - Please help (Shell Scripting)
- What is the use of unix shell scripting with oracle? (Shell Scripting)
- Returing Value from C++ code to unix shell script (C++)
- Implementing a unix shell running commands (Java)
- Making a UNIX Shell, so inexperienced at it (C++)
- i want help about the Simulate a Linux/UNIX shell, called MASH in the C (C)
- Bourne / Bash Unix/Linux shell scripting tutorial (Shell Scripting)
Other Threads in the C++ Forum
- Previous Thread: Menu
- Next Thread: Array separation
| Thread Tools | Search this Thread |
api array arrays based beginner binary bitmap c++ c/c++ calculator char class classes code compile compiler console conversion count data delete deploy desktop developer directshow dll download dynamic encryption error file forms fstream function functions game getline givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory news node number output parameter pointer problem program programming project proxy python read recursion recursive return string strings struct temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets





