11-13-2002
fork() and dup()
I have met this code:
switch(fork()) {
case 0:
close(1);
dup(p[1]);
close(p[0]);
close(p[1]);
execvp(<whatever>);
perror("Exec failed");
}
Can anyone tell me what this piece of code does?
Thx alot..
9 More Discussions You Might Find Interesting
1. Programming
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
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 everybody,
I wanna write a code to understand how fork works.
my target
--------------
-Parent creates a file(called temp) and writes into this file "1".Then it closes the file.
-Then parent creates a child and wait until execution of this child ends.
-Then child opens the same... (3 Replies)
Discussion started by: alexicopax
3 Replies
4. UNIX for Advanced & Expert Users
Hi,
I wrote a simple program for understanding the fork command. The code is as below
int main(void)
{
fork(); printf("hi 1 \n");
fork(); printf("hi 2 \n");
fork(); printf("hi 3 \n");
}
I am getting a variation in the number of times the printf is called if i remove the \n from each of... (1 Reply)
Discussion started by: xyz123456
1 Replies
5. Programming
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
6. Red Hat
Ntop is running on redhat. But It gives DUP! error while pinging to any places
I dont know why DUP! error is occured.
# ping google.com
PING google.com (74.125.39.147) 56(84) bytes of data.
64 bytes from fx-in-f147.1e100.net (74.125.39.147): icmp_seq=1 ttl=44 time=54.1 ms
64 bytes from... (6 Replies)
Discussion started by: getrue
6 Replies
7. Shell Programming and Scripting
Hi,
I have a requirement where I have to identify duplicates from a file based on the first 6 chars (It is fixed width file of 12 chars length) and whenever a duplicate row is found, its original and duplicate row's last 2 chars should be updated to all 0's if they are not same. (I mean last 2... (3 Replies)
Discussion started by: farawaydsky
3 Replies
8. Shell Programming and Scripting
Hi Gurus,
I need to count the duplicate records in file
file
abc
abc
def
ghi
ghi
jkl
I want to get below result:
abc ,2
abc, 2
def ,1
ghi ,2
ghi, 2
jkl ,1
or
abc ,2
def ,1 (3 Replies)
Discussion started by: ken6503
3 Replies
9. UNIX and Linux Applications
The problematic directory is the following:
/root/.cache/deja-dup
This directory grows until my "/" is full and then the restoring activity fails.
I already tried to create a symbolic link with origin another partition where I have more space. However during the restoring activity ... (4 Replies)
Discussion started by: puertas12
4 Replies
CLOSE(2) BSD System Calls Manual CLOSE(2)
NAME
close -- delete a descriptor
LIBRARY
Standard C Library (libc, -lc)
SYNOPSIS
#include <unistd.h>
int
close(int d);
DESCRIPTION
The close() system call deletes a descriptor from the per-process object reference table. If this is the last reference to the underlying
object, the object will be deactivated. For example, on the last close of a file the current seek pointer associated with the file is lost;
on the last close of a socket(2) associated naming information and queued data are discarded; on the last close of a file holding an advisory
lock the lock is released (see flock(2)).
When a process exits, all associated descriptors are freed, but since there is a limit on active descriptors per processes, the close() sys-
tem call is useful when a large quantity of file descriptors are being handled.
When a process calls fork(2), all descriptors for the new child process reference the same objects as they did in the parent before the
fork(). If a new process is then to be run using execve(2), the process would normally inherit these descriptors. Most of the descriptors
can be rearranged with dup2(2) or deleted with close() before the execve() is attempted, but if some of these descriptors will still be
needed if the execve() fails, it is necessary to arrange for them to be closed only if the execve() succeeds. For this reason, the system
call
fcntl(d, F_SETFD, 1);
is provided, which arranges that a descriptor ``d'' will be closed after a successful execve(); the system call
fcntl(d, F_SETFD, 0);
restores the default, which is to not close descriptor ``d''.
RETURN VALUES
Upon successful completion, a value of 0 is returned. Otherwise, a value of -1 is returned and errno is set to indicate the error.
ERRORS
close() will fail if:
[EBADF] d is not an active descriptor.
[EINTR] An interrupt was received.
SEE ALSO
accept(2), execve(2), fcntl(2), flock(2), open(2), pipe(2), socket(2), socketpair(2)
STANDARDS
The close() function conforms to ISO/IEC 9945-1:1990 (``POSIX.1'').
BSD
April 19, 1994 BSD