【linux程序设计4th】第三章1
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的更多相关文章
- 《JAVASCRIPT高级程序设计》第三章
<JAVASCRIPT高级程序设计>第三章主要讲述了这门语言的基础概念,内容多而浅,通过思维导图可以帮助我们很好的理清脉络. js函数使用function关键字来声明,以下是一个简单的例子 ...
- 《Java程序设计》第三章-基础语法
20145221<Java程序设计>第三章-基础语法 总结 教材学习内容总结 类型.变量与运算符 类型 Java可区分为基本类型(Primitive Type)和类类型(Class Typ ...
- Linux内核分析——第三章 进程管理
第三章 进程管理 3.1 进程 1.进程就是处于执行期的程序:进程就是正在执行的程序代码的实时结果:进程是处于执行期的程序以及相关的资源的总称:进程包括代码段和其他资源. 线程:是在进程中活动的对象. ...
- Linux内核分析第三章读书笔记
第三章 进程管理 3.1 进程 进程就是处于执行期的程序 进程就是正在执行的程序代码的实时结果 线程:在进程中活动的对象.每个线程都拥有一个独立的程序计数器.进程栈和一组进程寄存器. 内核调度的对象是 ...
- 第二章(java程序设计)第三章(语言基础)
第二章 2.1 对象 对象的概念是由现实世界引入问题模型: 对象包含有:状态和行为.具体地来说是: 数据封装:对象的方法的作用就是:将内部变量封装起来,提供给外界交互的窗口.(实现对数据的隐藏) 继承 ...
- 《windows程序设计》第三章学习心得
第三章是基于对一个windows窗口的学习,来达到对windows程序运行机制的理解. 从语言的角度看消息机制,Windows给程序发消息的本质就是调用"窗口过程"函数. Don' ...
- linux读书笔记第三章
第3章 进程管理20 3.1 进程20 进程就是处于执行期的程序(目标码存放在某种存储介质上),但进程并不仅仅局限于一段可执行程序代码.通常进程还要包含其他资源,像打开的文件,挂起的信号,内核内部数据 ...
- Intel汇编语言程序设计学习-第三章 汇编语言基础-下
3.4 定义数据 3.4.1 内部数据类型 MASM定义了多种内部数据类型,每种数据类型都描述了该模型的变量和表达式的取值集合.数据类型的基本特征是以数据位的数目量的大小:8,16,32,,48, ...
- 《深入理解linux内核》第三章 进程
进程的七种状态 在内核源码的 include/linux/sched.h文件中: task_struct的status可表示 #define TASK_RUNNING 0 #define TASK_I ...
随机推荐
- javascript-函数进阶
一.函数定义 1.函数声明 function add(i,j){ return i+j; } 特点:1.函数声明定义函数会被前置.要知道在js代码执行时,会有一个预解析,预解析时会把变量声明.函数声明 ...
- Ajax返回xml类型数据
ajax可以返回文本类型数据和xml类型数据,xml是计算机通用语言 可以使用js解析返回xml类型数据的dom对象 前端页面 <!doctype html> <html lang= ...
- Entity Framework 6.1-Database First介绍
原文:Entity Framework 6.1-Database First介绍 这种方式是比较传统的以数据库为核心的开发模式.比较适合有数据库DBA的团队.或者数据库已存在的情况. 优缺点: 1.优 ...
- iMAC——查看开机关机时间
每次下班都记不好早上几点打的卡,你是不是也经常有这样的情况: 那就用以下的代码考到Mac电脑的终端中,回车: mac终端输入上面命令行 查看开机时间: last | grep reboot 查看关机 ...
- 【leetcode】5. Longest Palindromic Substring
题目描述: Given a string S, find the longest palindromic substring in S. You may assume that the maximum ...
- C++与Lua交互(一)
引言 之前做手游项目时,客户端用lua做脚本,基本所有游戏逻辑都用它完成,玩起来有点不爽,感觉"太重"了.而我又比较偏服务端这边(仅有C++),所以热情不高.最近,加入了一个端游项 ...
- Ubuntu环境变量——添加与删除
转自:http://beanocean.diandian.com/post/2013-11-09/40060047963 注: 1.作者的系统是Ubuntu 13.10,在其他linux发行版中环境变 ...
- Ueditor设置默认字体
其实很简单,只需要将ueditor.all.js 以及 ueditor.all.min.js 两个文件中的字体改掉即可 修改方法: 在ueditor.all.js中搜索:设置默认字体和字号: 在ued ...
- 在Mac OS X中使用VIM开发STM32(4)
本文原创于http://www.cnblogs.com/humaoxiao,非法转载者请自重! 在上三篇文章中,我们基本搭建好了开发STM32的IDE环境,当然vim.ctags.tagl ...
- <c:forEach>用法
<c:forEach items="${xqsCheckDataList}" var="ch"> <c:if test="${ch. ...