//by lenky
利用select多路复用I/O的Web服务应用模型
fd_set readfds, writefds, exceptionfds;
FD_ZERO(&readfds);
FD_ZERO(&writefds);
FD_ZERO(&exceptionfds);
int max_fd;
sock = socket(...);
bind(sock, ...);
listen(sock, ...);
FD_SET(&readfds, sock);
max_fd = sock;
while(1) {
int i;
fd_set r,w,e;
memcpy(&r, &readfds, sizeof(fd_set));
memcpy(&w, &writefds, sizeof(fd_set));
memcpy(&e, &exceptionfds, sizeof(fd_set));
select(max_fd + 1, &r, &w, &e, NULL);
if(FD_ISSET(&r, sock)){
new_sock = accept(sock, ...);
FD_SET(&readfds, new_sock);
FD_SET(&writefds, new_sock);
max_fd = MAX(max_fd, new_sock);
}
for(i= sock+1; i<max_fd+1; ++i) {
if(FD_ISSET(&r, i))
doReadAction(i);
if(FD_ISSET(&w, i))
doWriteAction(i);
}
}
利用poll多路复用I/O的Web服务应用模型
struct pollfd fds[MAX_NUM_FDS];
int max_fd;
sock = socket(...);
bind(sock, ...);
listen(sock, ...);
fds[0].fd = sock;
fds[0].events = POLLIN;
max_fd = 1;
while(1) {
int i;
poll(fds, max_fd, -1);
if(fds[0].revents & POLLIN){
new_sock = accept(sock, ...);
fds[max_fd].fd = new_sock;
fds[max_fd].events = POLLIN | POLLOUT;
++ max_fd;
}
for(i=1; i<max_fd+1; ++i) {
if(fds.revents & POLLIN)
doReadAction(i);
if(fds.revents & POLLOUT)
doWriteAction(i);
}
}
利用epoll多路复用I/O的Web服务应用模型
struct epoll_event ev;
struct epoll_event events[MAX_EVENTS];
int epfd = epoll_create(MAX_EVENTS);
sock = socket(...);
bind(sock, ...);
listen(sock, ...);
ev.events = EPOLLIN;
ev.data.fd = sock;
epoll_ctl(epfd, EPOLL_CTL_ADD, sock, &ev);
while(1) {
int i;
int n = epoll_wait(epfd, events, MAX_EVENTS, -1);
for(i=0; i<n; ++i) {
if(events.data.fd == sock) {
if(events.events & POLLIN){
new_sock = accept(sock, ...);
ev.events = EPOLLIN | POLLOUT;
ev.data.fd = new_sock;
epoll_ctl(epfd, EPOLL_CTL_ADD, new_sock, &ev);
}
}else{
if(events.events & POLLIN)
doReadAction(i);
if(events.events & POLLOUT)
doWriteAction(i);
}
}
}
利用kqueue多路复用I/O的Web服务应用模型
struct kevent changelist[MAX_EVENTS];
struct kevent eventlist[MAX_EVENTS];
int count = 0;
int kqfd = kqueue();
sock = socket(...);
bind(sock, ...);
listen(sock, ...);
EV_SET(&changelist[0], sock, EVFILT_READ, EV_ADD | EV_ENABLE | EV_CLEAR,
0, 0, 0);
++ count;
while(1) {
int i;
int n = kevent(kqfd, changelist, count, eventlist, count, NULL);
for(i=0; i<n; ++i) {
if(eventlist.ident == sock) {
new_sock = accept(sock, ...);
EV_SET(&changelist[count], new_sock, EVFILT_READ,
EV_ADD | EV_ENABLE | EV_CLEAR, 0, 0, 0);
++ count;
}else{
doReadAction(i);
}
}
}
利用/dev/poll多路复用I/O的Web服务应用模型
struct pollfd pfd;
struct pollfd pollfds[MAX_EVENTS];
struct dvpoll dopoll;
int count = 0;
int dpfd = open("/dev/poll", O_RDWR);
sock = socket(...);
bind(sock, ...);
listen(sock, ...);
pfd.fd = sock;
pfd.events = EPOLLIN;
pfd.revents = 0;
write(dpfd, pfd, sizeof(pfd));
++ count;
while(1) {
int i;
dopoll.dp_timeout = -1;
dopoll.dp_nfds = count;
dopoll.dp_fds = &pollfds;
int n = ioctl(dpfd, DP_POLL, &dopoll);
for(i=0; i<n; ++i) {
if(pollfds.fd == sock) {
if(pollfds.revents & POLLIN){
new_sock = accept(sock, ...);
pfd.fd = new_sock;
pfd.events = EPOLLIN | POLLOUT;
pfd.revents = 0;
write(dpfd, pfd, sizeof(pfd));
++ count;
}
}else{
if(pollfds.revents & POLLIN)
doReadAction(i);
if(pollfds.revents & POLLOUT)
doWriteAction(i);
}
}
}
利用rtsig多路复用I/O的Web服务应用模型
sigset_t sigset;
siginfo_t siginfo;
sigemptyset(&sigset);
sigaddset(&sigset, SIGRTMIN + 1);
sigaddset(&sigset, SIGIO);
sock = socket(...);
bind(sock, ...);
listen(sock, ...);
fcntl(sock, F_SETSIG, SIGRTMIN + 1);
fcntl(sock, F_SETOWN, getpid());
fcntl(sock, F_SETFL, O_ASYNC | O_NONBLOCK | O_RDWR);
while(1) {
struct timespec ts;
ts.tv_sec = 1;
ts.tv_nsec = 0;
sigtimedwait(&sigset, &siginfo, &ts);
if(siginfo.si_fd == sock) {
new_sock = accept(sock, ...);
fcntl(new_sock , F_SETSIG, SIGRTMIN + 1);
fcntl(new_sock , F_SETOWN, getpid());
fcntl(new_sock , F_SETFL, O_ASYNC | O_NONBLOCK | O_RDWR);
}else {
doReadAction(i);
}
}
利用select多路复用I/O的Web服务应用模型
fd_set readfds, writefds, exceptionfds;
FD_ZERO(&readfds);
FD_ZERO(&writefds);
FD_ZERO(&exceptionfds);
int max_fd;
sock = socket(...);
bind(sock, ...);
listen(sock, ...);
FD_SET(&readfds, sock);
max_fd = sock;
while(1) {
int i;
fd_set r,w,e;
memcpy(&r, &readfds, sizeof(fd_set));
memcpy(&w, &writefds, sizeof(fd_set));
memcpy(&e, &exceptionfds, sizeof(fd_set));
select(max_fd + 1, &r, &w, &e, NULL);
if(FD_ISSET(&r, sock)){
new_sock = accept(sock, ...);
FD_SET(&readfds, new_sock);
FD_SET(&writefds, new_sock);
max_fd = MAX(max_fd, new_sock);
}
for(i= sock+1; i<max_fd+1; ++i) {
if(FD_ISSET(&r, i))
doReadAction(i);
if(FD_ISSET(&w, i))
doWriteAction(i);
}
}
利用poll多路复用I/O的Web服务应用模型
struct pollfd fds[MAX_NUM_FDS];
int max_fd;
sock = socket(...);
bind(sock, ...);
listen(sock, ...);
fds[0].fd = sock;
fds[0].events = POLLIN;
max_fd = 1;
while(1) {
int i;
poll(fds, max_fd, -1);
if(fds[0].revents & POLLIN){
new_sock = accept(sock, ...);
fds[max_fd].fd = new_sock;
fds[max_fd].events = POLLIN | POLLOUT;
++ max_fd;
}
for(i=1; i<max_fd+1; ++i) {
if(fds.revents & POLLIN)
doReadAction(i);
if(fds.revents & POLLOUT)
doWriteAction(i);
}
}
利用epoll多路复用I/O的Web服务应用模型
struct epoll_event ev;
struct epoll_event events[MAX_EVENTS];
int epfd = epoll_create(MAX_EVENTS);
sock = socket(...);
bind(sock, ...);
listen(sock, ...);
ev.events = EPOLLIN;
ev.data.fd = sock;
epoll_ctl(epfd, EPOLL_CTL_ADD, sock, &ev);
while(1) {
int i;
int n = epoll_wait(epfd, events, MAX_EVENTS, -1);
for(i=0; i<n; ++i) {
if(events.data.fd == sock) {
if(events.events & POLLIN){
new_sock = accept(sock, ...);
ev.events = EPOLLIN | POLLOUT;
ev.data.fd = new_sock;
epoll_ctl(epfd, EPOLL_CTL_ADD, new_sock, &ev);
}
}else{
if(events.events & POLLIN)
doReadAction(i);
if(events.events & POLLOUT)
doWriteAction(i);
}
}
}
利用kqueue多路复用I/O的Web服务应用模型
struct kevent changelist[MAX_EVENTS];
struct kevent eventlist[MAX_EVENTS];
int count = 0;
int kqfd = kqueue();
sock = socket(...);
bind(sock, ...);
listen(sock, ...);
EV_SET(&changelist[0], sock, EVFILT_READ, EV_ADD | EV_ENABLE | EV_CLEAR,
0, 0, 0);
++ count;
while(1) {
int i;
int n = kevent(kqfd, changelist, count, eventlist, count, NULL);
for(i=0; i<n; ++i) {
if(eventlist.ident == sock) {
new_sock = accept(sock, ...);
EV_SET(&changelist[count], new_sock, EVFILT_READ,
EV_ADD | EV_ENABLE | EV_CLEAR, 0, 0, 0);
++ count;
}else{
doReadAction(i);
}
}
}
利用/dev/poll多路复用I/O的Web服务应用模型
struct pollfd pfd;
struct pollfd pollfds[MAX_EVENTS];
struct dvpoll dopoll;
int count = 0;
int dpfd = open("/dev/poll", O_RDWR);
sock = socket(...);
bind(sock, ...);
listen(sock, ...);
pfd.fd = sock;
pfd.events = EPOLLIN;
pfd.revents = 0;
write(dpfd, pfd, sizeof(pfd));
++ count;
while(1) {
int i;
dopoll.dp_timeout = -1;
dopoll.dp_nfds = count;
dopoll.dp_fds = &pollfds;
int n = ioctl(dpfd, DP_POLL, &dopoll);
for(i=0; i<n; ++i) {
if(pollfds.fd == sock) {
if(pollfds.revents & POLLIN){
new_sock = accept(sock, ...);
pfd.fd = new_sock;
pfd.events = EPOLLIN | POLLOUT;
pfd.revents = 0;
write(dpfd, pfd, sizeof(pfd));
++ count;
}
}else{
if(pollfds.revents & POLLIN)
doReadAction(i);
if(pollfds.revents & POLLOUT)
doWriteAction(i);
}
}
}
利用rtsig多路复用I/O的Web服务应用模型
sigset_t sigset;
siginfo_t siginfo;
sigemptyset(&sigset);
sigaddset(&sigset, SIGRTMIN + 1);
sigaddset(&sigset, SIGIO);
sock = socket(...);
bind(sock, ...);
listen(sock, ...);
fcntl(sock, F_SETSIG, SIGRTMIN + 1);
fcntl(sock, F_SETOWN, getpid());
fcntl(sock, F_SETFL, O_ASYNC | O_NONBLOCK | O_RDWR);
while(1) {
struct timespec ts;
ts.tv_sec =
ts.tv_nsec = 0;
sigtimedwait(&sigset, &siginfo, &ts);
if(siginfo.si_fd == sock) {
new_sock = accept(sock, ...);
fcntl(new_sock , F_SETSIG, SIGRTMIN + 1);
fcntl(new_sock , F_SETOWN, getpid());
fcntl(new_sock , F_SETFL, O_ASYNC | O_NONBLOCK | O_RDWR);
}else {
doReadAction(i);
}
}