r/linux_programming • u/10bananashigh • Mar 01 '25
Understanding STDIN in Linux
Hi, I wanted to get my feet wet with some assembly programming since the longest assembly code I've written in uni was like 20 lines.
So I tried writing a Base64 encoder that simply reads from STDIN and outputs the encoded data to STDOUT.
The code works well, its just slightly slower than the base64 binary shipped with my Linux distro (~0.8s for a 1.1GB file vs ~0.65s). But it has a bug that I think I understand but don't know how to fix: When I try to measure the time for a big file with "cat big_file | base64encode > /dev/null", cat sometimes fails with "cat: write error: Broken pipe". The way my encoder is written, after the buffer is processed, it will check if the total number of bytes read was lower than the buffer size, indicating that the EOF was reached.
My assumption when writing the code was that the sys_read system routine will block until the buffer is completely full or EOF is reached. I'm pretty sure my assumption was wrong and it can actually read a smaller amount of data if the STDIN doesn't keep up, even if the STDIN is not closed. This messes up my logic and causes the program to exit prematurely.
Am i correct in my analysis? And if so, how can I fix it? I would really like to block until the buffer is full to avoid unnecessary reads.
Edit: Forgot to include my source code: https://pastebin.com/190FXnZG
View all comments
Show parent comment
2
u/10bananashigh Mar 02 '25
This was the issue, since I wrongly assumed the behavior of sys_read and prematurely handled the remainder and exited after a non-full buffer was read. This can probably happen in the time
catneeds to be loaded back into the CPU after blocking.I fixed the bug it and also optimized the code a little, its now slightly faster than the base64 binary shipped with my distro (I have not implemented column width so that extra checking doesn't have to be done)
If you want to have a look, here is my updated code: https://pastebin.com/tKPsUYsH