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
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

Can't read from a pipe after dup2() and fork. C

I'm writing a code that echo a string and sed it two times. My output is correct, but when I try to place that string on an array it blocks on read and goes on with the other calls. Here's the code: #include #include…
-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

send several strings through a pipe to a child process

I want the child process to listen in a loop the parent process. If it receives "exit" from the parent it terminates and if it receives "cmd" will read again for the actual command to execute using system(). So far I have this code, but the second…
-1
votes
1 answer

Having trouble tracing logic and output of code includes dup() dup2() and fork()

In a question from an Exam in operating system I have been trying to trace through the following code with no success. The question says that the assumptions are: at least the STDOUT is open. foo.txt has the string "abcdef" 6 bytes bar.txt has the…
user5175056
-1
votes
1 answer

dup or clone action in rails

Well im going to clarify im doing it class DuplicatesController < ApplicationController before_action :set_venue, only: [:new] def new end def create if @venue.duplicate(venue_params) flash[:success] =…
-1
votes
1 answer

open() using O_APPEND does not update EOF

I am writing a copy of the dup() function (I am studying a book for the Linux api). I have a file named temp.txt which contains one line with the following string: Hello, World. Here is the code: #include #include…
KostasRim
  • 2,053
  • 1
  • 16
  • 32
-1
votes
2 answers

Not getting stdout back

This code takes 2 files and puts the results into a third file. It does this just fine but what's causing me concern is that the last printf is not printing to the screen. The first one prints just fine. This tells me I didn't restore the stdout…
haiku_vi
  • 439
  • 1
  • 5
  • 6
-2
votes
1 answer

stat failed: -: Bad file descriptor

I am implementing a shell , pipe, and fork. I am basically passing messages between two child processes of the same parent . Individual command's work but pipe commands don't work I get errors like bad file descriptors . Can anyone help me with…
Sumanth
  • 363
  • 3
  • 17
-3
votes
1 answer

C restore stdin to the actual standard input after using dup2

how would I make it so that the fd of stdin returns to its original "file destination"? Could this be accomplished by originally forking, doing what I need to using the child process and then killing that child, leaving me back to the parent with…
Myron
  • 11
-4
votes
2 answers

How to execute the command ls|sort -r in C using pipe and fork()?

I was trying to solve this problem for school project but I couldn't understand how to open and close the read and write within the pipe: the question is: Create a C program that executes the following command ls| sort -r using pipe and creation of…
mary
  • 1
1 2 3
11
12