In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/03 Report--
Q1. Replace the lstat function in the program in figure 4-3 with the stat function. What happens if one of the command line arguments is a symbolic link?
Before the change:
[root@clstore3] #. / lstat / etc/passwd / etc/ dev/log / dev/tty / dev/sr0 / dev/cdrom
/ etc/passwd: regular
/ etc: directory
/ dev/log: socket
/ dev/tty: character special
/ dev/sr0: block special
/ dev/cdrom: symbolic link
As you can see, lstat looks at / dev/cdrom as a soft link.
Now that we have changed the program lstat to stat, the results are allowed as follows. What you can see in stat is the information of the file referenced by the symbolic link.
Conclusion: the lstat function is similar to stat, but when the named file is a symbolic link, lstat returns information about the symbolic link, not the file referenced by the symbolic link.
[root@clstore3] #. / stat / etc/passwd / etc/ dev/log / dev/tty / dev/sr0 / dev/cdrom
/ etc/passwd: regular
/ etc: directory
/ dev/log: socket
/ dev/tty: character special
/ dev/sr0: block special
/ dev/cdrom: block special
# include "apue.h"
Int
Main (int argc, char * argv [])
{
Int i
Struct stat buf
Char * ptr
For (I = 1; I
< argc; i++) { printf("%s: ", argv[i]); if (stat(argv[i], &buf) < 0){ /* this need to change lstat from stae*/ err_ret("lstat error"); continue; } if ( S_ISREG(buf.st_mode)) ptr = "regular"; else if ( S_ISDIR(buf.st_mode)) ptr = "directory"; else if ( S_ISCHR(buf.st_mode)) ptr = "character special"; else if ( S_ISBLK(buf.st_mode)) ptr = "block special"; else if ( S_ISFIFO(buf.st_mode)) ptr = "fifo special"; else if ( S_ISLNK(buf.st_mode)) ptr = "symbolic link"; else if ( S_ISSOCK(buf.st_mode)) ptr = "socket"; else ptr = "** unknow mode **"; printf("%s\n", ptr); } exit(0); } Q2. 如果文件模式创建屏蔽字是777(八进制),结果会怎样?用shell 的umask 命令验证该结果。 [root@clstore3 ~]# umask 0022 改为777后 [root@clstore3 ~]# umask 777 [root@clstore3 ~]# umask 0777 创建一个test3文件子后,只有root可以看,也不可以更改了。 [root@clstore3 ~]# vim test3.txt [root@clstore3 ~]# ls -al test3.txt ----------. 1 root root 12 Nov 13 10:44 test3.txt Q3.关闭一个你所有拥有的用户的读权限,将导致拒绝你访问你自己的文件,对此进行验证。 [root@clstore3 ~]# chmod 640 /home/zhangb/Makefile [root@clstore3 ~]# ls -al /home/zhangb/Makefile -rw-r-----. 1 root root 18709 May 22 17:32 /home/zhangb/Makefile 切换到用户zhangb,由于用户zhangb没有该文件的读权限,所以不能查看该文件。 [root@clstore3 ~]# su - zhangb 522868 $ ls -al Makefile -rw-r-----. 1 root root 18709 May 22 17:32 Makefile 522868 $ less Makefile Makefile: Permission denied 522868 $ whoami 522868 Q4,创建文件foo 和bar后,运行图4-9的程序,将发生什么情况? 什么都没有发生, 权限为没有任何改变。 [root@clstore3 ~]# touch foo bar [root@clstore3 ~]# ls -al foo bar -rw-r--r--. 1 root root 0 Nov 13 11:34 bar -rw-r--r--. 1 root root 0 Nov 13 11:34 foo [root@clstore3 ~]# ./create_foobar [root@clstore3 ~]# ls -al foo bar -rw-r--r--. 1 root root 0 Nov 13 11:34 bar -rw-r--r--. 1 root root 0 Nov 13 11:34 foo ############################################################### [root@clstore3 ~]# cat create_foobar.c #include "apue.h" #include #define RWRWRW (S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH) int main(void) { umask(0); if ( creat("foo",RWRWRW) < 0) err_sys("create error for foo"); umask ( S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH); if (creat("bar", RWRWRW) < 0 ) err_sys("create error for bar"); exit(0); } Q4.5 4.12节中讲到一个普通文件的大小可以是0,同时我们又知道st_size字段是为目录或符号链接定义的,那么目录和符号链接的长度可否为0? A:对于普通文件,其文件长度可以是0,在开始读写这种文件是,将得到文件(end-of-file)指示。 对于目录,文件长度通常是一个(16or512)的整倍数。 符号链接-文件长度是在文件名中的实际字节数。 [root@clstore3 /]# ls -al /dev/cdrom lrwxrwxrwx. 1 root root 3 May 29 21:55 /dev/cdrom ->Sr0
[root@clstore3 var] # ls-al mail
Lrwxrwxrwx. 1 root root 10 Sep 15 2014 mail-> spool/mail
Q 4.6, write a cp1-like program that copies files that contain holes, but does not write byte 0 to the input file.
[root@clstore3 ~] # od-c file.hole
0000000 a b c d e f g h i j\ 0\ 0\ 0\ 0\ 0\ 0
0000020\ 0\ 0\ 0\ 0\ 0\ 0\ 0\ 0\ 0
*
0040000 A B C D E F G H I J
0040012
[root@clstore3 ~] # od-c file.out
0000000 a b c d e f g h i j A B C D E F
0000020 G H I J
0000024
Program code part
# include
# include
/ * this program is to copy a file (ignore the hole part if there is a hole in it
* zhangbo 2015-11-13 * /
Int main ()
{
Int c
FILE * in, * out
In = fopen ("file.hole", "r")
Out = fopen ("file.out", "w")
C = fgetc (in)
While (c! = EOF)
{
/ * this is to copy the file, if encounter file with a hole in it'\ 0mm, do nothing
* otherwise, copy the content to file.out*/
If (c! ='\ 0')
Fputc (cjingout)
C = fgetc (in)
}
Exit (0)
}
Apue Note in output from the ls command in section 4.12 that the file core and core.copy have different access permission. If the umask value didn't change between the creation of these two files. Explain how the difference could have occurred. ?
Q4.8 when running program 4.5 we check the available disk space with the df (1) command. Why didn't we use the du (1) command.
Du command reports that the amount of disk space used by the file. 512-byte blocks.
Df displays the amount of disk space available on the file system containing each file name argument.
Q4.9 In figure 4.20, we show the unlink function as modifying the changed-status time of the file itself. How can this happen?
The modification time indicates when the contents of the file were last modified.
The changed-status time indicates when the i-node of the file was last modified.
Some operations the affect the i-node without changing the actual contents of the files:
Change the file access permissions/user ID, and change the number of links and so on.
The unlink function changes the i-node contents of the file and it is for changed-status time.
Test1 are the hard link to test.txt, we can see that after unlink test1.txt, the changed-status time of test.txt becomes 16:59, which proves that unlink changes the information in i-node link minus 1, causing the last changed-status to become the latest.
ZhangbodeMacBook-Pro:script zhangbo$ ls-lc test*
-rw-r--r-- 3 zhangbo staff 12 11 16 16:49 test.txt
-rw-r--r-- 3 zhangbo staff 12 11 16 16:49 test1.txt
ZhangbodeMacBook-Pro:script zhangbo$. / a.out
File unlinked
Done
ZhangbodeMacBook-Pro:script zhangbo$ ls-lc test*
-rw-r--r-- 2 zhangbo staff 12 11 16 16:59 test.txt
Three time fields are maintained for each file, Their purpose is summarized in the below.
St_atime last-access time of tile read-u (ls option ls-u)
St-mtime last-modification time of file date write
St-ctim last-change time of i-node status chmod,chown-c
Q4.10 in Section 4.22 how does the system's limit on the number of open files effect the mighty function?
After Opendir opens a directory, the dopath function is called recursively. Suppose opendir uses a file descriptor and calls closedir to release the descriptor only after the directory has been processed, which means that another descriptor will be used each time the first level is made. So the maximum number of descriptors a process can open limits the depth of the file system that we can traverse.
Q4.11 In section 4.22, our version of ftw never changes its directory, Modify this routine so that each time it encounters a directory, it uses the chdir function to change to that directory. Allowing it to use the filename and not the pathname for each call to stat. When all the entries in the directory have been processed, execute chdir ("..") Compare the time used by this version and the version int the next.
Chdir should be done twice each time, which will consume the resources of the system and feel time-consuming.
Q4.12. Each process also have a root directory that is used for resolution of absolute phonemes.
This root directory can be changed with the chroot function. Look up the description for this function in your manuals when might this function be useful?
The chroot function is used by Internet File transfer Protocol (FTP) programs to assist security. Users with baa accounts in the system (anonymous users) are placed in a separate directory and chroot is used to treat the directory as the new root directory, which prevents users from accessing files outside this directory.
Chroot can also construct a copy of the file system structure on another machine, and then modify the copy to change the original file system, which can be used to test the installation of new software packages (standard answer)
Q4.13 how can you set only one of the two time values with the utimes functions?
Utimes is too old to handle, so he uses a utime. It's pretty much the same, but the function is the same.
The structure of utime is
Struct utimbuf
{
_ _ time_t actime; / * Access time. , /
_ _ time_t modtime; / * Modification time. , /
}
[root@clstore3 ~] # vim file.out
[root@clstore3 ~] # ls-al file.out
-rw-r--r--. 1 root root 13 Nov 18 18:25 file.out
[root@clstore3 ~] # ls-lu file.out
-rw-r--r--. 1 root root 13 Nov 18 18:25 file.out
[root@clstore3 ~] #. / a.out
After running, the access time time did not change, but the mod time changed.
[root@clstore3 ~] # ls-al file.out
-rw-r--r--. 1 root root 13 Nov 15 18:25 file.out
[root@clstore3 ~] # ls-lu file.out
-rw-r--r--. 1 root root 13 Nov 18 18:25 file.out
# cat zap.c-this program comes from http://m.blog.csdn.net/blog/zhoulaowu/14158137
# include
# include
# include
# include
Int
Main (int argc, char * argv [])
{
Int i, fd
Struct stat statbuf
Char pathname [] = "/ root/file.out"
Struct utimbuf ubuf
/ * struct timespec times [2]; * /
Struct timespec times [2]
If (stat (pathname, & statbuf) =-1) {
Printf ("stat error for% s\ n", argv [I])
} else
{
Ubuf.modtime = statbuf.st_mtime-30000
Ubuf.actime = statbuf.st_atime
If (utime (pathname, & ubuf) =-1)
Printf ("utime error for% s\ n", pathname)
}
Exit (0)
}
Q4.14. Some versions of the finger command output "new mail received..." And "unread since..." Where... Are the corresponding time and dates. How can the program determine these two times and dates
Check the last access time of a file.
Q4.15. Examine the archive formats used by the clio and tar commands. (these descriptions are usually found in section 5 of the Unix programmer's manual) how many of the three possible time values are saved for each file? When a file is restored, what value do you think the access time is set to. And why?
Mtime and atime for tar.
When tar extracts a file, the modify time of the file is not changed by default, and the user's access time is modified.
[root@clstore3 test1] # tar-xf tar.tat
[root@clstore3 test1] # ls-l-you can view the user's modify time, which has not been modified.
Total 12
-rw-r--r--. 1 root root 0 Nov 19 15:51 blues-- mtime has not been modified.
-rw-r--r--. 1 root root 0 Nov 19 15:51 folk
-rw-r--r--. 1 root root 0 Nov 19 15:51 jazz
-rw-r--r--. 1 root root 10240 Nov 19 17:35 tar.tat
[root@clstore3 test1] # ls-u
Blues folk jazz tar.tat
[root@clstore3 test1] # ls-lu
Total 12
-rw-r--r--. 1 root root 0 Nov 19 17:39 blues-- the atime was modified to the extraction time of the file.
-rw-r--r--. 1 root root 0 Nov 19 17:39 folk
-rw-r--r--. 1 root root 0 Nov 19 17:39 jazz
-rw-r--r--. 1 root root 10240 Nov 19 17:35 tar.tat
[root@clstore3 test1] # date
Thu Nov 19 17:39:59 CST 2015
After the same extraction is the + M parameter, the mtime and atime is changed to the time of extraction, and you can see that the mtime and atime and the current time are always the same.
[root@clstore3 test1] # tar-mxf tar.tat
[root@clstore3 test1] # ls-al
Total 20
Drwxr-xr-x. 2 root root 4096 Nov 19 17:45.
Drwxr-xr-x. 4 root root 4096 Nov 19 17:28..
-rw-r--r--. 1 root root 0 Nov 19 17:45 blues
-rw-r--r--. 1 root root 0 Nov 19 17:45 folk
-rw-r--r--. 1 root root 0 Nov 19 17:45 jazz
-rw-r--r--. 1 root root 10240 Nov 19 17:35 tar.tat
[root@clstore3 test1] # ls-lu
Total 12
-rw-r--r--. 1 root root 0 Nov 19 17:45 blues
-rw-r--r--. 1 root root 0 Nov 19 17:45 folk
-rw-r--r--. 1 root root 0 Nov 19 17:45 jazz
-rw-r--r--. 1 root root 10240 Nov 19 17:35 tar.tat
[root@clstore3 test1] # date
Thu Nov 19 17:45:14 CST 2015
Q4.16. Does the unix system have a fundamental limitation on the depth of a directory tree? To find our, write a program that creates a directory and then changes to that directory in a loop. Make certain that the length of the absolute pathname of the of the leaf of the directory is greater than your system's PATH_MAX limit. Can you call getcwd to fetch the directory's pathname? How do the standard UNIX system tools deal with this long pathname?
Generally speaking, the depth of the file should be limited, but different system restrictions are different.
My machine can not be displayed when it is more than 203, so the result is too big. The mac and linux path is about 1052 characters long, and then it doesn't work. But mac can still be mkdir and successful. Under unix, it won't work directly to 203s.
Linux and mac os pathname ~ = 1052 characters
Mac os can continue to create folders, but linux can't.
New current directory is:Result too large
Current directory level: 8667
Mkdir successed.
New current directory is:Result too large
Current directory level: 8668
Mkdir successed.
New current directory is:Result too large
Current directory level: 8669
Mkdir successed.
[root@clstore3 ~] # tail test.txt | sed-n '5jue 7p'
Current directory level: 203
Mkdir successed.
New current directory is:/root/test/test/test/test/test/test/test/test/test/test/test/test/test/test/test/test/test/test/test/test/test/test/test/test/test/test/test/test/test/test/test/test/test/test/test/test/test/test/test/test/test/test/test/test/test/test/test/test/test/test/test/test/test/test/test/test/ Test/test/test/test/test/test/test/test/test/test/test/test/test/test/test/test/test/test/test/test/test/test/test/test/test/test/test/test/test/test/test/test/test/test/test/test/test/test/test/test/test/test/test/test/test/test/test/test/test/test/test/test/test/test/test/test/test/test/test/test/ Test/test/test/test/test/test/test/test/test/test/test/test/test/test/test/test/test/test/test/test/test/test/test/test/test/test/test/test/test/test/test/test/test/test/test/test/test/test/test/test/test/test/test/test/test/test/test/test/test/test/test/test/test/test/test/test/test/test/test/test/ Test/test/test/test/test/test/test/test/test/test/test/test/test/test/test/test/test/test/test/test/test/test/test/test/test
[root@clstore3 ~] # tail test.txt | sed-n '10p' | wc-c
1052
Program source code
# include
# include
# include
# include
# include
# include
# include
Int main (int argc, char * argv [])
{
Char path [1000]
Char file [1000]
Argv [1] = "test"
Char * d
Char * s = "/ view"
Int i
Getwd (path)
Printf ("current directory is:% s\ n", path)
For (I = 1; I / proc/self/fd)
[root@client2 ~] # ls-al / dev/fd/
Total dosage 0
Dr-x-. 2 root root 0 November 21 01:29.
Dr-xr-xr-x. 8 root root 0 November 21 01:29..
Lrwx-. 1 root root 64 November 21 01:29 0-> / dev/pts/3
Lrwx-. 1 root root 64 November 21 01:29 1-> / dev/pts/3
Lrwx-. 1 root root 64 November 21 01:29 2-> / dev/pts/3
Lr-x-. 1 root root 64 November 21 01:29 3-> / proc/53116/fd
[root@client2 ~] # ls-al / proc/self/fd
Total dosage 0
Dr-x-. 2 root root 0 November 21 01:29.
Dr-xr-xr-x. 8 root root 0 November 21 01:29..
Lrwx-. 1 root root 64 November 21 01:29 0-> / dev/pts/3
Lrwx-. 1 root root 64 November 21 01:29 1-> / dev/pts/3
Lrwx-. 1 root root 64 November 21 01:29 2-> / dev/pts/3
Lr-x-. 1 root root 64 November 21 01:29 3-> / proc/53129/fd
ZhangbodeMacBook-Pro:script zhangbo$. / a.out
Unlink error: Permission denied
ZhangbodeMacBook-Pro:script zhangbo$ ls-al / dev/fd
Total 0
Crw--w---- 1 zhangbo tty 16, 3 11 21 14:21 0
Crw--w---- 1 zhangbo tty 16, 3 11 21 14:21 1
Crw--w---- 1 zhangbo tty 16, 3 11 21 14:21 2
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.