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
2
votes
5 answers

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…
rfgamaral
  • 15,937
  • 49
  • 156
  • 269
2
votes
1 answer

Dup2() usage and output redirection

I'm trying to redirect the output of a process to the stdin of another process. This is done by dup2(). My question is: do stdin and stdout go back to their place(0,1) after function terminates, or do i have to do something like savestdin = dup(0).…
Sirona
  • 23
  • 2
2
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
  • 21
  • 1
2
votes
0 answers

Interaction between epoll_wait and dup2 across fork

The situation is that I have a shell and an interactive X-based application that receives commands over a socket dup2'd over stdin. Code follows // shell init int sockfds[2] = { -1, -1 }; socketpair(AF_UNIX, SOCK_STREAM, 0, sockfds); int epfd =…
Jesse Lactin
  • 191
  • 1
  • 10
2
votes
0 answers

data disappearing in a (TCP) socket

I have got this mostly-prototypical TCP socket server that accepts a connection and then runs a user-specified program to talk to the other side. The mysterious thing is that write() is called, and returns, but no output comes through to the…
Yannick Versley
  • 780
  • 3
  • 10
2
votes
3 answers

Are STDIN_FILENO and STDOUT_FILENO read only in c?

fd = open("/dev/null", O_RDWR); if (fd == -1) { ngx_log_error(NGX_LOG_EMERG, log, ngx_errno, "open(\"/dev/null\") failed"); return NGX_ERROR; } if (dup2(fd, STDIN_FILENO) == -1) { ngx_log_error(NGX_LOG_EMERG, log,…
cpuer
  • 6,413
  • 13
  • 32
  • 38
2
votes
1 answer

Win32 GUI C(++) app redirect both stdout and stderr to the same file on disk

I'm creating a Windows service, which cannot have an associated console. Therefore I want to redirect stdout and stderr to a (the same) file. Here is what I discovered so far: Redirecting cout and cerr in C++ can be done by changing the buffers,…
SWdV
  • 1,135
  • 1
  • 11
  • 25
2
votes
1 answer

Weird behaviour on redirection of exec() output

I am writing a program which creates a txt files input.txt and it uses it as input of exec(). I have problems redirecting its output to another file output.txt and they seems to be linked to input.txt writing. If I use fwrite(array, 4, 1, file) the…
Luca
  • 51
  • 3
2
votes
3 answers

Why redirecting to stdout with dup2 works only if printf was called before redirecting

I'm trying to write a program that connects to a server opened with nc -v -l 1337 on another terminal, and redirects stdin, stdout, stderr to the socket. Meaning, the other terminal will write to the socket, and my program will read it with…
Nadav G
  • 23
  • 4
2
votes
1 answer

Why when i use dup2() before fork() it doesn't work?

I want to make a shell and i want to handle multiple pipe. I tried to understand how works dup2() and fork. Normally, fork(), create a new child process who is exactly the same as the father. But when i use dup2 before the fork it doesn't seem to…
Progear
  • 157
  • 5
2
votes
1 answer

dup2 to redirect stdout and stderr to another file descriptor

i have a call like this. int fd[2]; pipe(fd) and then dup2(fd[WRITE],STDOUT_FILENO) is there a way to use the dup call to duplicate both 1 and 2 to fd[WRITE]?
Prasanth Madhavan
  • 10,929
  • 15
  • 56
  • 90
2
votes
1 answer

IPC using multiple pipes and forked processes to run Python programs

I am stuck with a problem for my assignment. I am trying to execute 3 concurrent processes (in C++) out of which 2 of them are Python programs and one of them is C++ program. My C++ program (sample.cpp): #include #include…
csGeek
  • 43
  • 3
2
votes
1 answer

when using different file descripter, why is the result different? (system programming)

I am studying about file descripter and realized that if I use dup2() function, the result will be different. The first snippet ... int main(void){ char buf1[BUFSIZ] = "I am low\n"; printf("i am high\n"); write(1, buf1, strlen(buf1)); …
whitehat
  • 63
  • 6
2
votes
1 answer

Pipe guarantee to close after the child has exited

In the code below, is it safe to rely on read() failure to detect termination of child? #include #include #include #include #include #include #include int…
2
votes
0 answers

When I dup2 STDOUT it takes all its contents

I am making a shell (works well). However, right now I am trying in implement output redirection. cat test1.txt > text2.txt. If I run commands without redirection, it works perfectly. So what am I missing in my redirection output…
Paul
  • 107
  • 1
  • 6
1 2
3
18 19