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

C program to pipe multiple commands

I have written the below method to fork and execute commands separated by multiple pipes( test with : ls -lrt | grep "check" | wc -l . However it is not resulting in any output, could any one please spot my mistake. Thanks. void execCmd (pInfo…
Nohsib
  • 3,614
  • 14
  • 51
  • 63
2
votes
1 answer

Redirecting shell from client to server via sockets using dup2 without netcat

Does anybody have any ideas on: How I could implement a server for this client without using netcat? I've tried different things but as I'm not too familiar with pipes I thought about making this post. If I use this code and i set up a netcat…
2
votes
1 answer

Program stuck on Pipe (exec ls grep sort)

I'm trying to make a program that executes the following commands connecting the output of one to the input of the next using pipes and taking two arguments DIR (directory) and ARG (filetype, example: jpg). ls DIR -laR | grep ARG | sort Here's the…
0kazaki
  • 23
  • 1
  • 7
2
votes
1 answer

Python os.dup2 redirect enables output buffering on windows python consoles

I'm using a strategy based around os.dup2 (similar to examples on this site) to redirect C/fortran level output into a temporary file for capturing. The only problem I've noticed is, if you use this code from an interactive shell in windows (either…
Michael Clerx
  • 2,928
  • 2
  • 33
  • 47
2
votes
2 answers

Why does this example of Forking not need mutex?

My professor has this sample code of a function that forks in order to act as a pipe. But how can he ensure that the parent executes before the child without having to use a mutex? void runpipe(int pfd[]) { int pid; switch (pid = fork()) { case…
2
votes
1 answer

Posix C Piping Delay

I faced a nasty problem: Suppose I have one program, let's name it HelloProgram which code is very simple: void print_bullshit() { int i; for (i = 0; i < 10; ++i) { printf("hello!"); sleep(3); } printf("bye!"); } And I have another program,…
inaumov17
  • 1,329
  • 11
  • 18
2
votes
2 answers

Clarification on how pipe() and dup2() work in C

I am writing a simple shell that handles piping. I have working code, but I don't quite understand how it all works under the hood. Here is a modified code snippet I need help understanding (I removed error checking to shorten it): int…
2
votes
2 answers

Toy shell not piping correctly

I'm not going to lie. This is a homework question. However, as far as I'm concerned, the points are gone baby gone. Right now, I'm just looking for an answer, because I -think- I might be insane. The goal of this program is to execute the command ps…
Gearov
  • 87
  • 6
2
votes
1 answer

Using Dup2 to Redirect Input and Output

I have been writing a Unix shell in C, and I am attempting to implement input and output redirection. I have been using Dup2 for this and am able to make it so my output redirects to a file, and my input is redirected correctly as well. However,…
user1174511
  • 309
  • 3
  • 5
  • 15
2
votes
1 answer

How can I exec and write to stdin and pipe stdout to a socket?

I'm writing an http server for my school project, and I'm trying to execute a CGI script. The following code successfully executes the cgi program, and the output of the program is sent to the browser, but there's something wrong with the way I'm…
Trevor Dixon
  • 23,216
  • 12
  • 72
  • 109
1
vote
1 answer

Trouble using dup2 to make a C program execute a command such as 'ls /bin | grep grep | grep b'

I'm having trouble using dup2 to make a c program execute a command such as ls /bin | grep grep | grep b. When I comment out the third command and associated pipe it executes ls /bin | grep grep fine, but with the last command it just returns right…
1
vote
1 answer

How to use dup2/close correctly to connect these three processes?

I'm trying to properly connect three processes in order to allow inter-process communication between them. I have one process, scanner, which takes the parent's STDIN and then processes the words within the stream. If a word length is odd, it sends…
thomascirca
  • 893
  • 4
  • 14
  • 30
1
vote
1 answer

Closing pipe, dup2, file descriptors in C?

I'm running a program that does piping. The command I want to run is ls | cat. int cmd(char** w, int* pipe, int action){ ... some code up here ... int fd; if(child_pid == 0) { if (pipe != 0) { if (action == 0){ fd =…
Derek
  • 11,980
  • 26
  • 103
  • 162
1
vote
1 answer

Does dup2 do more than copy a file descriptor?

First, I open a file, then I use dup2 to copy the file descriptor. Why, when the first file descriptor is closed, can I still read the file through the other one? #include #include #include #include int…
sinners
  • 121
  • 1
  • 4
1
vote
2 answers

dup2 bad file descriptor error

I'm trying to implement multiple piping using a tutorial I got from this website. I seem to get a bad file descriptor error after executing the function that takes care of multiple piping. When I'm duping for the first time it sends me this error.…
mkab
  • 933
  • 4
  • 16
  • 31