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

Redirecting input to thread

I have a really simple code where I fork once and the child node mutates with execlp and the parent keeps doing its work. Nothing too fancy. What I want is to redirect all the standard input to the execlp-ed thread, not the parent. So if I…
JnxF
  • 121
  • 5
1
vote
2 answers

How to use dup2 to redirect stdin and stdout to pipe file descriptors?

I was trying to fork a process and redirect stdout of the parent to the writing end of the pipe and stdin of the child to the reading end of the pipe. The child is supposed to read integers until the parent prints zero. the parent prints from 1 to 3…
StackExchange123
  • 1,871
  • 9
  • 24
1
vote
2 answers

C redirecting stdout to file with predefined message

I want to duplicate the message that is being printed in stdout when I hit case 's' to the opened file for case 'f' using dup() and dup2(). I'm not sure how the dup system calls work and how I could dup the stdout to the file. I know I would have…
HotWheels
  • 444
  • 3
  • 23
1
vote
3 answers

Redirect standard output to file and re-establish the standard output with function

I have to create a function that temporarily redirects the standard output to a file name "file", then executes the function f, then re-establishes the original standard output. I'm having some trouble realizing it since I am clearly not good at…
KonanWS
  • 13
  • 5
1
vote
2 answers

ruby clone an object

I need to clone an existing object and change that cloned object. The problem is that my changes change original object. Here's the code: require "httparty" class Http attr_accessor :options attr_accessor :rescue_response include HTTParty …
kotyara85
  • 305
  • 3
  • 12
1
vote
2 answers

Strange behavior of dup system call

So, I was reading about File I/O in Linux and thought of playing around with it. However, I encountered two strange behaviors in the code and I am struggling to find out a reason for them. /* * This program shows the usage of dup and dup2…
Crazy Psychild
  • 552
  • 5
  • 16
1
vote
1 answer

Ruby on Rails Active associations clone and dup methods

In my ruby on rails application I have a problem while I'm trying to save a model after cloning it. I have the following models. class Company < ApplicationRecord has_many :employees end class Employee < ApplicationRecord belongs_to…
Hilmi Yamcı
  • 463
  • 8
  • 20
1
vote
1 answer

Make multiple pipe with waiting for return code of each process

I want to reproduce the pipe system of UNIX Shell in C using execve, dup2, fork, waitpid & pipe functions. Right, for example this command : /bin/ls -l | /usr/bin/head -2 | /usr/bin/wc is reproduced by : #include #include…
Rakim Faoui
  • 107
  • 2
  • 8
1
vote
1 answer

C++ pipe and fork

I'm working on a sample program to learn how pipes and forking works. In my very basic implementation, in my child process, i closed 0 and duplicated the read end of the pipe so that file descriptor 0 is now the read end of my pipe. From my parent…
Mohit Athwani
  • 883
  • 9
  • 17
1
vote
2 answers

Why doesn't dup2 occur in sequential order?

Here is a code snippet. int saved_stdout = dup(1); int fd = open("file.txt", O_WRONLY | O_CREAT, 0640); close(1); dup(fd); close(fd); printf("This text should go into the file\n"); //restore stdout dup2(saved_stdout, 1); printf("stdout…
posixKing
  • 408
  • 1
  • 8
  • 17
1
vote
1 answer

How to use dup and or dup2 to redirect standard out into a pipe, then out to antoher pipe and then back out to standard out?

Ok guys, there are a billion demos relating to dup, dup2, fcntl, pipe and all kinds of stuff that are wonderful when multiple processes exist. However, I have yet to see one very basic thing that I think will help explain the behavior of pipe and…
cmg
  • 29
  • 3
  • 6
1
vote
2 answers

Duplicating epoll file descriptor

Is there a way to duplicate a file descriptor created using epoll_create, in such a way that the copy can be modified (adding/removing watched file descriptors using epoll_ctl) independently. E.g. I create an epoll file descriptor A which waits for…
tmlen
  • 8,533
  • 5
  • 31
  • 84
1
vote
2 answers

Is closing a pipe necessary when followed by execlp()?

Before stating my question, I have read several related questions on stack overflow, such as pipe & dup functions in UNIX, and several others,but didn't clarify my confusion. First, the code, which is an example code from 'Beginning Linux…
momoxinduo
  • 83
  • 7
1
vote
1 answer

C Redirecting stdout to a file for process called by exec in child process

I am unable to redirect STDOUT to a file. I have two processes. These should run for 5 seconds and then be killed. Process 1: redirects its STDOUT to a pipe and then prints random nubers. Process 2: redirects its STDIN to the pipe and its STDOUT to…
Jura Brazdil
  • 970
  • 7
  • 15
1
vote
1 answer

Xterm pager - two terminal output - using pipe and dup2

I am trying to implement a pager in C, I want the code to open another terminal (xterm) and print some outputs in it. So I first create a pipe and fork the main program, the child will execute xterm with the tail command, the main program will…