嵌入式学习DAY17 --- IO进程的学习

嵌入式入门学习笔记,遇到的问题以及心得体会!
DAY17
概述:

什么是IO
什么是标准IO
什么是文件IO
文件IO和标准IO的区别
什么时候使用标准IO和文件IO
标准IO的特点
标准IO的接口

笔记:

什么是IO
凡是操作输入输出的都是IO
操作文件属于IO
在Linux/类Unix系统中,操作设备也是IO

什么是标准IO
标准IO是由ANSI C定义的一组操作IO的接口函数。是由标准C库提供。
只要安装了标准C库,就能够使用标准IO。

什么是文件IO
文件IO是类Unix操作系统提供给用户操作IO的系统调用。

系统调用
->什么是系统调用
类Unix给用户提供的访问有限计算机资源的函数接口。
->为什么需要系统调用
1.因为计算机资源是有限的
2.为了防止使用有限资源时产生的竞态或不安全操作
->哪些都是系统调用
文件IO的函数
进程线程操作
进程间通信的函数
网络编程的函数
->系统调用的过程
进程是资源分配的最小单位。资源就是内存空间(进程的地址空间)
进程地址空间的分布
在32bit的操作系统中:
最大的寻址范围:0-4G
每一个进程都会有一个4G大小的地址空间
|---------------| 4G
| kernel |
| |
|---------------| 3G
| User Space |
| |
| |
| |
| |
| |
|---------------| 0
由于操作系统和用户进程是并行执行的,所以,每个0-4G的地址空间,
都有1G的Kernel运行空间,给操作系统使用。

read读取磁盘中某一个文件的内容
1.用户态调用系统调用函数
2.触发系统调用的中断信号
3.保存用户态当前进程的执行过程
4.此时kernel拥有CPU的使用权限
5.遍历系统调用表,找到对应的内核函数
6.通过内核函数(驱动函数)操作IO设备
7.得到内核函数的执行结果
8.将kernel的执行结果拷贝到用户态
9.保存kernel的运行状态,将CPU的使用权限移交给用户态
10.用户态接着执行,得到了系统调用的结果

>Linux系统中的文件类型
	b:块设备(以扇区为操作单元的设备,即存储设备)
	c:字符设备(以字节为操作单元的设备,串口,鼠标,屏幕,键盘)
	d:文件夹
	-/f:普通文件
	l:符号链接(软链接)
	p:管道文件
	s:socket文件
	
>Linux文件系统
	文件系统是用来组织文件的存储,管理,以及给用户提供操作操作系统的接口(软件)。

	Linux中文件系统的结构

				用户
				 |
				 |
	-----------------------------
	|			VFS 			|
	-----------------------------
	|			|			|...|
	|磁盘文件系统|管道文件系统|...|
	|-----------|----------------
	|NTFS		|pip 		|
	|EXT2		|			|
	|EXT3		|			|
	|EXT4		|			|
	|-----------|-----------|
		|
		|

	具体硬件层
	-------------
	    磁盘

虚拟文件系统:
	Linux提供给用户最外层的文件系统接口,具体体现 /(根目录)

		/bin
		/usr
		/mnt
		/dev
		/etc
		/lib
		/root
		/home

标准IO和文件IO的区别
在Linux中标准IO是基于文件IO实现的
-------------------------------------------------------------------------------
特点 | 标准IO |文件IO
------------|-------------------------------|----------------------------------
库(来源) | 标准C库函数 |Linux系统调用(POSIX)
------------|-------------------------------|----------------------------------
操作对象 | 文件流指针 |文件描述符
------------|-------------------------------|----------------------------------
缓冲区 | 带缓冲机制,带缓冲区 |没有缓冲机制,不带缓冲区
------------|-------------------------------|----------------------------------
函数 | fopen,fclose … |open,close…
------------|-------------------------------|----------------------------------
可移植性 | 可移植性强 |只能在类Unix上使用
------------|-------------------------------|----------------------------------
使用范围 | 一般用于普通文件的操作 |普通文件和设备都可以操作
------------|-------------------------------|----------------------------------
POSIX:可移植操作系统规范(Portable Operating System Interface)
Unix 和 Linux都遵循这个规范

标准IO的特点
1.带缓冲区
2.因为带缓冲区,相对于系统调用开销较小
3.可移植性强
4.操作对象是文件流指针

文件流指针:
	是标准IO操作文件的对象,由fopen函数获得
	由fclose函数释放

	特殊的文件流指针:
		stdin:标准输入流
		stdout:标准输出流
		stderr: 标准错误

标准IO的函数接口:
打开文件

	fopen
			/*需要添加的头文件*/
			#include <stdio.h>
			/*
			 *函数名:fopen
			 *函数功能:流打开函数(打开一个文件流)
			 *函数参数:
			 *			const char *path:带路径的文件名
			 *			const char *mode:打开方式
			 *函数返回值:FILE *:成功返回打开的流指针,失败返回NULL,并且返回错误码
			 */
			FILE *fopen(const char *path, const char *mode);
		 The argument mode points to a string beginning with one of the  follow‐
	       ing sequences (Additional characters may follow these sequences.):

	       r      Open  text  file  for  reading.  The stream is positioned at the
	              beginning of the file.(以读方式打开文本文件,流指针定位在文件开头)

	       r+     Open for reading and writing.  The stream is positioned  at  the
	              beginning of the file.(以读写方式打开文件,流指针定位在文件开头)

	       w      Truncate  file  to  zero length or create text file for writing.
	              The stream is positioned at the beginning of the file.(以擦除方式打开文件或创建文件,该文件只能被写入,流指针定位在文件开头)

	       w+     Open for reading and writing.  The file is created  if  it  does
	              not  exist, otherwise it is truncated.  The stream is positioned
	              at the beginning of the file.(以读写方式打开或创建文件,该文件会被擦除,流指针定位在文件开头)

	       a      Open for appending (writing at end of file).  The file  is  cre‐
	              ated  if it does not exist.  The stream is positioned at the end
	              of the file.(以追加的方式在文件末尾写数据,如果文件不存在,则创建,流指针定位在文件末尾)
				  
		   a+     Open for reading and appending (writing at end  of  file).   The
	              file is created if it does not exist.  The initial file position
	              for reading is at the beginning  of  the  file,  but  output  is
	              always appended to the end of the file.(以读写追加的方式打开,文件不存在就创建,读指针在文件头,写指针在文件末尾)
		   
		   The  mode string can also include the letter 'b' either as a last char‐
	       acter or as a character between the characters in any of the  two-char‐
	       acter strings described above.  This is strictly for compatibility with
	       C89 and has no effect; the 'b' is ignored on all POSIX conforming  sys‐
	       tems,  including Linux.  (Other systems may treat text files and binary
	       files differently, and adding the 'b' may be a good idea if you do  I/O
	       to a binary file and expect that your program may be ported to non-UNIX
	       environments.)针对于非unix类操作系统,想要读取二进制文件,可以加字母b
创建文件
读取文件
	fread
	/*
	 *函数名:fread
	 *函数功能:从文件流中读出内容
	 *函数参数:
	 *		void *ptr:想要存储读出数据的内存首地址
	 *		size_t size:想要读取块所占的大小
	 *		size_t nmemb:想要读取的块的个数
	 *		FILE *stream:被读的文件流指针
	 *函数返回值:int:成功返回读成功块的个数,否则返回EOF或者0或者其他值。
	 */
	size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream);
往文件中写入数据
	fwrite
	/*
	 *函数名:fwrite
	 *函数功能:往文件流中写入内容
	 *函数参数:
	 *		void *ptr:存储想要写的数据的内存首地址
	 *		size_t size:想要写的块所占的大小
	 *		size_t nmemb:想要写的块的个数
	 *		FILE *stream:被写的文件流指针
	 *函数返回值:int:成功返回写成功块的个数,否则返回EOF或者0或者其他值。
	 */
	size_t fwrite(const void *ptr, size_t size, size_t nmemb,
                 FILE *stream);
关闭文件
	fclose
	/*需要添加的头文件*/
	#include <stdio.h>
	/*
	 *函数名:fclose
	 *函数功能:关闭一个文件流
	 *函数参数:
	 *			FILE *:想要关闭的流指针
	 *函数返回值:int:成功返回0,失败返回错误码
	 */
	int fclose(FILE *fp);
文件重定位:
	fseek
	/*
	 *函数名:fseek
	 *函数功能:
	 *函数参数:
	 *		FILE *stream:被重定位的文件流指针
	 *		long offset:是相对于基准的偏移量
	 *		int whence:基准
	 *函数返回值:key_t:成功返回0,否则返回-1或者错误码。
	 */
	int fseek(FILE *stream, long offset, int whence);
		基准值的取值:
			SEEK_SET:文件头
			SEEK_CUR:当前流指针的位置
			SEEK_END:文件末尾

		offset > 0:往文件末尾的方向移动
		offset < 0:往文件头的方向移动


	ftell
		/*
		 *函数名:ftell
		 *函数功能:
		 *函数参数:
		 *		FILE *stream:想要获取位置的文件流指针
		 *函数返回值:long:返回文件流指针的位置
		 */
		
		long ftell(FILE *stream);

注意:打开文件和关闭文件配对存在,使用完就得关闭

如何判断是否到文件末尾:
	feof()

时间处理:
linux下C获取系统时间的方法:
asctime(将时间和时间以字符串格式表示)
相关函数:
time
ctime
gmtime
localtime

/*需要包含的头文件*/
#include <time.h>
/*
 *函数名:time
 *函数功能:返回从 1970-01-01 00:00:00 +0000 (UTC).到现在的秒数。
 *		   如果参数t非空,返回值也会存入t中。
 *函数参数:
 *		time_t *t:t是一个time_t类型的指针,指向的内存空间用于存储秒数。
 *函数返回值:成功返回从1970年到现在的秒数,失败返回-1/错误码
 */
time_t time(time_t *t);

/*需要包含的头文件*/
#include <time.h>
/*
 *函数名:ctime
 *函数功能:将日期和时间转换为Broken-down时间或ASCII
 *函数参数:
 *		time_t *timep:从time函数获取到的时间
 *函数返回值:char *成功返回时间的描述,失败返回NULL
 */
char *ctime(const time_t *timep);

Broken-down  time  is  stored  in  the structure tm which is defined in
<time.h> as follows:
       struct tm {
           int tm_sec;         /* seconds */
           int tm_min;         /* minutes */
           int tm_hour;        /* hours */
           int tm_mday;        /* day of the month */
           int tm_mon;         /* month */
           int tm_year;        /* year */
           int tm_wday;        /* day of the week */
           int tm_yday;        /* day in the year */
           int tm_isdst;       /* daylight saving time */
       };

The members of the tm structure are:

   tm_sec    The number of seconds after the minute, normally in the range
             0 to 59, but can be up to 60 to allow for leap seconds.

   tm_min    The number of minutes after the hour, in the range 0 to 59.

   tm_hour   The number of hours past midnight, in the range 0 to 23.

   tm_mday   The day of the month, in the range 1 to 31.

   tm_mon    The number of months since January, in the range 0 to 11.

   tm_year   The number of years since 1900.

   tm_wday   The number of days since Sunday, in the range 0 to 6.

   tm_yday   The number of days since January 1, in the range 0 to 365.

   tm_isdst  A  flag  that  indicates  whether  daylight saving time is in
             effect at the time described.  The value is positive if  day‐
             light  saving time is in effect, zero if it is not, and nega‐
             tive if the information is not available.

例子:(打印当前时间)

	#include <stdio.h>
		#include <time.h>

		int main()
		{
		        time_t t;
		        time(&t);
		        char *p = NULL;
		        printf("%s",ctime(&t));
		        return 0;
		}

/*需要包含的头文件*/
#include <time.h>
/*
 *函数名:gmtime
 *函数功能:将time_t转换为struct tm类型
 *函数参数:
 *		const time_t *timep:time函数获取到的
 *函数返回值:
 *		struct tm *:成功返回结构体指针,失败返回NULL
 */
struct tm *gmtime(const time_t *timep);

/*需要包含的头文件*/
#include <time.h>
/*
 *函数名:localtime
 *函数功能:将time_t转换为struct tm类型
 *函数参数:
 *			const time_t *timep:time函数获取到的
 *函数返回值:
 *			struct tm *:成功返回结构体指针,失败返回NULL
 */
struct tm *localtime(const time_t *timep);

/*需要包含的头文件*/
#include <time.h>
/*
 *函数名:asctime
 *函数功能:将struct tm转换为char *类型
 *函数参数:
 *			struct tm *:gmtime获取到的结构体指针
 *函数返回值:
 *			char *:成功返回字符串形式的时间指针,失败返回NULL
 */
char *asctime(const struct tm *tm);

例子:

#include <stdio.h>
		#include <time.h>

		int main()
		{
		        time_t t;
		        time(&t);
		        printf("%s",asctime(gmtime(&t)));
		        return 0;
		}

建议使用ctime:获取的是当前时区时间/(时区由系统决定)
不建议使用asctime:获取的是格林尼治时间

文件夹操作:
增:mkdir
删:rmdir
查:readdir
打开:opendir/fdopendir
关闭:closedir

1.创建一个文件夹:
/需要添加的头文件/
#include <sys/stat.h>
#include <sys/types.h>
/*函数声明
*函数名:mkdir
*函数功能:创建一个文件夹
*函数参数:

  •  	const char *pathname:被创建的文件夹的名字(带路径)
    
  •  	mode_t mode:(被创建的文件夹的权限)(mode & ~umask & 0777).
    

*函数返回值:成功返回0,失败返回-1或者错误码
*/
int mkdir(const char *pathname, mode_t mode);

umask:文件权限掩码,002(八进制)
775 & mode

例子:
#include <stdio.h>
#include <sys/stat.h>
#include <sys/types.h>

int main(int argc, char *argv[])
{
if(argc != 2)
{
puts(“Usage:./a.out dirname.”);
return -1;
}
mkdir(argv[1], 0777);
return 0;
}

2.删除一个空文件夹:
/需要添加的头文件/
#include <unistd.h>
/*
*函数名:rmdir
*函数功能:删除一个空的文件夹
*函数参数:

  •  	const char *pathname:想要删除的文件夹的名字(带路径)
    

*函数返回值:成功返回0,失败返回-1或者错误码
*/
int rmdir(const char *pathname);

例子:

3.打开一个文件夹
/需要添加的头文件/
#include <sys/types.h>
#include <dirent.h>

/*
*函数名:opendir
*函数功能:打开一个文件夹
*函数参数:const char *name:文件夹名
函数返回值:DIR:成功返回一个存储文件夹基本信息的结构体指针(即文件夹流指针),失败返回NULL或错误码
*/
DIR *opendir(const char name);
/

*函数名:fdopendir
*函数功能:打开一个文件夹
*函数参数:int fd:文件夹的文件描述符
函数返回值:DIR:成功返回一个存储文件夹基本信息的结构体指针,失败返回NULL或错误码
*/
DIR *fdopendir(int fd);

/* Directory stream type.该格式用于守护进程维护的目录
The Hurd directory format is the same as struct dirent', soreaddir’
returns a pointer into the buffer we read directory data into. */
struct __dirstream
{
void __fd; / struct hurd_fd' pointer for descriptor. */ char *__data; /* Directory block. */ int __entry_data; /* Entry number__data’ corresponds to. */
char __ptr; / Current pointer into the block. /
int __entry_ptr; /
Entry number `__ptr’ corresponds to. /
size_t __allocation; /
Space allocated for the block. /
size_t __size; /
Total valid data in the block. /
__libc_lock_define (, __lock) /
Mutex lock for this structure. */
};

/* Directory stream type.

The miscellaneous Unix readdir' implementations read directory data into a buffer and returnstruct dirent *’ pointers into it. */

struct __dirstream
{
int fd; /* File descriptor. /
__libc_lock_define (, lock) /
Mutex lock for this structure. /
size_t allocation; /
Space allocated for the block. /
size_t size; /
Total valid data in the block. /
size_t offset; /
Current offset into the block. /
off_t filepos; /
Position of next entry to read. /
int errcode; /
Delayed error code. /
/
Directory block. We must make sure that this block starts
at an address that is aligned adequately enough to store
dirent entries. Using the alignment of “void *” is not
sufficient because dirents on 32-bit platforms can require
64-bit alignment. We use “long double” here to be consistent
with what malloc uses. */
char data[0] attribute ((aligned (alignof (long double))));
};

4.关闭文件夹
/需要添加的头文件/
#include <sys/types.h>
#include <dirent.h>
/*函数声明
*函数名:closedir
*函数功能:关闭一个文件夹
*函数返参数:DIR *dirp:想要关闭的文件夹的流指针
*函数返回值:int:成功返回0,失败返回-1或者错误码
*/
int closedir(DIR *dirp);

5.读取文件夹的内容
/需要添加的头文件/
#include <dirent.h>
/*函数声明
*函数名:readdir
*函数功能:读取文件夹的信息
*函数参数:DIR *dirp:打开后的文件夹的流指针
*函数返回值:struct dirent *:成功返回文件夹信息的指针,失败返回NULL或错误码
*/
struct dirent *readdir(DIR *dirp);

struct dirent
{
#ifdef __USE_FILE_OFFSET64
__ino64_t d_ino;
#else
__ino_t d_ino;
int __pad;
#endif
__off_t d_off;
unsigned short int d_reclen;
unsigned char d_type;
char d_name[256]; /* We must not include limits.h! */
};

struct dirent
{
ino_t d_ino; /* inode number /
off_t d_off; /
offset to the next dirent /
unsigned short d_reclen; /
length of this record /
unsigned char d_type; /
type of file; not supported by all file system types /
char d_name[256]; /
filename */
};

d_type:

DT_BLK This is a block device.
DT_CHR This is a character device.
DT_DIR This is a directory.
DT_FIFO This is a named pipe (FIFO).
DT_LNK This is a symbolic link.
DT_REG This is a regular file.
DT_SOCK This is a UNIX domain socket.
DT_UNKNOWN The file type is unknown.

例子:

#include <stdio.h>
#include <dirent.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>

int readFileList(char *path);

int main(int argc, char *argv[])
{
	if(argc != 2)
	{
		puts("Usage:./a.out dirname");
		return -1;
	}
	readFileList(argv[1]);
	return 0;
}

int readFileList(char *path)
{
	if(NULL == path)
	{
		puts("this path is no exist.");
		return -1;
	}

	DIR *dir;
	struct dirent *ptr;
	char base[1000];
	
	dir = opendir(path);
	if(NULL == dir)
	{
		puts("opendir error.");
		return -2;
	}

	while((ptr = readdir(dir)) != NULL)
	{
		if(strcmp(ptr->d_name,".") == 0 || strcmp(ptr->d_name,"..") == 0)
		{
			continue;
		}
		else if(ptr->d_type == DT_REG)
		{
			printf("普通文件:%s\n",ptr->d_name);
		}
		else if(ptr->d_type == DT_LNK)
		{
			printf("链接文件:%s\n",ptr->d_name);
		}
		else if(ptr->d_type == DT_SOCK)
		{
			printf("套接字文件:%s\n",ptr->d_name);
		}
		else if(ptr->d_type == DT_FIFO)
		{
			printf("管道文件:%s\n",ptr->d_name);
		}
		else if(ptr->d_type == DT_CHR)
		{
			printf("字符设备文件:%s\n",ptr->d_name);
		}
		else if(ptr->d_type == DT_BLK)
		{
			printf("块设备文件:%s\n",ptr->d_name);
		}
		else if(ptr->d_type == DT_UNKNOWN)
		{
			printf("未知类型文件:%s\n",ptr->d_name);
		}
		else if(ptr->d_type == DT_DIR)
		{
			printf("文件夹:%s\n",ptr->d_name);
			memset(base,0,sizeof(base));
			strcpy(base,path);
			strcat(base,"/");
			strcat(base,ptr->d_name);
			readFileList(base);
		}
	}
	closedir(dir);
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值