| | |
Problem with piping commands in C
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Nov 2009
Posts: 1
Reputation:
Solved Threads: 0
Hi,
I'm trying to create a simple shell in C for Unix. I've been able to do all the parsing of commands and execution, but I'm having a problem with piping. I think the problem is that I'm not hooking into the correct pipe for the input of the second command.
For example, if I type "ls | wc", it will pause after the "wc" command, which I think is because its waiting for input. I think the problem is when I use dup2(reading[i],0), and its not hooking into the correct pipe.
I know this is a bit of a broad question, but if there are any pointers I could get, I would appreciate it. Here is the code that creates new processes and tries to pipe them.
I'm trying to create a simple shell in C for Unix. I've been able to do all the parsing of commands and execution, but I'm having a problem with piping. I think the problem is that I'm not hooking into the correct pipe for the input of the second command.
For example, if I type "ls | wc", it will pause after the "wc" command, which I think is because its waiting for input. I think the problem is when I use dup2(reading[i],0), and its not hooking into the correct pipe.
I know this is a bit of a broad question, but if there are any pointers I could get, I would appreciate it. Here is the code that creates new processes and tries to pipe them.
C Syntax (Toggle Plain Text)
int fileds[2]; int reading[num_cmds]; int writing[num_cmds]; int p; for(p=0; p < num_cmds; p++) { reading[p] = -1; writing[p] = -1; } int j; for(j=0; j < num_cmds-1; j++) //Create pipes for commands { int fileds[2]; pipe(fileds); reading[j+1] = fileds[0]; writing[j] = fileds[1]; } int i = 0; for(i = 0; i < num_cmds;i++) { cmd_args = parse_cmd(cmds[i],output_file,input_file,&run_bg); //Get command and args pid_t childpid; int status; childpid=fork(); if (childpid >= 0) { if (childpid == 0) { if(writing[i] != -1) { dup2(writing[i],1); close(writing[i]); } if(reading[i] != -1) { dup2(reading[i],0); close(reading[i]); } int h; for(h = 0; h < num_cmds; h++) { close(writing[h]); close(reading[h]); } if(execvp(cmd_args[0],cmd_args) == -1) { perror("Problem with command"); exit(0); } } else { wait(&status); int m; for(m = 0; m < num_cmds; m++) { if( writing[m] != -1) close(writing[m]); if( reading[m] != -1) close(reading[m]); } } } else { perror("fork"); continue; } input_file[0] = 0; output_file[0] = 0; run_bg = 0; } }
0
#2 25 Days Ago
I have this old piping program example...maybe you'll get something out of it.
Usage ./filename ps wc
will pipe the output of ps into wc
Usage ./filename ps wc
will pipe the output of ps into wc
C Syntax (Toggle Plain Text)
#include <unistd.h> enum PIPES {READ, WRITE}; int main(int argc, char**argv) { if (argv[2]) { int hpipe[2]; pipe(hpipe); if (fork()) { close (hpipe[WRITE]); dup2 (hpipe[READ], 0); close (hpipe[READ]); execlp (argv[2],argv[2],NULL); } else { close (hpipe[READ]); dup2 (hpipe[WRITE], 1); close (hpipe[WRITE]); execlp (argv[1],argv[1],NULL); } } return 0; }
![]() |
Similar Threads
- C# Dynamic Textboxes Problem (C#)
- Problem using Windows commands in a Java Networking application (Java)
- Tough time solving this problem. Month names to store in array and manual entry? (C++)
- Trying to solve this problem but have no idea where to start...need alot of help!! (C++)
- is this a form of piping in c++? (C++)
- Linux Shell Piping Problem (C)
- problem with Sendkeys (Visual Basic 4 / 5 / 6)
Other Threads in the C Forum
- Previous Thread: Zeroing bits
- Next Thread: check 1 number is a prime or not
| Thread Tools | Search this Thread |
adobe api array arrays binarysearch calculate char cm convert copyanyfile copypdffile cprogramme createcopyoffile createprocess() csyntax directory dynamic feet fflush file floatingpointvalidation fork forloop frequency getlasterror givemetehcodez global graphics gtkgcurlcompiling hacking hardware highest homework i/o inches incrementoperators initialization iso kernel kilometer km linked linkedlist linux linuxsegmentationfault list lists locate logical_drives loopinsideloop. match matrix microsoft motherboard mqqueue mysql odf open opensource openwebfoundation owf pattern pdf performance pointer pointers posix power probleminc program programming pyramidusingturboccodes read recursion recv recvblocked repetition research scanf scheduling scripting segmentationfault send shape socketprograming socketprogramming stack standard string suggestions systemcall test testautomation unix urboc user voidmain() wab win32api windows.h





