安工大李沁tcpip课件note6

  • 格式:pdf
  • 大小:172.65 KB
  • 文档页数:26

Even with select, blocking might still happen: a dis-match on speed between the producer and the consumer can lead either to blocking. We can avoid the blocking by turning O_NONBLOCK flag on. Some tricks, however, are needed to bring the producer and the consumer into line.
Whenever we find the need to use nonblocking I/O, it will usually be simpler to split the application into either processes (using fork) or threads.
A Simpler, but Less Efficient Version
Operation input output accept connect
Reason no available data no enough space in send buffer no arrived request reply has not arrived yet
Error EWOULDBLOCK EWOULDBLOCK EWOULDBLOCK EINPROGRESS
available to produce
Begin
ConsumePtr
Code Fragment:1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
v a l = F c n t l ( f d . . . , F_GETFL, 0 ) ; F c n t l ( f d . . . , F_SETFL, v a l | O_NONBLOCK) ; t o i p t r = t o o p t r = t o ; /£ i n i t i a l i z e b u f f e r p o i n t e r s £/ fr i p t r = froptr = fr ; stdineof = 0; for ( ; ; ) { FD_ZERO(& r s e t ) ; FD_ZERO(& wset ) ; i f ( s t d i n e o f == 0 && t o i p t r < &t o [MAXLINE] ) FD_SET(STDIN_FILENO, &r s e t ) ; i f ( f r i p t r < &f r [MAXLINE] ) FD_SET( s o c k f d , &r s e t ) ; i f ( t o o p t r != t o i p t r ) FD_SET( s o c k f d , &wset ) ; i f ( f r o p t r != f r i p t r ) FD_SET(STDOUT_FILENO, &wset ) ; S e l e c t ( maxfdp1 , &r s e t , &wset , NULL, NULL ) ;
Non-Blocking I/O Qin Introduction Non-Blocking Reads and Writes Non-Blocking connect
connect: Non-Blocking Version Web Client
Table: The reasons and error types of blocking
Non-Blocking connect
connect: Non-Blocking Version Web Client
Non-Blocking Reads and Writes
Non-Blocking I/O Qin Introduction Non-Blocking Reads and Writes
Non-Blocking Reads and Writes
Non-Blocking I/O Qin Introduction Non-Blocking Reads and Writes
Even with select, blocking might still happen: a dis-match on speed between the producer and the consumer can lead either to blocking.
Non-Blocking I/O Qin Introduction Non-Blocking Reads and Writes Non-Blocking connect
connect: Non-Blocking Version Web Client
Lecture Notes for Network Programming with TCP/IP
Non-Blocking connect
connect: Non-Blocking Version Web Client
Echo Client Revisited: Buffer for Non-Blocking I/O
ProducePtr
MXLN
already consumed
can be consumed
Non-Blocking I/O Qin Introduction Non-Blocking Reads and Writes Non-Blocking connect
connect: Non-Blocking Version Web Client
Whenever we find the need to use nonblocking I/O, it will usually be simpler to split the application into either processes (using fork) or threads. pid = fork(); switch pid: case 0: read socket into stdout; kill parent; case 1: write stdin into socket; pause;
connect: Non-Blocking Version Web Client
Introduction
Non-Blocking Reads and Writes
Non-Blocking connect connect: Non-Blocking Version Web Client
Four Categories of Blocking Functions
Non-Blocking I/O Qin Li1
1 Anhui
University of Technology qinli@ submit_qinli@
Spring 2012
Outline
Non-Blocking I/O Qin Introduction Non-Blocking Reads and Writes Non-Blocking connect
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
v o i d s t r _ c l i ( FILE £ fp , i n t s o c k f d ) { pid_t p i d ; c h a r s e n d l i n e [ MAXLINE] , r e c v l i n e [MAXLINE] ; i f ( ( p i d = Fork ( ) ) == 0 ) { / £ c h i l d : s e r v e r ´> s t d o u t £ / w h i l e ( R e a d l i n e ( s o c k f d , r e c v l i n e , MAXLINE) > 0 ) Fputs ( r e c v l i n e , s t d o u t ) ; / £ i n c a s e p a r e n t s t i l l running £ / k i l l ( g e t p p i d ( ) , SIGTERM ) ; exit (0); } / £ p a r e n t : s t d i n ´> s e r v e r £ / w h i l e ( F g e t s ( s e n d l i n e , MAXLINE, f p ) != NULL) Writen ( s o c k f d , s e n d l i n e , s t r l e n ( s e n d l i n e ) ) ; Shutdown ( s o c k f d , SHUT_WR) ; / £ EOF on s t d i n , send FIN £ / pause ( ) ; return ; }
Code Fragment: Producer stdin
i f (FD_ISSET(STDIN_FILENO, &r s e t ) ) { i f ( ( n = r e a d (STDIN_FILENO, t o i p t r , &t o [MAXLINE] ´ t o i p t r ) ) < 0 ) { i f ( e r r n o != EWOULDBLOCK) e r r _ s y s ( " r e a d e r r o r on s t d i n " ) ; } e l s e i f ( n == 0 ) { f p r i n t f ( s t d e r r , "%s : EOF on s t d i n \n" , gf_time ( ) ) ; s t d i n e o f = 1 ; / £ a l l done with s t d i n £ / i f ( t o o p t r == t o i p t r ) Shutdown ( s o c k f d , SHUT_WR) ; / £ send FIN £ / } else { f p r i n t f ( s t d e r r , "%s : r e a d %d b y t e s from s t d i n \n" , gf_time ( ) , n ) ; t o i p t r += n ; / £ # j u s t r e a d £ / FD_SET( s o c k f d , &wset ) ; } }