系统级I/O
开工日期:2018年6月9号 完成日期:2018年?月?日 耗时:?日
配置:Ubuntu 64位+gcc4.8.5
10.6
#include "csapp.h"
int main()
{
int fd1,fd2;
fd1 = open("README",O_RDONLY,0); //fd1 = 3
fd2 = open("tiny.c",O_RDONLY,0); //fd2 = 4
close(fd2); //fd2被close
fd2 = open("home.html",O_RDONLY,0); //fd2 = 4
printf("fd2 = %d\n",fd2);
exit(0);
return 0;
}
带有缓冲的I/O和不带缓冲I/O的区别好文
10.7
在tiny.tar,下载并解压,里面就有csapp.h和csapp.c。
gcc -c csapp.c
生成的csapp.o和你的源文件一起编译即可。(还要带上 -lpthread)。详细可见下方截图
#include "csapp.h"
int main()
{
int n;
rio_t rio;
char buf[MAXBUF];
rio_readinitb(&rio,STDIN_FILENO);
while( (n = rio_readlineb(&rio,buf,MAXBUF)) != 0){
rio_writen(STDOUT_FILENO,buf,n);
}
return 0;
}
10.8
#include "csapp.h"
int main(int argc,char **argv)
{
struct stat Stat;
char *type;
char *readok;
int fd1 = open(argv[1],O_RDONLY,0);
fstat(fd1,&Stat);
if(S_ISREG(Stat.st_mode))
type = "regular";
else if(S_ISDIR(Stat.st_mode))
type = "directory";
else
type = "other";
if((Stat.st_mode & S_IRUSR))
readok = "yes";
else
readok = "no";
printf("type: %s,read: %s\n",type,readok);
close(fd1);
return 0;
}
10.9