110 likes | 226 Views
Advanced UNIX programming. Fall 2002 Instructor: Ashok Srinivasan Lecture 37. Acknowledgements: The syllabus and power point presentations are modified versions of those by T. Baker and X. Yuan. Announcements. Project Presentations next week Demo next week Quiz Monday
E N D
Advanced UNIX programming Fall 2002 Instructor: Ashok Srinivasan Lecture 37 Acknowledgements: The syllabus and power point presentations are modified versions of those by T. Baker and X. Yuan
Announcements • Project • Presentations next week • Demo next week • Quiz Monday • Monday’s and today’s lecture material • Monday’s lecture material included in finals
Week 14 Topics • Signals and threads • Terminal I/O • APUE chapter 11 • Signal driven I/O • UNP chapter 22 • Non-blocking I/O • UNP chapter 15
Terminal I/O • Canonical mode (not our focus) • The default mode, input line by line • Non-canonical mode • Input characters are not assembled • termios structure • tcgetattr and tcsetattr • Turning on the non-canonical mode
termios structure • struct termios • Stores all the characteristics of a terminal device that we can examine and change • termios.h struct termios { tcflag_t c_iflag; /* input flag */ tcflag_t c_oflag; /* output flag */ tcflag_t c_cflag; /* control flags */ tcflag_t c_lflag; /* local flags */ cc_t c_cc[NCCS]; /* control characters */ };
Functions to get and set the fields in the termios structure • #include <termios.h> • int tcgetattr(int fildes, struct termios *termios_p) • int tcsetattr(int fildes, int optional_actions, const struct termios *termios_p) • optional_actions • TCSANOW • TCSADRAIN • TCSAFLUSH
Turn on the noncanonical mode • Unset the ICANON flag in c_lflag • myterm.c_lflag & = ~ICANON • When will a read return using the noncanonical mode for input? • Number of characters (VMIN) • Time (VTIME) • Specified in the c_cc field • c_cc[VMIN] = ???, c_cc[VTIME] = ??? • VMIN > 0, VTIME > 0 • VMIN = 0, VTIME > 0 • VMIN > 0, VTIME = 0 • VMIN = 0, VTIME = 0 • See example1.c and example2.c
Signal driven I/O • The kernel raises a signal when something happens to a file descriptor • Signal driven I/O for sockets • Establish a signal handler for SIGIO • Set the socket owner • Enable signal-driven I/O for the socket
When is SIGIO raised? • For UDP sockets • A datagram arrives • An error occurs • For TCP sockets • A connection request has completed • A disconnect request has been initiated • A disconnect request has completed • Half of a connection has been shut down • Data has arrived • Data has been sent • Too many SIGIOs for a TCP socket – rarely used
Non-blocking I/O • For input operations • read, recv, recvfrom, etc. • If the operation cannot be satisfied, return with an error of EWOULDBLOCK • For output operations • write send, sendto, etc. • If no buffer space, return with an error of WOULDBLOCK • For accept • If a new connection is not available, return with an error of EWOULDBLOCK • For connect • If the connection cannot be established right away, EINPROGRESS is returned