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

execvp failed: no such file or directory

I'm writing a program in C that reads from a file (e.g a file called ffff in the same folder of my .c source file): this files contains two command: ls -l tac My program open this file, reads the commands and the relative arguments and by using a…
2
votes
1 answer

Streaming execvp output via socket

I know this question has been asked a billion times, but all the solutions are not working for me. I need to stream the stdout of an execvp via a socket to a client. The relevant client code is this static void execute_cmd(int fd, char *cmd, char…
Lanbo
  • 15,118
  • 16
  • 70
  • 147
2
votes
2 answers

cat with fork execvp pipe and dup2

This is one step of a set of exercises I'm doing. The program I write should take more than two arguments. The use of the first argument is not implemented yet. The rest of the arguments are a list of directories. In this step what I have to do is…
HZero
  • 69
  • 1
  • 1
  • 7
2
votes
2 answers

Ruby hash.delete(:key) deleting copies and clones as well

From what I understand, when you set an object = to another, it will always be a reference, so we have methods like .dup and .clone to actually create a copy of an object and not a reference. However, I am duplicating or cloning an array of hashes…
kakubei
  • 5,321
  • 4
  • 44
  • 66
2
votes
3 answers

Difference between creating a duplicate file descriptor using dup() and creating a hard link?

I just tried out this program where I use dup to duplicate the file desciptor of an opened file. I had made a hard link to this same file and I opened the same file to read the contents of the file in the program. My question is what is the…
Arpith
  • 570
  • 2
  • 10
  • 26
2
votes
2 answers

Duplicate a rails object with associations and paperclip attachments

I have an object with several associations. Some of these associated objects have paperclip-attachments stored at S3. If I duplicate the object and the associations it works fine but the attachments are not duplicated. This here works without…
Marcus Franzen
  • 868
  • 10
  • 25
1
vote
2 answers

How to replace STDIN, STDOUT, STDERR in ruby19

In ruby18 I sometimes did the following to get a subprocess with full control: stdin, @stdin= IO.pipe @stdout, stdout= IO.pipe @stderr, stderr= IO.pipe @pid= fork do @stdin.close STDIN.close stdin.dup @stdout.close STDOUT.close …
johannes
  • 7,262
  • 5
  • 38
  • 57
1
vote
1 answer

Issues with file redirection when building a simple shell

I'm trying to implement file redirection in my simple shell program. The issue is that when I enter a prompt when I run my shell program to test it (e.g ./test1 > test2 ) instead of executing the command and going onto the next prompt, it sort of…
iknn
  • 19
  • 2
1
vote
0 answers

In C how can i get the size of a pipe file descriptor without using READ or Write?

Hi I would like to know if its possible to count the size of bytes of a pipe's content like the read function but without using read (I don't want to lose it's content just know the size). I tried using struct stat sb; fstat(fd,&sb); and then…
1
vote
0 answers

Restore contents of a file descriptor after reading it?

To give you a bit of context my program basically takes commands and pipes them together and records the amount of bytes of each output in a file. My problem is that while I managed to pipe the processes together the moment I read the output of the…
1
vote
1 answer

Redirecting stdout to pipe and reading from it from a single process

I'm porting a Linux program to a system which has no fork(), so everything runs in a single process. This program creates pipe, redirects stdout to pipe's input, forks, children calls printf and parent reads data from pipe. I removed fork, and tried…
trexxet
  • 176
  • 3
  • 14
1
vote
0 answers

Error while testing assets : can't dup NilClass

I'm using rails 3.0.9 with ruby 1.9.2. I am doing a system that allow users to put items into different closets. One of the user's possibility is to copy an item of an other user into his own closet (please tell me if you don't understand, I'm not…
JennyR
  • 11
  • 2
1
vote
1 answer

How to implement pipes and redirections in my shell?

From what I can see there is a lot of info on piping and file redirection. But I can't find anything specific about doing both piping and file redirection. If there is something like that that I maybe missed please link it for me if you don't…
1
vote
0 answers

Why is the getline in my child process blocking?

I'm spawning a child process with stdin and stdout set up as pipes between parent and child. I want to pipe lines into the child and - for now - echo them. With standard cat it's working, but with my own cat-clone based on getline() it's not. I…
Jann Poppinga
  • 444
  • 4
  • 18
1
vote
1 answer

dup2() is blocking with child processes? C

I'm writing a function that echo an input to a sed and then another sed. I thinck i used all my wait signal in the right way but the last print i can get is before the call to dup2() in my first child process in the echo. void sendbc (char * str_ )…