Linux-system calls and library functions

Source: Internet
Author: User
Tags function prototype symlink

Basic concepts of documentation

File
Objects that can be read and written
Have a certain right attribute, including access rights, types, etc.
File system
A collection of files and their attributes, providing a naming and management mechanism
File structure
Byte stream, no specific internal structure

File type
普通文件目录设备文件字符文件/块文件FIFO文件(如管道文件)Socket文件符号链接
Hard Connect

Different filenames correspond to the same inode
Cannot Cross file system
corresponding system calls
Link ()

[Root@localhost Link]# ls-lTotal1-rw-r--r--1Root root667Oct the  -: theA[root@localhost Link]# ln A b[Root@localhost Link]# ls-lTotal2-rw-r--r--2Root root667Oct the  -: thea-rw-r--r--2Root root667Oct the  -: theB[root@localhost Link]# RM aRm:remove' a '? Y[[email protected] link]# ls-ltotal 1-rw-r--r--1 root root 667 Oct 13:39 b
Symbolic Links

Store the file name of the linked file (not the Inode) implementation link
Can span file systems
corresponding system calls
Symlink ()

[Root@localhost symlink]# ls-lTotal1-rw-r--r--1Root root667Oct the  -: theA[root@localhost symlink]# Ln-s a b[Root@localhost symlink]# ls-lTotal1-rw-r--r--1Root root667Oct the  -: theAlrwxrwxrwx1Root root1Oct the  -: -B-A[root@localhostyy# RM aRm:remove' a '? Y[[email protected] symlink]# ls-ltotal 0lrwxrwxrwx 1 root root 1 Oct 14:20 B-A[[emai L protected] symlink]# cat bcat:b: No such file or directory
File system type
2.0.x2.2.x2.4.x中首次使用)xfs(源于SGI )Jfs(源于IBM )嵌入式小型文件系统CRAMFSJFFS2
Virtualization of

exists only in memory
Composition
Super Block
Information for storing installed file systems
File system control blocks that typically correspond to disk file systems
File Object
Inode Object
Storing information for a single file
File control blocks that typically correspond to disk file systems
Dentry Object
Describe the file name and associated inode information

directory entries for virtual files
Dentry Definition Location: include/linux/dcache.h
Tree describing the directory and file
Catalog cache (Directory cache)
Save Dentry entries for each directory/file that has been read
Faster file Lookup efficiency
Relationships between objects in the virtual file system

inode对象与文件:一对一dentry对象与inode对象:多对一(硬连接)文件对象与dentry对象:多对一

EXT2 File System

第一个磁盘块用于引导,其余部分被分成若干组各组大小相同且顺序存放可通过组序号确定组在磁盘上的位置组的构成文件系统超级块 所有组描述符 数据块位图 inode位图inode表 数据块
EXT2 File System Architecture

Each group has a copy of the Super block and all group description information
Under normal circumstances, the kernel only uses the No. 0 Group of information
Recover from a copy of another group when the copy of group 0 is damaged
Data Block bitmap
Size is a block
One block in each order corresponding to the group
0 indicates available
1 indicates used
Inode table
Storing inode data for files and directories
Inode bitmap
Indicates whether the corresponding Inode table space has been occupied

EXT3 file

Log filesystem (journaling file system)
Log technology using the database (log, checkpoint)
3 Ways to log
Journal, ordered, writeback
Log records in/.journal (hidden files)
Kjournald-5s

Proc File system

Virtual file system, only in memory
Can query, set the operation of the system and various system parameters
Many applications in the system depend on the proc file system
such as command lsmod equivalent to Cat/proc/modules
File size is 0
Many file names reflect the corresponding parameters of the kernel
Parameter values can be modified by this file name
Modify the limits of shared memory segments, such as #echo 2048 >/proc/sys/shmmni
The "Digital catalogue" under/PROC refers to the process of corresponding PID
The contents of the directory "1" is the information of process 1th

Basic concepts of system calls and library functions

All appear in the form of a C function
System calls
External interface of the Linux kernel
The only interface between the user program and the kernel
Minimum interface available
Programmer's Manual section II
Library functions
Relies on system calls
Provides more complex features
Example: Standard I/O Library
Programmer's Manual Section III

No cache I/O with cached I/O

No cache I/O
Read/write Devices
File descriptor
ANSI c not supported, POSIX supported
Use when device file access
have cache I/O
Standard I/O library provides
Deal with a lot of details
Cache allocation
Performing I/O with optimized lengths
Stream--File pointer
Normal file

Basic I/O system calls
文件描述符基本I/Oopen() / creat(), close(), read(), write(), lseek()dup()/dup2()fcntl()ioctl()
File descriptor

Non-negative integers
Definition form
int FD;
Standard file Descriptor Definitions (

STDIN_FILENO (0)STDOUT_FILENO (1)STDERR_FILENO (2)

General Procedures for file operations
Open-read/write-[positioning]-off

/* a rudimentary example program */#include <fcntl.h>main(){    int fd, nread;    char buf[1024];    filefor reading */    fd = open(“data”, O_RDONLY);    readinthe data */    read1024);    thefile */    close(fd);}
Error handling
UNIX风格返回值“errno” 变量(/usr/include/errno.h)externint#include <string.h>char *strerror(int errnum);返回出错的字符串描述perror()#include <stdio.h>void perror(constchar *msg);打印错误原因字符串
Open ()/creat () system call
 function to open or create a file or device header file  #include <sys/types.h>   #include <sys/stat.h>   #include <fcntl.h>  function prototype int  open (const  char  *pathname, int  flags); int  open (const   Char  *pathname, int  flags, mode_t mode); int  creat (const   Char  *pathname, mode_t mode); Returns a new file descriptor if the return value is successful otherwise return-1   
功能文件访问模式取值说明(/usr/include/fcntl.h)O_RDONLY:只读形式打开O_WRONLYO_RDWR:读写形式打开O_APPEND:追加模式打开O_TRUNC :若文件存在且为只读或只写成功打开,则将长度截为0O_CREAT:若文件不存在则创建之使用此选项时,需同时说明参数mode,说明文件的存取许可权位O_EXCL:若同时指定O_CREAT,而文件已经存在,则出错该参数可测试文件是否存在,如果不存在则创建此文件creat()函数说明等价于已参数O_CREAT|O_WRONLY|O_TRUNC执行open()
Close () system call
功能关闭文件描述符头文件#include <unistd.h>函数原型int close(int fd);成功:返回0失败:返回-1
Read ()/write () system call
read()功能从文件描述符读取数据头文件#include <unistd.h>read(int*buf, size_t count);返回值: 读到的字节数,若已到文件尾为0,若出错为-1write()功能将数据写入文件描述符头文件#include <unistd.h>write(int*buf, size_t count);返回值: 若成功为已写的字节数,若出错为-1
Lseek () system call
功能调节读写的偏移量头文件#include <sys/types.h>#include <unistd.h>函数原型off_t lseek(intint whence);返回值成功时返回偏移量位置否则返回-1whence说明SEEK_SET: 从文件头开始的偏移量SEEK_CUR: 从当前位置开始加offset后的偏移值SEEK_END: 从文件末开始加offset后的偏移值
DUP () system call
功能复制文件描述符头文件#include <unistd.h>函数原型int dup(int oldfd);传给该函数一个现有描述符,返回一个新的描述符新描述符是传给它的描述符的拷贝,即两描述符共享同一数据结构如果对一个文件描述符执行lseek操作,得到的第一个文件的位置和第二个是一样的返回值成功时返回新文件描述符否则-1
功能复制文件描述符头文件#include <unistd.h>函数原型int dup2(intint newfd);允许规定一个有效描述符( oldfd )和目标描述符( newfd )目标描述符将变成源描述符的复制品,即两个文件描述符指向同一文件,且是源描述符指向的文件返回值成功时返回新文件描述符否则-1
FCNTL () system call
 function to manipulate the file's attribute header file according to the file descriptor  #include < Unistd.h>   #include <fcntl.h>  function prototype int  fcntl  (int  FD, int  cmd); int  fcntl  (int  fd, int  cmd, long arg); int  fcntl  (int  FD, int  cmd, struct flock  * Lock ); parameter description fd: File descriptor cmd: Operation command ARG: command using the parameter lock: the same as the return value if successful, if the error is-1  
cmd参数说明F_DUPFD:复制文件描述符 FD_CLOEXEC:设置close-on-exec标志F_GETFD:读取文件描述符标志 F_SETFD:设置文件描述符标志 F_GETFL:读取文件状态标志F_SETFL:设置文件状态标志F_GETLK:获取记录锁F_SETLK :释放记录锁F_SETLKW:测试记录锁

Linux-system calls and library functions

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.