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
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
  • 8
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
  • 12,657
  • 15
  • 62
  • 94
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
  • 53
  • 4
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
  • 127
  • 1
  • 1
  • 7
2
votes
1 answer

Bad File Descriptor In Recursive Piping Function using Execvp, fork, and pipe

I'm attempting to create a recursive piping function using dup2(), fork(), and pipe(). However, when my array is something like {"ls", "grep shell"} (where shell is the name of my shell), it goes in an endless loop of displaying the results of ls…
rDev
  • 237
  • 2
  • 15
2
votes
2 answers

Redirect FROM stderr to another file descriptor

My program calls library functions which print to stderr. I want to intervene so that all write calls to file descriptor #2 will instead get sent to somewhere else. Here is my first attempt: bool redirect_stderr (int fd) { return dup2 (2, fd) >…
spraff
  • 32,570
  • 22
  • 121
  • 229
2
votes
3 answers

Read an entire pipe - c

I've got some difficulties with this code. I need to get all the information from the pipe at its end. But, I get a segfault error. #include #include #include int main(void){ int tube[2]; if(pipe(tube) ==…
ztarky
  • 53
  • 1
  • 5
2
votes
1 answer

dup2 paramater order confusion

I have written this simple program: #include #include #include #include int main(){ int fd = open("theFile.txt", O_CREAT | O_RDWR, 0666); if(fd<0){ printf("coudlnt open File descriptor \n"); …
fresh learner
  • 467
  • 4
  • 22
2
votes
3 answers

Zombie process and fork

i have a code like this... c = fork(); if(c==0) { close(fd[READ]); if (dup2(fd[WRITE],STDOUT_FILENO) != -1) execlp("ssh", "ssh", host, "ls" , NULL); _exit(1); } close(fd[WRITE]); fd[READ] and fd[WRITE] are pipe file…
Prasanth Madhavan
  • 12,657
  • 15
  • 62
  • 94
2
votes
2 answers

python: multiprocessing.Pipe and redirecting stdout

I am using multiprocessing package to spawn a second process from which I would like to redirect stdout and stderr into the first process. I am using multiprocessing.Pipe object: dup2(output_pipe.fileno(), 1) Where output_pipe is an instance of…
gruszczy
  • 40,948
  • 31
  • 128
  • 181
2
votes
0 answers

dup2 for Input File Redirection Not Working

I am relatively new to C, and am working on an assignment in which we are required to write a shell. The shell must have most standard functionality, including file redirection and piping. File redirection must be done using dup2, and I have been…
2
votes
1 answer

pipe stdout of a child to stdin of another child in c

This is what I'm trying to do: a parent process creates two child processes, then pipes stdout of one to stdin of another. Then parent waits for 5 secs and kills the first child. This is how I approached it: First I create a pipe. Then fork to…
2
votes
1 answer

Can I call dup2 after vfork?

I want to vfork() a child process, but have its stdout be different than the parent's stdout. The obvious way to achieve this with fork() would be to dup2() (and close() the original file descriptor) in the child after forking. Let's say I have the…
talshorer
  • 662
  • 3
  • 9