makefile

.PHONY:clean all
CC=gcc
CFLAGS=-Wall -g ###replace your bin
BIN=simple_write simple_read copy_system all:$(BIN)
%.o:%.c
$(CC) $(CFLAGS) -c $< -o $@
clean:
rm -f *.o $(BIN)

simple_write.c

/*
#include <unistd.h>
ssize_t write(int fd, const void *buf, size_t count); #include <string.h>
size_t strlen(const char *s); */
/*unistd.h头文件必须放在前面,
它定义了posix有关的标志会影响到其他头文件
*/
#include <unistd.h>
#include <string.h> int main()
{
char *str="第一次测试write函数\n";
int n=strlen(str); if(write(,str,n) != n)
write(,"error\n",);
return ;
}

simple_read.c

/*
//man 3 exit
#include <unistd.h>
ssize_t read(int fd, void *buf, size_t count); #include <stdlib.h>
void exit(int status); */
#include <stdlib.h>
#include <unistd.h> int main()
{
char buf[];
int nread;
nread=read(, buf, );
if(nread == -){
write(,"error\n",sizeof("error\n"));
exit(-);
}
if(write(,buf,nread) != nread){
write(,"error\n",sizeof("error\n"));
exit(-);
}
return ;
} /*======================================+
+
[shuai@shuaiPC 3rd]$ ./simple_read +
hello world +
hello world +
[shuai@shuaiPC 3rd]$ ./simple_read +
hello world +
hello world +
[shuai@shuaiPC 3rd]$ +
+
注意 空格,制表,回车 +
管道命令 echo "hello" | ./simple_read +
========================================*/

copy_system.c

/*
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
int open(const char *pathname, int flags);
int open(const char *pathname, int flags, mode_t mode);
int creat(const char *pathname, mode_t mode); #include <unistd.h>
int close(int fd); #include <sys/ioctl.h>
int ioctl(int d, int request, ...);
*/
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
int main()
{
char c;
int in, out; in = open("file.in", O_RDONLY);/*file.in自己准备*/
out=open("file.out", O_WRONLY|O_CREAT, S_IRUSR|S_IWUSR);
while(read(in, &c, )==)
write(out,&c, ); return ;
}

主要几点需要记住

1.  unistd.h头文件必须放在前面,它定义了posix有关的标志会影响到其他头文件。

2.  对文件,管道命令的理解

3. sizeof()和strlen() 对字符串求长度有点区别

4. read() write() 对空格 制表符 回车换行的处理

2016年11月23日22:54:51

==================================================================================================

[shuai@shuaiPC 11.23]$ TIMEFORMAT="" time ./copy_system
.00user .02system :.02elapsed %CPU (0avgtext+0avgdata 1360maxresident)k
0inputs+64outputs (0major+107minor)pagefaults 0swaps
用time工具对改程序进行尸检测算。1M的测试文件,使用以上代码在旧系统中会达到数百万次系统调用。--摘自书
进行改善的话,不使用逐个字符复制的办法,扩大缓冲块。
makefile
.PHONY:clean all
CC=gcc
CFLAGS=-Wall -g ###replace your bin
BIN=copy_block copy_stdio all:$(BIN)
%.o:%.c
$(CC) $(CFLAGS) -c $< -o $@
clean:
rm -f *.o $(BIN)

copy_block.c

/*
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
int open(const char *pathname, int flags);
int open(const char *pathname, int flags, mode_t mode);
int creat(const char *pathname, mode_t mode); #include <unistd.h>
int close(int fd); #include <sys/ioctl.h>
int ioctl(int d, int request, ...);
*/
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
int main()
{
char buf[];
int in, out, nread; in = open("file.in", O_RDONLY);/*file.in自己准备*/
out=open("file.out", O_WRONLY|O_CREAT, S_IRUSR|S_IWUSR);
while( (nread=read(in, buf, sizeof(buf))) > )
write(out,buf, nread); return ;
}

使用标准C写的copy程序

copy_stdio.c

/*
#include <stdio.h>
FILE *fopen(const char *path, const char *mode); #include <stdio.h>
int fgetc(FILE *stream); #include <stdio.h>
int fputc(int c, FILE *stream); */ #include <stdio.h>
int main()
{
char c;
FILE *in, *out; in = fopen("file.in","r");
out= fopen("file.out","w");
while((c=fgetc(in))!=EOF)
{
fputc(c,out);
} return ;
}

主要几点需要记住

1. 复制程序的优化,适当调整缓冲区

2. scanf() gets()等函数的缺点,使用时考虑。

3. 还就是fopen_max的疑问

2016年11月24日23:14:03

【linux程序设计4th】第三章1的更多相关文章

  1. 《JAVASCRIPT高级程序设计》第三章

    <JAVASCRIPT高级程序设计>第三章主要讲述了这门语言的基础概念,内容多而浅,通过思维导图可以帮助我们很好的理清脉络. js函数使用function关键字来声明,以下是一个简单的例子 ...

  2. 《Java程序设计》第三章-基础语法

    20145221<Java程序设计>第三章-基础语法 总结 教材学习内容总结 类型.变量与运算符 类型 Java可区分为基本类型(Primitive Type)和类类型(Class Typ ...

  3. Linux内核分析——第三章 进程管理

    第三章 进程管理 3.1 进程 1.进程就是处于执行期的程序:进程就是正在执行的程序代码的实时结果:进程是处于执行期的程序以及相关的资源的总称:进程包括代码段和其他资源. 线程:是在进程中活动的对象. ...

  4. Linux内核分析第三章读书笔记

    第三章 进程管理 3.1 进程 进程就是处于执行期的程序 进程就是正在执行的程序代码的实时结果 线程:在进程中活动的对象.每个线程都拥有一个独立的程序计数器.进程栈和一组进程寄存器. 内核调度的对象是 ...

  5. 第二章(java程序设计)第三章(语言基础)

    第二章 2.1 对象 对象的概念是由现实世界引入问题模型: 对象包含有:状态和行为.具体地来说是: 数据封装:对象的方法的作用就是:将内部变量封装起来,提供给外界交互的窗口.(实现对数据的隐藏) 继承 ...

  6. 《windows程序设计》第三章学习心得

    第三章是基于对一个windows窗口的学习,来达到对windows程序运行机制的理解. 从语言的角度看消息机制,Windows给程序发消息的本质就是调用"窗口过程"函数. Don' ...

  7. linux读书笔记第三章

    第3章 进程管理20 3.1 进程20 进程就是处于执行期的程序(目标码存放在某种存储介质上),但进程并不仅仅局限于一段可执行程序代码.通常进程还要包含其他资源,像打开的文件,挂起的信号,内核内部数据 ...

  8. Intel汇编语言程序设计学习-第三章 汇编语言基础-下

    3.4  定义数据 3.4.1  内部数据类型 MASM定义了多种内部数据类型,每种数据类型都描述了该模型的变量和表达式的取值集合.数据类型的基本特征是以数据位的数目量的大小:8,16,32,,48, ...

  9. 《深入理解linux内核》第三章 进程

    进程的七种状态 在内核源码的 include/linux/sched.h文件中: task_struct的status可表示 #define TASK_RUNNING 0 #define TASK_I ...

随机推荐

  1. Git CMD - branch: List, create, or delete branches

    命令格式 git branch [--color[=<when>] | --no-color] [-r | -a] [--list] [-v [--abbrev=<length> ...

  2. 【转】Android 布局学习之——LinearLayout属性baselineAligned的作用及baseline

    相信大家对LinearLayout已经相当熟悉,但你们是否了解它的属性baselineAligned呢? Android官方文档是这么描述的:

  3. 利用ExpandableListView和gridview 显示可展开折叠菜单导航

    这篇随身笔带来的是结合聚合数据“菜谱大全”做的一个菜谱可折叠一级+二级列表. 先发来一些截图一睹为快吧. ExpandableListView 可用于折叠型菜单列表,其布局主要通过getGroupVi ...

  4. Java Servlet-http协议

    ---恢复内容开始--- 互联网三大基石: url:定位数据 html:显示数据 http:传输数据

  5. Entity Framework 6.1-Database First介绍

    原文:Entity Framework 6.1-Database First介绍 这种方式是比较传统的以数据库为核心的开发模式.比较适合有数据库DBA的团队.或者数据库已存在的情况. 优缺点: 1.优 ...

  6. js 验证电话号 座机及手机号

    function CheckTel() { /*验证电话号码 验证规则:区号+号码,区号以0开头,3位或4位号码由7位或8位数字组成 区号与号码之间可以无连接符,也可以“-”连接 如010888888 ...

  7. JqGrid在IE8中表头不能分组的解决办法

    修改JqGrid的js脚本: for (d = 0; d < c; d++) { if (b[d] != undefined) { //主要是添加这个判断 if (b[d].startColum ...

  8. 深入探析koa之中间件流程控制篇

    koa被认为是第二代web后端开发框架,相比于前代express而言,其最大的特色无疑就是解决了回调金字塔的问题,让异步的写法更加的简洁.在使用koa的过程中,其实一直比较好奇koa内部的实现机理.最 ...

  9. 本地安装discuz

    出处:http://jingyan.baidu.com/article/b87fe19eb57ff252183568d9.html 网站建目前都很简单,建站容易,管理难,网站做大优化更难.本人有建站经 ...

  10. 函数 swap

    1,default swap namespace std { template<typename T> void swap( T& a, T& b) { T temp(a) ...