Ziggy54354 Posted September 28, 2008 Posted September 28, 2008 (edited) I've been trying to write a program that replicates how you would type in a series of commands that pipe into one another in linux. I've gotten the program to work with just 2 commands but when i try 3 or more, it does not output anything. If anyone can provide any help with this, that would be great. This is what i've written so far and is very much just for testing... but if I can get this to work then i can probably write the whole program. # include <stdio.h> # include <stdlib.h> # include <unistd.h> # include <sys/types.h> # include <sys/stat.h> # include <sys/wait.h> # include <fcntl.h> # include <string.h> # include <ctype.h> #include <iostream> using namespace std; int main() { pid_t child = 0; int rotate = 0; int pipe1[2]; int pipe2[2]; pipe(pipe1); char string[1000]; /* char *arg1[] = { "cat", "text.txt", (char *) 0 }; char *arg2[] = { "sort", (char *) 0 }; char *arg3[] = { "uniq", (char *) 0 }; */ char *arg1[] = { "ls", (char *) 0 }; char *arg2[] = { "sort", (char *) 0 }; char *arg3[] = { "grep", "test", (char *) 0 }; /* char *arg1[] = { "echo", "hi buddy", (char *) 0 }; char *arg2[] = { "grep", "hi", (char *) 0 }; //char *arg3[] = { "grep", "hi", (char *) 0 }; */ int numcommands = 3; for(int i = 0; i < numcommands; i++){ child = fork(); if (child == 0) { //cout << "I am child " << i << " " << rotate << endl; if (i == 0 or i == numcommands -1){ //first or last if (i == 0){ //only needs input //cout << "this should happen first" << endl; dup2(pipe1[0], 0); // read end of pipe and stdin close(pipe1[1]); } if (i == numcommands - 1 and rotate == 0){ //does not need input but needs to put something something out //cout << "this should happen last" << endl; dup2(pipe2[1], 1); // write end of pipe1 and stdout close(pipe2[0]); } if (i == numcommands - 1 and rotate == 1){ //does not need input but needs to put something something out //cout << "this should happen last" << endl; dup2(pipe1[1], 1); // write end of pipe1 and stdout close(pipe1[0]); } } else { if (rotate == 1){ cout << "this should happen 2nd" << endl; dup2(pipe1[1], 1); // write end of pipe1 and stdout close(pipe1[0]); sleep(1); //read(pipe2[0], string, 1000); cout << "testing" << endl; dup2(pipe2[0], 0); // read end of pipe2 and stdin close(pipe2[1]); } else{ //cout << "this should also not happen" << endl; dup2(pipe1[0], 0); // read end of pipe2 and stdin close(pipe1[1]); dup2(pipe2[1], 1); // write end of pipe1 and stdout close(pipe2[0]); } } //cout << "about to execute "<< i << endl; sleep(1); //this is a test if( i == 0 ) { execvp (arg3[0], arg3); } if (i == 1 ) { execvp (arg2[0], arg2); } if (i == 2 ) { execvp (arg1[0], arg1); } } else { pipe(pipe2); if (rotate == 0) { rotate = 1; } else { rotate = 0; } //cout << "I am parent" << endl; sleep(1); } } return 0; } Edited September 28, 2008 by Ziggy54354 Share this post Link to post Share on other sites More sharing options...
Recommended Posts
Please sign in to comment
You will be able to leave a comment after signing in
Sign In Now