a normal tcp connection costs a full round trip before you send one byte of real data. SYN, SYN-ACK, ACK, and only then your GET.
tcp fast open cuts that out.
the first time you connect, the server gives you a cookie in its SYN-ACK. next time, you put that cookie and your GET right inside the SYN.
the server checks the cookie and starts answering before the handshake is even done.
both sides have to support it. the cookie is there so it can't be abused for syn floods.
@nioq4sen the syscall is the kernel's, C is just where you call it from. and yes, new servers use epoll/kqueue/io_uring. the 1024 cap is on the fd number though, not the count, so any process with lots of files open can trip it inside an old lib
one thread can serve many clients in C. no thread per connection. that's what select() is for.
you put every socket fd in an fd_set and call select(max_fd + 1, &readfds, NULL, NULL, NULL). it sleeps until at least one of them has data.
when it returns, only the ready fds are left in the set. FD_ISSET tells you which. new connection on the listening socket, accept(). data on a client, read() and echo.
since the set gets overwritten, you rebuild it with FD_ZERO and FD_SET every loop.
the catch is 1024 fds max and a full scan on every call.
have you ever hit that 1024 wall in a real server?
@0xileri at -O2 that's very likely. a and b turn into constants or registers and never get a stack slot. and since d[1] is out of bounds (UB), the compiler is free to drop that write too. that's why the demo used -O0
declare four ints in a row and they usually end up right next to each other on the stack.
int a = 5000; int b = 23; int c[1] = {1}; int d[1] = {2};
with gcc -O0 they came out 4 bytes apart. d at the lowest address, a at the top.
so d[1] = 42 doesn't crash. it quietly turns c[0] into 42.
d[2] lands on b. d[3] lands on a.
a variable is just a name for a spot in one long strip of memory. C won't stop you from walking past the end of it. and another compiler can order them differently, so the damage moves around too.
@c4rtmann that 1024 is FD_SETSIZE, and the scan happens twice: the kernel walks every fd up to max_fd, then your loop walks them again. poll drops the cap, epoll hands back only the ready fds
@ElJoputismoDC exacto, the code only moves because it's built as PIE. compile with -no-pie and the code sits at 0x400000 every run, while the stack, heap and libs still get randomized
run the same program twice and print a few addresses.
code 0x55f94b40a139 -> 0x564d77d3d139
stack 0x7ffd521f41c4 -> 0x7ffeed714c44
that's ASLR. every run, linux picks a new random start for the code, stack, heap and shared libraries.
but things inside one block keep their distance. code to a global variable is 0x2ee3 bytes in both runs. so if one address leaks, the rest are easy to work out.
linux has a setting for how much it lies to malloc.
/proc/sys/vm/overcommit_memory
0 is the default. the kernel guesses, and only refuses requests that are clearly too big.
1 never refuses. every malloc works, and the OOM killer sorts it out later.
2 is strict. it counts every byte it promised, and malloc returns NULL once you pass the limit.
@Ayoub__Khater just tried it, clang 19 at -O0 gives movsx then shl eax, 1. gcc picks add eax, eax. same math, times 2 is a left shift by one. the ir still says mul by 2, clang's backend swaps it for a shift even at -O0
int8_t x = 30; int8_t y = x * 2; return y;
at -O0 gcc does what you wrote. it puts 30 on the stack, loads it back, adds it to itself (that's the x * 2), stores y, then returns it.
at -O2 the whole thing is two lines. mov eax, 60 and ret.
the compiler saw x is always 30, so it did the math itself. that's constant folding. the multiply never runs.
your gpu never really draws a triangle. it turns it into pixels in a few steps.
the vertex shader runs once per corner and moves each point to its spot on screen.
the rasterizer works out which pixels the triangle covers. that part is fixed hardware.
the fragment shader runs once per covered pixel and picks its color.
then the depth test drops pixels hidden behind something closer.
i wrote the whole path up on lowlevelcraft.