C, unix, pipes, fork, recursion


 
Thread Tools Search this Thread
Top Forums Programming C, unix, pipes, fork, recursion
# 1  
Old 07-05-2012
C, unix, pipes, fork, recursion

Hi, I will try to keep my post as compressed as my title was.
I am writing on pseudo code on a recursive function that I want to read from the one-above function-run and then give the result to the function-run down below until a stop is triggered. Example:
Code:
$ ls -la | grep x | sort

In my function what I want ls to run and send the result to the next function call.
What I've got so far:
Code:
main:
create a char **program_names and set int times_to_run to amount of names.
call rec(program_names, times_to_run)

rec(char** program_names, int times_to_run):
if times_to_run is 0 return, else do
int fd[2]
pipe(fd)
replace stdout with fd[1]
fork
parent:
    execl(*(program_names+times_to_run))
child:
    replace stdin with fd[0]
    //waitpid(getppid()) ?
    call rec(program_names, times_to_run-1)

Does this seem right? I'm having a hard time with recursion. Smilie
Cheers.

Moderator's Comments:
Mod Comment Please view this code tag video for how to use code tags when posting code and data.

Last edited by vbe; 07-06-2012 at 05:44 AM..
# 2  
Old 07-06-2012
You aren't closing all the pipe ends you don't need. Each process gets an independent copy of the pipe ends. Any ends they don't need must be closed or they will gum up the works later -- your pipe won't ever close when the program using it closes if there are 3 extra writing ends hanging around which haven't.

Also, you need two pipes, because you can't add a pipe to a process later -- your grep has to have a pipe for input and a pipe for output right when you run it. And then you have to save the reading end of its writing pipe for the sort...
# 3  
Old 07-06-2012
Also: Why bother with recursion, when a for-loop will do the same thing with less doublethink?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Troubles with pipes, fork, and dup2

I want to execute metasploit by two pipes to communicate with it, but I have troubles with that communication. When I run my program, I get this error: "stty: standard input: Inappropriate ioctl for device" and I don't receive the metasploit promt. just select an exploit. This is my code:... (2 Replies)
Discussion started by: dano88
2 Replies

2. Programming

C++ socket, fork & pipes

Hello, I'm stuck and this is a matter which I need to resolve quite fast (but I couldn't post in the "Emergency" section); the problem is this : I have created a chat program in which the client sends the sentence to the server and then the server should send it to all the clients connected,... (2 Replies)
Discussion started by: timmyyyyy
2 Replies

3. SCO

-sh: fork failed - too many processes in sco unix 5.0.5

Dear experts, I have done a re-installation of sco unix openserver 5.0.5 and managed to create users. The problem am facing is that of one user logging in more than 5 times. How can i overcome this problem. the system give the error below. -sh: fork failed - too many processes in sco unix... (5 Replies)
Discussion started by: njoroge
5 Replies

4. UNIX for Dummies Questions & Answers

Quick help with UNIX commands (pipes and filters)

Hey all, I need a command line that creates a new file named whatever, say stuff.txt in the current working directory which contains the number of directories in the current working directory, followed by the number of empty files in the current working directory, followed by the name of the... (2 Replies)
Discussion started by: corpsegrinder
2 Replies

5. Shell Programming and Scripting

How Unix tee to send pipeline output to 2 pipes ?

Hi, I would like to process, filter the same ASCII asynchronous live data stream in more than one pipe pipeline. So the one pipeline should filter out some records using grep key word and more than one pipes pipelines each should grep for another key words, each set seperately for each... (5 Replies)
Discussion started by: jack2
5 Replies

6. Programming

Recursion

I want to halt a tail recursive function after certain validation. I want to come out of entire recursion without unwinding phase. How can i achieve that . The coding is done in C language. (5 Replies)
Discussion started by: joshighanshyam
5 Replies

7. UNIX for Dummies Questions & Answers

How to write a script by fork() in unix

Hello to UNIX Champs, Can any body help me out to write the script using fork() thru shell scripting.....i am a layman to fork(), so please give me the link or any scripts which will help me out to know the details about fork. (1 Reply)
Discussion started by: manas_ranjan
1 Replies

8. UNIX for Dummies Questions & Answers

Cannot fork , too many process - SCO Unix 5.05

I need a help... I have a HP Netserver LH 6000 U with Hardware Raid . Sco 5.0.5 installed with Oracle database. Total number of users, normally logged in are around 60 nos. The system is very slow ( takes 6 hours for processing one Lakh bills) during the Billing process. Also it is... (1 Reply)
Discussion started by: saleeshpl
1 Replies

9. UNIX for Advanced & Expert Users

Interprocess communication using pipes and fork

I'm very worried. I have an assignment that is due in 3 weeks, and also tute exercises which I can't seem to understand and work out. Okay, the question: The parent process will convert the command arguments into integer values using atoi() and store them into an integer array which you will... (2 Replies)
Discussion started by: scmay
2 Replies

10. Filesystems, Disks and Memory

PIPEs and Named PIPEs (FIFO) Buffer size

Hello! How I can increase or decrease predefined pipe buffer size? System FreeBSD 4.9 and RedHat Linux 9.0 Thanks! (1 Reply)
Discussion started by: Jus
1 Replies
Login or Register to Ask a Question