int server = socket();
bind(server, 80);
listen(server)
int server = socket();
bind(server, 80);
listen(server)
while(int connection = accept(server)) {
pthread_create(echo, connection)
}
while(int connection = accept(server)) {
pthread_create(echo, connection)
}
void echo(int connection) {
char buf[4096];
while(int size = read(connection, buffer, sizeof buf)) {
write(connection, buffer, size)
}
}
int server = ... //like before
int eventdf = epoll_create1(0);
struct epoll_event ev = {.events = EPOLLIN, .data.fd = server};
epoll_ctl(epollfd, EPOL_CTL_ADD, server, &ev);
while((int max = epoll_wait(eventsfd, events, 10))) {
....
}
struct epoll_events events[10];
while((int max = epoll_wait(eventsfd, events, 10))) {
for(int n=0; n < max ; n++) {
if(events[n].data.fd.fd == server) {
//Server has socket connection
int connection = accept(server);
ev.events = EPOLLIN;
ev.data.fd = connection;
epoll_ctl(eventfd, EPOLL_CTL_ADD, connection, &ev);
}
}
}
while((int max = epoll_wait(eventsfd, events, 10))) {
for(int n=0; n < max ; n++) {
if(events[n].data.fd.fd == server) {
//Server has socket connection
int connection = accept(server);
ev.events = EPOLLIN;
ev.data.fd = connection;
epoll_ctl(eventfd, EPOLL_CTL_ADD, connection, &ev);
}
else {
char buf[4096];
while(int size = read(connection, buffer, sizeof buf)) {
write(connection, buffer, size);
}
}
}
}
A semi infinite while loop, polling and blocking the OS untill some of the descriptors are ready.
Pollable | Time | Everything Else |
---|---|---|
can be directly waited upon | can be directly waited upon | must happen off loop and signal back to the loop when done |
sockets (net, http, dgram) etc. |
timeouts and intervals | Everything in fs.* uses the uv threadpool |
fs
Shared by
Default number of threads is 4