Problem 21. how to use readv() and writev() in linux platform?

Ans:

readv()and writev()称为scattered or gathered IO, 俗称向量化IO,一次可以读写多个数据块,与其他的称为线性IO不同。

#include <sys/uio.h>

ssize_t readv (int fd,

const struct iovec *iov,

int count);

ssize_t writev (int fd,

const struct iovec *iov,

int count);

struct iovec {

void *iov_base;/* pointer to start of buffer */

size_t iov_len;/* size of buffer in bytes */

};

1. writev( ) example

#include  <stdio.h>

#include  <sys/types.h>

#include  <sys/stat.h>

#include  <fcntl.h>

#include  <string.h>

#include  <sys/uio.h>

int main ( )

{

struct iovec iov[3];

ssize_t nr;

int fd, i;

char *buf[] = {

"The term buccaneer comes from the word boucan./n",

"A boucan is a wooden frame used for cooking meat./n",

"Buccaneer is the West Indies name for a pirate./n" };

fd = open ("buccaneer.txt", O_WRONLY | O_CREAT | O_TRUNC);

if (fd == -1) {

perror ("open");

return 1;

}

/* fill out three iovec structures */

for (i = 0; i < 3; i++) {

iov[i].iov_base = buf[i];

iov[i].iov_len = strlen (buf[i]);

}

/* with a single call, write them all out */

nr = writev (fd, iov, 3);

if (nr == -1) {

perror ("writev");

return 1;

}

printf ("wrote %d bytes/n", nr);

if (close (fd)) {

perror ("close");

return 1;

}

return 0;

}

2. readv( ) example

#include  <stdio.h>

#include  <sys/types.h>

#include  <sys/stat.h>

#include  <fcntl.h>

#include  <sys/uio.h>

int main ( )

{

char foo[48], bar[51], baz[49];

struct iovec iov[3];

ssize_t nr;

int fd, i;

fd = open ("buccaneer.txt", O_RDONLY);

if (fd == -1) {

perror ("open");

return 1;

}

/* set up our iovec structures */

iov[0].iov_base = foo;

iov[0].iov_len = sizeof (foo);

iov[1].iov_base = bar;

iov[1].iov_len = sizeof (bar);

iov[2].iov_base = baz;

iov[2].iov_len = sizeof (baz);

/* read into the structures with a single call */

nr = readv (fd, iov, 3);

if (nr == -1) {

perror ("readv");

return 1;

}

for (i = 0; i < 3; i++)

printf ("%d: %s", i, (char *) iov[i].iov_base);

if (close (fd)) {

perror ("close");

return 1;

}

return 0;

}

PS:

Linux implements readv( ) and writev( ) as system calls, and internally performs scatter/gather I/O. In fact, all I/O inside the Linux kernel is vectored; read( ) and write( ) are implemented as vectored I/O with a vector of only one segment.一次同时读取数据块的数量以8个或少于8个时性能最佳。

Problem 22. how to use mmap()?

Ans:

#include  <stdio.h>

#include  <sys/types.h>

#include  <sys/stat.h>

#include  <fcntl.h>

#include  <unistd.h>

#include  <sys/mman.h>

int main (int argc, char *argv[])

{

struct stat sb;

off_t len;

char *p;

int fd;

if (argc < 2) {

fprintf (stderr, "usage: %s <file>/n", argv[0]);

return 1;

}

fd = open (argv[1], O_RDONLY);

if (fd == -1) {

perror ("open");

return 1;

}

if (fstat (fd, &sb) == -1) {

perror ("fstat");

return 1;

}

if (!S_ISREG (sb.st_mode)) {

fprintf (stderr, "%s is not a file/n", argv[1]);

return 1;

}

p = mmap (0, sb.st_size, PROT_READ, MAP_SHARED, fd, 0);

if (p == MAP_FAILED) {

perror ("mmap");

return 1;

}

if (close (fd) == -1) {

perror ("close");

return 1;

}

for (len = 0; len < sb.st_size; len++)

putchar (p[len]);

if (munmap (p, sb.st_size) == -1) {

perror ("munmap");

return 1;

}

return 0;

}

PS:映射的地址空间以页边界对齐的,所以此种方法适用于大文件的映射。

Problem 23. How to realize thread-based asynchronous I/O?

Ans:

First, let’s look at why an application developer would want asynchronous I/O:

• To perform I/O without blocking(性能方面的考量)

• To separate the acts of queuing I/O, submitting I/O to the kernel, and receiving

notification of operation completion(不同的实现方式)

开发步骤:

1. Create a pool of “worker threads” to handle all I/O.(创建工作者线程)

2. Implement a set of interfaces for placing I/O operations onto awork queue.

3. Have each of these interfaces return an I/O descriptor uniquely identifying the

associated I/O operation. In each worker thread, grab I/O requests from the

head of the queue and submit them, waiting for their completion.

4. Upon completion, place the results of the operation (return values, error codes,

any read data) onto a results queue.

5. Implement a set of interfaces for retrieving status information from the results

queue, using the originally returned I/O descriptors to identify each operation.

Problem 24 some information about vfork() and atexit()?

vfork( ) users should call _exit( ), and notexit( ), after a fork.

atexit( ):

#include <stdlib.h>

int atexit (void (*function)(void));

PS:

A successful invocation of atexit( ) registers the given function to run during nor-

mal process termination; i.e., when a process is terminated via either exit( ) or areturn from main( ). If a process invokes an exec function, the list of registered functions is cleared (as the functions no longer exist in the new process’ address space). Ifa process terminates via a signal, the registered functions are not called.

Registered functions must not call exit( ), lest they begin an endless recursion

#include <stdio.h>

#include <stdlib.h>

void out (void)

{

printf ("atexit( ) succeeded!/n");

}

int main (void)

{

if (atexit (out))

fprintf(stderr, "atexit( ) failed!/n");

return 0;

}

on_exit( ):

#include <stdlib.h>

int on_exit (void (*function)(int , void *), void *arg);

Problem 25 how to check a null string or empty string in C?

Ans:

char *str;

//判断字符串非空或长度不为零

if(str == NULL || str[0] == ‘/0’)

{

return BAD_PARAMETER

}

转载于:https://my.oschina.net/fuyajun1983cn/blog/263870

问题集锦(21-25)相关推荐

  1. 高品质免费字体集锦:25款英文艺术字体下载

    今天分享字体集合特别分享给平面设计师.在这篇文章中,你可以找到25款新鲜出炉的免费英文艺术字体.在之前,我发表了众多高品质字体相关的文章,有手写字体.空心字体.火焰字体.简历字体.涂鸦字体.节日字体. ...

  2. 考研数学笔记 21~25

    考研数学笔记 21~25

  3. sql-labs 闯关 21~25

    sql-labs 闯关 21~25 友善爱国法治诚信富强友善公正爱国和谐友善爱国爱国友善自由民主爱国友善公正 复习笔记1 内容: sql-labs第21关(Cookie注入-base64编码-单引号和 ...

  4. Python3算法基础练习:编程100例( 21~ 25)

    往期练习: Python3算法基础练习:编程100例(1~5) Python3算法基础练习:编程100例(6 ~ 10) Python3算法基础练习:编程100例(11 ~ 15) Python3算法 ...

  5. javascri基础知识21~25

    21.JavaScript 数组迭代方法 数组迭代方法对每个数组项进行操作. Array.forEach() forEach() 方法为每个数组元素调用一次函数(回调函数). 实例 var txt = ...

  6. Python 小型项目大全 21~25

    二十一.DNA 可视化 原文:http://inventwithpython.com/bigbookpython/project21.html 脱氧核糖核酸是一种微小的分子,存在于我们身体的每个细胞中 ...

  7. 线程 sleep 取消_Java面试集锦:25道线程类相关面试题与答案(下)

    26. 并发编程三要素? 1)原子性 原子性指的是一个或者多个操作,要么全部执行并且在执行的过程中不被其他操作打断,要么就全部都不执行. 2)可见性 可见性指多个线程操作一个共享变量时,其中一个线程对 ...

  8. 算法手撕代码21~25

    深度学习/机器视觉/数字IC/FPGA/算法手撕代码目录总汇 目录 1.从上往下打印二叉树 2.二叉树的下一个节点 3.二进制中1的个数

  9. 蓝桥杯 算法训练 - 连续正整数的和 78这个数可以表示为连续正整数的和,1+2+3,18+19+20+21,25+26+27。   输入一个正整数 n(<=10000)   输出 m 行(n有m

    问题描述 78这个数可以表示为连续正整数的和,1+2+3,18+19+20+21,25+26+27. 输入一个正整数 n(<=10000) 输出 m 行(n有m种表示法),每行是两个正整数a,b ...

  10. 横竖三个数的和相等_把21、23、25、27、29五个数使横竖的三个数和都相等

    展开全部 竖行应该是21,25,29, 横行应该是23,25,27 23+25 +27=21+25+29 x1 x2 x3 y1 y2 y3 z1 z2 z3 x1+x2+x3=x1+y1+z1 简化 ...

最新文章

  1. 经典排序算法 - 鸽巢排序Pigeonhole sort
  2. 【PC工具】更新图片批量处理工具Image Tuner,绿色免安装工具软件
  3. BZOJ3091: 城市旅行
  4. 以大多数人的努力程度之低,根本轮不到去拼天赋
  5. 【Poj1017】Packets
  6. oracle写转帐的存储过程,Oracle存储过程及块编程基础经典案例
  7. c++ 返回数组中最大的值_Swift语言必学秘技:数组里面的使用方法
  8. MySQL Spatial Extensions 地理信息
  9. 选择排序详解(Java实现)
  10. modalTransitionStyle各种present效果
  11. j3455跑mysql_看烦了千篇一律的J3455?让黑群晖显示真实的CPU信息
  12. Ubuntu安装搜狗拼音输入法(中文输入法)
  13. html影院选座模板,html影院前台模板
  14. python合法整型常量要符合什么条件_1.下列字符列中,合法的长整型常量是: 【 】...
  15. 机器学习之置信区间上界算法
  16. Capture One 21 Pro v14.3.0.185 飞思顶级图像后期处理编辑软件
  17. [官方Flink入门笔记 ] 五、客户端操作
  18. JS逆向——破解百度翻译参数(sign)爬虫 超级详细
  19. 游戏开发杂记(一) 万事开头难
  20. SVG之Animation

热门文章

  1. 关于maven工程中一直报和依赖包json-lib-2.4-jdk15.jar相关错误的问题解决方法
  2. 国办支持乡村医生建设 医疗信息化提速
  3. PHP设计模式:观察者模式
  4. 微信公众平台消息接口开发(13)多语种互译
  5. 职场中不可深交的五种人
  6. linux openssl libcurl库 交叉编译
  7. golang 数据库null值错误 解决方法
  8. mysql top 语句简介
  9. linux 路由跟踪表 nf_conntrack 数据结构 参数 简介
  10. linux 路由跟踪表满错误 nf_conntrack: table full, dropping packet 原理解决方法