1.使用create創建文件:python
#include <stdio.h> #include <stdlib.h> #include <fcntl.h> #include <unistd.h> int main() { int res = creat("./file.txt", 0700); if (res == -1) { perror("Create File Error!"); } else { printf("Create OK!\n"); } return 0; }
2.從輸入到輸出:優化
// 從stdin到sdtout #include <stdlib.h> #include <stdio.h> int main (int argc, char *argv[]) { int c; while ((c = getchar()) != EOF) { putchar(c); } return 0; }
3.簡單實現who命令:spa
#include <stdio.h> #include <stdlib.h> #include <fcntl.h> #include <utmp.h> #include <unistd.h> #include <time.h> #define SHOWHOST void show_info(struct utmp* user_record); void login_time(long *num_time); void show_info(struct utmp* user_record) { if (user_record->ut_type != USER_PROCESS) { return ; } printf("-------------------\n"); printf("User Name is: %10s\n", user_record->ut_name); printf("User CMD is: %10s\n", user_record->ut_line); login_time(&(user_record->ut_time)); #ifdef SHOWHOST printf("User Host is: %s\n", user_record->ut_host); #endif printf("\n"); } void login_time(long *num_time) { char *timestr; timestr = ctime(num_time); printf("User login time is: %30s\n", timestr); } int main() { struct utmp current_record; int record_len = sizeof(current_record); int user_info_fd = -1; // user info in the utmp if ((user_info_fd = open(UTMP_FILE, O_RDONLY)) == -1) { perror("read file error!"); exit(1); } while (record_len == (read(user_info_fd, ¤t_record, record_len))) { show_info(¤t_record); } close(user_info_fd); return 0; }
4.簡單實現cp命令:code
#include <stdio.h> #include <stdlib.h> #include <fcntl.h> #include <unistd.h> #define BUFFERSIZE 4096 #define FILEMODE 0700 // 111 000 000 User all per void opps(char *file, char* argv); void opps(char *file, char* argv) { fprintf(stderr, "Error : %s", file); perror(argv); exit(1); } int main(int argc, char* argv[]) { int in_fd = -1; int out_fd = -1; int n_chars = -1; char buf[BUFFERSIZE]; if (argc != 3) { fprintf(stderr, "Usage: cp source dest\n"); exit(1); } if ((in_fd = open(argv[1], O_RDONLY)) == -1) { opps("Open First File Error", argv[1]); } if ((out_fd = creat(argv[2], FILEMODE)) == -1) { opps("Creat Second File Error", argv[2]); } while ((n_chars = read(in_fd, buf, BUFFERSIZE)) > 0) { if (n_chars != write(out_fd, buf, n_chars)) { opps("Write Error!", argv[2]); } } if (-1 == n_chars) { opps("Read Error!", argv[1]); } if (close(in_fd) == -1 || close(out_fd)) { opps("Close Error!", " "); } return 0; }
5.使用不一樣緩衝區的cp實驗:blog
使用python獲得5M多的一個文件進程
#!/usr/bin/env python #-*- coding:utf-8 -*- fd = open("./data", "w+"); for i in xrange(500000): fd.writelines("hello world!") fd.close()
分別使用1 4026 20000作爲buf的cp實驗:utf-8
使用緩衝區的利弊:get
利:
1. 提升磁盤I/O效率
2. 優化磁盤的寫操做
利弊:
若是不及時寫入磁盤,會致使數據丟失。string
能夠使用sync 將緩衝區數據寫入磁盤 經過
man sync 來查看詳細說明it
6.一個進程屢次打開一個文件:
#include <stdio.h> #include <stdlib.h> #include <fcntl.h> #include <unistd.h> #include <string.h> int main() { int read_fd = open("./data", O_RDONLY); int write_fd = open("./data", O_WRONLY); int read_again_fd = open("./data", O_RDWR); char buf[1024]; read(read_fd, buf, 1024); buf[strlen(buf) - 1] = '\0'; puts(buf); close(read_fd); char str[100] = "testing 123..."; write(write_fd, str, strlen(str)); close(write_fd); buf[0] = '\0'; read(read_again_fd, buf, 1024); buf[strlen(buf) - 1] = '\0'; puts(buf); close(read_again_fd); return 0; }
每次都從最開始讀取。