Socket Programming Part 2 Sop, Fan 07302010028@fudan.edu.cn Reference: Daniel Spangenberger Computer Networks PPT-4 Socket Programming
Sop, Fan 07302010028@fudan.edu.cn Reference: Daniel Spangenberger Computer Networks PPT-4 Socket Programming
Topics ° Concurrency review Why should we use∴? Our experiences? Socket Programming related System requirement(robustness. How to cooperate with socket AP[? ° Example How to modify the example to fit further requirement?
Concurrency Review Why should we use …? Our experiences? Socket Programming related System requirement(robustness…) How to cooperate with socket API? Example How to modify the example to fit further requirement?
A scenario。。 Clients Server User 1 connect() accept( fgets() (goes to lunch Blocks read() User connect( Blocked
Clients User 1 (goes to lunch) Server connect() accept() fgets() User 2 read() connect() Blocked! Blocks!
How did we add concurrency Processes Uses forko Easy to understand(actually we have implemented one version A lot to consider about causing complexity(zombie, syscall.) e Threads Natural concurrency (new thread per connection) Easier to understand you know it already) Complexity is increased(possible race conditions) Use non- blocking I/O Uses select () Explicit control flow(no race conditions Explicit control flow more complicated though
Processes Uses fork() Easy to understand(actually we have implemented one version!) A lot to consider about causing complexity(zombie, syscall…) Threads Natural concurrency (new thread per connection) Easier to understand (you know it already) Complexity is increased (possible race conditions) Use non-blocking I/O Uses select() Explicit control flow (no race conditions!) Explicit control flow more complicated though
Multi-process Forko Use Pid to verify different process Assign different task flow accordingly Signal& waitpid(…) Tracing child processes, kill the zombies e Other process control methods?
Fork() Use Pid to verify different process Assign different task flow accordingly Signal & waitpid(…) Tracing child processes, kill the zombies Other process control methods?
Multi-thread ° pthread create Create thread according to detailed settings Pthread(series: join, detach, canceL.) Imply different polices other thread control methods
pthread_create Create thread according to detailed settings Pthread(series: join, detach, cancel…) Imply different polices. Other thread control methods?
lO Multiplexing Monitor sockets with select( int select(int maxed fd set *readfds, fd set *writefds, fd set *exceptfds, const struct timespec *timeout)i ● So what' san fd set? Bit vector with FD setsize bits maxed- Max file descriptor+1 readfs-Bit vector of read descriptors to monitor writefds -Bit vector of write descriptors to monitor exceptfds -Read the manpage, set to NULL timeout-How long to wait with no activity before returning, NULI for eternity
Monitor sockets with select() int select(int maxfd, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, const struct timespec *timeout); So what’s an fd_set? Bit vector with FD_SETSIZE bits maxfd – Max file descriptor + 1 readfs – Bit vector of read descriptors to monitor writefds – Bit vector of write descriptors to monitor exceptfds – Read the manpage, set to NULL timeout – How long to wait with no activity before returning, NULL for eternity
So what about bit vectors? e void Fd ZERo (fd set *fdset)i Clears all the bits e void FD Set(int fd, fd set *fdset Sets the bit for fd void Fd Clr (int fdfd set faset Clears the bit for fd int FD ISSet (int fd, fd set *fdset)i Checks whether fds bit is set
void FD_ZERO(fd_set *fdset); Clears all the bits void FD_SET(int fd, fd_set *fdset); Sets the bit for fd void FD_CLR(int fd, fd_set *fdset); Clears the bit for fd int FD_ISSET(int fd, fd_set *fdset); Checks whether fd’s bit is set
Robustness? ° Requirements Mass users User Experience o Incidents Server/NetworkClient breakdown? Hacking?
Requirements Mass users User Experience Incidents Server/Network/Client breakdown? Hacking? …
What happened if.? Client(s) Server socket( socket( bind( listen( connect select( FD ISSET(sfd) accept( Hacking! Hacking!!! write( read Server breakdown! read() write( Exceptions here! close() reado check clients() main loop close
Client(s) Server socket() connect() write() read() close() socket() bind() listen() select() write() read() close() read() Hacking! Hacking!!! Exceptions here! Server breakdown! FD_ISSET(sfd) accept() check_clients() main loop …