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
3
votes
1 answer

dup2 : write() redirected but not fprintf() or puts()

I'm trying to read the STDOUT of a child process. Here is the fork function (yes, c++). bool ForkAvecBus::start() { child_pid = fork(); if(child_pid == -1) return true; if(child_pid == 0) { /* Child process closes up…
Salamandar
  • 589
  • 6
  • 16
3
votes
2 answers

C: Got stuck with dup2() :-(

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…
0xDEFACE
  • 33
  • 4
3
votes
2 answers

Is it possible to redirect stdout to two places in C?

I've been stuck on this for a while now, is it possible to redirect stdout to two different places? I am writing my own shell for practice, and it can currently run commands like ps aux | wc -l or ps aux | wc -l > output.file. However, when I try to…
osama
  • 622
  • 2
  • 10
  • 19
3
votes
1 answer

Dining Philosophers in C using fork()

I wrote a C program for the Dining Philosophers Problem using pthread some time ago and am now trying to change it to use fork() instead. This is an exercive for a lecture I already passed. But a friend asked me for help and I can't seem to get it…
sfey
  • 97
  • 2
  • 8
3
votes
1 answer

Error using pipes and exec.Second command does not exit

The code takes a command as input and executes it. Pipes are also handled. The issue is that suppose if i enter ls | grep x as command. The process grep does not exit and so the program halts. Any ideas. #include #include…
ZURA
  • 31
  • 2
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
  • 16,546
  • 57
  • 163
  • 275
2
votes
1 answer

realtime redirecting stdout to file in linux c

I am trying to implement following bash line in c. while true; do echo Hello; done > out.log I can collect log in log file . But logs are written only when executable finishes execution. my test case which uses non exiting executable fails. how do…
Devidas
  • 2,479
  • 9
  • 24
2
votes
1 answer

Redirecting stdin and stdout using dup2

I'm writing my own custom shell, and as a part of my project, when the program reads either "<" or ">" character from user input, it needs to redirect stdin and stdout to a custom file. I wrote a function for this below. The part where I'm…
Susamate
  • 39
  • 6
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

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
  • 301
  • 3
  • 12
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
  • 7,413
  • 14
  • 35
  • 39
2
votes
2 answers

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,715
  • 1
  • 15
  • 36
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
1 2
3
20 21