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
1
vote
2 answers

C++ dup2 and execl

I am working on an assignment and I need to create pipes so that other programs handle different functions. I am able to pipe through the command line no problem, thats easy. However using dup2 and execl have been tricky for me. At one point I was…
user975044
  • 375
  • 4
  • 11
  • 26
1
vote
1 answer

Write to file with C++ dup2

Alright I am trying to read from one file and write to another. I have other things to add such as grabbing info from the first file but for testing I am trying to get it to write to the second file. My understanding was that everything after the…
user975044
  • 375
  • 4
  • 11
  • 26
1
vote
0 answers

STDIN dup2 for OpenBSD/NetBSD

I have a child using dup2: case OPGAME: users[userid]->userNextEntry = redirectuser( userid, 0, USERMODULES[MENU] ); pid = fork(); if ( pid == -1 ) parseerror( SYSTEMERROR ); users[userid]->userChildPID = pid; if (…
1
vote
1 answer

Implementing dup2 in xv6

I'm taking an OS class and we have to implement dup2() in xv6. The code I've written should theoretically work, but, when I try to execute tests, it doesn't pass all of them. This is my system call: int sys_dup2(void) { int oldfd, newfd; struct…
1
vote
1 answer

Child process not reading from pipe, unless parent calls printf before dup2

The code below forks a child process, and redirects stdout to the pipe. The child is supposed to read from the pipe but it's not happening. Strangely, if the parent is made to call printf at least once before calling dup2, things seem to work. I…
cobra
  • 435
  • 3
  • 9
1
vote
1 answer

Interprocess communication, reading from multiple children stdout

I'm trying to write a custom shell-like program, where multiple commands can be executed concurrently. For a single command this is not much complicated. However, when I try to concurrently execute multiple commands (each one in a separate child)…
null
  • 1,944
  • 1
  • 14
  • 24
1
vote
1 answer

How to change stdio of a given process (i.e. not current process)?

Here is a way to change stdio of a given process by external tools such as gdb: https://www.baeldung.com/linux/redirect-output-of-running-process My question is, how can I do this programatically? I am using Go, bug example in C is also OK. EDIT My…
xrfang
  • 1,754
  • 4
  • 18
  • 36
1
vote
1 answer

Can't redirect printf to pipe

I want to redirect the output of printf to a pipe, but for some reason it doesn't seem to work. What does work is using write instead. This is my program #include #include #include #include #include…
Paul
  • 776
  • 1
  • 5
  • 18
1
vote
1 answer

Trying to replicate basic bash pipe but i get a stdin: Input/output error

I am currently working on a university project to basically built my own simple shell. Everything is working great so far. The only thing giving me trouble is pipes. To make it easier for myself to figure out why they are not working as intended I…
Panini
  • 11
  • 1
1
vote
2 answers

multi chains of pipes

I am desperate, I am searching for a month for reference/source code for multi chains of pipes, meaning that I can run something: cat /tmp/test.log | wc -l --> stdout | grep test1 --> stdout |…
Lyn
  • 21
  • 1
1
vote
1 answer

C : (standard input): Bad file descriptor

I try to recreate for a school project a program who emulate this type of command < infile grep g | wc -c > outfile Someone i have any idea why i'm getting grep: (standard input): Bad file descriptor when running the following code void …
FoReal
  • 27
  • 6
1
vote
2 answers

Multiple pipes in C

I want to implement multi pipes in c so I can do something like this, where ||| means duplicate the stdin to N pipe commands): cat /tmp/test.log ||| wc -l ||| grep test1 ||| grep test2 | grep test3 This will return to me the number of lines in the…
Lyn
  • 21
  • 1
1
vote
1 answer

can't read pipe after using dup2 to copy stdout

I am trying to use a pipe to rederect stdout into a pipe and read it later. I will use this later with fork(), where the child process starts a different program that I need to comunicate with. This is my Code: #include #include…
1
vote
2 answers

How to use pipe and dup2 in c

I have to simulate this command using pipes in c: echo "" | bc -lq. Process A must read a string and send it to process B; Process B executes the "bc -lq" command and returns the result to A. The code is this, but I can't understand why it…
Shyvert
  • 27
  • 4
1
vote
1 answer

custom shell multiple pipes succeed, but no output to stdout

I am creating a custom shell and currently working on getting multiple piping to work. Eg, ls -al | wc -l returns the number of all file directories in current directory. I am closely following the "solution code" at this link. Here is my…
Aaron Li
  • 89
  • 10