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.

313 questions
0
votes
1 answer

Emulating pipes

I've just recently learned about pipes and I would like to emulate the "|" gimmick provided by shells. In the code below, the parent process spawns 2 child processes, after which they do their piping and get replaced by ls and grep. While that…
Diana Dcn
  • 3
  • 2
0
votes
2 answers

dup2 blocking printf, but not fprintf?

so, I have an assignment for my Operating Systems class wherein i am to create a ring of processes connected with pipes in order to pass messages between them. i found some example code which i was looking to adapt (or at least understand) for my…
Drake
  • 433
  • 2
  • 7
  • 17
0
votes
1 answer

Why could dup2 work here?

I got this code clip from APUE, if (dup2(clfd, STDOUT_FILENO) != STDOUT_FILENO || dup2(clfd, STDERR_FILENO) != STDERR_FILENO) { syslog(LOG_ERR, "ruptimed: unexpected error"); exit(1); } Which will redirect…
daisy
  • 22,498
  • 29
  • 129
  • 265
-1
votes
1 answer

Problem when redirecting standard output with dup and dup2 in c

I have a problem with dup and dup2. int copy (char * file1, char * file2) { int dr = open(file1, O_RDONLY); if (dr == -1) { return -1; } int dw = open(file2, O_WRONLY|O_TRUNC|O_CREAT, 0644); …
-1
votes
1 answer

How to properly perform multiple redirection in C

I have a question regarding multiple redirections. As what I have for now writes in file1.txt only. I have to implement echo hello > file1.txt > file2.txt > file3.txt on my shell Here is my code: int fd1 = open(file1.txt, O_RDWR); int fd2 =…
Cyborg_Trick
  • 174
  • 3
  • 12
-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
  • 3,444
  • 4
  • 23
  • 42
-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
  • 1
  • 7
-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
  • 159
  • 2
  • 4
  • 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
  • 529
  • 2
  • 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
20
21