Advanced programming in Unix

本文介绍了UNIX系统的编程基础知识,包括错误处理、用户身份识别、信号处理、时间测量等核心概念,并详细解析了文件操作相关的系统调用及数据结构,如文件描述符、文件表和v-node表等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

.

^^^^^^chapter 1^^^^^^^^

password file /etc/password

 

seven colon-separated fileds: login name, encrypted passwd, numeric user ID, numeric groop ID, a comment field,

home directory, and shell program.

 

getpid() to obtain its process ID

 

**ABOUT ERROR

In an environment that supports threads, should access to errno by defining it as

   extern int *_ _errno_location(void);

   #define errno  (*_ _errno_location())

 

char *strerror(int errnum)       returns:pointer to message string

 

void perror(const char *msg)     produce an error message based on the current value of errno

 

 

**ABOUT USER IDENTIFICATION

user ID = 0 that user is root or the superuser

group ID     there is a group file that maps group names into numeric group IDs. (/etc/group)

   password file maintains the mapping between login names and user ID, and the group file provides

   the mapping between group names and group IDs.

 

getuid() returns the user ID

getgid() returns the group ID

 

 

**ABOUT SIGNAL

signal function specify the name of the function(sig_int) to call when the SIGINT signal is generated.

  static void sig_int(int);

  ....      

  if (signal(SIGINT, sig_int) == SIG_ERR)

       err_sys("signal error");

 

**ABOUT TIME VALUES

two different time values: 1. Calender time     time_t

                        2. Process time      clock_t

 

we will see the UNIX System maintains thress values for a process when measuring the execution time

1. clock time        is the amount of the time the process takes to run.

2. user CPU time     is the CPU time attributed to user instructions

3. system CPU time   is the CPU time attributed to the kernel when it executes on behalf of the process.

The sum of user CPU time and system CPU time is often called the CPU time.

   

**ABOUT SYSTEM CALL AND LIB CALL

malloc uses sbrk system call.

 

 

^^^^^^chapter 2^^^^^^^^

 

 

^^^^^^chapter 3^^^^^^^^

“无缓冲的”是指每一次的read/write都会激发一次内核调用。

five functions:open, read, write, lseek and close.

To the kernel, all open files are referred to by file descriptors.(non-negative integer)

0-standard input    STDIN_FILENO,

1-standard output   STDOUT_FILENO,

2-standard error    STDERR_FILENO

Early historical implementations of the UNIX System had an upper limit of 19, allowing a maximum of 20 open files per process, but many systems increased this limit to 63.

原来最多编号到19,也就是一个进程允许同时打开最多20个文件,但现在最大编号已经增加到了63

 

A hole in a file isn't required to have storage backing it on disk.It depends on the file system implementation.

 

**ABOUT DATA STRUCTURE

Three data structures that the kernel uses to represents an open file:

1. Every process has an entry in the process table. [process table entry]

    a. The file descriptor flags

    b. a pointer to a file entry

2. The kernel maintains a file table for all open files. [file table]

    a. the file status flags for the file(read, write, append, sync, nonblocking)

    b. the current file offset

    c. a pointer to the v-node table entry for the file

3. Each open file(device) has a v-node structure that contains info about the type of file and pointers to    functions that operate on the file.For most files, the v-node also contains the i-node for the file.[v-node table]

   Linux has no v-node.

   The i-node contains the owner of the file, the size of the file, pointers to where the actual data blocks for the file are located on disk, and so on.

Write Method: the current file offset in the file table entry is increamented by the number of bytes written.If

              this causes the current file offset to exceed the current file size, the current file size in the

              i-node table entry is set to the current file offset.

Open Method: with O_APPEND flags. Set the file status flags of the file table entry. Each time a write performed

             for a file with this append flag set, the current file offset in the file table entry is first set to

             the current file size from the i-node table entry.

lseek Method:modified only the current file offset in the file table entry. No I/O takes place.

 

注意文件描述符标记与文件状态标记之间的区别:前者每个进程都有一个,而后者是所有的进程共享一个。

 

It is possible for more than one files descriptor entry to point to the same file table entry.

 

**ABOUT SOME FUNCTIONS

Calling pread(pwrite) is equivalent to calling lseek followed by a call to read(write),and there is no way to interrupt the two operations using pread.

 

int dup(int filedes)

   returned is guaranteed to be the lowest-numbered available file descriptor.

int dup2(int filedes, int filedes2)

   filedes2 = specify the new descriptor.if filedes2 is already open, first close. if filedes equals filedes2, then dup2 returns filedes2 without closing it.

 

the sync, fsync and fdatasync functions are provided to ensure consistency of the file system on disk with the contents of the buffer cache.

sync function simply queues all the modified block buffers for writing.Do not wait for the disk writes to take place.

fsync function refers only to a single file which specified by the file descriptor, and waits for the disk writes to complete before returning.

fatasync function is similar to fsync, but it affects only the data portions of a file.fsync updates the file's attributes synchronously.

 

int fcntl(int filedes, int cmd, ... /* int arg */ );

fcntl function can change the properties of a file that is open.

 

Opening the file /dev/fd/n is equivalent to duplicating descriptor n, assuming that descriptor n is open.

 

^^^^^^chapter 4^^^^^^^^

int stat(const char *restrict pathname, struct stat * restrict buf)

     return a structure of information about the named file

int fstat(int filedes, struct stat *buf)

    obtains info about the opened descriptor filedes

int lstat(const char *restrict pathname, struct stat * restrict buf)   

    similar to stat, but to a symbolic link, lstat return info about the symbolic link,while stat return the info      about the file referenced by the symbolic link.

 

     struct stat {

       mode_t    st_mode;      /* file type & mode (permissions) */

       ino_t     st_ino;       /* i-node number (serial number) */

       dev_t     st_dev;       /* device number (file system) */

       dev_t     st_rdev;      /* device number for special files */

       nlink_t   st_nlink;     /* number of links */

       uid_t     st_uid;       /* user ID of owner */

       gid_t     st_gid;       /* group ID of owner */

       off_t     st_size;      /* size in bytes, for regular files */

       time_t    st_atime;     /* time of last access */

       time_t    st_mtime;     /* time of last modification */

       time_t    st_ctime;     /* time of last file status change */

       blksize_t st_blksize;   /* best I/O block size */

       blkcnt_t  st_blocks;    /* number of disk blocks allocated */

     };

 

 

File type macros in <sys/stat.h>

S_ISREG()    regular file

 

S_ISDIR()    directory file

 

S_ISCHR()    character special file

               A type of file providing unbuffered I/O access in variable-sized units to devices.

S_ISBLK()    block special file  

                  A type of file providing buffered I/O access in fixed-size units to devices such as disk drives.

 

S_ISFIFO()   pipe or FIFO

 

S_ISLNK()    symbolic link

 

S_ISSOCK()   socket

 

every process has six IDs at least.

(1)real user ID

(2)real group ID

 Identify who we really are.These are taken from our entry in the paswd file when we log in.

 

(3)effective user ID

(4)effective group ID

(5)supplementary group IDs

  Determine our file access permissions.

 

(6)saved set-user-ID

(7)saved set-group-ID

  The two IDs contain copies of the effective user ID and the effective group ID when a program is executed.

 

Normally, the effective user ID  = the real user ID

          the effective group ID = the real group ID

 

set-user-id bit: the bit of st_mode which indicate "when this file executed, set the effective user ID of the             process to be the owner of the file"

set-group-id bit:.....

 

File Access Permission Rules

1.Whenever we want to open any type of file by name, we must have execute permission in each directory mentioned in the name, including the current directory, if it is implied.

我们以文件名为参数打开文件的时候 ,我们需要对名字中提到的所有目录有execute的权限,包括当前目录。

2.we must have write permission for a file to specify the O_TRUNC falg in the open function.

如果调用open函数时指定了O_TRUNC标记,那么需要对文件有write的权限。

3.we cannot create a new file in a directory unless we have write & execute permisson in the directory.

创建文件的时候我们需要对当前目录有write&execute权限。

4.To delete an existing file, we need write & execute permission in the directory containing the file.

  we don't need read or write permission for the file itself.

删除文件时,需要对当前目录有write&execute权限,不需要对文件的read/write权限。

 

Note that if the process owns the file, access is granted or denied based only on the user access permissions; the group permissions are never looked at. Similarly, if the process does not own the file, but belongs to an appropriate group, access is granted or denied based only on the group access permissions; the other permissions are not looked at.

 

The user ID of a new file is set to the effective user ID of the process.

新文件的用户id时进程的有效用户id

The group ID of a new file can be the effective group ID of the process,or the group ID of the directory in which the file is being created.

新文件的组id可以是进程的有效组id,或者是文件所在目录的有效组id

 

access function based its tests on the real user and group IDs, even though a process might be set-user-ID to root.

 

umask function set the file mode creation mask for the process and returns the previous vaule.

 

for a symbolic link, the file size is the number of bytes in the filename.

For example, the file size of 7 is the length of the path "usr/lib"

 

st_blksize    the preferred block size for I/O for the file.

st_blocks     the actual number of 512-byte blocks that are allocated.

du command reports that the amount of disk space used by the file xxx 512-byte blocks.

 

Holes are created by seeking past the current end of the file and writing some data.

 

几乎所有stat结构里的信息都是从i-node里面获得的。只有两个是从目录里面得到的:目录名和i-node的数量。

 

struct dirent *readdir(DIR *dp);

 

      struct dirent {

        ino_t d_ino;                  /* i-node number */

        char  d_name[NAME_MAX + 1];   /* null-terminated filename */

      }

第五章

 

三种缓存方式:

1、全缓冲  真正的io直至缓存满了才发生,或者是fflush

2line缓存

3unbuffer

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值