Linux system programming class notes 1 (20121013)
1. Each application is exclusive:
1. for 32-bit CPUs, application programming and kernel occupy 4 GB space:
The application layer is 0-3g, and the kernel layer is 3G-4g;
2. Each program is exclusive:
Each program has 3G space for use. This space is virtual memory. during actual calls, each application maps to different physical memory.
For example, if both a. Out and B. Out operate on the address 0x1000, the physical memory is different.
Ii. system call ):
Explanation from fopen:
To open a file, you need to call the kernel, for example, file system identification (ext4), hard drive, and other programs. This is complicated. Therefore, the kernel and app are separated and there is a clear boundary, with POSIX or other calling interfaces in the middle;
The kernel/operating system provides simple application interfaces for upper-layer applications (such as fopen, and applications (such as ls CD ).) The operating system shields hardware drivers or process scheduling, so upper-layer applications do not need to consider hardware details.
The operating system is provided to the application layer as system call)
Note: differences between system call functions and C standard Libraries;
For example, file operations:
C language standard library |
File Operations Fopen, fclose, fread, fwrite, fseek; |
I/O buffer, code optimized, and high efficiency. For example, some operations are merged to reduce access to the kernel. For example, printf (). Sometimes you need to refresh (\ n can refresh) to print |
System Interface POSIX standard |
File Operations; Open Close read write lseek |
I/O buffer, not optimized, with many accesses to the kernel, which is closer to kernel calls. |
Iii. System IO/operations:
1. File Operations
Open read write lseek close
Example: int open (const char * pathname, int flags, mode_t Mode)
Return: the return value is an integer. It is a file descriptor. The default value is 0 (standard input), 1 (standard output), and 2 (standard error ).
If one of them is enabled, it will be allocated from 3, 3, 4, 5, 6 ......, If 3 is deleted, 3 is prioritized.
The flags example is as follows:
O_creat: create a file in combination with read/write operations. If yes, open the file;
O_excl: used with o_creat. If yes, an error is returned;
O_trunc: used with o_creat. If yes, clear the content in the original file.
File operations, whether in the C library or the system call interface, are all file pointer operations, this is the same.
2. Folder operations: Dir
Opendir readdir seek telldir rewinddir
Dir * opendir (const char * Name (Folder name ));
Struct dirent * readdir (dir * dirp );
The key is the structure parsing ..
Struct dirent {
Ino_t d_ino;/* inode Number */
Off_t d_off;/* offset to the next dirent */
Unsigned short d_reclen;/* length of this record */
Unsigned char d_type;/* type of file; not supported
By all file system types */
Char d_name [2, 256];/* filename */
};
Obtain further information about the target file:
Fstat ()
Stat
Lstats
It mainly reads information about the struct stat:
Operation Method:
Struct stat Buf = NULL;
If (STAT ("path", Buf) <0)
{Error handling}
Buf. The Member is called.
Struct stat {
Dev_t st_dev;/* ID of device containing file */
Ino_t st_ino;/* inode Number */
Mode_t st_mode;/* Protection */
Nlink_t st_nlink;/* Number of hard links */
Uid_t st_uid;/* User ID of owner */
Gid_t st_gid;/* Group ID of owner */
Dev_t st_rdev;/* device ID (if special file )*/
Off_t st_size;/* Total size, in bytes */
Blksize_t st_blksize;/* blocksize for file system I/O */
Blkcnt_t st_blocks;/* Number of 512b blocks allocated */
Time_t st_atime;/* time of last access */
Time_t st_mtime;/* time of last modification */
Time_t st_ctime;/* time of last status change */
};
Time_t: It can be parsed using ctime ..
Soft link and hard link: (soft link and hard link)
Content |
Soft link |
Hard Link |
Indicates |
Ll property does not exist |
Number of hard links in the ll attribute |
|
Tracking path, which belongs to path binding. If the link or source file is moved, the link becomes invalid, the source file is changed to the file name, and the link becomes invalid. |
File System Level binding. There are no primary or secondary differences between the two files. Move or delete one of the files, or rename the files. Any files will continue to be valid. |
|
Cross-partition connections |
Cross-partition connections are not allowed |
|
|
|
|
|
Chdir: change the path of the Operation folder .. Very useful ..
Iv. err_no:
Error Code
Is a global variable, exclusive between processes, only one err_no;
Perror ();
V. environment variables:
Path and other environment variables:
Extern char ** environ
------------------------------------------ End ----------- 20121013 ---------------------------------------------