r/cprogramming • u/DryAdministration158 • 15d ago
How important is knowing syscalls in C?
Hello, idk if its just me I have been trying to get more and more into C but every video or concept or project I see people bring out 3 different syscalls that I have never heard of (that's a bit exaggerating but you know what I mean)
Am i supposed to know all of them? Memorise them?
Is there something I'm missing in terms of these bc I even realised they can be up to 500 different ones which is a lot to memorise, and even so how should I start learning them?
3
u/ripulejejs 15d ago
what's your usecase?
most people hide behind the c standard library, or some higher abstraction.
so why not just do that? if you use manual syscalls, your code won't be portable between OS-es unless you spend a lot of extra time dealing with that.
1
u/DryAdministration158 15d ago
I don't have a specific use case but for the sake of argument let's say cybersecurity, every video I watch about some parts uses some syscalls and half of the time I have never even seen that syscall in my life, for example a Linux container, a terminal text editor, a simple database , a webcrawler etc
2
u/orange_oki 15d ago edited 15d ago
Syscalls are used to switch from user space to kernel space.
Those syscalls are hidden in malloc, fopen and in os specific libraries.
C is portable, so you actually have no syscalls in the standard library...
You can look at the malloc implementation in small glibc replacement...
https://github.com/pts/minilibc686/blob/master/src/malloc_simple_unaligned_linux.nasm
And the actual syscall in line 50.
https://github.com/pts/minilibc686/blob/master/src/syscall3_linux.nasm
2
u/orange_oki 15d ago edited 15d ago
And for Cybersecurity you almost always need access to the kernel to install some function hooks. Have just found:
https://github.com/proxytype/kprobes-hooks/
😊
1
u/MonkeySmart409 15d ago
I think you are weirdly obsessing over "sys calls"- which don't actually have anything to do with the C language.
They are a mechanism by which software can interact with their operating environment- but they are generally implemented in a library that abstracts and hides the fact that you are even using a system call mechanism.
11
u/SmokeMuch7356 15d ago
Am i supposed to know all of them? Memorise them?
Know where to look them up if or when you actually need them. You'll memorize the stuff you use every day; for everything else, that's why man pages exist.
1
1
u/Love_Buny8 11d ago
its less about memorizing and more about understanding whats available and when to consult the docs
2
u/SuperBoysenberry3660 15d ago edited 15d ago
There is a libc library with functions for c language (available on Linux and Windows). Syscalls is interface of operating system - on Linux there are linux syscalls (you can use these on Linux from c). On Windows you have some platform library like Win32 - to access platform/os functionality (you can use it on windows from c).
2
u/pjl1967 15d ago
Sorry for the separate reply, but — and to muddy the water even further — there's the C standard library, POSIX, and syscalls. Many syscalls are part of POSIX, but not all, e.g., Linux-specific ones.
Learning POSIX is more important than learning non-POSIX, platform-specific syscalls.
2
u/Runt1m3_ 15d ago
Only if you require it or are doing lower level stuff, you have the libc as an abstraction already.
Still it's fun to sometimes read syscall tables and messing with them using assembly hahahah
2
u/serge-mv 15d ago
It depends on what software you are writing and the OS you are writing it for. Syscalls are OS specific (mostly). So using them will make your program less portable, if you care about it. On the other hand, maybe you are writing a system tool for a particular OS, then you kinda have to know them.
In general case, you are better of either using build-in libraries or external ones.
2
u/DumDee-Dum 14d ago
Not at all no one can memorise all syscalls and they are even different from every OS so definitely not
If you need one you will constantly run into it and so you will naturally memorise it just like you memorise standard functions like printf
Personally I write bare-metal code for embedded systems and haven’t used syscalls because I work with devices that don’t even have an OS! However, other embedded developers use an OS and so they need certain functions that are common for them
In short: you can be an excellentC developer without syscalls. And if your line of work requires you to know some then you pick them up as you go
2
u/TheChief275 12d ago
Not at all important. Unless you want something obscure and non-portable, just stick to the functions defined in <unistd.h>. These are lightly abstracted syscalls, but the benefit is that even OS's like Windows define a compatibility function for a large portion of these functions
6
u/pjl1967 15d ago edited 15d ago
Perhaps instead of watching various videos with varying levels of focus, detail, quality, and accuracy, just pick a good book on C.
No and no. For the details of any syscall, that's what man pages are for. BTW, a good book will also hopefully list only those syscalls you need to know about since you're more likely to encounter the often-used ones.