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

Why does reading from pipe that has STDOUT_FILENO redirected to it hang without explicit endl or closing the pipe's write-end?

The following code redirects stdout to the write-end of a pipe, whose read-end I then read from. // main.cpp #include #include #include int main( int argc, char* argv[] ) { int my_pipe[2] = { -1, -1 }; char…
StoneThrow
  • 5,314
  • 4
  • 44
  • 86
0
votes
1 answer

Difficulty with switching between stdout and file using Dup2

I have a problem with dup2 syscall. I added a while loop(a part of my program) that running over a students directory, each student has a "c" file that I compile to an "exe" file. the students' programs scan from testInput file (using dup2 to take…
0
votes
0 answers

Capture sdtout and stderr with pipe and dupe2 using C langage

I am writing a program in C language that acts like a shell terminal, the user enters a command and I parse the line then use "execv()" to execute the command. Now I'm trying to print the standard output and the error output with different colors…
0
votes
1 answer

Output written twice and overlapping after pipe and fork

When I compile and run the following code: #include #include #define N 20 int main() { int fd[2], p, n; char c[N]; pipe(fd); p = fork(); if (p == 0) { dup2(fd[1], 1); execlp("date" , "date" , NULL); …
lsole
  • 75
  • 5
0
votes
1 answer

why does printf in c seems to gives its result after other action occured?

i'm sure of my difficulty but not of the reason for it, so my title might not be a good question for this problem :/ i'm creating a function that works as printf, let's call it print2, and for the sake of testing it i've written this little code…
hugogogo
  • 501
  • 4
  • 24
0
votes
2 answers

Problem restoring stdout after using pipe

Using the advise found here: Restoring stdout after using dup I tried to restore the stdin and stdout. However, when using printf to check if stdout was restored I could not read an output. The code is as follows. After restoring stdout as 1, I…
0
votes
1 answer

Why is it that my pipe does not read in the printf despite replacing stdin?

The objective is to use dup, dup2 and pipe to communicate between parent and child processes. Just to get a feel of how to use dup and pipes. The function multby, called in by the child process, takes a number as an argument (3) and multiply it with…
0
votes
0 answers

Why does changing a duped object alter the original?

I came across this problem and I need some help understanding why the original object was also changed; I was under the impression that ".dup" will create a new object for the pointer to reference, is this wrong? class Pawn attr_accessor…
user2387766
0
votes
2 answers

Recursive function in ruby is overwriting nested attributes of cloned(object.dup) variable

I have a hash like this: entity = {1=> nil, 2 => {3 => nil, 4 => 1}} I wrote a function which can remove the null values of the given entity using recursion. def clear_null_values(entity) entity.each do |key, value| if value == nil ||…
Sk. Irfan
  • 299
  • 3
  • 15
0
votes
1 answer

C Shell hanging when dealing with piping

I'm working on a C shell and am having trouble with getting an arbitrary amount of pipes to work. When I run the shell, it hangs on any piping. For some reason, when I do ls -la | sort, it hangs on the sort until I enter stuff and hit Ctrl+D. I…
MacStation
  • 411
  • 4
  • 20
0
votes
0 answers

Issue with piping data between processes in C

I need to pipe from my read function to my write function. I tested using the command line ./mapstore retrieve ABCD 2>/dev/null | ./mapstore -p ./new_dir/ stream ABCD and this successfully wrote the data into a pipe and stream successfully read the…
0
votes
1 answer

Deep Copying of Objects Ruby

I finished the pecs of tic tac toe, and was working on improving my computer player. This necessitates creating copies of new board objects from old board objects. I am having trouble creating deep copies of a board. Here is the code in question: …
0
votes
1 answer

What's wrong with this C code? The child is not returning?

Here's my attempt to try to understand how to do correct piping between two child processes. I'm simply trying to pass the output of one Linux command to another (ls to cat) and have the program return successfully. However, I'm guessing that the…
user3499524
  • 173
  • 9
0
votes
2 answers

C Print what file is in the default stdin after using dup2

I am writing a small shell in C that runs on Linux. Because there are a lot of dups involved, I would like to ask if there is a way to print what is inside position 0. For example if I call dup2(file_name,0) is there a way to print the file_name so…
user7385384
0
votes
1 answer

Read stdin (and store value) pipe to child, do a cut, and return value

I'm doing a program that will receive 3 arguments, ./a.out a b c, where a and c are column numbers and b and operand from lines separated by :. When its true reproduces the stdin else no results. examples: $ ./a.out 1 > 2 $ 5:2:1:6 5:2:1:6 $…