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
6
votes
2 answers

can't dup NilClass - Error

I am stuck in this error for quite sometime now and have hit a dead end. I get this totally unhelpful error can't dup NilClass This is the situation. I have one class which is in a relationship with another. Say class Parent end class Child <…
ZX12R
  • 4,760
  • 8
  • 38
  • 53
6
votes
4 answers

How can I test STDERR with Test::More?

I am writing some tests using Test::More, and one of the functions I'm testing prints to STDERR. I'd like to test the output to STDERR, but am a little unsure how to do this. I know I'm close. This works: use strict; use warnings; use feature…
David W.
  • 105,218
  • 39
  • 216
  • 337
6
votes
1 answer

running "less" from perl pipeline

I am trying to set up arbitrary pipelines from Perl, in much the same way as a shell might. This has the desired effect, it's like "echo foo | sed s/oo/ar/": #!/usr/bin/perl use strict; use IO::Handle; $| = 1; my ($first_prog, $second_prog) =…
projix
  • 211
  • 1
  • 4
5
votes
1 answer

C: dup and close-on-exec

In a (German) book about C programming (Linux-UNIX-Programmierung, by Jürgen Wolf) I have found a statement, which translated to English would be as following (sentences numbered by me): In some cases it can be necessary that you need to duplicate…
lanoxx
  • 12,249
  • 13
  • 87
  • 142
5
votes
1 answer

How to order objects by attributes?

I am facing this issue for some time now, please look into it . object.inspect gives me this output
Dev R
  • 1,892
  • 1
  • 25
  • 38
4
votes
3 answers

why close() system call flushing the output?

Here is my code: #include #include #include #include #include #include #include int main(int argc,char *argv[]) { int oldfd; int newfd; if(argc!=2) { printf("Usgae :…
Dinesh
  • 16,014
  • 23
  • 80
  • 122
4
votes
1 answer

How to copy nested arrays and making sure that the copy is a complete dup of the original

Is there an easy way to copy a nested Array so that every object in the array will be a 'dup' of the original? I recently run into this: irb(main):001:0> a = [[1,2],[3,4]] => [[1, 2], [3, 4]] irb(main):002:0> b = a.dup => [[1, 2], [3,…
karatedog
  • 2,508
  • 19
  • 29
4
votes
2 answers

Redirect stdout/stderr using dup2, then resinstate later

Background: We have an embedded linux system running Busybox (limited resources), we have a process running which normally spits out lots of info on the console (via stdout / stderr) but we'd like to temporarily redirect it to syslog (using syslogd…
John U
  • 2,886
  • 3
  • 27
  • 39
4
votes
1 answer

I/O stream redirection in a Linux shell. How does the shell process a command with redirection?

Currently I'm coding a small shell (redirection, pipes, exec, etc.). Been trying to figure out the steps the Linux shell takes in addressing I/O redirection. Some questions on what I need help on: In which direction does the shell read from the…
John
  • 377
  • 3
  • 14
4
votes
1 answer

Unix dup pipes after fork

I want to implement communication between child process and it's parent using pipe. Here is the code: #include int main() { int pipe_dsc[2]; if (pipe(pipe_dsc) == -1) { printf("error\n"); } switch (fork()) { …
3
votes
1 answer

windows8 - _dup,_dup2

I use win8 Consumer preview build 8250 for executing a program, which works OK on win7 The program uses the following macros/functions: #if defined(_WIN32) #include #define streamDup(fd1) _dup(fd1) #define streamDup2(fd1,fd2)…
YAKOVM
  • 9,805
  • 31
  • 116
  • 217
3
votes
4 answers

What is best practice in Ruby to avoid misusing assignment "="?

I've been bitten a couple of times by forgetting that x = y in Ruby makes x refer to the same object as y; I'm too used to languages where it means, in Ruby terms, x = y.dup. Forgetting this, I inadvertently change y when I think it's safe on the…
Mike Blyth
  • 4,158
  • 4
  • 30
  • 41
3
votes
1 answer

How to redirect stdout to a file and then restore stdout back?

Here is my code and I can't get it to work. int pfd = open("file", O_WRONLY, 0777); int saved = dup(1); close(1); dup(pfd); close(pfd); printf("This goes into file\n"); // restore it back dup2(saved, 1); close(saved); printf("this goes to…
posixKing
  • 408
  • 1
  • 8
  • 17
3
votes
0 answers

printf printing all strings to output

#include #include int main(){ int fd[2]; int old = dup(1); char buf[10]; pipe(fd); dup2(fd[1],1); printf("Don't Print!\n"); dup2(old,1); printf("Hello World!\n"); return 0; } In the…
AAB
  • 1,594
  • 2
  • 25
  • 40
3
votes
2 answers

dup() and cache flush

I am a C beginner, trying to use dup(), I wrote a program to test this function, the result is a little different from what I expected. Code: // unistd.h, dup() test #include #include #include extern void…
Eric
  • 22,183
  • 20
  • 145
  • 196
1
2
3
11 12