Questions tagged [dup2]

dup2() is a c system call that duplicates a file descriptor. Also use this tag for questions about the related system call dup().

Usage: int dup2(int oldfd, int newfd);

The dup2() is a system call in C that creates a copy of a given file descriptor specified in newfd. dup2() is often used with pipes.

The difference between dup() and dup2() is that dup() always uses the lowest-numbered unused file descriptor whereas you specify the new destination file descriptor number in the second argument newfd.

dup2(), along with dup(), conforms to POSIX.1-2001, POSIX.1-2008, SVr4, 4.3BSD.

See the Linux manual page for dup2(), or the POSIX specification for dup2(), for more details.

278 questions
24
votes
8 answers

Can someone explain what dup() in C does?

I know that dup, dup2, dup3 "create a copy of the file descriptor oldfd"(from man pages). However I can't digest it. As I know file descriptors are just numbers to keep track of file locations and their direction(input/output). Wouldn't it be easier…
Pithikos
  • 14,773
  • 14
  • 98
  • 115
24
votes
6 answers

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…
Taylor Edmiston
  • 9,072
  • 4
  • 45
  • 65
19
votes
2 answers

Using dup2 for piping

How do I use dup2 to perform the following command? ls -al | grep alpha | more
Rob Kearnes
  • 191
  • 1
  • 1
  • 3
13
votes
2 answers

Race condition when using dup2

This manpage for the dup2 system call says: EBUSY (Linux only) This may be returned by dup2() or dup3() during a race condition with open(2) and dup(). What race condition does it talk about and what should I do if dup2 gives EBUSY error? Should…
vitaut
  • 37,224
  • 19
  • 144
  • 248
12
votes
1 answer

In C, how do I redirect STDOUT_FILENO to /dev/null using dup2 and then redirect back to its original value later?

I have an assignment I'm working on and I'm having difficulty finishing it. The idea is to write a program if.c that executes one program and if that succeeds it executes the second program. I'm supposed to suppress the standard output of the first…
Frank
  • 135
  • 1
  • 2
  • 9
10
votes
3 answers

Redirect stdout from python for C calls

This is a follow up question from here specifically concerning its answer. From a python module I am calling a Hello World executable that simply prints Hello World to the stdout. I am interested in redirecting that output to a python StringIO and…
Woltan
  • 12,751
  • 12
  • 70
  • 97
9
votes
6 answers

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

Here's my code: #include #include #include #include #include #define NUMPIPES 2 int main(int argc, char *argv[]) { char *bBuffer, *sPtr, *aPtr = NULL, *pipeComms[NUMPIPES],…
rfgamaral
  • 15,937
  • 49
  • 156
  • 269
8
votes
3 answers

Pipes, dup2 and exec()

I have to write a shell that can run pipes. For example commands like ls -l | wc -l". I have successfully parsed the command given by the user as below: "ls" = firstcmd "-l" = frsarg "wc" = scmd "-l" = secarg Now I have to use two forks since the…
Aris Kantas
  • 285
  • 1
  • 3
  • 13
7
votes
1 answer

How do you use dup2 and fork together?

I'm taking an operating systems course and I'm having a hard time how input is redirected with dup2 when you have forks. I wrote this small program to try and get a sense for it but I wasn't successful in passing the output of a grand-child to a…
ShrimpCrackers
  • 4,040
  • 14
  • 47
  • 69
5
votes
4 answers

dup2() and exec()

#include #include #include #include #include int main( int argc, char **argv) { int pfds[ 2], i; size_t pbytrd; pid_t childpid; char buffer[ 200]; pipe( pfds); if( (…
The Champ
  • 51
  • 1
  • 2
5
votes
1 answer

Use dup2 to swap stdout with file descriptor and back again

Here is the code: int main() { std::cout << "In stdout" << std::endl; int stdoutBack = dup(1); close(1); int output = open("buffer.txt", O_RDWR|O_CREAT|O_APPEND, 0777); dup2(output, 1); std::cout << "In buffer" << std::endl; …
5
votes
1 answer

What does dup2() do in C

I looked it up in the man page but I still don't get it... let's say you have dup2(f1,0). Does that switch filedesc.1 with stdin and then locks stdin?
jabk
  • 1,216
  • 3
  • 21
  • 39
4
votes
1 answer

How to recover stdin overwritten by dup2?

I am trying to replace stdin with another pipe, then place the original stdin back to fd #0. e.g. dup2(p, 0); // p is a pre-existing fd of a pipe exec(/* some commands */); //what will be here in order to have the original stdin back? scanf(...)…
Yi Lin Liu
  • 149
  • 1
  • 7
4
votes
1 answer

Why does touch call the dup2() syscall?

This is coming off this question. Why is touch calling the dup2() syscall? $ > strace touch 1 2>&1 | tail close(3) = 0 open("1", O_WRONLY|O_CREAT|O_NOCTTY|O_NONBLOCK, 0666) = 3 dup2(3, 0) =…
4
votes
1 answer

Duplicate, but still use stdout

Is there some magic I can do with dup2 (or fcntl), so that I redirect stdout to a file (i.e., anything written to descriptor 1 would go to a file), but then if I used some other mechanism, it would go to the terminal output? So loosely: int…
foxcub
  • 2,287
  • 1
  • 24
  • 23
1
2 3
18 19