Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game. |
Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
 |
10-12-2010, 02:24 AM
|
#1
|
LQ Newbie
Registered: Oct 2010
Posts: 3
Rep:
|
Multiple pipes with recursion
Hi all,
I'd like to perform the piping utility as performed by the unix shell..
It should support multiple piping.
I was told that it can be done using recursion.
can someone help?
ex: ps -ef | grep httpd | sort | wc
(or)
ls -ltr | wc
etc....
assuming commands are in an array called "COMMAND_ARRAY[i][0] and its arguments are in COMMAND_ARRAY[i][1..n]
Please help!
|
|
|
10-12-2010, 07:09 PM
|
#2
|
Senior Member
Registered: May 2005
Posts: 4,481
|
Quote:
Originally Posted by ambika_29
...
Please help!
|
What have you done yourself ? For example, what documentation regarding pipes have you read ?
You can start from here: http://www.cs.uleth.ca/~holzmann/C/s...eforkexec.html .
|
|
|
10-13-2010, 01:15 AM
|
#3
|
LQ Newbie
Registered: Oct 2010
Posts: 3
Original Poster
Rep:
|
Hi,
I have done the following..I have tried to write a code that handles two commands with one pipe...
void executePipeFunctionality()
{
int fildes1[2];
int fildes2[2];
pid_t first, second, pid;
int i = 0, index = 0, initIndex1 = 0, initIndex2 = 0, innerIndex = 0;
char* arg1[MAXITEM] = {NULL};
char* arg2[MAXITEM] = {NULL};
int returnValue = 0,status=0;
printf("NUM_PIPE_INSTANCES: %d\n", NUM_PIPE_INSTANCES);
printf("INPUT_SIZE: %d\n", INPUT_SIZE);
//printf("##1##");
arg1[0] = (char*)malloc(sizeof(char) * strlen(GLOBAL_WORD_ARRAY[0]));
arg1[1]=(char*)malloc(sizeof(char) * strlen(GLOBAL_WORD_ARRAY[1]));
arg2[0]=(char*)malloc(sizeof(char) * strlen(GLOBAL_WORD_ARRAY[3]));
arg2[1]=(char*)malloc(sizeof(char) * strlen(GLOBAL_WORD_ARRAY[4]));
//printf("hi");
strcpy(arg1[0],GLOBAL_WORD_ARRAY[0]);
strcpy(arg1[1],GLOBAL_WORD_ARRAY[1]);
strcpy(arg2[0],GLOBAL_WORD_ARRAY[3]);
strcpy(arg2[1],GLOBAL_WORD_ARRAY[4]);
//printf("^^%s^^.^^%s^^.^^%s^^",arg1[0],arg1[1],arg2[0]);
if(fork()==0)
{
pipe(fildes1);
/* We are the child process, but since we have TWO commands to exec we
* need to have two disposable processes, so fork again */
switch (fork()) {
case 0:
close(fildes1[1]); /* the other side of the pipe */
dup2(fildes1[0], 0); /* automatically closes previous fd 0 */
close(fildes1[0]); /* cleanup */
/* exec tr */
returnValue=execvp(arg2[0], arg2);
default:
close(fildes1[0]); /* the other side of the pipe */
dup2(fildes1[1], 1); /* automatically closes previous fd 1 */
close(fildes1[1]); /* cleanup */
returnValue=execvp(arg1[0], arg1);
}
}
else
{
wait(&status);
}
}
|
|
|
12-02-2010, 02:42 PM
|
#4
|
LQ Newbie
Registered: Dec 2010
Posts: 2
Rep:
|
I have implemented 2 pipes in c...........
it will get you the value for :-
ls -l | tee f1 | wc -l
where f1 is a file created by tee...
Here goes the program..........
#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include<malloc.h>
#include<ctype.h>
#include <unistd.h>
int main()
{
int fd[2], fd1[2];
int pid, pid2, pid3;
if (pipe(fd) == -1) {
printf("Error: Pipe failed.\n");
return 1;
}
if (pipe(fd1) == -1) {
printf("Error: Pipe failed.\n");
return 1;
}
if ((pid = fork()) < 0) {
printf("Error: Fork failed.\n");
return 1;
}
if (pid == 0) {
// child process
close(fd1[1]);
close(fd[1]);
dup2(fd1[0], 0);
close(fd1[0]);
char *argv1[2];
argv1[0]="wc";
argv1[1]="-l";
argv1[2]=NULL;
execvp(argv1[0],argv1);
}
else {
if ((pid2 = fork()) < 0) {
printf("Error: Fork failed.\n");
return 1;
}
if (pid2 == 0) {
// child process
close(fd[1]);
close(fd1[0]);
dup2(fd[0], 0);
close(fd[0]);
dup2(fd1[1], 1);
close(fd1[1]);
char *argv2[2];
argv2[0]="tee";
argv2[1]="f1";
argv2[2]=NULL;
execvp(argv2[0],argv2);
}
else
{
if ((pid3 = fork()) < 0)
{
printf("Error: Fork failed.\n");
return 1;}
if (pid3 == 0) {
// child process
close(fd[0]);
close(fd1[1]);
dup2(fd[1],1);
close(fd[1]);
char *argv3[2];
argv3[0]="ls";
argv3[1]="-l";
argv3[2]=NULL;
execvp(argv3[0], argv3);
printf("Error: execv failed.\n");
return 1;
}
else
{
wait(NULL);
}
}
}
return 0;
} // for main
|
|
0 members found this post helpful.
|
12-02-2010, 02:47 PM
|
#5
|
LQ Newbie
Registered: Dec 2010
Posts: 2
Rep:
|
may be u can convert it into recursion...........m also working on it............
|
|
0 members found this post helpful.
|
All times are GMT -5. The time now is 04:40 PM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|