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
0
votes
1 answer

Making simple shell in c while implementing pipe between children

I want to make a simple shell with pipe. I think I almost did it but it does not return to main. I think exec is not finishing. Below is part of my code. I parsed the command to blocks of struct defined as below. symbol type 5 means that it has |…
0
votes
0 answers

C - using dup to redirect stdout to execl(binary file)'s input

I need help with the following program, It's supposed to fork() two childs, child1 should send two random numbers, in a string separated by space, to child2 trough pipe, wait 1 sec and do it again until it receives SIGUSR1 from the parent(parent…
Petr Kroupa
  • 53
  • 3
  • 8
0
votes
2 answers

How can I make 'fork()'ed children share stdin?

I have this small program: #include #include #include #include int main() { int orig = 1; for (int i = 0; (i != 3) && orig; ++i) { orig = orig && fork(); } if (orig) { for…
Akiiino
  • 1,050
  • 1
  • 10
  • 28
0
votes
2 answers

redirecting stdout to pipe write end

I'm writing a little program, and here is what it should do. In the main process I have to create a new one and that one should execute another program which only does a printf("text"). I want to redirect the pipe write end on stdout and the main…
Robin
  • 71
  • 3
  • 7
0
votes
0 answers

Does anyone know how to I should close the file descriptors right for my pipes to work right?

int main() { int x=10,y=20,z=0, p1[2], p2[2]; pipe(p1); pipe(p2); if(!fork()) { close(p1[1]); close(p2[0]); while(read(p1[0],&z,sizeof(int))) { z=z*3;write(p2[1],&z,sizeof(int)); } close(p1[0]); close(p2[1]); …
LarmadVara
  • 47
  • 8
0
votes
1 answer

dup( fileno( stdin ) ) and then spawn of 32 threads -> I/O errors

I've written Zsh module. There I have a builtin – function mapped to Zsh command. This function duplicates its stdin file descriptor: /* Duplicate standard input */ oconf->stream = fdopen( dup( fileno( stdin ) ), "r" ); Then a thread is spawned…
Digger
  • 103
  • 1
  • 1
  • 4
0
votes
2 answers

Selection output file using dup()

So I'm trying to redirect standard output to a file using dup(). int save_fd; save_fd=dup(1); //saves the current stdout close(1); //closes stdout dup2(file.txt, 1);//redirect output to file.txt //output goes to file.txt dup2(save_fd, 1); restore…
0
votes
0 answers

C - Using two pipes into two different child processes one for each

Iv'e been trying to figure out why the code is not working - My assignment is to use pipe in order to send the child's data (instead of the standard output - so I've used dup2) to the father process and then print it. I would really appreciate…
0
votes
0 answers

No output when using a pipe command execution with execvp

I am running commands in my microshell. The single commands seem to be working fine , but when giving a pipe commands like ls || sort, it doesn't give output immediately. When I exit the program the output of all the pipe commands is printed. …
Sumanth
  • 363
  • 3
  • 17
0
votes
1 answer

How does this example use of dup work?

I've been wanting to create a child process that forks off twice to create two child processes. With the output of one, sent to the other. I found this example code. But I'm confused as to how it works. I found an example here. But I'm confused…
hookenz
  • 36,432
  • 45
  • 177
  • 286
0
votes
1 answer

Requirements in forked child programs to have a pipe communication with the launcher program

Let's say that I have a program called "parent" who uses fork() and execl() to launch another program called "child" and I want to keep communications between this two programs. It seems that the best way to keep this communications would bee using…
kroketor
  • 106
  • 9
0
votes
1 answer

dup2 vs close+open for stdout redirection

Redirecting stdout with close and open: close(STDOUT_FILENO); int fd = open("log", O_RDWR); printf("My output\n"); differs from redirection through dup2: int fd = open("log", O_RDWR); dup2(fd, STDOUT_FILENO); printf("My output\n"); with strace i…
MirkoBanchi
  • 2,173
  • 5
  • 35
  • 52
0
votes
0 answers

Why dup2 has to be atomic? What can happen in between close(oldFd) dup(newFd)?

There is a question in my Operating Systems test about which I'm not sure: given the following code: int fd = open("File", O_RDWR); dup2(fd, 1); write(1, "Hi", 2); exit(0); (1)How can you replace the dup2() call using others system calls? My…
0
votes
1 answer

print to screen from fifo stdout failed

I have a program which has 2 children (running 2 processes by execl), and one fifo. I can't use printf, and I want both children to write and read from fifo. problem is, I want only first child to make sure that everything he writes to my FIFO will…
Zohar Argov
  • 143
  • 1
  • 1
  • 11
0
votes
1 answer

Duplicate File Pointer at the same position

Hy everyone, I've this code: int lenInput; char input[64], buffer[512], temp[512], *ret, tagName[] = "", tagItem[] = "
Federico Cuozzo
  • 363
  • 1
  • 7
  • 20