当前位置:网站首页>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
边栏推荐
- Summary of knowledge points of catboost
- Cross validation -- a story that cannot be explained clearly
- Viwi interface
- PTA class a simulated fifth bomb: 1148-1151
- 正则表达式
- Procédure de désinstallation complète de la base de données Oracle (pas de capture d'écran)
- One stop solution EMQ for hundreds of millions of communication of Internet of things
- GUN make (2) 总述
- Web Testing
- Install tensorflow GPU miscellaneous
猜你喜欢

The answer skills and examples of practical cases of the second construction company are full of essence

Embedded c learning notes

cyclegan:unpaired image-to-image translation using cycle-consistent adversarial network

Make a row of the dataframe a column name

Can bus transceiver principle

What happens from entering a web address in the browser's input box to seeing the contents of the web page?

Assertion of postman interface test

26. histogram back projection

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

RT thread project engineering construction and configuration - (Env kconfig)
随机推荐
OTA trigger
Model integration and cascading
2021-1-15 摸魚做的筆記Ctrl+c /v來的
Sweet cool girl jinshuyi was invited to be the spokesperson for the global finals of the sixth season perfect children's model
Assertion of postman interface test
俏皮少女王艺璇 受邀担任第六季完美童模全球总决赛推广大使
Principle of voice wake-up
Abnova丨抗GBA单克隆抗体解决方案
ActivityManager kill reason
清甜女孩李斯霞 受邀担任第六季完美童模全球总决赛小主持人
20. Hough line transformation
recv & send
Abnova丨CMV CISH 探头解决方案
Common deep learning optimizers
What happens from entering a web address in the browser's input box to seeing the contents of the web page?
Oracle數據庫完全卸載步驟(暫無截圖)
Talking about interface test (I)
Installing MySQL databases in FreeBSD
Interpretation of script corresponding to postman assertion
regular expression