Thread #108227695
File: 00a3f5ff-8811-43cf-aa86-0210dfda3f06.png (573.6 KB)
573.6 KB PNG
previous: >>108219864#define __NR_exit 60
#define __NR_exit_group 231
https://man7.org/linux/man-pages/man2/exit.2.html
https://man7.org/linux/man-pages/man2/exit_group.2.html
tl;dr:
exit causes your current thread to exit. if your current thread is the thread group leader, then it has the same effect as exit_group. that is, it causes your current process to exit
updating my tl;dr because i was WRONG (should've read the manpage first, i guess)
exit doesn't exit your process, even if you're the thread group leader. it still only exits that thread. it just keeps the leader thread in a zombie state until all of the other threads exit, or one of them calls exit_group
https://elixir.bootlin.com/linux/v6.19.3/source/kernel/exit.c#L918
https://elixir.bootlin.com/linux/v6.19.3/source/kernel/exit.c#L267
this is actually psychotic and one of the grossest things i have ever seen in linux
useful for skipping over any pesky atexit hooks. i never really understood the use case for those. like, sure, i understand how they work, and why you might want them in theory, but geeze. talk about niche use cases. when are you actually ever going to use these in the real world? something something graceful shutdown or whatever, but idk. feels pretty pointless. i guess the one actual use case is probably:
>Since glibc 2.2.3, atexit() (and on_exit(3)) can be used within a shared library to establish functions that are called when the shared library is unloaded.
that seems kind of handy. but for an actual process exit? nah
relevant resources:man manman syscalls
https://man7.org/linux/man-pages/
https://linux.die.net/man/
https://elixir.bootlin.com/linux/
https://elixir.bootlin.com/musl/
https://elixir.bootlin.com/glibc/
12 RepliesView Thread
>>
>>
>>108227813
thanks! i am glad the last couple have gotten more posts. i suspected that they, more so than a lot of others, might. it's a bit disappointing when like a week straight of threads die before i even get home from work, so this is a nice change of pace
>>
>>
>>108227695
I use atexit basically every time I'm dealing with some external file artifact, be that an actual result or just temp files.
whenever I use exit(3) (basically only from within error handlers), I can try to write out any results or at least bring already written results into a somewhat coherent state.
also I like deleting any temp files I might've used during execution.
>>
>>
>>
>>108227695
>it just keeps the leader thread in a zombie state until all of the other threads exit, or one of them calls exit_group
__NR_exit(60) being called on the main thread in a multithreaded program is so odd. Probably always a mistake, unless you're trying to obscure your program in htop or something.
It's actually possible on Windows too, with ExitThread / TerminateThread.
>https://devblogs.microsoft.com/oldnewthing/20100827-00/?p=13023
>https://learn.microsoft.com/en-us/windows/win32/procthread/terminatin g-a-thread
>>
>>
>>108232580
I have not... But I don't think it would work, because most of those file handles are created fairly high-up, whereas the programme can bail quite deep into the call graph, so the file handles are very much still in scope up above.
>>
>>
>>108232691
your control flow doesn't bubble back up on error? interesting. not the way i would do it personally, but if it works
>>108232713
rude >:(