这样算计算读写速度吗?#include <stdio.h> #include <string.h> #include <sys/stat.h> #include <fcntl.h> #include <unistd.h> #include <sys/time.h> int main(int argc, char *argv[]) { char filename[] = "aFile"; // 即将创建和打开的文件名 char *bufForReadWrite = (char *)malloc(1024 * 1024 * 100); // 申请100MB空间作为读写缓冲区 size_t bufSize = sizeof(bufForReadWrite); //测量出读写缓冲区大小 int fd; //用于存放open函数返回的文件描述符 struct timeval tv_start, tv_end; // 用于存放开始和结束时间 // 打开或创建文件并写入数据 fd = open(filename, O_RDWR | O_CREAT, 0644); // 创建并打开文件,可读可写,创建权限为644 if(fd < 0) { // 创建文件失败时返回 perror("Unable to create file"); return -1; } gettimeofday(&tv_start, NULL); // 记录写入开始时间 ssize_t sizeWritten = write(fd, (void *)bufForReadWrite, bufSize); printf("Write: %ld bytes to %s\n", sizeWritten, filename); close(fd); // 写入完毕后关闭文件 // 打开文件并读取数据 fd = open(filename, O_RDWR); // 打开文件后的权限为可读可写 if(fd < 0) { //打开文件失败时返回 perror("Unable to open file"); return -1; } ssize_t sizeRead = read(fd, (void *)bufForReadWrite, bufSize); gettimeofday(&tv_end, NULL); // 记录读取结束时间 printf("Read: %ld bytes from %s, read content:%s\n", sizeRead, filename, bufForReadWrite); close(fd); // 读完毕后关闭文件 // 计算读写速度 double time = (tv_end.tv_sec - tv_start.tv_sec) * 1000.0; // 将时间转为豪秒 time += (tv_end.tv_usec - tv_start.tv_usec) / 1000.0; // 加上微秒部分的时间 double speed__write_read = (sizeWritten / (1024.0 * 1024.0)) / (time / 1000.0); // 读写速度 printf("Read speed: %.2f MB/s\n", speed_write_read); return 0; }
时间: 2024-02-06 19:03:36 浏览: 22
这段代码是用来测试磁盘读写速度的。它首先创建一个文件并写入数据,然后再读取该文件中的数据,并计算出读写速度。具体来说,代码中使用了系统调用函数open、write和read来进行文件操作,使用了结构体timeval来记录时间,使用了malloc函数动态申请内存空间。需要注意的是,代码中的变量名speed__write_read应该改为speed_write_read。
相关问题
#include<stdlib.h> #include<string.h> #include<unistd.h> #include<sys/stat.h> #include<sys/types.h> #include<fcntl.h> #include<stdio.h> int main(int argc,char *argv[]) { char buff[512] = {0}; int val = mkfifo(argv[2],0644); if(val) { perror("mkfifo"); exit(1); } int fd1 = open(argv[1],O_RDONLY); int fd2 = open(argv[2],O_WRONLY); while(1) { if(fgets(buff,512,stdin) != NULL) { char *newline = strchr(buff,'\n'); if(newline != NULL) { *newline = '\0'; } } write(fd2,buff,strlen(buff)); read(fd1,buff,sizeof(buff)); printf("client:%s\n",buff); } close(fd1); close(fd2); return 0; }#include<stdio.h> #include<stdlib.h> #include<string.h> #include<unistd.h> #include<sys/stat.h> #include<sys/types.h> #include<fcntl.h> int main(int argc,char *argv[]) { int val = mkfifo(argv[1],0644); char buff[512] = {0}; if(val) { perror("mkfifo"); exit(1); } int fd1 = open(argv[1],O_WRONLY); int fd2 = open(argv[2],O_RDONLY); while(1) { if(fgets(buff,512,stdin) != NULL) { char *newline = strchr(buff,'\n'); if(newline != NULL) { *newline = '\0'; } } write(fd1,buff,strlen(buff)+1); read(fd2,buff,sizeof(buff)); printf("server:%s\n",buff); } close(fd1); close(fd2); return 0; }使用非阻塞式 I/O 进行读写操作,或者使用多线程或多进程技术,使得读写操作可以并行进行,从而避免阻塞。把代码呈现出来
这里提供的是两个简单的命名管道通信程序,一个是服务器端,一个是客户端。它们使用mkfifo函数创建命名管道,并使用open函数打开相应的管道进行通信。代码如下:
服务器端:
```
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<unistd.h>
#include<sys/stat.h>
#include<sys/types.h>
#include<fcntl.h>
int main(int argc,char *argv[])
{
int val = mkfifo(argv[1],0644);
char buff[512] = {0};
if(val)
{
perror("mkfifo");
exit(1);
}
int fd1 = open(argv[1],O_WRONLY);
int fd2 = open(argv[2],O_RDONLY);
while(1)
{
if(fgets(buff,512,stdin) != NULL)
{
char *newline = strchr(buff,'\n');
if(newline != NULL)
{
*newline = '\0';
}
}
write(fd1,buff,strlen(buff)+1);
read(fd2,buff,sizeof(buff));
printf("server:%s\n",buff);
}
close(fd1);
close(fd2);
return 0;
}
```
客户端:
```
#include<stdlib.h>
#include<string.h>
#include<unistd.h>
#include<sys/stat.h>
#include<sys/types.h>
#include<fcntl.h>
#include<stdio.h>
int main(int argc,char *argv[])
{
char buff[512] = {0};
int val = mkfifo(argv[2],0644);
if(val)
{
perror("mkfifo");
exit(1);
}
int fd1 = open(argv[1],O_RDONLY);
int fd2 = open(argv[2],O_WRONLY);
while(1)
{
if(fgets(buff,512,stdin) != NULL)
{
char *newline = strchr(buff,'\n');
if(newline != NULL)
{
*newline = '\0';
}
}
write(fd2,buff,strlen(buff));
read(fd1,buff,sizeof(buff));
printf("client:%s\n",buff);
}
close(fd1);
close(fd2);
return 0;
}
```
这里的通信方式是阻塞式I/O,即读写操作会一直阻塞直到有数据可读或写。为了避免阻塞,可以使用非阻塞式I/O进行读写操作,或者使用多线程或多进程技术,使得读写操作可以并行进行。
例 2:命名管道通信实例 分别编写读写进程的程序 write.c 和 read.c,两个程序之一在当前目录下创建一个 命名管道“mypipe”,然后 write 向管道写数据,read 从管道读数据,两个进程可 任意顺序运行。 编写 write.c: //write.c #include<stdio.h> #include <stdlib.h> #include <sys/types.h> #include <sys/stat.h> #include <error.h> #include <fcntl.h> #include <unistd.h> #define N 256 int main(){ char buf[N]; int fd= open("./mypipe",O_WRONLY|O_CREAT,0666); if(fd!=-1) { printf("FIFO file is opened\n"); } else { perror("open failed"); exit(0); } printf("please input string\n"); scanf("%s",buf); getchar(); if ( write(fd,buf,sizeof(buf))!=-1 ) printf("write successful\n"); else perror("write failed:"); exit(EXIT_SUCCESS); } 编写 read.c: //read.c #include<stdio.h> #include <stdlib.h> #include <sys/types.h> #include <sys/stat.h> #include <error.h> #include <fcntl.h> #include <unistd.h> #define N 256 int main(){ int fd= open("./mypipe",O_RDONLY|O_CREAT,0666); char buf[N]; if(fd!=-1) { printf("FIFO file is opened\n"); } else { perror("open failed"); exit(0); } if ( read(fd,buf,N )!=-1 ) printf("I received data %s\n",buf); else perror("read error:"); exit(EXIT_SUCCESS); } 运行方式:打开 2 个终端,分别运行读写进程。 请完成以下练习与回答问题: 练习 1:改写本例,使得写进程可以不断的向管道文件写,读进程可以不断的读, 思考如何控制读写顺序。 练习 2:本例中用于管道通信的是一个普通文件,请用 mkfifo 命令或 mkfifo( )函 数创建一个标准管道文件改写本例,查看一下通过管道文件不断读写有什么不同? 问题 1:请说明匿名管道与命名管道在创建方式上有何不同?为什么说匿名管道 只能用于有亲缘关系的进程间进行通信?
练习 1:
可以在写进程和读进程中都用 while 循环,不断地读写数据。为了控制读写顺序,可以在读进程中使用 sleep() 函数来暂停一段时间,等待写进程写入数据。
练习 2:
可以使用 mkfifo() 函数创建一个标准管道文件,具体操作可以参考以下代码:
```c
mkfifo("mypipe", 0666);
```
使用标准管道文件和命名管道文件进行通信的方式是一样的,不同的只是创建方式。通过标准管道文件进行通信时,需要先手动创建管道文件,而命名管道则可以在程序中动态创建。
问题 1:
匿名管道和命名管道在创建方式上的不同在于,匿名管道需要通过 pipe() 函数创建,而命名管道需要通过 mkfifo() 函数创建。
匿名管道只能用于有亲缘关系的进程间进行通信,因为它只能在创建它的进程及其子进程之间使用。而命名管道则可以被多个进程共享使用,因为它是通过文件系统中的文件来实现的。