Questions tagged [dup]

dup() is a c system call that duplicates a file descriptor

Usage: int dup(int oldfd); (#include <unistd.h>)

The C system call dup duplicates a file descriptor. It returns a second file descriptor that points to the same file table entry as precious file descriptor does, so that we can treat the two file descriptors as identical.

The dup system call uses the lowest-numbered unused file descriptor as its new file descriptor. Compare which has two arguments, and that you specify the new destination file descriptor number in the second argument.

See http://man7.org/linux/man-pages/man2/dup.2.html for more details.

176 questions
241
votes
6 answers

What's the difference between Ruby's dup and clone methods?

The Ruby docs for dup say: In general, clone and dup may have different semantics in descendent classes. While clone is used to duplicate an object, including its internal state, dup typically uses the class of the descendent object to create the…
cali-1337500
  • 2,421
  • 2
  • 14
  • 8
73
votes
5 answers

practical examples use dup or dup2

I know what dup / dup2 does, but I have no idea when it would be used. Any practical examples? Thanks.
pierrotlefou
  • 39,805
  • 37
  • 135
  • 175
27
votes
8 answers

Can someone explain what dup() in C does?

I know that dup, dup2, dup3 "create a copy of the file descriptor oldfd"(from man pages). However I can't digest it. As I know file descriptors are just numbers to keep track of file locations and their direction(input/output). Wouldn't it be easier…
Pithikos
  • 18,827
  • 15
  • 113
  • 136
24
votes
5 answers

Ruby dup/clone recursively

I have a hash like: h = {'name' => 'sayuj', 'age' => 22, 'project' => {'project_name' => 'abc', 'duration' => 'prq'}} I need a dup of this hash, the change should not affect the original hash. When I try, d = h.dup #…
Sayuj
  • 7,464
  • 13
  • 59
  • 76
14
votes
1 answer

In C, how do I redirect STDOUT_FILENO to /dev/null using dup2 and then redirect back to its original value later?

I have an assignment I'm working on and I'm having difficulty finishing it. The idea is to write a program if.c that executes one program and if that succeeds it executes the second program. I'm supposed to suppress the standard output of the first…
Frank
  • 155
  • 1
  • 2
  • 9
14
votes
1 answer

Which method to define on a Ruby class to provide dup / clone for its instances?

I have a Pointer class with a single attribute :contents, that points to an object of class MyObject. class MyObject def hello; "hello" end end class Pointer attr_reader :contents def initialize( cont ); @contents = cont end # perhaps…
Boris Stitnicky
  • 12,444
  • 5
  • 57
  • 74
13
votes
2 answers

Change read/write permissions on a file descriptor

I'm working on a linux C project and I'm having trouble working with file descriptors. I have an orphan file descriptor (the file was open()'d then unlink()'d but the fd is still good) that has write-only permission. The original backing file had…
Andrew Klofas
  • 610
  • 1
  • 7
  • 19
13
votes
2 answers

When to use dup, and when to use clone in Ruby?

What's the difference between Ruby's dup and clone methods? describes the difference in the behavior of dup and clone. But when should I use dup, and when should I use clone instead? Examples from actual projects which discuss why they used dup…
Andrew Grimm
  • 78,473
  • 57
  • 200
  • 338
11
votes
3 answers

Redirect stdout from python for C calls

This is a follow up question from here specifically concerning its answer. From a python module I am calling a Hello World executable that simply prints Hello World to the stdout. I am interested in redirecting that output to a python StringIO and…
Woltan
  • 13,723
  • 15
  • 78
  • 104
11
votes
6 answers

Duplicating a Ruby array of strings

arr = ["red","green","yellow"] arr2 = arr.clone arr2[0].replace("blue") puts arr.inspect puts arr2.inspect produces: ["blue", "green", "yellow"] ["blue", "green", "yellow"] Is there anyway to do a deep copy of an array of strings, other than…
dangerousdave
  • 6,331
  • 8
  • 45
  • 62
9
votes
4 answers

Redirect STDOUT and STDERR to socket in C?

I am trying to redirect STDOUT AND STDERR to a socket. I did: if(fork() == 0) { dup2(newsock, STDOUT_FILENO); dup2(newsock, STDERR_FILENO); execvp(); } Somehow, it only showed the first little part of the output. for example, it showed on…
JJ Liu
  • 1,351
  • 8
  • 20
  • 36
8
votes
3 answers

Python: fork, pipe and exec

I want to execute a program in a python application, it will run in the background but eventually come to the foreground. A GUI is used to interact with it. But controls are offered via a console on stdin and stdout. I want to be able to control it…
Aki
  • 329
  • 3
  • 13
  • 28
8
votes
3 answers

Instance variable still references after 'dup'

I have an object of a class, and I want to duplicate it with dup. One of the instance variables is an array, and it seems to be referencing it. I thought dup actually created a DUPLICATE. Here's my IRB session: irb(main):094:0> class…
itdoesntwork
  • 4,666
  • 3
  • 25
  • 38
7
votes
1 answer

dup, dup2, tmpfile and stdout in python

This is a follow up question from here. Where I want do go I would like to be able to temporarily redirect the stdout into a temp file, while python still is able to print to stdout. This would involve the following steps: Create a copy of stdout…
Woltan
  • 13,723
  • 15
  • 78
  • 104
7
votes
5 answers

How to redirect the output of system() to a file?

In this C program #include #include #include #include int main() { int file = open("Result", O_CREAT|O_WRONLY, S_IRWXU); dup2(stdout, file); system("ls -l"); return 0; } I'm trying to…
Eight
  • 4,194
  • 5
  • 30
  • 51
1
2 3
11 12