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. Sublime Text 插件之常用20个插件

    作为一个开发者你不可能没听说过 Sublime Text.不过你没听说过也没关系,下面让你明白. Sublime Text是一款非常精巧的文本编辑器,适合编写代码.做笔记.写文章.它用户界面十分整洁, ...

  2. Web.config加密和解密

    在系统部署的时候,大家都会遇到关于用户凭证的安全性问题,而对于数据库连接的相关的信息,有些时候客户也需要我们对其加密,防止信息泄露,在此将加密和解的方法记录于此: 首先用管理员的权限启动cmd命令窗口 ...

  3. jQuery选择器容易忽视的小知识大问题

    1 关于检查某个元素在网页上的存在 很多人会惯性的写成  } 其实应该根据获取到元素的长度来判断 if($("#tt").length>0){ //do something博 ...

  4. HTTP - Cookie 机制

    HTTP 是种无状态的协议,即使用 HTTP 协议时,每次发送请求都会产生对应的新响应,协议本身不会保留之前一切的请求或响应报文的信息.这是为了更快地处理大量事务,确保协议的可伸缩性,而特意把 HTT ...

  5. 第四十四篇、iOS开发中git添加.gitignore文件

    .gitignore文件可以直接使用https://github.com/github/gitignore 1.在项目中设置忽略文件(1)将从github上荡下来的对应的.gitignore文件(Sw ...

  6. 第五篇、Uber用视频播放做启动动画

    import UIKit import AVFoundation class GuidePage: FxBasePage { @IBOutlet var backImageView:UIImageVi ...

  7. Objective-C 【内存管理&手动内存管理 综述】

    ------------------------------------------- 内存管理 (1)Objective-C的内存管理 栈区    存放局部变量(由于基本数据类型占用的存储空间是固定 ...

  8. NodeJS V8 GC概览

    [A tour of V8: Garbage Collection] http://jayconrod.com/posts/55/a-tour-of-v8-garbage-collection 基本是 ...

  9. 【译】 Node.js v0.12的新特性 -- 性能优化

    原文: https://strongloop.com/strongblog/performance-node-js-v-0-12-whats-new/ January 21, 2014/in Comm ...

  10. 前端面试题和setTimeout异步

    var len=4; while(len--){ setTimeout(function(){ alert(len); },0); alert(len); } 这个题目的答案我先说出来,读者请仔细考虑 ...