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
vote
1 answer

dup return value is always zero

I would to know why does dup always return zeroes in the following code (in which a file is opened, than 10 dup are done successively) : #include #include #include #include #include void…
Moka
  • 988
  • 10
  • 10
1
vote
1 answer

Copy model instances in Rails with single table inheritance

I have BaseProject, ProjectTemplate and Project class ProjectTemplate << BaseProject; end class Project << BaseProject; end I would like to copy project_template attributes to a new project instance as defaults. The problem is if i use dup project…
bonyiii
  • 2,833
  • 29
  • 25
1
vote
2 answers

Confusion regarding usage of dup()

When we use dup to redirect STDOUT to a pipe we do: close(1); dup(fd[1]); close(fd[0]); close(fd[1]); execlp("ls","-al",(char *) NULL); but we are closing both ends end of the pipe. then how the STDOUT can be written to the pipe?
1
vote
2 answers

Implementing a pipe in C?

I am trying to implement a simple shell. I fork processes this way: void forkProcess(char* cmd[]) { pid_t pid; char programPath[BUFFERLENGTH] = "/bin/"; strcat(programPath, cmd[0]); int exitStatus; pid = fork(); switch (pid)…
oopbase
  • 11,157
  • 12
  • 40
  • 59
1
vote
3 answers

dup gives different results when hash is one vs. two dimensions

dup is shallow copy, so when doing this: h = {one: {a:'a', b: 'b'}} h_copy = h.dup h_copy[:one][:b] = 'new b' now h and h_copy is same: {:one=>{:a=>"a", :b=>"new b"}} yes, that right. But when h is a one dimension hash: h = {a:'a', b: 'b'} h_copy =…
1
vote
3 answers

Duplicating a record with associated images using Carrierwave

I have an app which you can store order/invoices in. I'm building a simple feature where you can duplicate invoices for my customers. I wrote this method in my Order.rb model which: Takes the invoice, duplicates the associated lineitems, adds the…
bcackerman
  • 1,486
  • 2
  • 22
  • 36
1
vote
1 answer

dup and dup2 commands

What I'm trying to do is that put the output of the ls command in a file, and then use grep command to read from that file and store it in a new file and based on the contents on that file, print something on the terminal. So the following output…
Ole Gooner
  • 567
  • 2
  • 10
  • 25
0
votes
1 answer

Stream descriptor loss in fopen + stream _dup

I have the following code example (in windows): int fd = _dup(fileno(stdout)); freopen("tmp","w",stdout); printf("1111"); close(stdout); char buf[100]; FILE *fp; fp = fopen("tmp","r");//in this line fd turns to be 0 if(NULL == fp) return…
YAKOVM
  • 9,805
  • 31
  • 116
  • 217
0
votes
1 answer

How to supply input to a thread which is polling for stdin, form another thread in the same process?

Referring to following code example, I want the main thread to supply the number num that the child thread is expecting using scanf. I tried this way to write the wordcount (9) to stdin which is to be read by child thread, but it is not…
Harish Reddy
  • 65
  • 1
  • 6
0
votes
0 answers

STM32 .hex file convert to .dup file

I used STM32 IDE to create a .hex extension. But need convert this .hex file to .dup file(for FOTA). Previous convert tool was lost. I just have the flow like this. stm32.py full fileName.hex > fileName.hupg upgrade-gen.py hupg fileName.hupg -o…
Leon
  • 11
  • 2
0
votes
1 answer

C program prints to terminal instead of file even after using dup2/dup

I'm in an operating systems course and doing assignments with C on Linux. In one of the assignments I was supposed to redirect and output to a file, but for some reason, I keep getting the output in the terminal instead. I tried writing a simple…
0
votes
1 answer

How do I use 2 child processes one for executing command and the other one for reading output and passing it to the next?

So my program needs to pipe multiple processes and read the number of bytes each process output has. The way I implemented it, in a for loop, we have two children: Child 1: dups output and executes the process Child 2: reads the output and writes it…
0
votes
1 answer

In C how to read the output of a process and write it in the input of another?

Hi I have a C programme that is basacally suppose to simulate the pipe function in linux and write the amount of bytes that are read in a .txt file so ./a.out cat test : grep -v le : wc -l The problem that I'm trying to figure out is Why is the same…
0
votes
1 answer

piping and redirection for execv command

Why piping doesn't work for the following code? int p_fds[2]; pipe(p_fds); int pid_left = fork(); if (pid_left == 0){ dup2(p_fds[1],STDOUT_FILENO); close(p_fds[0]); close(p_fds[1]); execv("cat", (char*[]){ "cat", "afile"…
0
votes
1 answer

Rails 6 Dup in nested controller

I have a System Controller that is nested under my Site Controller. I want to .dup a system, but I am getting "Couldn't find System without an ID" error. My Button Looks like this <%= link_to 'Duplicate System', site_system_path(@site, item.system),…
Jeremy Bray
  • 444
  • 5
  • 18