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. Android之Android studio安装

    首先下载jdk:http://www.oracle.com/technetwork/java/javase/downloads/index.html 然后下载Android SDK:http://ww ...

  2. Math.random获得随机数

    function GetRandomNum(Min,Max){           var Range = Max - Min;           var Rand = Math.random(); ...

  3. union on

    UNION 操作符用于合并两个或多个 SELECT 语句的结果集. 请注意,UNION 内部的 SELECT 语句必须拥有相同数量的列.列也必须拥有相似的数据类型.同时,每条 SELECT 语句中的列 ...

  4. 关于Class.forName("oracle.jdbc.driver.OracleDriver");报ClassNotFoundException 的异常

    关于try { Class.forName("oracle.jdbc.driver.OracleDriver"); }catch(ClassNotFoundException e) ...

  5. C#保存上传来的图片示例代码

    保存上传图片的方法有很多,在接下来的文章中为大家详细介绍下使用C#是如何做到的,感兴趣的朋友不要错过 复制代码代码如下: [HttpPost]  public string UploadImage() ...

  6. .net求两个数的最大公约数和最小公倍数

    最大公约数:指两个或多个整数共有约束中最大的一个. 最小公倍数:如果有一个自然数a能被自然数b整除,则称a为b的倍数,b为a的约数,对于两个整数来说,指该两数共有倍数中最小的一个. /// <s ...

  7. HDU(搜索专题) 1000 N皇后问题(深度优先搜索DFS)解题报告

    前几天一直在忙一些事情,所以一直没来得及开始这个搜索专题的训练,今天做了下这个专题的第一题,皇后问题在我没有开始接受Axie的算法低强度训练前,就早有耳闻了,但一直不知道是什么类型的题目,今天一看,原 ...

  8. ms mpi error: unable to allocate launching block

    问题描述: 在VS 2015中使用Microsoft MPI(ms mpi)构建控制台应用,使用" mpiexec -n 4 myprog.exe"运行时退出并提示"un ...

  9. 动态磁盘恢复为基本磁盘--DiskGenius

    近日在老电脑中安装了Win8.1,想不到使用起来比Win7还流畅. 周末,手贱,由于C盘只有10GB,为主分区,D盘有40GB,为扩展分区,想要将C.D两个分区合二为一,在Win8.1的磁盘管理器中, ...

  10. ios 一个app启动另一个app

    问题描述:需要从一个ios应用程序中,能启动另一个ios应用程序. 开发环境:xcode7.3.1 关键词:白名单(LSApplicationQueriesSchemes).注册自己的URL Demo ...