select多路复用

来源:互联网 发布:db2执行sql脚本 编辑:程序博客网 时间:2024/05/02 02:14

1.在socket的使用中我们经常用多路复用的方式来避免使用while的循环查询监听,至于如何使用详细的请看man 2 select

int select(int nfds, fd_set *readfds, fd_set *writefds,                  fd_set *exceptfds, struct timeval *timeout);


nfds:被监听的描述符

readfs:可读描述符集

writedfs:可写描述符集

timeout:堵塞时间

 

 

例子:

#include <stdio.h>#include <sys/time.h>#include <unistd.h>#include <time.h>void display_time(const char *str){int seconds;seconds = time((time_t*)NULL);printf("%s,%d\n",str,seconds);}int main(void){fd_set readfds;struct timeval timeout;int ret;FD_ZERO(&readfds);FD_SET(0,&readfds);timeout.tv_sec = 10;timeout.tv_usec = 0;while(1){display_time("before select");ret = select(1,&readfds,NULL,NULL,&timeout);switch(ret){case 0:printf("NO DATA IN TEN SECONDS.\n");exit(0);break;case -1:perror("SELECT");exit(1);break;default:getchar();printf("Data is avilable now.\n");}}return 0;}

重要的是时间的设置,这里我们使用10秒堵塞的时间,在十秒内如果描述符readfds有可读的加入就立即返回,否则一直等待知道10秒后返回0

 

不过有趣的是我在反汇编看到

.file"select.c".section.rodata.str1.1,"aMS",@progbits,1.LC0:.string"%s,%d\n".text.globl display_time.typedisplay_time, @functiondisplay_time:.LFB33:.cfi_startprocpushq%rbx.cfi_def_cfa_offset 16movq%rdi, %rbx.cfi_offset 3, -16movl$0, %edicalltimemovl%eax, %ecxmovq%rbx, %rdxmovl$.LC0, %esimovl$1, %edimovl$0, %eaxcall__printf_chkpopq%rbxret.cfi_endproc.LFE33:.sizedisplay_time, .-display_time.section.rodata.str1.1.LC1:.string"before select".LC2:.string"NO DATA IN TEN SECONDS.\n".LC3:.string"SELECT".LC4:.string"Data is avilable now.\n".text.globl main.typemain, @functionmain:.LFB34:.cfi_startprocpushq%rbp.cfi_def_cfa_offset 16pushq%rbx.cfi_def_cfa_offset 24subq$152, %rsp.cfi_def_cfa_offset 176movq%rsp, %rdimovl$16, %ecxmovl$0, %eax#APP# 18 "select.c" 1cld; rep; stosq# 0 "" 2#NO_APPorq$1, (%rsp)movq$10, 128(%rsp)movq$0, 136(%rsp)leaq128(%rsp), %rbx.cfi_offset 3, -24.cfi_offset 6, -16.L7:movl$.LC1, %edicalldisplay_timemovq%rbx, %r8movl$0, %ecxmovl$0, %edxmovq%rsp, %rsimovl$1, %edicallselectcmpl$-1, %eaxje.L5testl%eax, %eax//这里不是一直为正确的状态么?(意味着.L9的跳转是必然)jne.L9movl$.LC2, %esimovl$1, %edicall__printf_chkmovl$0, %edicallexit.L5:movl$.LC3, %edicallperrormovl$1, %edicallexit.L9:movqstdin(%rip), %rdicall_IO_getcmovl$.LC4, %esimovl$1, %edimovl$0, %eaxcall__printf_chkjmp.L7.cfi_endproc.LFE34:.sizemain, .-main.ident"GCC: (Ubuntu/Linaro 4.4.4-14ubuntu5.1) 4.4.5".section.note.GNU-stack,"",@progbits


 


 

 

0 0
原创粉丝点击