Sponsored Content
Full Discussion: Implementation of dup2
Top Forums Programming Implementation of dup2 Post 302490450 by homeboy on Monday 24th of January 2011 09:13:31 PM
Old 01-24-2011
Quote:
Originally Posted by Corona688
Just close it, if close succeeds it was open, if it wasn't, it was already closed...
yes,that's a good idea except i have to drop some status of the fd such as current position.Btw,thanks.
Quote:
Originally Posted by Perderabo
Btw, fstat can tell you by succeeding or failing if fd n is open, but I see no reason for that here.
pls tell me some detail about that.I did't figure out some method to use "st_mode" to tell whether the fd is opened.Thanks a lot.
 

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Shell Implementation

I want to implement my own simple multi tasking shell in Unix which will take care of redirection (<, >, >>) and piping. I am just unable to get a concrete idea of how exactly I have to start. I have several books...some are.. 1. Maurice Bach- Design Of Unix Operating System 2. Richard... (3 Replies)
Discussion started by: clickonline1
3 Replies

2. Programming

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... (1 Reply)
Discussion started by: Yifan_Guo
1 Replies

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

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

5. UNIX for Advanced & Expert Users

Malloc Implementation in C

Hey Guys Some of my friends have got together and we are trying to write a basic kernel similar to Linux. I am trying to implement the malloc function in C and I am using a doubly linked list as the primary data structure. I need to allocate memory for this link list (duh...) and I don't feel... (2 Replies)
Discussion started by: rbansal2
2 Replies

6. Linux

CAPWAP implementation

Hi I'm trying to implement CAPWAP protocol for my application.i'm able to configure my server side but i'm getting error at client(WTP) side as IOCTL error.while running the command #./WTP /mnt/cf/capwap/ : wlan2 Starting WTP... # WTP Loads... (0 Replies)
Discussion started by: ran789
0 Replies

7. UNIX for Dummies Questions & Answers

Lseek implementation

Hi everybody, i've been googling for ages now and gotten kinda desperate... The question, however, might be rather trivial for the experts: What is it exactly, i.e. physically, the POSIX function (for a file) "lseek" does? Does it trigger some kind of synchronization on disk? Is it just for the... (4 Replies)
Discussion started by: Humudituu
4 Replies

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

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

10. 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
DUP(2)							     Linux Programmer's Manual							    DUP(2)

NAME
dup, dup2 - duplicate a file descriptor SYNOPSIS
#include <unistd.h> int dup(int oldfd); int dup2(int oldfd, int newfd); DESCRIPTION
dup and dup2 create a copy of the file descriptor oldfd. After successful return of dup or dup2, the old and new descriptors may be used interchangeably. They share locks, file position pointers and flags; for example, if the file position is modified by using lseek on one of the descriptors, the position is also changed for the other. The two descriptors do not share the close-on-exec flag, however. dup uses the lowest-numbered unused descriptor for the new descriptor. dup2 makes newfd be the copy of oldfd, closing newfd first if necessary. RETURN VALUE
dup and dup2 return the new descriptor, or -1 if an error occurred (in which case, errno is set appropriately). ERRORS
EBADF oldfd isn't an open file descriptor, or newfd is out of the allowed range for file descriptors. EMFILE The process already has the maximum number of file descriptors open and tried to open a new one. WARNING
The error returned by dup2 is different to that returned by fcntl(..., F_DUPFD, ...) when newfd is out of range. On some systems dup2 also sometimes returns EINVAL like F_DUPFD. CONFORMING TO
SVr4, SVID, POSIX, X/OPEN, BSD 4.3. SVr4 documents additional EINTR and ENOLINK error conditions. POSIX.1 adds EINTR. SEE ALSO
fcntl(2), open(2), close(2) Linux 1.1.46 1994-08-21 DUP(2)
All times are GMT -4. The time now is 02:37 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy