首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> C語言 >> 關於C語言

Linux 系統程式開發備忘錄 (四):時間、系統資源、檔案操作、目錄遍歷、inotify 監控

     編輯:關於C語言

Linux 系統程式開發備忘錄 (四):時間、系統資源、檔案操作、目錄遍歷、inotify 監控

本筆記整理 Linux 系統程式開發中時間處理、系統配置查詢、檔案屬性操作、目錄遍歷、檔案系統監控等核心 API,按功能分類並附帶關鍵參數說明,適合系統工程師隨時查閱。

時間處理

#include <sys/time.h>
int gettimeofday(struct timeval *tv, struct timezone *tz); // 微秒級時間
#include <time.h>
time_t time(time_t *t);           // 秒級時間 (Epoch 至今)
char *ctime(const time_t *t);     // 轉字串 "Wed Jun 8 14:22:34 2011"
struct tm *gmtime(const time_t *t);   // UTC 時間結構
struct tm *localtime(const time_t *t); // 本地時間結構
time_t mktime(struct tm *tm);     // 結構體→time_t
size_t strftime(char *s, size_t max, const char *fmt, const struct tm *tm); // 格式化輸出
char *strptime(const char *s, const char *fmt, struct tm *tm); // 字串解析 (需 _XOPEN_SOURCE)

系統資源查詢

#include <unistd.h>
long sysconf(int name);           // 運行時系統配置 (如 _SC_PAGESIZE)
long pathconf(const char *path, int name); // 檔案系統限制 (如 _PC_PATH_MAX)
#include <sys/utsname.h>
int uname(struct utsname *buf);   // 核心/主機資訊

檔案緩衝控制

#include <stdio.h>
int setvbuf(FILE *fp, char *buf, int mode, size_t size); // _IOFBF/_IOLBF/_IONBF
void setbuf(FILE *fp, char *buf); // 簡化版
void setbuffer(FILE *fp, char *buf, size_t size); // BSD 擴展
int fflush(FILE *fp);             // 刷新緩衝
int fileno(FILE *fp);             // 取得檔案描述符
FILE *fdopen(int fd, const char *mode); // fd→FILE*

檔案屬性與操作

#include <sys/stat.h>
int stat/lstat/fstat(const char *path, struct stat *buf); // lstat 不追蹤符號連結
#include <unistd.h>
int chown/lchown/fchown(const char *path, uid_t owner, gid_t group);
int access(const char *path, int mode); // F_OK/R_OK/W_OK/X_OK
mode_t umask(mode_t mask);             // 設定權限遮罩
int link(const char *old, const char *new);     // 硬連結
int symlink(const char *target, const char *link); // 軟連結
int unlink(const char *path);              // 移除連結 (最後一個硬連結則刪檔)
int rename(const char *old, const char *new); // 重命名
int mkdir(const char *path, mode_t mode);     // 建立目錄
int rmdir(const char *path);                  // 刪除空目錄
int remove(const char *path);                 // 刪檔或空目錄

目錄遍歷

#include <dirent.h>
DIR *opendir(const char *path); // 或 fdopendir(int fd)
struct dirent *readdir(DIR *dp); // 逐項讀取
void rewinddir(DIR *dp);         // 重置至開頭
int closedir(DIR *dp);           // 關閉
int dirfd(DIR *dp);              // 取得目錄 fd
#include <unistd.h>
char *getcwd(char *buf, size_t size); // 取得當前工作目錄
int chdir(const char *path);            // 切換目錄
int fchdir(int fd);                     // 以 fd 切換
int chroot(const char *path);           // 改變根目錄 (需 root)
#include <stdlib.h>
char *realpath(const char *path, char *resolved); // 解析符號連結真實路徑
#include <libgen.h>
char *dirname(char *path);  // 取得目錄部分
char *basename(char *path); // 取得檔名部分

inotify 檔案系統監控

#include <sys/inotify.h>
int inotify_init(void);                          // 建立實例,返回 fd
int inotify_add_watch(int fd, const char *path, uint32_t mask); // 添加監控
// mask: IN_ACCESS, IN_MODIFY, IN_CREATE, IN_DELETE, IN_MOVED_FROM/TO 等
// 讀取事件:read(fd, buf, sizeof(buf)) → 解析 struct inotify_event
int inotify_rm_watch(int fd, uint32_t wd);       // 移除監控
// 注意:inotify 非遞歸,需手動遍歷子目錄添加
// 可搭配 select/poll/epoll 進行非阻塞事件循環

檔案系統掛載

#include <sys/mount.h>
int mount(const char *src, const char *target, const char *fstype,
          unsigned long flags, const void *data);
int umount(const char *target);

這些 API 構成了 Linux 系統程式與檔案系統互動的完整詞彙表,熟練組合使用即可構建健壯的系統級應用。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved