unix/Linux低级IO函数的用法

系统 Linux
unix/Linux 低级IO函数的用法,必须掌握写module ,必要的应用编程知识是应该要有的, 否则 ,怎么写应用程序来测试你的module或者driver呢?

unix/Linux 低级IO函数的用法,必须掌握写module ,必要的应用编程知识是应该要有的, 否则 ,怎么写应用程序来测试你的module或者driver呢?

而且最主要的是 , 了解上层调用的接口, 你就明白了上层的开发者需要什么, 自己才好实现什么。

把手里的看书的事情都放下, 先把 Unix环境高级编程这本书 的第三章 彻底搞熟它。

内容包括:

open() ,尤其是各种常见的参数,到底是什么意思, 比如常用创建一个空文件: fd = open("/tmp/xx.txt",O_RDWR | O_CREAT | O_TRUNC);

读一个文件,fd = open("/dev/hello",O_RDONLY );

如果就是要写一个既有的文件,fd = open("/dev/hello",O_WRONLY );

具体的参数: man 2 open

read(),

write()

ioctl() ,这个最重要。

lseek() , 文件指针移动

#p#

比如下面的例子:

就是简单的读一个文件:

#include

#include

#include

#include

int main(int argc,char **argv)

{

int fd = 0;

int pid = 0;

char buffer[20] = {'\0'};

char *read_buffer[20] = {'\0'};

//fd = open("/dev/hello",O_RDWR | O_CREAT | O_TRUNC);

fd = open("/dev/hello",O_RDONLY ); //| O_NONBLOCK);

printf("fd=%d\n",fd);

if(fd < 0) {

perror("/dev/hello");

return -1;

}

read(fd,read_buffer,sizeof(read_buffer)-1);

printf("read_buffer=%s\n",read_buffer);

close(fd);

return 0;

}

下面这个就是简单的写一个文件 ,

#include

#include

#include

#include

int main(int argc,char **argv)

{

int fd = 0;

int pid = 0;

char buffer[20] = {'\0'};

char write_buffer[20] = {'\0'};

strcpy(write_buffer,"zhanglinbao");

fd = open("/dev/hello",O_RDWR | O_CREAT | O_TRUNC);

//fd = open("/dev/hello",O_RDONLY);

printf("fd=%d\n",fd);

if(fd < 0) {

perror("/dev/hello");

return -1;

}

write(fd,write_buffer,sizeof(write_buffer)-1);

close(fd);

return 0;

}

【编辑推荐】

  1. 如何优化Linux服务器硬盘性能实用技巧
  2. 技巧:安装linux后的内核调优
  3. 有备无患Linux操作系统的备份方法介绍
责任编辑:赵宁宁 来源: chinaitlab
相关推荐

2017-01-17 14:21:27

LinuxIO模型Unix

2012-02-22 21:54:57

UnixLinuxCron

2010-04-20 10:27:23

Unix操作系统

2011-09-05 17:44:49

LinuxUnix

2009-12-03 10:12:24

LinuxUnix

2010-05-11 11:29:11

Unix awk

2021-06-21 11:11:29

LinuxIO磁盘IO

2011-07-12 15:21:51

POSIX规范Linux

2009-09-29 10:45:17

UnixLinuxshell

2010-10-25 16:52:48

oracle管道函数

2010-10-25 14:28:53

oracle trun

2011-07-14 09:17:01

Unix数据中心

2010-05-11 13:16:21

Unix awk

2010-03-08 09:27:55

Linux Unix区

2014-02-26 09:13:39

2013-07-23 17:26:07

2020-12-07 10:30:39

LinuxUnix物联网

2012-05-22 15:37:10

2020-05-12 16:58:05

LinuxUnix技术

2010-04-27 12:51:49

Oracle 函数de
点赞
收藏

51CTO技术栈公众号