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
0
votes
0 answers

Multi-piping using for loop

I'm trying to create a simple shell that supports pipes and file redirects. Here's the execute function I came up with so far: void execute(std::vector cmds) { int inp[2], out[2]; pipe(inp); pipe(out); int status, fd =…
user1563544
  • 379
  • 3
  • 17
0
votes
1 answer

running pico using execvp after dup2 in C

What I want to do is basically make standard out of the child process write in the write end of the pipe using dup2 and run pico using execvp and the parent would read the read end of the file and do something with it and write it out in the…
yellow
  • 1
  • 1
0
votes
1 answer

dup2() is preventing output

my code is pasted below. I'm trying to use dup2 to redirect my output to file. if I use it to redirect it works fine (if I remove the comments), output in file and not on stdout. ex: ls > test , results in ls outputting to test. the problem…
d0m1n1c
  • 157
  • 4
  • 16
0
votes
1 answer

dup2 in child persistent to parent

I have a weird issue with a server written in C. I have a function that is treating each client that connects to my server, so this function calls a fork() for each connection that the server receives. The issue is that I have other forks inside…
Edeph
  • 732
  • 2
  • 12
  • 29
0
votes
1 answer

Questions about dup2 and multiple thread

I meet a very string problem about dup2 and multiple thread, the code is like this: pipe out, err; int forkpid = fork(); if (forkpid == 0) { dup2(out.writeFd, STDOUT_FILENO); dup2(err.writeFd, STDERR_FILENO); printf("hello\n"); } do { …
Jet
  • 129
  • 1
  • 8
0
votes
1 answer

fork and wait process does not work with mke2fs when I redirect output

I need to fork a process, redirect output (stdout and stderr) in buffer. My code seems to work with most of binary but not all. For example I can run my code with a very long "ls" like ls -R /proc/ and it is working perfectly. When I run mke2fs…
ArthurLambert
  • 749
  • 1
  • 7
  • 30
0
votes
0 answers

Premature exit from script after fork call in Python (creating pipeline)

Code fragment inside call(argv) function if '|' in argv: # Split argv into two commands, lst[0] and lst[1] r,w=os.pipe() pid=fork() # Parent if pid >0: os.close(w) os.dup2(r,0) run(lst[0]) os.close(r) os.wait() #…
0
votes
3 answers

Almost done linux shell pipe

Hi i'm trying to build a shell on linux and i'm stuck with the pipelining part.First i take the inputs from the user like "ls | sort" then when i try to run the program it lookls like the commands ls and sort doesnt work It looks like i've done…
0
votes
1 answer

I/O redirection on Linux shell

Been working on a shell project. I have set up the I/O redirection, but I'm clearly missing something because when test it with a line like: "ls -al > outfile" it creates the outfile on my desktop, but leaves it empty and the program returns the…
Gus
  • 215
  • 1
  • 6
  • 16
0
votes
1 answer

Difficulty in redirecting output in a dup2 and pipe code in Unix

I am new in unix. In the following code, I pass three arguments from the command line "~$ foo last sort more" in order to replicate "~$ last | sort | more". I am trying to create a program that will take three argument(at least 3 for now). The…
0
votes
1 answer

Multi pipe in C (childs dont stop reading)

I am trying to implement multi pipe in C, to run multiple commands like a shell. I have made a linked list (called t_launch in my code) which look like that if you type "ls | grep src | wc" : wc -- PIPE -- grep src -- PIPE -- ls Every PIPE node…
Algo
  • 65
  • 6
0
votes
3 answers

closing a file descriptor and then using it

Below is a code segment which explains dup2 system call. What I don't understand is, after duplicating both file descriptors why do we need to close the old file descriptor. Since "out" descriptor is closed now, how does a message sent to…
DesirePRG
  • 6,122
  • 15
  • 69
  • 114
0
votes
0 answers

execlp multiple commands using pipes dup2 - c language

Im have a little C program which executes bash commands (kind of shell program). When user sends command such a: ls -l | grep .[c,h]$ | wc –l ls echo | ls I have decides to use linked list data sturcture, and this is the function that…
sharonooo
  • 684
  • 3
  • 8
  • 25
0
votes
1 answer

Using pipe/dup2 to communicate with a Python subprocess

I'd like to use Python to implement a user interface for my C program. However, I can't seem to get communication working. Here's what I've done so far, test.c: int main() { int pipe_in[2], pipe_out[2]; if (pipe(pipe_in) != 0 ||…
Steve
  • 8,153
  • 9
  • 44
  • 91
0
votes
3 answers

use dup2 system call twice?

in the code below int main () { printf ("dup2 example!\n"); int myfd= creat ( "./etest.txt", 777); dup2(myfd, 1); printf("i am in output file!\n" ); dup2(1,1); printf("i am in STDOUT!" ); return 0; } i have two dup2 calls. the first one redirects…
CSawy
  • 904
  • 2
  • 14
  • 25
1 2 3
20
21