Here's my code:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <wait.h>
#include <readline/readline.h>
#define NUMPIPES 2
int main(int argc, char *argv[]) {
char *bBuffer, *sPtr, *aPtr = NULL, *pipeComms[NUMPIPES], *cmdArgs[10];
int fdPipe[2], pCount, aCount, i, status, lPids[NUMPIPES];
pid_t pid;
...
I've created a question about this a few days. My solution is something in the lines of what was suggested in the accepted answer. However, a friend of mine came up with the following solution:
Please note that the code has been updated a few times (check the edit revisions) to reflect the suggestions in the answers below. If you intend...
I want to use dup2 to read from input file and redirect it to the input of exec function. but my problem is i have three running process all of them have to open same input file but they do different jobs. what your suggest in such case? i don't know if it is possible to use "cat data.txt" to feed the input for the three other process bu...
I see it's easy to open pipe between two process using fork, but how we can passing open pipe to threads.
Assume we need to pass out of PROGRAM A to PROGRAM B "may by more than one thread",
PROGRAM B send his output to PROGRAM C
EDIT:
I come again after modifying the code to become more easy for reading.
#include <stdio.h>
#include <s...
so if I do:
dup2(0, backup); // backup stdin
dup2(somefile, 0); // somefile has four lines of content
fgets(...stdin); // consume one line
fgets(....stdin); // consume two lines
dup2(backup, 0); // switch stdin back to keyboard
I am finding at this point.. stdin still contains the two lines I haven't consumed. Why is that? Because the...
Hello colleagues!
I have prepared a program which emulates shell (cmd) interface using pipes. There are two versions of the program:
1. Using one pipe (using a pipe from parent to child communication)
2. Using double pipe (using two pipes from parent to child and from child to parent to communicate).
So, the first program provides desi...
After incorporating Ben Voigt's answer into the code, it appears to work
Original question:
I'm trying to use dup2 to:
pass the output of "ls -al" as input to "grep foo",
whose output becomes input for "grep bar",
which finally outputs to stdout.
The final output is (blank), the file "in" is (blank) & the file "out" has the outp...
How do I use dup2 to perform the following command?
ls -al | grep alpha | more
...
I'm implementing piping on a simulated file system in C++ (with mostly C). It needs to run commands in the host shell but perform the piping itself on the simulated file system.
I could achieve this with the pipe(), fork(), and system() system calls, but I'd prefer to use popen() (which handles creating a pipe, forking a process, and...
I have successfully piped the output of one command into the input of another and then show the output of the second command to the screen.
I want to do this with three successive commands. (actually eventually I want to do it with N commands passed into the program at run time.
This is my attempt at pipelining three commands together....
When this program is run, the "stderr" line is displayed before the "stdout" line. Why? I thought dup2 would make stderr and stdout use the same file descriptor so there should be no problem with buffering. I'm using gcc 3.4.6 on Solaris 10.
#include <errno.h>
#include <stdio.h>
#include <unistd.h>
int main()
{
int fd[2];
int p...