Linux 系统编程

1、I/O操作

#include <iostream> #include <cstdio> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <unistd.h> #include <sys/mman.h> #include <string> using namespace std; /* function: get the inode of the input file input: file descriptor */ int get_inode(int fd) { struct stat buf; int ret; ret = fstat(fd, &buf); if(ret < 0) { perror("fstat"); return -1; } return buf.st_ino; } int main() { struct stat sb; off_t len; char *p; int fd; printf("the memory page size is: %d/n", getpagesize()); printf("the memory page size is: %d/n", sysconf(_SC_PAGE_SIZE)); string filename; cout<<"please input the file name:"; cin>>filename; fd = open(filename.c_str(), O_RDONLY); printf("the file descriptor is %d/n", get_inode(fd)); if(-1 == fd) { perror("open"); return 1; } if(fstat(fd, &sb) == -1) { perror("fsat"); return 1; } if(!S_ISREG(sb.st_mode)) { fprintf(stderr, "%s is not a file/n", filename.c_str()); } //chuang jian nei cun wen jian ying she p = static_cast<char*>(mmap(0, sb.st_size, PROT_READ, MAP_SHARED, fd, 0)); if(p == MAP_FAILED) { perror("mmap"); return 1; } if(close(fd) == -1) { perror("close"); return 1; } for(len = 0; len < sb.st_size; ++len) putchar(p[len]); // close the memory file mapping if(munmap(p, sb.st_size) == -1) { perror("munmap"); return 1; } return 0; }  

 

 

2、文件和目录管理

#include <iostream> #include <cstdio> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <unistd.h> #include <sys/mman.h> #include <cstring> #include <dirent.h> using namespace std; /* *find the file in dir, *if the file in the dir, return 0 */ int find_file_in_dir(const char *path, const char *file) { struct dirent *entry; int ret = 1; DIR *dir = opendir(path); while((entry = readdir(dir)) != NULL) { if(strcmp(entry->d_name, file)) { ret = 0; break; } } if(!entry) perror("readdir"); closedir(dir); return ret; } int main() { string path, file; cout<<"please input the path name:"; cin>>path; cout<<"please input the file name:"; cin>>file; if(find_file_in_dir(path.c_str(), file.c_str()) == 0) cout<<"Find!"<<endl; else cout<<"Not find!"<<endl; //create hard link link("/home/flybird/array.sh", "/home/flybird/array.sh.hard"); //create soft link symlink("/home/flybird/array.sh", "/home/flybird/array.sh.soft"); }

 

 

3、内存管理

1、创建匿名内存映射

2、映射/dev/zero文件

int main() { void *p; int fd; /*open file /dev/zero to read and write*/ fd = open("/dev/zero", O_RDWR); if(fd == -1) { perror("open"); return -1; } /*make maping to /dev/zero*/ p = mmap(NULL, getpagesize(), PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0); if(p == MAP_FAILED) { perror("mmap"); if(close(fd)) perror("close"); return -1; } /*close the /dev/zero */ if(close(fd)) perror("close"); /*free p*/ int ret = munmap(p, getpagesize()); if(ret) perror("munmap"); } 

关于Linux下的/dev/null和/dev/zero文件

类UNIX 操作系统中, /dev/zero 是一个特殊的文件,当你读它的时候,它会提供无限的空字符(NULL, ASCII NUL, 0x00)。其中的一个典型用法是用它提供的字符流来覆盖信息,另一个常见用法是产生一个特定大小的空白文件。BSD就是通过mmap把/dev/zero映射到虚地址空间实现共享内存的。可以使用mmap将/dev/zero映射到一个虚拟的内存空间,这个操作的效果等同于使用一段匿名的内存(没有和任何文件相关)。

 

使用/dev/null
把/dev/null看作"黑洞". 它非常等价于一个只写文件. 所有写入它的内容都会永远丢失. 而尝试从它那儿读取内容则什么也读不到. 然而, /dev/null对 命令行和脚本都非常的有用.
 
禁止标准 输出
cat $filename >/dev/null
# 文件内容丢失,而不会输出到标准输出.


禁止标准错误
rm $badname 2>/dev/null
 # 这样错误信息[标准错误]就被丢到太平洋去了


禁止标准输出和标准错误的输出
cat $filename 2>/dev/null >/dev/null
# 如果"$filename"不存在,将不会有任何错误信息提示.
# 如果"$filename"存在, 文件的内容不会打印到标准输出.
# 因此Therefore, 上面的代码根本不会输出任何信息.
# 当只想测试命令的退出码而不想有任何输出时非常有用。

cat $filename &>/dev/null
# 也可以, 由 Baris Cicek 指出

自动清空 日志文件的内容
(特别适合处理这些由商业Web站点发送的讨厌的"cookies"):

if [ -f ~/.netscape/cookies ] # 如果存在则删除.
then
rm -f ~/.netscape/cookies
fi
ln -s /dev/null ~/.netscape/cookies
现在所有的cookies都会丢入黑洞而不会保存在磁盘上了.

使用/dev/zero

像/dev/null一样, /dev/zero也是一个伪文件, 但它实际上产生连续不断的null的流(二进制的零流,而不是ASCII型的). 写入它的输出会丢失不见, 而从/dev/zero读出一连串的null也比较困难, 虽然这也能通过od或一个十六进制编辑器来做到. /dev/zero主要的用处是用来创建一个指定长度用于初始化的空文件,就像临时交换文件.

关于 /dev/zero 的另一个应用是为特定的目的而用零去填充一个指定大小的文件, 如挂载一个文件 系统到环回设备(loopback device)或"安全地" 删除一个文件.

 

你可能感兴趣的:(Linux 系统编程)