LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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


Reply
  Search this Thread
Old 10-12-2010, 02:24 AM   #1
ambika_29
LQ Newbie
 
Registered: Oct 2010
Posts: 3

Rep: Reputation: 0
Question 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!
 
Old 10-12-2010, 07:09 PM   #2
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by ambika_29 View Post
...
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 .
 
Old 10-13-2010, 01:15 AM   #3
ambika_29
LQ Newbie
 
Registered: Oct 2010
Posts: 3

Original Poster
Rep: Reputation: 0
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);
}

}
 
Old 12-02-2010, 02:42 PM   #4
visyuvi
LQ Newbie
 
Registered: Dec 2010
Posts: 2

Rep: Reputation: 0
Thumbs up

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.
Old 12-02-2010, 02:47 PM   #5
visyuvi
LQ Newbie
 
Registered: Dec 2010
Posts: 2

Rep: Reputation: 0
may be u can convert it into recursion...........m also working on it............
 
0 members found this post helpful.
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
C - Using pipes with a parent that has multiple children golmschenk Programming 5 03-07-2010 03:30 PM
Recursion in C hubabuba Programming 12 10-03-2005 07:46 AM
tar: '--no-recursion' option doesn't prevent recursion Earl Parker II Slackware 12 08-17-2004 02:49 AM
Multiple 100mb pipes on 100mb switches DLinkOZ Linux - Networking 2 10-22-2003 11:35 AM
help with recursion function debdas Programming 4 05-14-2003 03:03 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:30 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration