System V IPC分爲三種:node
#include <sys/ipc.h> /* Generates key for System V style IPC. */ key_t ftok (const char *pathname, int proj_id);
pathname 一般是跟本應用用關的目錄;proj_id指的是本應用所用到的IPC的一個序列號;成功返回IPC鍵,失敗返回-1;
使用ftok()須要注意的問題:編程
struct stat { dev_t st_dev; /* device */ ino_t st_ino; /* inode */ 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 type (if inode device) */ off_t st_size; /* total size, in bytes */ blksize_t st_blksize; /* blocksize for filesystem I/O */ blkcnt_t st_blocks; /* number of 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 */ };
獲取文件屬性的函數有以下幾個:
int stat(const char *file_name, struct stat *buf); int fstat(int filedes, struct stat *buf); int lstat(const char *file_name, struct stat *buf);
下面經過例子來看一下如何獲取:
#include <stdio.h> #include <stdlib.h> #include <sys/types.h> #include <sys/stat.h> #include <unistd.h> int main() { const char fname[] = "main.c"; struct stat stat_info; if(0 != stat(fname, &stat_info)) { perror("取得文件信息失敗!"); exit(1); } printf("文件所在設備編號:%ld\r\n", stat_info.st_dev); printf("文件所在文件系統索引:%ld\r\n", stat_info.st_ino); printf("文件的類型和存取的權限:%d\r\n", stat_info.st_mode); printf("連到該文件的硬鏈接數目:%d\r\n", stat_info.st_nlink); printf("文件全部者的用戶識別碼:%d\r\n", stat_info.st_uid); printf("文件全部者的組識別碼:%d\r\n", stat_info.st_gid); printf("裝置設備文件:%ld\r\n", stat_info.st_rdev); printf("文件大小:%ld\r\n", stat_info.st_size); printf("文件系統的I/O緩衝區大小:%ld\r\n", stat_info.st_blksize); printf("佔用文件區塊的個數(每一區塊大小爲512個字節):%ld\r\n", stat_info.st_blocks); printf("文件最近一次被存取或被執行的時間:%ld\r\n", stat_info.st_atime); printf("文件最後一次被修改的時間:%ld\r\n", stat_info.st_mtime); printf("最近一次被更改的時間:%ld\r\n", stat_info.st_ctime); return 0; }