Dup and dup2 system call

Source: Internet
Author: User

The following code is taken from Linux 0.11. Although the code is outdated, its implementation is very intuitive and simple. It is of great value and help for understanding dup implementation. Through this code, you can easily understand the thoughts behind dup.

//// Copy the file handle (descriptor ). -- Fs/fcntl. c
// The fd parameter is the file handle to be copied. arg specifies the minimum value of the new file handle.
// Return the new file handle or error code.
Static int
Dupfd (unsigned int fd, unsigned int arg)
{
// If the file handle value is greater than the maximum number of files opened by a program, or the file structure of the handle does not exist, an error occurs,
// Return the error code and exit.
If (fd> = NR_OPEN |! Current-> filp [fd])
Return-EBADF;
// If the specified new handle value arg is greater than the maximum number of files opened, an error is returned and an error code is exited.
If (arg> = NR_OPEN)
Return-EINVAL;
// Search for the items whose index number is greater than or equal to arg but not used in the file structure pointer array of the current process.
While (arg <NR_OPEN)
If (current-> filp [arg])
Arg ++;
Else
Break;
// If the new handle value arg is greater than the maximum number of files opened, an error occurs. The system returns an error code and exits.
If (arg> = NR_OPEN)
Return-EMFILE;
// Close the flag bitmap to reset the handle during execution. That is, the handle is not closed when running the exec () class function.
Current-> close_on_exec & = ~ (1 <arg );
// Make the file structure pointer equal to the pointer of the original handle fd, and increase the file reference count by 1.
(Current-> filp [arg] = current-> filp [fd])-> f_count ++;
Return arg; // return the new file handle.
}

//// Copy the file handle. The system calls the function.
// Copy the specified file handle oldfd. The new handle value is newfd. If newfd is enabled, disable it first.
Int
Sys_dup2 (unsigned int oldfd, unsigned int newfd)
{
Sys_close (newfd); // If the handle newfd has been opened, close it first.
Return dupfd (oldfd, newfd); // copy and return the new handle.
}

//// Copy the file handle. The system calls the function.
// Copy the specified file handle oldfd. The value of the new handle is the minimum unused handle currently.
Int
Sys_dup (unsigned int fildes)
{
Return dupfd (fildes, 0 );
}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.