Sponsored Content
Top Forums Programming Understanding the purpose of dup/dup2 Post 66230 by Yifan_Guo on Friday 11th of March 2005 05:40:50 PM
Old 03-11-2005
Understanding the purpose of dup/dup2

I'm having difficulty understanding the purposes of using dup/dup2 when involving forks.

for example, if we call fork() once, that is, we are creating a child process. In what cases would we need to use dup or dup2 to duplicate the file descriptors for standard output and standard error? What happens if we don't do this?

also, what is meant by the following:

"This function starts the child process, redirects the standard output and standard error to the log file (logfile)"

What is this redirection?

Please be as specific as you can please. Thank you very much.

Last edited by Yifan_Guo; 03-11-2005 at 06:49 PM..
 

10 More Discussions You Might Find Interesting

1. Programming

dup()

Would anyone be so kind to explain to me the function of dup() in UNIX? As far as I am concerned, it duplicates a file descriptor. Under what circumstances would we need to duplicate a file descriptor in a UNIX environment? Thank you. vinchen (3 Replies)
Discussion started by: vinchen
3 Replies

2. Programming

fork() and dup()

I have met this code: switch(fork()) { case 0: close(1); dup(p); close(p); close(p); execvp(<whatever>); perror("Exec failed"); } Can anyone tell me what this piece of code does? Thx alot.. (1 Reply)
Discussion started by: AkumaTay
1 Replies

3. Programming

dup()

when i want to replace standard output with output file int out; out = open("out", O_WRONLY)p; dup2(out,1); What Shall I do in case of appending??? I am using here O_WRONLY TO WRITE.BUT IF i wanna append, whats the word? (5 Replies)
Discussion started by: joey
5 Replies

4. Programming

a issue with dup2

Hi, i have in one program such a pice of code ................ static int old_stderr_handle = -1; static int old_stdout_handle = -1; log_handle = open(log_file_name,O_CREAT|O_RDWR,932); old_stderr_handle = dup(STDERR_FILENO); if (dup2(log_handle,STDERR_FILENO) < 0) { //... (1 Reply)
Discussion started by: vovan
1 Replies

5. UNIX for Advanced & Expert Users

dup2 filedescriptor redirecting output

int redirect() { int fd,rc; fd = open("sample.DAT",O_CREAT | O_RDWR , 00777 ); rc = dup2(fd , 1 ) ; close (fd ); return 0; } I used the above to redirect all the cout statements to sample.DAT. process is redirecting the output and I had two questions 1. All stdout/cout statements... (2 Replies)
Discussion started by: satish@123
2 Replies

6. Programming

Implementation of dup2

Hi all,I'm reading <Advanced programming in the UNIX environment>,that book asked the reader to implement a function which has same functions with dup2 without calling fcntl.Could anyone give me a tip?Any help will be appreciated.:) (8 Replies)
Discussion started by: homeboy
8 Replies

7. UNIX for Advanced & Expert Users

Dup2 - for file descriptor opened by a different process

is it possible to duplicate file descriptors(opened by a different process) with the help of dup or dup2. the two process do not share parent child relationship as well. (2 Replies)
Discussion started by: replytoshishir
2 Replies

8. Shell Programming and Scripting

Troubles with pipes, fork, and dup2

I want to execute metasploit by two pipes to communicate with it, but I have troubles with that communication. When I run my program, I get this error: "stty: standard input: Inappropriate ioctl for device" and I don't receive the metasploit promt. just select an exploit. This is my code:... (2 Replies)
Discussion started by: dano88
2 Replies

9. Programming

C++ stuck with dup2 and pipe

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 parent and what else I should include into the child.... (1 Reply)
Discussion started by: ramono
1 Replies

10. Shell Programming and Scripting

understanding the purpose of <If env | grep -q $EXAMPLE>

I have seen this code in a few places and my understanding is they are using it to determine what app called the script. I have a script that is called by two different applications and what it to do one thing when called by one and something else when called by the other. How do I determine... (1 Reply)
Discussion started by: croyleje
1 Replies
DUP(2)							      BSD System Calls Manual							    DUP(2)

NAME
dup, dup2 -- duplicate an existing file descriptor SYNOPSIS
#include <unistd.h> int dup(int fildes); int dup2(int fildes, int fildes2); DESCRIPTION
dup() duplicates an existing object descriptor and returns its value to the calling process (fildes2 = dup(fildes)). The argument fildes is a small non-negative integer index in the per-process descriptor table. The value must be less than the size of the table, which is returned by getdtablesize(2). The new descriptor returned by the call is the lowest numbered descriptor currently not in use by the process. The object referenced by the descriptor does not distinguish between fildes and fildes2 in any way. Thus if fildes2 and fildes are duplicate references to an open file, read(2), write(2) and lseek(2) calls all move a single pointer into the file, and append mode, non-blocking I/O and asynchronous I/O options are shared between the references. If a separate pointer into the file is desired, a different object reference to the file must be obtained by issuing an additional open(2) call. The close-on-exec flag on the new file descriptor is unset. In dup2(), the value of the new descriptor fildes2 is specified. If fildes and fildes2 are equal, then dup2() just returns fildes2; no other changes are made to the existing descriptor. Otherwise, if descriptor fildes2 is already in use, it is first deallocated as if a close(2) call had been done first. RETURN VALUES
Upon successful completion, the new file descriptor is returned. Otherwise, a value of -1 is returned and the global integer variable errno is set to indicate the error. ERRORS
The dup() and dup2() system calls will fail if: [EBADF] fildes is not an active, valid file descriptor. [EINTR] Execution is interrupted by a signal. [EMFILE] Too many file descriptors are active. The dup2() system call will fail if: [EBADF] fildes2 is negative or greater than the maximum allowable number (see getdtablesize(2)). SEE ALSO
accept(2), close(2), fcntl(2), getdtablesize(2), open(2), pipe(2), socket(2), socketpair(2) STANDARDS
dup() and dup2() are expected to conform to IEEE Std 1003.1-1988 (``POSIX.1''). 4th Berkeley Distribution December 1, 2010 4th Berkeley Distribution
All times are GMT -4. The time now is 08:33 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy