dup2

Having trouble with fork(), pipe(), dup2() and exec() in C

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; ...

Does this multiple pipes code in C makes sense?

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...

Multiple open file for reading at same time

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...

Thread feeding other MultiThreading

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...

after dup2, stream still contains old contents?

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...

C: Got stuck with dup2() :-(

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...

Trouble with dup2

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...

Using dup2 for piping

How do I use dup2 to perform the following command? ls -al | grep alpha | more ...

Can popen() make bidirectional pipes like pipe() + fork()?

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...

trouble pipeline three commands "dmesg|sort|more" c++/c

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....

Trouble with dup2, stdout, and stderr

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...