Apue Exercise 8.7

Source: Internet
Author: User

Reading the time found that the problem is not the answer, so I want to put their own results posted, and share with you!

First put the title on it:

/***********
The posix.1 mentioned in section 8.10 requires that open directory streams be closed when exec is called. Verify this by calling Opendir on the root directory, viewing the DIR structure implemented on your system, and then printing the Execute-time close flag. Then open the same directory read and print the close flag when executed
***********/

First of all, about the role of the closure mark on execution, Jesseeisen this blog has been explained very well, (portal here) I am not here caught dead, I want to talk about Opendir and open function of a little difference.

  The Opendir function opens the directory stream with the close-on-exec (close on execution) flag set, and the open function does not.

  Specifically, this code:

1 /***2 the Err_exit () function here is my own definition, and the function is to call the Strerror () function to print out the error message and call the exit () function to exit! 3 ***/4 5#include <dirent.h>6#include <errno.h>7#include <fcntl.h>8#include <string.h>9#include <stdlib.h>Ten#include <stdarg.h> One#include <stdio.h> A#include <sys/types.h> -#include <unistd.h> - voidErr_exit (Char*fmt,...); the intMainintargcChar*argv[]) - { -DIR *Dirp; -     intdir_fd; +     intVal; -  +     /*Open the directory in Opendir mode and get the file descriptor and view its close-on-exec flag*/ A     if(NULL = = (Dirp=opendir ("/"))) atErr_exit ("[Opendir]:"); -     if(-1= = (DIR_FD=DIRFD (DIRP)))//gets the file descriptor of the Open Directory stream -Err_exit ("[DIRFD]:"); -     if(-1= = (val=fcntl (DIR_FD,F_GETFD))) -Err_exit ("[Fcntl]:"); -  inprintf"%-9s:","[Opendir]"); -     if(Val &fd_cloexec) toprintf"close-on-exec flag is on\n"); +     Else -printf"close-on-exec flag is off\n"); the  *     if(-1==Closedir (DIRP)) $Err_exit ("[Closedir]:");Panax Notoginseng  -     /*Open the directory and view its close-on-exec flag*/ the     if(-1= = (Dir_fd=open ("/", o_directory)))//The Open function plus the O_DIRECTORY flag opens the directory. +Err_exit ("[Open]:"); A     if(-1= = (val=fcntl (DIR_FD,F_GETFD))) theErr_exit ("[Fcntl]:"); +  -printf"%-9s:","[Open]"); $     if(Val &fd_cloexec) $printf"close-on-exec flag is on\n"); -     Else -printf"close-on-exec flag is off\n"); the  -     if(-1==Close (DIR_FD))WuyiErr_exit ("[Close]:"); the  -     return 0; Wu}

This code function is like this:

First, open a directory through the Opendir function, and then extract the file descriptor of the directory stream through the DIRFD function, and then use the fcntl to get the CLOSE-ON-EXEC flag.

Then come through the Open function (add the O_DIRECTORY flag to open the directory, see Open (2)) opens the same directory, and then the FCNTL function to see its close-on-exec flag.

The running results of the program are as follows:

  

As we can see from the results, the Opendir Open Directory stream has the CLOSE-ON-EXEC flag bit, and the open function opens the directory stream without the CLOSE-ON-EXEC flag bit, which confirms the narrative of the APUE8.10 section:

  

Apue Exercise 8.7

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.