您的位置:首页 > Web前端

read-write ordinary file

2011-08-13 19:44 369 查看
read
#include <sys/time.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#include <linux/fs.h>
#include <errno.h>
#include <string.h>

int main(int argc,char* argv[])
{
if(argc!=4)
{
printf("usage:%s filename bytespertime times\n",argv[0]);
return 1;
}

char *filename=argv[1];
int bytespertime = atoi(argv[2]);
int times = atoi(argv[3]);

struct timeval tv1;
struct timeval tv2;

int fd = open(filename, O_RDWR|O_CREAT|O_TRUNC,00100);
if (fd < 0) {
perror("open file ");
return 1;
}

char *buf=(char*)malloc(bytespertime);
if(buf==NULL){
perror("malloc");
return 1;
}

memset(buf,0xfe,bytespertime);

gettimeofday(&tv1,NULL);
printf("now tv1_sec=%d,tv1_usec=%d\n",tv1.tv_sec,tv1.tv_usec);
int i;
for(i=0;i<times;i++)
{
int len = write(fd, buf, bytespertime);
if (len > 0) {
;
} else {
perror("write:");
return 1;
}
}
close(fd);

gettimeofday(&tv2,NULL);
printf("now tv2_sec=%d,tv2_usec=%d\n",tv2.tv_sec,tv2.tv_usec);

float diff = 1000000 * (tv2.tv_sec-tv1.tv_sec)+ tv2.tv_usec-tv1.tv_usec;
printf("time:%f us\n",diff);
float speed =bytespertime*times/diff;
printf("speed:%f bytes/us\n",speed);
printf("speed:%f MB/s\n",speed*1000000 /1024/1024);
free(buf);

}


write
#include <sys/time.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#include <linux/fs.h>
#include <errno.h>
#include <string.h>

int main(int argc,char* argv[])
{
if(argc!=3)
{
printf("usage:%s filename bytespertime \n",argv[0]);
return 1;
}

char *filename=argv[1];
int bytespertime = atoi(argv[2]);

struct timeval tv1;
struct timeval tv2;

int fd = open(filename, O_RDONLY);
if (fd < 0) {
perror("open file ");
return 1;
}

char *buf=(char*)malloc(bytespertime);
if(buf==NULL){
perror("malloc");
return 1;
}

memset(buf,0xfe,bytespertime);

gettimeofday(&tv1,NULL);
printf("now tv1_sec=%d,tv1_usec=%d\n",tv1.tv_sec,tv1.tv_usec);
int sum=0;
while(1)
{
int len = read(fd, buf, bytespertime);
if(len<=0)
break;
else
sum+=len;
}
close(fd);

gettimeofday(&tv2,NULL);
printf("now tv2_sec=%d,tv2_usec=%d\n",tv2.tv_sec,tv2.tv_usec);

float diff = 1000000 * (tv2.tv_sec-tv1.tv_sec)+ tv2.tv_usec-tv1.tv_usec;
printf("time:%f us\n",diff);
float speed =sum/diff;
printf("total:%d bytes\n",sum);
printf("speed:%f bytes/us\n",speed);
printf("speed:%f MB/s\n",speed*1000000 /1024/1024);
free(buf);

}


int open( const char * pathname,int flags, mode_t mode);

参数pathname 指向欲打开的文件路径字符串。下列是参数flags 所能使用的旗标:

O_RDONLY 以只读方式打开文件

O_WRONLY 以只写方式打开文件

O_RDWR 以可读写方式打开文件。上述三种旗标是互斥的,也就是不可同时使用,但可与下列的旗标利用OR(|)运算符组合。

O_CREAT 若欲打开的文件不存在则自动建立该文件。

O_EXCL 如果O_CREAT 也被设置,此指令会去检查文件是否存在。文件若不存在则建立该文件,否则将导致打开文件错误。此外,若O_CREAT与O_EXCL同时设置,并且欲打开的文件为符号连接,则会打开文件失败。

O_NOCTTY 如果欲打开的文件为终端机设备时,则不会将该终端机当成进程控制终端机。

O_TRUNC 若文件存在并且以可写的方式打开时,此旗标会令文件长度清为0,而原来存于该文件的资料也会消失。

O_APPEND 当读写文件时会从文件尾开始移动,也就是所写入的数据会以附加的方式加入到文件后面。

O_NONBLOCK 以不可阻断的方式打开文件,也就是无论有无数据读取或等待,都会立即返回进程之中。

O_NDELAY 同O_NONBLOCK。

O_SYNC 以同步的方式打开文件。

O_NOFOLLOW 如果参数pathname 所指的文件为一符号连接,则会令打开文件失败。

O_DIRECTORY 如果参数pathname 所指的文件并非为一目录,则会令打开文件失败。

此为Linux2.2以后特有的旗标,以避免一些系统安全问题。参数mode 则有下列数种组合,只有在建立新文件时才会生效,此外真正建文件时的权限会受到umask值所影响,因此该文件权限应该为(mode-umaks)。

S_IRWXU00700 权限,代表该文件所有者具有可读、可写及可执行的权限。

S_IRUSR 或S_IREAD,00400权限,代表该文件所有者具有可读取的权限。

S_IWUSR 或S_IWRITE,00200 权限,代表该文件所有者具有可写入的权限。

S_IXUSR 或S_IEXEC,00100 权限,代表该文件所有者具有可执行的权限。

S_IRWXG 00070权限,代表该文件用户组具有可读、可写及可执行的权限。

S_IRGRP 00040 权限,代表该文件用户组具有可读的权限。

S_IWGRP 00020权限,代表该文件用户组具有可写入的权限。

S_IXGRP 00010 权限,代表该文件用户组具有可执行的权限。

S_IRWXO 00007权限,代表其他用户具有可读、可写及可执行的权限。

S_IROTH 00004 权限,代表其他用户具有可读的权限

S_IWOTH 00002权限,代表其他用户具有可写入的权限。

S_IXOTH 00001 权限,代表其他用户具有可执行的权限。



                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息