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.
10 More Discussions You Might Find Interesting
1. UNIX for Dummies Questions & Answers
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
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
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
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
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
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
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
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
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
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
dup2(2) System Calls Manual dup2(2)
NAME
dup2 - duplicate an open file descriptor to a specific slot
SYNOPSIS
DESCRIPTION
fildes is a file descriptor obtained from a or system call.
fildes2 is a non-negative integer less than the maximum value allowed for file descriptors.
causes fildes2 to refer to the same file as fildes. If fildes2 refers to an already open file, the open file is closed first.
The file descriptor returned by has the following in common with fildes:
o Same open file (or pipe).
o Same file pointer (that is, both file descriptors share one file pointer.)
o Same access mode (read, write or read/write).
o Same file status flags (see fcntl(2),
The new file descriptor is set to remain open across system calls. See fcntl(2).
This routine is found in the C library. Programs using but not using other routines from the Berkeley importability library (such as the
routines described in bsdproc(3C)) should not give the option to ld(1).
RETURN VALUE
Upon successful completion, returns the new file descriptor as a non-negative integer, fildes2. Otherwise, it returns -1 and sets to indi-
cate the error.
ERRORS
fails if the following is true:
fildes is not a valid open file descriptor or fildes2 is not in the range of legal file descriptors.
An attempt to close
fildes2 was interrupted by a signal. The file is still open.
WARNINGS
A multithreaded application or an application with a signal handler can exhibit a race between on one thread and another kernel call that
assigns a new file descriptor while running on a second thread or signal handler. If fildes2 is free before the call was made, the other
thread or signal handler may win the race and acquire that descriptor (e.g., in an call). The thread calling can then close this file and
reuse the descriptor. This results in multiple functions improperly referring to the same file.
This race can be avoided either by ensuring that fildes2 references an open file before calling or by providing user-level synchronization
(or signal disabling) which makes sure that the thread and another thread or signal handler don't make competing calls into the kernel at
the same time.
SEE ALSO
close(2), creat(2), dup(2), exec(2), fcntl(2), open(2), pipe(2), privileges(5).
STANDARDS CONFORMANCE
dup2(2)