Implementation of dup2


 
Thread Tools Search this Thread
Top Forums Programming Implementation of dup2
# 1  
Old 01-22-2011
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.Smilie
# 2  
Old 01-23-2011
I don't think there's a straightfoward, platform-independent way to do that, which is probably why it exists... I can think of lots of half-hacks that'd work in very specific circumstances but wouldn't be able to copy other things -- i.e. no sockets, no pipes, etc.

Another hackish solution would be to close the original and use dup() until you get the FD you want. For really low FD's you might be able to depend on getting the one you want, if there aren't any closed FD's lower than it.

On linux I'd cheat and use dup3.
# 3  
Old 01-23-2011
Remember that the reader is expected to use only those concepts introduced so far in the book. So there is no reason to think about exotic solutions. This is why I think that looping on dup() is the answer Rich was looking for. Remember to clean up after the dup loop. I don't see the dup loop as a real terrible problem... it just seems that way because we are mentally comparing it to the real dup2.

It's off the subject, but here is a worse example of looping... around 1990 I was sysadmin of a Vax running Ultrix. We had some game (rogue maybe?) where you could save your status and then do a restore if you "died", but only during the current run. The game stored the pid in the save file and the restore would fail if the current pid was different. So one of our programmers wrote an "allocate_pid" function. Smilie Guess how it worked. And this was before today's fast fork. Smilie
# 4  
Old 01-23-2011
Quote:
Originally Posted by Perderabo
This is why I think that looping on dup() is the answer Rich was looking for.
Thanks for your reply,the only question is how could I know if the file is opened? i.e. I have 0,1,2 already opened,and i wanna dup 1 on 7,i must first open 0~6 and close 7 then call dup,is there a easy way to know the stat of the file such as opened or closed?I see the manpage of fcntl and seems there's no information about that.
# 5  
Old 01-24-2011
Quote:
Originally Posted by homeboy
Thanks for your reply,the only question is how could I know if the file is opened? i.e. I have 0,1,2 already opened,and i wanna dup 1 on 7,i must first open 0~6 and close 7 then call dup,is there a easy way to know the stat of the file such as opened or closed?I see the manpage of fcntl and seems there's no information about that.
The same way you find out if a window is open using your fist. Smilie

Just close it, if close succeeds it was open, if it wasn't, it was already closed...
This User Gave Thanks to Corona688 For This Post:
# 6  
Old 01-24-2011
I would just close 7 and then dup until I finally get 7. Then close 4 through 6 or whatever. But if 5 was already open, the dup loop will go from 4 straight to 6 and in this case you should not close 5. You need to store in an array that fd's returned by dup.

Btw, fstat can tell you by succeeding or failing if fd n is open, but I see no reason for that here.

Last edited by Perderabo; 01-24-2011 at 12:50 PM.. Reason: add fstat comment
This User Gave Thanks to Perderabo For This Post:
# 7  
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.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

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

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

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

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

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

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

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

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

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

10. 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
Login or Register to Ask a Question