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.

313 questions
27
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
  • 18,827
  • 15
  • 113
  • 136
25
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 D. Edmiston
  • 12,088
  • 6
  • 56
  • 76
20
votes
2 answers

Using dup2 for piping

How do I use dup2 to perform the following command? ls -al | grep alpha | more
Rob Kearnes
  • 201
  • 1
  • 2
  • 3
15
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
  • 375
  • 1
  • 5
  • 15
14
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
  • 155
  • 1
  • 2
  • 9
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
  • 49,672
  • 25
  • 199
  • 336
11
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
  • 13,723
  • 15
  • 78
  • 104
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
  • 16,546
  • 57
  • 163
  • 275
8
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,388
  • 4
  • 25
  • 43
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,388
  • 17
  • 50
  • 76
7
votes
1 answer

dup, dup2, tmpfile and stdout in python

This is a follow up question from here. Where I want do go I would like to be able to temporarily redirect the stdout into a temp file, while python still is able to print to stdout. This would involve the following steps: Create a copy of stdout…
Woltan
  • 13,723
  • 15
  • 78
  • 104
7
votes
1 answer

Understanding dup2 and closing file descriptors

I'm posting my code simply for context of my question. I'm not explicitly looking for you to help fix it, I'm more so looking to understand the dup2 system call that I'm just not picking up from the man page and the numerous other stackoverflow…
MichaelCook
  • 163
  • 1
  • 1
  • 9
5
votes
1 answer

Using pipes to communicate between two programs

I need the main prog to get two strings from the user and an argument for the other program, call fork() and then in child process I need to write the strings into pipe and send them to the other program which returns an int which I want to pass to…
5
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
  • 169
  • 1
  • 11
5
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,517
  • 2
  • 27
  • 27
1
2 3
20 21