leonzhang

 
 

常用链接

  • 我的随笔
  • 我的评论
  • 我参与的随笔

留言簿

  • 给我留言
  • 查看公开留言
  • 查看私人留言

随笔分类

  • programming C on linux platform with API & stand C lib (rss)

随笔档案

  • 2012年5月 (4)

搜索

  •  

最新评论

  • 1. re: C/C++ on Liunx platform 第四篇 thread control
  • linux都写错了我狂汗
  • --nk_ysg
  • 2. re: C/C++ on Liunx platform 第四篇 thread control
  • 准确的说,多线程可以提高吞吐量。
    如果说提高效率,需要多CPU,或者multicore的硬件支持。
  • --leo_ zhang
  • 3. re: C/C++ on Liunx platform 第三篇 process control
  • 很好,言简意赅,再全面些会更好。
  • --javafans
  • 4. re: C/C++ on Liunx platform 第二篇 I/O
  • 基础知识,很好
  • --javafans
  • 5. re: C/C++ on Liunx platform 第一篇 基本概念和必要知识
  • 多谢捧场,交流共同进步。
  • --leon_zhang

阅读排行榜

  • 1. C/C++ on Liunx platform 第一篇 基本概念和必要知识(1512)
  • 2. C/C++ on Liunx platform 第四篇 thread control (1471)
  • 3. C/C++ on Liunx platform 第三篇 process control (1462)
  • 4. C/C++ on Liunx platform 第二篇 I/O(1348)

评论排行榜

  • 1. C/C++ on Liunx platform 第一篇 基本概念和必要知识(3)
  • 2. C/C++ on Liunx platform 第四篇 thread control (2)
  • 3. C/C++ on Liunx platform 第二篇 I/O(1)
  • 4. C/C++ on Liunx platform 第三篇 process control (1)

Powered by: 博客园
模板提供:沪江博客
C++博客 | 首页 | 发新随笔 | 发新文章 | 联系 | 聚合 | 管理

2012年5月8日

C/C++ on Liunx platform 第四篇 thread control
线程优点(对比进程):
1.开销小
2.由于同在一个进程中,各线程之间很方便的访问共享数据。
线程典型应用:异步处理机制;处理blocking IO;
线程函数定义在<pthread.h>
线程对于共享数据的同步机制:
1.显式互斥锁mutex_t
...
避免频繁创建销毁线程的开销,可使用线程池复用线程,参考boost threadpool.

<to be continue...>
posted @ 2012-05-08 22:23 leon_zhang 阅读(1471) | 评论 (2) | 编辑 收藏
 

2012年5月6日

C/C++ on Liunx platform 第三篇 process control
趁着休息这两天,把基本的C linux API和标准库的内容发布下~~,作为一个记录,也提供给大家做参考,请多提comments。

Create process API定义在unistd.h中。
函数原型:
pid_t fork(void); 
对子进程返回0,对父进程返回子进程pid.
如果父进程不处理子进程的exit status, 子进程就成为 Z process。
避免Z process:
1. 使用 pid_t waitpid(pid_t,int*,int);来阻塞等待子进程退出。
2. 父进程使用singal进行异步处理,当子进程结束时,发送SIGCHLD signal 给父进程,父进程收到SIGCHLD后再调用waitpid()。
    关于信号的定义和操作见signal.h

<to be continue...>
posted @ 2012-05-06 19:21 leon_zhang 阅读(1462) | 评论 (1) | 编辑 收藏
 
C/C++ on Liunx platform 第二篇 I/O
UNIX API提供最基本的IO函数,声明在fcntl.h unistd.h中。(fcntrl<=>fd control)
应用程序使用file descriper(文件描述符)和内核交互。
这些函数与标准库中的IO对比,称为unbuffered I/O.
UNIX中一切都是文件,磁盘上文件,socket都可以用FD来标识,也都可以使用这些函数操作I/O.
UNIX 常用IO模型(部分):
1.blocking IO
2. Non-blocking IO
3. IO 复用
在打开文件时,默认I/O方式是blocking.
函数原型:
ssize_t read(int,void*,size_t); //-1 error , 0 EOF
ssize_t write(int,void*,size_t);
read和write一般都放到一个循环里,出错或者EOF才break
在socket IO时候,如果是blocking mode,在读操作的时候,如果内核没有准备好数据,那么read函数就会阻塞,自然需要一个thread来处理。
IO复用技术可以使服务器不用为每一个客户端都分配2个thread.
< to be continue...>

posted @ 2012-05-06 13:14 leon_zhang 阅读(1348) | 评论 (1) | 编辑 收藏
 

2012年5月5日

C/C++ on Liunx platform 第一篇 基本概念和必要知识
Linux内核:其作用是管理计算机硬件;以C函数形式提供API。
C语言标准库:在API之上构建的一层应用程序库,封装了一些低级的API,为上层应用提供更高效的C函数;例如malloc,printf。标准库中的一些函数不使用API,仅是提供在用户态的一些算法,例如strcpy。
开发工具de生命力:C/C++,从计算机开天辟地发展到今天,一直都没有过时。就好象UNIX一样,极具生命力。

<to be continue ...>

posted @ 2012-05-05 21:34 leon_zhang 阅读(1512) | 评论 (3) | 编辑 收藏
 
仅列出标题