五:延时函数(分系统编程延时与内核延时)
系统编程延时——1,unsigned int sleep(unsigned int seconds); eg:sleep(1);就是延时一秒,成功则返回0,如果延时被打断则返回剩余的秒速(切记是剩余的!,2,int usleep(useconds_t usec);eg:usleep(10);就是延时10us,成功返回0 失败返回-1.
内核延时——1,void mdelay(unsigned long msecs);mdelay(10)就是延时10ms 2,void udelay(unsigned long usecs); eg:ndelay(10)就是延时10us 4,void ndelay(unigned long nsecs); eg:ndelay(10)就是延时10纳秒。
六:时间调用与转换——在Linux中,采用了多种数据结构来表示时间概念,并在此基础上实现了各自的时间管理函数。1:常用时间结构体struct tm{
int tm_sec;int tm_min;
int tm_hour; int tm_mday;
int tm_mon;int tm_year;
int tm_wday;int tm_yday;
int tm_isdst;
};可以看出都是一些时间量,tm_isdst—夏令表标识符,实行夏令时的时候其为正,不实行的时候它为0。
时间调用函数time_t time(time_t *t);其中 如果有参数就则数据传送到time_t中,如果为NULL则返回time_t类型,出错返回-1.然后弊端在于time_t类型的时间是人类无法识别的,需要用转换函数将它编程我们常用的时间。
时间转换函数1,char*ctime(const time_t *timep);将时间转化为字符串格式,2,struct tm *gmtime(const time_t *timep);将时间转化为格林威治时间,用tm结构存储。3,char asctime(const struct tm *tm);将时间转化为字符格式。4,struct tm *localtime(const time_t *colck);将时间转化为本地时间!
六:管理文件目录(Linux中目录也是文件)Linux中提供了一组获取文件元数据的函数int stat(const *path,struct stat *buf);
int fstat(int fd,struct stat*buf);
int lstat(const char *path,struct stat *buf);
其中path是文件路径,参数*buf是文件信息,fd为文件描述符。
在linux下经常用int chmod(const char*path,mode_t mode);
int fchmod(int fd, mode_t mode);这两个函数来修改权限
用char *getcwd(char *buf,size_t size);
char *getwd(cahr*buf);
char *get_current_dir_name(void);
来获取当前路径,参数*buf为保存当前目录的缓冲区,参数size:在现代linux中buf的长度至少可以为255字节,成功则返回指向当前目录的指针,错误返回NULL。
int mkdir(const char*pathname,mode_t mode);
int rmdir(constnchar*pathname);参数pathname为路径名,mode为权限(eg:777)成功返回0,错误返回-1.第一个函数用来创建文件夹,第二个函数用来删除文件夹。
目录跳转函数chdir(const cahr*path);
int fchdir(int fd);参数*path为文件路径fd为句柄,成功返回0,失败返回-1.
获取·目录内容函数 DIR *opendir(const char*name);
int closedir(DIR *dirp);
struct dirent *readdir(DIR *dirp);用这三个函数能读取目录内容,相当于ls命令的信息。
链接——1,硬链接int link(const char*oldpath,const char*newpath) 参数oldpath:已有的文件路径,newpath:新的文件路径,成功返回0,失败返回-1.
2, 软连接(又叫符号链接, symlink)int symlink(const char*oldpath,const char *newpath);成功返回0 ,失败返回-1.
3,解除链接int ulink(const char*pathname);成功返回0,失败返回-1.当指向的链接是软链接的时候则会删除软链接,不会删除目标文件,当ulink指向硬链接,如果是最后的一个链接则相当于删除了文件。
还有一个·remove函数用来完成linux下的文件移动
int rename(const char*oldpath,const char*newpath)
如果path是目录则remove调用rmdir
如果path是文件则remove调用unlink。
(就到此为止,下次写进程线程,网络编程)