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.

278 questions
-1
votes
1 answer

Segmentation fault (core dumped) Redirecting input

For my program, I have done funcs void clear_forward() { if (saved_stdout) { dup2(saved_stdout, 1); if (close(saved_stdout)) saved_stdout = 0; } if (saved_stdin) { dup2(saved_stdin, 0); if…
Anjei
  • 1
  • 2
-1
votes
2 answers

TCP client / server : when stop read socket

I am facing multiple problem creating a small ftp like client / server (tcp) the client have a prompt. the How to stop receiving issue. Sending data throught my socket from my client to server or vise-versa. For example, from the server sending some…
albttx
  • 2,404
  • 3
  • 18
  • 39
-1
votes
1 answer

C - dup2() not executing

This is my first question so I apologize if I'm omitting anything important. So I've been working on an assignment that handles piping via forking. My code is pretty messy, littered with printf statements so I see what's going on. I've looked…
Ken
  • 11
  • 3
-1
votes
1 answer

Unable to use pipe as input for grep in C

On Ubuntu 16 I am trying to write a program exercising pipes, forking, and execing: the program will accept a file name via a command-line argument; a child process will open the named file and exec cat to transfer the content to a second child…
R. Barbus
  • 73
  • 6
-1
votes
1 answer

C How to get the result of other program with exec functions and pipes

I'm writting in c a program that the father ask the user to insert the number to calculate the factorial of a number. That number is calculated in factorial.c So i guess what i need to do is: -Father ask a number to the user: -Factorial.c read the…
Joseph
  • 129
  • 2
  • 3
  • 12
-1
votes
1 answer

Can someone help me explain why the following C program(system calls) has this kind of output?

Here is my program: I am confused about it. I don't understand why c1 and c2 share the same value, but c3's value is different from c1 and c2's? Can someone help me explain it? Thank you. Here is the program: #include #include…
user144600
  • 451
  • 1
  • 7
  • 18
-1
votes
1 answer

what is wrong with this dup2 and pipes?

What this code should do is: there are parent.cpp and child.cpp. Parent will send whatever is in the buffer to child and child will send back whatever received to parent. I do not know what I am doing wrong. I am confused what is missing in the…
-3
votes
1 answer

pipe - fork - c - why the program is unresponsive without any error?

Hi i was writing a program to do cat inputs.txt | sort | uniq in c programming using pipe, fork and dup2 but the program seems to not run any thing at all. I placed a printf at the beginning, right after main() and i do not see any output still.I…
1 2 3
18
19