当前位置:网站首页>readv & writev
readv & writev
2022-06-26 01:49:00 【Chen_ Hulk】
readv and writev The feature is that it allows a single system call to read in or write out One or more buffers . These operations are called Spread reading and Centralized writing .
Because the input data from the read operation is scattered into multiple application buffers , The output data from multiple application buffers is provided to a single write operation .
ssize_t readv(int filedes, const struct iovec *iov, int iovcnt);
ssize_t writev(int filedes, const struct iovec *iov, int iovcnt);
The second argument to these two functions points to iovec A pointer to an array of structures ,iov It's an array of structures , Each of its elements indicates a buffer in memory :
struct iovec{
void *iov_base; //starting address of buffer
size_t iov_len; //size of buffer
}
writev In order iov[0],iov[1] to iov[iovcnt-1] Aggregate output data from buffer .
readv Then the read data is scattered into the buffer in the same order as above ,readv Always fill a buffer first , Then fill in the next .
With these two functions , When you want to write a linked list together , Just let iov Each element of the array contains the address and length of each item in the linked list , And then iov And the number of its elements are passed as parameters to writev(), These data can be written at once .
Function implementation :
experience Dispersive readout and Centralized write The meaning of :
#include <stdio.h>
#include <sys/uio.h>
#include <fcntl.h>
#define IOVENT 3
#define PER_IOVENT 5
int main(void)
{
char buf1[PER_IOVENT],buf2[PER_IOVENT],buf3[PER_IOVENT];
struct iovec iov[IOVENT];// Distribute 3 Structures are stored together abcdefghijklnmo
iov[0].iov_base = buf1;
iov[0].iov_len = PER_IOVENT;
iov[1].iov_base = buf2;
iov[1].iov_len = PER_IOVENT;
iov[2].iov_base = buf3;
iov[2].iov_len = PER_IOVENT;
int fd = open("a.txt",O_RDWR);
if(fd < 0){
perror("open");
return -1;
}
int rsize = readv(fd, iov, IOVENT);//a.txt The content of Dispersive readout Into three structures
printf("buf1 = %s\n",buf1);
printf("buf2 = %s\n",buf2);
printf("buf3 = %s\n",buf3);
close(fd);
fd = open("b.txt", O_RDWR|O_CREAT, S_IRUSR|S_IWUSR);
if(fd < 0){
perror("open");
return -1;
}
int wsize = writev(fd,iov,IOVENT);// The contents of the three structures Centralized write b.txt in
close(fd);
return 0;
}
#if 0
Function execution :
[email protected]:~/study/network$ ./a.out
buf1 = abcdefghijklnmo
buf2 = fghijklnmo
buf3 = klnmo
[email protected]:~/study/network$ cat a.txt
abcdefghijklnmo
[email protected]:~/study/network$ cat b.txt
[email protected]:~/study/network$
[email protected]:~/study/network$
#endif
边栏推荐
- CyCa children's physical etiquette Yueqing City training results assessment successfully concluded
- biggan:large scale gan training for high fidelity natural image synthesis
- Loss function of depth model
- Obtain WiFi password through computer (only connected WiFi)
- Pixel6 unlock bootloader
- Distributed systems (II) understanding of distributed transactions
- Sweet girl lisixia was invited to be the little host of the global finals of the sixth season perfect child model
- 蒟蒻初学单片机的一丢丢笔记
- leetcode 300. Longest increasing subsequence (medium)
- Embedded c learning notes
猜你喜欢

"Hot post" Statistics

Sunshine boy chenhaotian was invited to be the spokesperson for the global finals of the sixth season perfect children's model

--SQL of urban cultivation manual -- Chapter 1 basic review

28. contour discovery

CyCa children's physical etiquette Yueqing City training results assessment successfully concluded

Installing MySQL databases in FreeBSD

biggan:large scale gan training for high fidelity natural image synthesis

如何为政企移动办公加上一道“安全锁”?

biggan:large scale gan training for high fidelity natural image synthesis

Embedded c learning notes
随机推荐
Data arrangement of machinetranslation
biggan:large scale gan training for high fidelity natural image synthesis
代码覆盖率测试(一)
弹性蛋白酶的用途和化学性质
王老吉药业“关爱烈日下最可爱的人”公益活动在杭启动
25. histogram comparison
2021-1-15 摸魚做的筆記Ctrl+c /v來的
Operation of simulated examination platform for electrical test questions in 2022
Show spirit chenzitong was invited to be the chief experience officer of the global finals of the sixth season perfect children's model
CyCa children's physical etiquette Yueqing City training results assessment successfully concluded
PTA class a simulated sixth bomb: 1156-1159
The answer skills and examples of practical cases of the second construction company are full of essence
Eight principles of element positioning
Principle of voice wake-up
22. pixel remapping
Embedded c learning notes
Pixel6 unlock bootloader
Fasttext knowledge points summary
输入3个整数,从大到小输出出来
leetcode 300. Longest increasing subsequence (medium)