DEV Community

C Programming!

Mahmoud Salama on August 22, 2018

What are some of the cool projects a beginner can work on to learn C? I've only been a *nix user for my job but I'm interested in low level details...
Collapse
 
pavonz profile image
Andrea Pavoni • Edited

I would go for the classic "address book" to start with something that goes beyond the "Hello world" :-)

An interesting book is Exercises for programmers. You can use it to pratice with other languages. There are, however, several free websites where you can find inspiration for practice, especially with algorithms.

If you want to dive into low level details, well, the first step is to get that knowledge about the theory behind OS and kernels in general. Maybe a manual on Operative Systems or Computer Architectures would help a lot in this regard.

If you really want to go with something practical, then you could start tweaking with kernel modules, and build some backdoor that hijacks system calls to hide files or connections >:-]. I did it back in the times (15+ years ago) just for fun, and it's a relatively easy task: it requires basic knowledge on kernel and few lines of C code.
Try starting from this article: uwnthesis.wordpress.com/2016/12/26.... If you see you lack some knowledge, then it's the right opportunity to fix the missing bit and come back to the problem when you'll feel ready to continue.

good luck! :-)

Collapse
 
nichartley profile image
Nic Hartley

I can highly recommend this, with one small note: Be prepared to accidentally and permanently fuck up your kernel. In other words, do this on a machine you can dual-boot, in a partition with nothing but the OS in it. Simple kernel hacking like this is relatively harmless, and the odds that you'll break something too badly are small, but as you naturally expand into it you'll be more and more at risk of bricking your computer.

Strictly speaking, you don't even need to dual-boot -- a USB drive with a portable installation of the Linux flavor of your choice is enough. I'd recommend dual-booting, though, so that you literally can't lose your recovery method without losing the bricked computer and invalidating the problem.

Collapse
 
pavonz profile image
Andrea Pavoni

Well, working with a LKM and some syscalls should be harmless, especially if you load it manually. Of course you might incur in some kernel panic and thus a forced reboot, but the chances to turn your computer into a brick are very low :-P

Thread Thread
 
nichartley profile image
Nic Hartley

Oh, for sure. But it's easy to go from the not-so-dangerous stuff to the very dangerous stuff without ever quite realizing, and if you already have a known-good recovery solution (in my case, nuke the partition and reinstall since there was nothing important on it anyway), you avoid the minor heart attack.

Thread Thread
 
pavonz profile image
Andrea Pavoni

how about a VM like VirtualBox? :-)

Thread Thread
 
nichartley profile image
Nic Hartley

I've actually run into some issues with the more esoteric bits of kernel dev when running a VM, so I tend to stay away from them. That said, if they work for you, then absolutely! Way easier than reimaging a partition.

Thread Thread
 
pavonz profile image
Andrea Pavoni

To be honest, I don't do kernel hacking since at least a decade or more (IIRC it was still kernel 2.4 or 2.6).
That said, a simple LKM with syscall hijacking should work even on a VM (given that you're running a non-monolitic kernel :-P)

Thread Thread
 
madsalama profile image
Mahmoud Salama

I never even thought of the possibility of "bricking" my computer, I only thought the worst I could do is wipe it clean or causing a kernel panic! Weirdly, I'm now interested to know how I can actually do that! 🙉

Thread Thread
 
pavonz profile image
Andrea Pavoni • Edited

You risk to “brick” your computer if you mainly touch drivers with low-level access to hardware. Re-defining high-level syscalls related to “reads” should not damage your hardware ;-)

Edit:
Check this advanced example, I studied/played a lot with this (well, the first versions from the original author, Stealth):

github.com/trimpsyw/adore-ng

Thread Thread
 
madsalama profile image
Mahmoud Salama

That's insightful, thanks!
Gotta get going! I'm currently reading Robert Love's 'system programming' and I have been inspired with this post to contextualize a bit. Let's see! 🤓✌️

Thread Thread
 
pavonz profile image
Andrea Pavoni

In case you missed my edit to the previous content:

github.com/trimpsyw/adore-ng

Collapse
 
ghost profile image
Ghost

Simpler applications such as filter utilities are fun and rewarding to write in C. This would be anything that takes input (perhaps on stdin), transforms it in some way, and prints the output.

Something more advanced would be a networking application. Code up a TCP client or server and begin to write some code that communicates over a socket. Fun stuff. This kind of thing is easy to test as you either write the client or server and then use netcat, nc to do the other side.

Collapse
 
dmfay profile image
Dian Fay

It's not the kernel, but the PostgreSQL database management system is a C project with great documentation and a very active community. Getting involved may take a bit more effort than some other projects since development is coordinated over an old-fashioned mailing list.

Collapse
 
__mateidavid profile image
Matei David

Hi,

It depends on what level you currently are in C and how much you can do.

In my C/C++ module in university, whenever we would talk about memory management and pointers (and how to use malloc() and free() ) we would always be told that we can write our own memory allocator.

It's not an easy thing to do, but there are a lot of examples already on GitHub about own implementations of malloc. As far as I know, the standard library malloc is not as efficient as others that have been published. It would give you a lot of context into how to operate at a really low-level, work with the stack/heap, manage memory, pointers, addressed etc and there are already a lot of examples, guides and tutorials online so if you ever get stuck just do a google search.

In short, try making your own memory allocator and your own free function and see where that goes! Doesn't have to be efficient or as good as malloc but it will surely put you right in the middle of C and you will get to work or at least see structs, unions, tagged unions, pointers and most of C's features.

Hope this helps!

Collapse
 
drewknab profile image
Drew Knab

I'm interested to see where this goes, I'm in the same boat.